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.
 
 
 
 
 
 

242 lines
7.2 KiB

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