compareversion_test.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Version Test - Simple program to run through a file and comare versions.
  5. Each version is compared and the result is checked against an expected
  6. result in the file. The format of the file is
  7. a b Res
  8. Where Res is -1, 1, 0. dpkg -D=1 --compare-versions a "<" b can be
  9. used to determine what Res should be. # at the start of the line
  10. is a comment and blank lines are skipped
  11. The runner will also call dpkg --compare-versions to check if APT and
  12. dpkg have (still) the same idea.
  13. ##################################################################### */
  14. /*}}}*/
  15. #include <config.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/debversion.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <fstream>
  20. #include <string>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/wait.h>
  24. #include <gtest/gtest.h>
  25. using namespace std;
  26. static bool callDPKG(const char *val, const char *ref, const char &op) {
  27. pid_t Process = ExecFork();
  28. if (Process == 0)
  29. {
  30. const char * args[6];
  31. args[0] = "/usr/bin/dpkg";
  32. args[1] = "--compare-versions";
  33. args[2] = val;
  34. args[3] = (op == 1) ? ">>" : ( (op == 0) ? "=" : "<<");
  35. args[4] = ref;
  36. args[5] = 0;
  37. execv(args[0], (char**) args);
  38. exit(1);
  39. }
  40. int Ret;
  41. waitpid(Process, &Ret, 0);
  42. return WIFEXITED(Ret) == true && WEXITSTATUS(Ret) == 0;
  43. }
  44. #define EXPECT_VERSION_PART(A, compare, B) \
  45. { \
  46. int Res = debVS.CmpVersion(A, B); \
  47. Res = (Res < 0) ? -1 : ( (Res > 0) ? 1 : Res); \
  48. EXPECT_EQ(compare, Res) << "APT: A: »" << A << "« B: »" << B << "«"; \
  49. EXPECT_PRED3(callDPKG, A, B, compare); \
  50. }
  51. #define EXPECT_VERSION(A, compare, B) \
  52. EXPECT_VERSION_PART(A, compare, B); \
  53. EXPECT_VERSION_PART(B, compare * -1, A)
  54. // History-Remark: The versions used to be specified in a versions.lst file
  55. enum CompareVersionType { LESS = -1, GREATER = 1, EQUAL = 0 };
  56. TEST(CompareVersionTest,Basic)
  57. {
  58. EXPECT_VERSION("7.6p2-4", GREATER, "7.6-0");
  59. EXPECT_VERSION("1.0.3-3", GREATER, "1.0-1");
  60. EXPECT_VERSION("1.3", GREATER, "1.2.2-2");
  61. EXPECT_VERSION("1.3", GREATER, "1.2.2");
  62. /* disabled as dpkg doesn't like them… (versions have to start with a number)
  63. EXPECT_VERSION("-", LESS, ".");
  64. EXPECT_VERSION("p", LESS, "-");
  65. EXPECT_VERSION("a", LESS, "-");
  66. EXPECT_VERSION("z", LESS, "-");
  67. EXPECT_VERSION("a", LESS, ".");
  68. EXPECT_VERSION("z", LESS, ".");
  69. // */
  70. /* disabled as dpkg doesn't like them… (versions have to start with a number)
  71. EXPECT_VERSION("III-alpha9.8", LESS, "III-alpha9.8-1.5");
  72. // */
  73. // Test some properties of text strings
  74. EXPECT_VERSION("0-pre", EQUAL, "0-pre");
  75. EXPECT_VERSION("0-pre", LESS, "0-pree");
  76. EXPECT_VERSION("1.1.6r2-2", GREATER, "1.1.6r-1");
  77. EXPECT_VERSION("2.6b2-1", GREATER, "2.6b-2");
  78. EXPECT_VERSION("98.1p5-1", LESS, "98.1-pre2-b6-2");
  79. EXPECT_VERSION("0.4a6-2", GREATER, "0.4-1");
  80. EXPECT_VERSION("1:3.0.5-2", LESS, "1:3.0.5.1");
  81. }
  82. TEST(CompareVersionTest,Epochs)
  83. {
  84. EXPECT_VERSION("1:0.4", GREATER, "10.3");
  85. EXPECT_VERSION("1:1.25-4", LESS, "1:1.25-8");
  86. EXPECT_VERSION("0:1.18.36", EQUAL, "1.18.36");
  87. EXPECT_VERSION("1.18.36", GREATER, "1.18.35");
  88. EXPECT_VERSION("0:1.18.36", GREATER, "1.18.35");
  89. }
  90. TEST(CompareVersionTest,Strangeness)
  91. {
  92. // Funky, but allowed, characters in upstream version
  93. EXPECT_VERSION("9:1.18.36:5.4-20", LESS, "10:0.5.1-22");
  94. EXPECT_VERSION("9:1.18.36:5.4-20", LESS, "9:1.18.36:5.5-1");
  95. EXPECT_VERSION("9:1.18.36:5.4-20", LESS, " 9:1.18.37:4.3-22");
  96. EXPECT_VERSION("1.18.36-0.17.35-18", GREATER, "1.18.36-19");
  97. // Junk
  98. EXPECT_VERSION("1:1.2.13-3", LESS, "1:1.2.13-3.1");
  99. EXPECT_VERSION("2.0.7pre1-4", LESS, "2.0.7r-1");
  100. // if a version includes a dash, it should be the debrev dash - policy says so…
  101. EXPECT_VERSION("0:0-0-0", GREATER, "0-0");
  102. // do we like strange versions? Yes we like strange versions…
  103. EXPECT_VERSION("0", EQUAL, "0");
  104. EXPECT_VERSION("0", EQUAL, "00");
  105. }
  106. TEST(CompareVersionTest,DebianBug)
  107. {
  108. // #205960
  109. EXPECT_VERSION("3.0~rc1-1", LESS, "3.0-1");
  110. // #573592 - debian policy 5.6.12
  111. EXPECT_VERSION("1.0", EQUAL, "1.0-0");
  112. EXPECT_VERSION("0.2", LESS, "1.0-0");
  113. EXPECT_VERSION("1.0", LESS, "1.0-0+b1");
  114. EXPECT_VERSION("1.0", GREATER, "1.0-0~");
  115. }
  116. TEST(CompareVersionTest,CuptTests)
  117. {
  118. // "steal" the testcases from (old perl) cupt
  119. EXPECT_VERSION("1.2.3", EQUAL, "1.2.3"); // identical
  120. EXPECT_VERSION("4.4.3-2", EQUAL, "4.4.3-2"); // identical
  121. EXPECT_VERSION("1:2ab:5", EQUAL, "1:2ab:5"); // this is correct...
  122. EXPECT_VERSION("7:1-a:b-5", EQUAL, "7:1-a:b-5"); // and this
  123. EXPECT_VERSION("57:1.2.3abYZ+~-4-5", EQUAL, "57:1.2.3abYZ+~-4-5"); // and those too
  124. EXPECT_VERSION("1.2.3", EQUAL, "0:1.2.3"); // zero epoch
  125. EXPECT_VERSION("1.2.3", EQUAL, "1.2.3-0"); // zero revision
  126. EXPECT_VERSION("009", EQUAL, "9"); // zeroes…
  127. EXPECT_VERSION("009ab5", EQUAL, "9ab5"); // there as well
  128. EXPECT_VERSION("1.2.3", LESS, "1.2.3-1"); // added non-zero revision
  129. EXPECT_VERSION("1.2.3", LESS, "1.2.4"); // just bigger
  130. EXPECT_VERSION("1.2.4", GREATER, "1.2.3"); // order doesn't matter
  131. EXPECT_VERSION("1.2.24", GREATER, "1.2.3"); // bigger, eh?
  132. EXPECT_VERSION("0.10.0", GREATER, "0.8.7"); // bigger, eh?
  133. EXPECT_VERSION("3.2", GREATER, "2.3"); // major number rocks
  134. EXPECT_VERSION("1.3.2a", GREATER, "1.3.2"); // letters rock
  135. EXPECT_VERSION("0.5.0~git", LESS, "0.5.0~git2"); // numbers rock
  136. EXPECT_VERSION("2a", LESS, "21"); // but not in all places
  137. EXPECT_VERSION("1.3.2a", LESS, "1.3.2b"); // but there is another letter
  138. EXPECT_VERSION("1:1.2.3", GREATER, "1.2.4"); // epoch rocks
  139. EXPECT_VERSION("1:1.2.3", LESS, "1:1.2.4"); // bigger anyway
  140. EXPECT_VERSION("1.2a+~bCd3", LESS, "1.2a++"); // tilde doesn't rock
  141. EXPECT_VERSION("1.2a+~bCd3", GREATER, "1.2a+~"); // but first is longer!
  142. EXPECT_VERSION("5:2", GREATER, "304-2"); // epoch rocks
  143. EXPECT_VERSION("5:2", LESS, "304:2"); // so big epoch?
  144. EXPECT_VERSION("25:2", GREATER, "3:2"); // 25 > 3, obviously
  145. EXPECT_VERSION("1:2:123", LESS, "1:12:3"); // 12 > 2
  146. EXPECT_VERSION("1.2-5", LESS, "1.2-3-5"); // 1.2 < 1.2-3
  147. EXPECT_VERSION("5.10.0", GREATER, "5.005"); // preceding zeroes don't matters
  148. EXPECT_VERSION("3a9.8", LESS, "3.10.2"); // letters are before all letter symbols
  149. EXPECT_VERSION("3a9.8", GREATER, "3~10"); // but after the tilde
  150. EXPECT_VERSION("1.4+OOo3.0.0~", LESS, "1.4+OOo3.0.0-4"); // another tilde check
  151. EXPECT_VERSION("2.4.7-1", LESS, "2.4.7-z"); // revision comparing
  152. EXPECT_VERSION("1.002-1+b2", GREATER, "1.00"); // whatever...
  153. /* disabled as dpkg doesn't like them… (versions with illegal char)
  154. EXPECT_VERSION("2.2.4-47978_Debian_lenny", EQUAL, "2.2.4-47978_Debian_lenny"); // and underscore...
  155. // */
  156. }