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.
 
 
 
 
 
 

281 lines
12 KiB

  1. #include <config.h>
  2. #include <apt-pkg/configuration.h>
  3. #include <apt-pkg/deblistparser.h>
  4. #include <apt-pkg/pkgcache.h>
  5. #include <string>
  6. #include <string.h>
  7. #include <gtest/gtest.h>
  8. static void parseDependency(bool const StripMultiArch, bool const ParseArchFlags, bool const ParseRestrictionsList, std::string Arch)
  9. {
  10. std::string Package;
  11. std::string Version;
  12. unsigned int Op = 5;
  13. unsigned int Null = 0;
  14. // The tests are made for amd64. Specify a different arch here to check if
  15. // they still work.
  16. _config->Set("APT::Architecture",Arch);
  17. _config->Set("APT::Build-Profiles","stage1");
  18. const char* Depends =
  19. "debhelper:any (>= 5.0), "
  20. "libdb-dev:any, "
  21. "gettext:native (<= 0.12), "
  22. "libcurl4-gnutls-dev:native | libcurl3-gnutls-dev (>> 7.15.5), "
  23. "docbook-xml, "
  24. "apt (>= 0.7.25), "
  25. "not-for-me [ !amd64 ], "
  26. "only-for-me [ amd64 ], "
  27. "any-for-me [ any ], "
  28. "not-for-darwin [ !darwin-any ], "
  29. "cpu-for-me [ any-amd64 ], "
  30. "os-for-me [ linux-any ], "
  31. "libc-for-me [ gnu-linux-any ], "
  32. "libc-not-for-me [ musl-linux-any ], "
  33. "cpu-not-for-me [ any-armel ], "
  34. "os-not-for-me [ kfreebsd-any ], "
  35. "not-in-stage1 <!stage1>, "
  36. "not-stage1-and-not-nodoc <!nodoc !stage1>, "
  37. "not-stage1-or-not-nodoc <!nodoc> <!stage1>, "
  38. "unknown-profile <unknown stage1>, "
  39. "overlord-dev:any (= 7.15.3~) | overlord-dev:native (>> 7.15.5), "
  40. ;
  41. // Stripping MultiArch is currently the default setting to not confuse
  42. // non-MultiArch capable users of the library with "strange" extensions.
  43. const char* Start = Depends;
  44. const char* End = Depends + strlen(Depends);
  45. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  46. if (StripMultiArch == true)
  47. EXPECT_EQ("debhelper", Package);
  48. else
  49. EXPECT_EQ("debhelper:any", Package);
  50. EXPECT_EQ("5.0", Version);
  51. EXPECT_EQ(Null | pkgCache::Dep::GreaterEq, Op);
  52. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  53. if (StripMultiArch == true)
  54. EXPECT_EQ("libdb-dev", Package);
  55. else
  56. EXPECT_EQ("libdb-dev:any", Package);
  57. EXPECT_EQ("", Version);
  58. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  59. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  60. if (StripMultiArch == true)
  61. EXPECT_EQ("gettext", Package);
  62. else
  63. EXPECT_EQ("gettext:native", Package);
  64. EXPECT_EQ("0.12", Version);
  65. EXPECT_EQ(Null | pkgCache::Dep::LessEq, Op);
  66. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  67. if (StripMultiArch == true)
  68. EXPECT_EQ("libcurl4-gnutls-dev", Package);
  69. else
  70. EXPECT_EQ("libcurl4-gnutls-dev:native", Package);
  71. EXPECT_EQ("", Version);
  72. EXPECT_EQ(Null | pkgCache::Dep::Or, Op);
  73. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  74. EXPECT_EQ("libcurl3-gnutls-dev", Package);
  75. EXPECT_EQ("7.15.5", Version);
  76. EXPECT_EQ(Null | pkgCache::Dep::Greater, Op);
  77. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  78. EXPECT_EQ("docbook-xml", Package);
  79. EXPECT_EQ("", Version);
  80. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  81. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  82. EXPECT_EQ("apt", Package);
  83. EXPECT_EQ("0.7.25", Version);
  84. EXPECT_EQ(Null | pkgCache::Dep::GreaterEq, Op);
  85. if (ParseArchFlags == true) {
  86. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  87. EXPECT_EQ("", Package); // not-for-me
  88. } else {
  89. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  90. Start = strstr(Start, ",");
  91. Start++;
  92. }
  93. if (ParseArchFlags == true) {
  94. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  95. EXPECT_EQ("only-for-me", Package);
  96. EXPECT_EQ("", Version);
  97. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  98. } else {
  99. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  100. Start = strstr(Start, ",");
  101. Start++;
  102. }
  103. if (ParseArchFlags == true) {
  104. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  105. EXPECT_EQ("any-for-me", Package);
  106. EXPECT_EQ("", Version);
  107. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  108. } else {
  109. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  110. Start = strstr(Start, ",");
  111. Start++;
  112. }
  113. if (ParseArchFlags == true) {
  114. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  115. EXPECT_EQ("not-for-darwin", Package);
  116. EXPECT_EQ("", Version);
  117. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  118. } else {
  119. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  120. Start = strstr(Start, ",");
  121. Start++;
  122. }
  123. if (ParseArchFlags == true) {
  124. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  125. EXPECT_EQ("cpu-for-me", Package);
  126. EXPECT_EQ("", Version);
  127. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  128. } else {
  129. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  130. Start = strstr(Start, ",");
  131. Start++;
  132. }
  133. if (ParseArchFlags == true) {
  134. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  135. EXPECT_EQ("os-for-me", Package);
  136. EXPECT_EQ("", Version);
  137. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  138. } else {
  139. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  140. Start = strstr(Start, ",");
  141. Start++;
  142. }
  143. if (ParseArchFlags == true) {
  144. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  145. EXPECT_EQ("libc-for-me", Package);
  146. EXPECT_EQ("", Version);
  147. EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
  148. } else {
  149. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  150. Start = strstr(Start, ",");
  151. Start++;
  152. }
  153. if (ParseArchFlags == true) {
  154. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  155. EXPECT_EQ("", Package); // libc-not-for-me
  156. } else {
  157. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  158. Start = strstr(Start, ",");
  159. Start++;
  160. }
  161. if (ParseArchFlags == true) {
  162. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  163. EXPECT_EQ("", Package); // cpu-not-for-me
  164. } else {
  165. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  166. Start = strstr(Start, ",");
  167. Start++;
  168. }
  169. if (ParseArchFlags == true) {
  170. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  171. EXPECT_EQ("", Package); // os-not-for-me
  172. } else {
  173. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  174. Start = strstr(Start, ",");
  175. Start++;
  176. }
  177. if (ParseRestrictionsList == true) {
  178. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  179. EXPECT_EQ("", Package); // not-in-stage1
  180. } else {
  181. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  182. Start = strstr(Start, ",");
  183. Start++;
  184. }
  185. if (ParseRestrictionsList == true) {
  186. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  187. EXPECT_EQ("", Package); // not-stage1-and-not-nodoc
  188. } else {
  189. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  190. Start = strstr(Start, ",");
  191. Start++;
  192. }
  193. if (ParseRestrictionsList == true) {
  194. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  195. EXPECT_EQ("not-stage1-or-not-nodoc", Package);
  196. } else {
  197. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  198. Start = strstr(Start, ",");
  199. Start++;
  200. }
  201. if (ParseRestrictionsList == true) {
  202. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  203. EXPECT_EQ("", Package); // unknown-profile
  204. } else {
  205. EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64"));
  206. Start = strstr(Start, ",");
  207. Start++;
  208. }
  209. Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  210. if (StripMultiArch == true)
  211. EXPECT_EQ("overlord-dev", Package);
  212. else
  213. EXPECT_EQ("overlord-dev:any", Package);
  214. EXPECT_EQ("7.15.3~", Version);
  215. EXPECT_EQ(Null | pkgCache::Dep::Equals | pkgCache::Dep::Or, Op);
  216. debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList, "amd64");
  217. if (StripMultiArch == true)
  218. EXPECT_EQ("overlord-dev", Package);
  219. else
  220. EXPECT_EQ("overlord-dev:native", Package);
  221. EXPECT_EQ("7.15.5", Version);
  222. EXPECT_EQ(Null | pkgCache::Dep::Greater, Op);
  223. }
  224. // FIXME: This testcase is too big/complex
  225. TEST(ParseDependsTest, Everything)
  226. {
  227. bool StripMultiArch = true;
  228. bool ParseArchFlags = false;
  229. bool ParseRestrictionsList = false;
  230. unsigned short runner = 0;
  231. test:
  232. {
  233. SCOPED_TRACE(std::string("StripMultiArch: ") + (StripMultiArch ? "true" : "false"));
  234. SCOPED_TRACE(std::string("ParseArchFlags: ") + (ParseArchFlags ? "true" : "false"));
  235. SCOPED_TRACE(std::string("ParseRestrictionsList: ") + (ParseRestrictionsList ? "true" : "false"));
  236. parseDependency(StripMultiArch, ParseArchFlags, ParseRestrictionsList, "kfreebsd-i386");
  237. parseDependency(StripMultiArch, ParseArchFlags, ParseRestrictionsList, "amd64");
  238. }
  239. if (StripMultiArch == false) {
  240. if (ParseArchFlags == false)
  241. ParseRestrictionsList = !ParseRestrictionsList;
  242. ParseArchFlags = !ParseArchFlags;
  243. }
  244. StripMultiArch = !StripMultiArch;
  245. runner++;
  246. if (runner < 8)
  247. goto test; // this is the prove: tests are really evil ;)
  248. }