summaryrefslogtreecommitdiff
path: root/test/libapt/getlanguages_test.cc
blob: c50ff6ff8504db65b22e1d90061e964c48fb3cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <config.h>

#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/fileutl.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <gtest/gtest.h>

#include "file-helpers.h"

TEST(LanguagesTest,Environment)
{
   _config->Clear();

   char const* env[2];
   env[0] = "de_DE.UTF-8";
   env[1] = "";

   std::vector<std::string> vec = APT::Configuration::getLanguages(false, false, env);
   ASSERT_EQ(3 ,vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);

   // Special: Check if the cache is actually in use
   env[0] = "en_GB.UTF-8";
   vec = APT::Configuration::getLanguages(false, true, env);
   ASSERT_EQ(3, vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);

   env[0] = "en_GB.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   ASSERT_EQ(2, vec.size());
   EXPECT_EQ("en_GB", vec[0]);
   EXPECT_EQ("en", vec[1]);

   // esperanto
   env[0] = "eo.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   ASSERT_EQ(2, vec.size());
   EXPECT_EQ("eo", vec[0]);
   EXPECT_EQ("en", vec[1]);

   env[0] = "tr_DE@euro";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("tr_DE", vec[0]);
   EXPECT_EQ("tr", vec[1]);
   EXPECT_EQ("en", vec[2]);

   env[0] = "de_NO";
   env[1] = "de_NO:en_GB:nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(6, vec.size());
   EXPECT_EQ("de_NO", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en_GB", vec[2]);
   EXPECT_EQ("nb_NO", vec[3]);
   EXPECT_EQ("nb", vec[4]);
   EXPECT_EQ("en", vec[5]);

   env[0] = "pt_PR.UTF-8";
   env[1] = "";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("pt_PR", vec[0]);
   EXPECT_EQ("pt", vec[1]);
   EXPECT_EQ("en", vec[2]);

   env[0] = "ast_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env); // bogus, but syntactical correct
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("ast_DE", vec[0]);
   EXPECT_EQ("ast", vec[1]);
   EXPECT_EQ("en", vec[2]);

   env[0] = "C";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("en", vec[0]);

   _config->Set("Acquire::Languages", "none");
   env[0] = "C";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_TRUE(vec.empty());

   _config->Set("Acquire::Languages", "environment");
   env[0] = "C";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("en", vec[0]);

   _config->Set("Acquire::Languages", "de");
   env[0] = "C";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("de", vec[0]);

   _config->Set("Acquire::Languages", "fr");
   env[0] = "ast_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("fr", vec[0]);

   _config->Set("Acquire::Languages", "environment,en");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);
   _config->Set("Acquire::Languages", "");

   _config->Set("Acquire::Languages::1", "environment");
   _config->Set("Acquire::Languages::2", "en");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);

   _config->Set("Acquire::Languages::3", "de");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(3, vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);

   _config->Clear();
}

TEST(LanguagesTest,TranslationFiles)
{
   _config->Clear();
   _config->Set("Acquire::Languages::1", "environment");
   _config->Set("Acquire::Languages::2", "en");
   _config->Set("Acquire::Languages::3", "de");

   char const* env[2];
   env[0] = "de_DE.UTF-8";
   env[1] = "";

   std::string tempdir;
   createTemporaryDirectory("languages", tempdir);

#define createTranslation(lang) \
   createFile(tempdir, std::string("/ftp.de.debian.org_debian_dists_sid_main_i18n_Translation-").append(lang));

   createTranslation("tr");
   createTranslation("pt");
   createTranslation("se~");
   createTranslation("st.bak");
   createTranslation("ast_DE");
   createTranslation("tlh%5fDE");

   _config->Set("Dir::State::lists", tempdir);
   std::vector<std::string> vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(8, vec.size());
   EXPECT_EQ("de_DE", vec[0]);
   EXPECT_EQ("de", vec[1]);
   EXPECT_EQ("en", vec[2]);
   EXPECT_EQ("none", vec[3]);
   EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "pt"));
   EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tr"));
   EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "ast_DE"));
   EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tlh_DE"));
   EXPECT_NE(vec[4], vec[5]);
   EXPECT_NE(vec[4], vec[6]);
   EXPECT_NE(vec[4], vec[7]);
   EXPECT_NE(vec[5], vec[6]);
   EXPECT_NE(vec[5], vec[7]);
   EXPECT_NE(vec[6], vec[7]);

   _config->Set("Acquire::Languages", "none");
   vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("none", vec[0]);
   _config->Set("Acquire::Languages", "");

   _config->Set("Dir::State::lists", "/non-existing-dir");
   _config->Set("Acquire::Languages::1", "none");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_TRUE(vec.empty());
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(2, vec.size());
   EXPECT_EQ("en", vec[0]);
   EXPECT_EQ("de", vec[1]);

   _config->Set("Acquire::Languages::1", "fr");
   _config->Set("Acquire::Languages", "de_DE");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(false, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("de_DE", vec[0]);

   _config->Set("Acquire::Languages", "none");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("none", vec[0]);

   _config->Set("Acquire::Languages", "");
   //FIXME: Remove support for this deprecated setting
   _config->Set("APT::Acquire::Translation", "ast_DE");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(2, vec.size());
   EXPECT_EQ("ast_DE", vec[0]);
   EXPECT_EQ("en", vec[1]);
   _config->Set("APT::Acquire::Translation", "none");
   env[0] = "de_DE.UTF-8";
   vec = APT::Configuration::getLanguages(true, false, env);
   EXPECT_EQ(1, vec.size());
   EXPECT_EQ("en", vec[0]);


   EXPECT_EQ(0, system(std::string("rm -rf ").append(tempdir).c_str()));
   _config->Clear();
}