You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

235 lines
7.0 KiB

  1. #include <config.h>
  2. #include <apt-pkg/aptconfiguration.h>
  3. #include <apt-pkg/configuration.h>
  4. #include <apt-pkg/fileutl.h>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <gtest/gtest.h>
  13. #include "file-helpers.h"
  14. TEST(LanguagesTest,Environment)
  15. {
  16. _config->Clear();
  17. char const* env[2];
  18. env[0] = "de_DE.UTF-8";
  19. env[1] = "";
  20. std::vector<std::string> vec = APT::Configuration::getLanguages(false, false, env);
  21. ASSERT_EQ(3 ,vec.size());
  22. EXPECT_EQ("de_DE", vec[0]);
  23. EXPECT_EQ("de", vec[1]);
  24. EXPECT_EQ("en", vec[2]);
  25. // Special: Check if the cache is actually in use
  26. env[0] = "en_GB.UTF-8";
  27. vec = APT::Configuration::getLanguages(false, true, env);
  28. ASSERT_EQ(3, vec.size());
  29. EXPECT_EQ("de_DE", vec[0]);
  30. EXPECT_EQ("de", vec[1]);
  31. EXPECT_EQ("en", vec[2]);
  32. env[0] = "en_GB.UTF-8";
  33. vec = APT::Configuration::getLanguages(false, false, env);
  34. ASSERT_EQ(2, vec.size());
  35. EXPECT_EQ("en_GB", vec[0]);
  36. EXPECT_EQ("en", vec[1]);
  37. // esperanto
  38. env[0] = "eo.UTF-8";
  39. vec = APT::Configuration::getLanguages(false, false, env);
  40. ASSERT_EQ(2, vec.size());
  41. EXPECT_EQ("eo", vec[0]);
  42. EXPECT_EQ("en", vec[1]);
  43. env[0] = "tr_DE@euro";
  44. vec = APT::Configuration::getLanguages(false, false, env);
  45. EXPECT_EQ(3, vec.size());
  46. EXPECT_EQ("tr_DE", vec[0]);
  47. EXPECT_EQ("tr", vec[1]);
  48. EXPECT_EQ("en", vec[2]);
  49. env[0] = "de_NO";
  50. env[1] = "de_NO:en_GB:nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en";
  51. vec = APT::Configuration::getLanguages(false, false, env);
  52. EXPECT_EQ(6, vec.size());
  53. EXPECT_EQ("de_NO", vec[0]);
  54. EXPECT_EQ("de", vec[1]);
  55. EXPECT_EQ("en_GB", vec[2]);
  56. EXPECT_EQ("nb_NO", vec[3]);
  57. EXPECT_EQ("nb", vec[4]);
  58. EXPECT_EQ("en", vec[5]);
  59. env[0] = "pt_PR.UTF-8";
  60. env[1] = "";
  61. vec = APT::Configuration::getLanguages(false, false, env);
  62. EXPECT_EQ(3, vec.size());
  63. EXPECT_EQ("pt_PR", vec[0]);
  64. EXPECT_EQ("pt", vec[1]);
  65. EXPECT_EQ("en", vec[2]);
  66. env[0] = "ast_DE.UTF-8";
  67. vec = APT::Configuration::getLanguages(false, false, env); // bogus, but syntactical correct
  68. EXPECT_EQ(3, vec.size());
  69. EXPECT_EQ("ast_DE", vec[0]);
  70. EXPECT_EQ("ast", vec[1]);
  71. EXPECT_EQ("en", vec[2]);
  72. env[0] = "C";
  73. vec = APT::Configuration::getLanguages(false, false, env);
  74. EXPECT_EQ(1, vec.size());
  75. EXPECT_EQ("en", vec[0]);
  76. _config->Set("Acquire::Languages", "none");
  77. env[0] = "C";
  78. vec = APT::Configuration::getLanguages(false, false, env);
  79. EXPECT_TRUE(vec.empty());
  80. _config->Set("Acquire::Languages", "environment");
  81. env[0] = "C";
  82. vec = APT::Configuration::getLanguages(false, false, env);
  83. EXPECT_EQ(1, vec.size());
  84. EXPECT_EQ("en", vec[0]);
  85. _config->Set("Acquire::Languages", "de");
  86. env[0] = "C";
  87. vec = APT::Configuration::getLanguages(false, false, env);
  88. EXPECT_EQ(1, vec.size());
  89. EXPECT_EQ("de", vec[0]);
  90. _config->Set("Acquire::Languages", "fr");
  91. env[0] = "ast_DE.UTF-8";
  92. vec = APT::Configuration::getLanguages(false, false, env);
  93. EXPECT_EQ(1, vec.size());
  94. EXPECT_EQ("fr", vec[0]);
  95. _config->Set("Acquire::Languages", "environment,en");
  96. env[0] = "de_DE.UTF-8";
  97. vec = APT::Configuration::getLanguages(false, false, env);
  98. EXPECT_EQ(3, vec.size());
  99. EXPECT_EQ("de_DE", vec[0]);
  100. EXPECT_EQ("de", vec[1]);
  101. EXPECT_EQ("en", vec[2]);
  102. _config->Set("Acquire::Languages", "");
  103. _config->Set("Acquire::Languages::1", "environment");
  104. _config->Set("Acquire::Languages::2", "en");
  105. env[0] = "de_DE.UTF-8";
  106. vec = APT::Configuration::getLanguages(false, false, env);
  107. EXPECT_EQ(3, vec.size());
  108. EXPECT_EQ("de_DE", vec[0]);
  109. EXPECT_EQ("de", vec[1]);
  110. EXPECT_EQ("en", vec[2]);
  111. _config->Set("Acquire::Languages::3", "de");
  112. env[0] = "de_DE.UTF-8";
  113. vec = APT::Configuration::getLanguages(false, false, env);
  114. EXPECT_EQ(3, vec.size());
  115. EXPECT_EQ("de_DE", vec[0]);
  116. EXPECT_EQ("de", vec[1]);
  117. EXPECT_EQ("en", vec[2]);
  118. _config->Clear();
  119. }
  120. TEST(LanguagesTest,TranslationFiles)
  121. {
  122. _config->Clear();
  123. _config->Set("Acquire::Languages::1", "environment");
  124. _config->Set("Acquire::Languages::2", "en");
  125. _config->Set("Acquire::Languages::3", "de");
  126. char const* env[2];
  127. env[0] = "de_DE.UTF-8";
  128. env[1] = "";
  129. std::string tempdir;
  130. createTemporaryDirectory("languages", tempdir);
  131. #define createTranslation(lang) \
  132. createFile(tempdir, std::string("/ftp.de.debian.org_debian_dists_sid_main_i18n_Translation-").append(lang));
  133. createTranslation("tr");
  134. createTranslation("pt");
  135. createTranslation("se~");
  136. createTranslation("st.bak");
  137. createTranslation("ast_DE");
  138. createTranslation("tlh%5fDE");
  139. _config->Set("Dir::State::lists", tempdir);
  140. std::vector<std::string> vec = APT::Configuration::getLanguages(true, false, env);
  141. EXPECT_EQ(8, vec.size());
  142. EXPECT_EQ("de_DE", vec[0]);
  143. EXPECT_EQ("de", vec[1]);
  144. EXPECT_EQ("en", vec[2]);
  145. EXPECT_EQ("none", vec[3]);
  146. EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "pt"));
  147. EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tr"));
  148. EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "ast_DE"));
  149. EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tlh_DE"));
  150. EXPECT_NE(vec[4], vec[5]);
  151. EXPECT_NE(vec[4], vec[6]);
  152. EXPECT_NE(vec[4], vec[7]);
  153. EXPECT_NE(vec[5], vec[6]);
  154. EXPECT_NE(vec[5], vec[7]);
  155. EXPECT_NE(vec[6], vec[7]);
  156. _config->Set("Acquire::Languages", "none");
  157. vec = APT::Configuration::getLanguages(true, false, env);
  158. EXPECT_EQ(1, vec.size());
  159. EXPECT_EQ("none", vec[0]);
  160. _config->Set("Acquire::Languages", "");
  161. _config->Set("Dir::State::lists", "/non-existing-dir");
  162. _config->Set("Acquire::Languages::1", "none");
  163. env[0] = "de_DE.UTF-8";
  164. vec = APT::Configuration::getLanguages(false, false, env);
  165. EXPECT_TRUE(vec.empty());
  166. env[0] = "de_DE.UTF-8";
  167. vec = APT::Configuration::getLanguages(true, false, env);
  168. EXPECT_EQ(2, vec.size());
  169. EXPECT_EQ("en", vec[0]);
  170. EXPECT_EQ("de", vec[1]);
  171. _config->Set("Acquire::Languages::1", "fr");
  172. _config->Set("Acquire::Languages", "de_DE");
  173. env[0] = "de_DE.UTF-8";
  174. vec = APT::Configuration::getLanguages(false, false, env);
  175. EXPECT_EQ(1, vec.size());
  176. EXPECT_EQ("de_DE", vec[0]);
  177. _config->Set("Acquire::Languages", "none");
  178. env[0] = "de_DE.UTF-8";
  179. vec = APT::Configuration::getLanguages(true, false, env);
  180. EXPECT_EQ(1, vec.size());
  181. EXPECT_EQ("none", vec[0]);
  182. _config->Set("Acquire::Languages", "");
  183. //FIXME: Remove support for this deprecated setting
  184. _config->Set("APT::Acquire::Translation", "ast_DE");
  185. env[0] = "de_DE.UTF-8";
  186. vec = APT::Configuration::getLanguages(true, false, env);
  187. EXPECT_EQ(2, vec.size());
  188. EXPECT_EQ("ast_DE", vec[0]);
  189. EXPECT_EQ("en", vec[1]);
  190. _config->Set("APT::Acquire::Translation", "none");
  191. env[0] = "de_DE.UTF-8";
  192. vec = APT::Configuration::getLanguages(true, false, env);
  193. EXPECT_EQ(1, vec.size());
  194. EXPECT_EQ("en", vec[0]);
  195. EXPECT_EQ(0, system(std::string("rm -rf ").append(tempdir).c_str()));
  196. _config->Clear();
  197. }