t-version.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-version.c - test version handling
  4. *
  5. * Copyright © 2009 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <dpkg-test.h>
  22. #include <dpkg-db.h>
  23. #define version(epoch, version, revision) \
  24. (struct versionrevision) { (epoch), (version), (revision) }
  25. static void
  26. test_version_compare(void)
  27. {
  28. struct versionrevision a, b;
  29. blankversion(&a);
  30. blankversion(&b);
  31. test_fail(epochsdiffer(&a, &b));
  32. a.epoch = 1;
  33. b.epoch = 2;
  34. test_pass(epochsdiffer(&a, &b));
  35. /* Test for version equality. */
  36. a = b = version(0, "0", "0");
  37. test_pass(versioncompare(&a, &b) == 0);
  38. a = version(0, "0", "00");
  39. b = version(0, "00", "0");
  40. test_pass(versioncompare(&a, &b) == 0);
  41. a = b = version(1, "2", "3");
  42. test_pass(versioncompare(&a, &b) == 0);
  43. /* Test for epoch difference. */
  44. a = version(0, "0", "0");
  45. b = version(1, "0", "0");
  46. test_pass(versioncompare(&a, &b) < 0);
  47. test_pass(versioncompare(&b, &a) > 0);
  48. /* FIXME: Complete. */
  49. }
  50. static void
  51. test_version_parse(void)
  52. {
  53. struct versionrevision a, b;
  54. /* Test 0 versions. */
  55. blankversion(&a);
  56. b = version(0, "0", "");
  57. test_pass(parseversion(&a, "0") == NULL);
  58. test_pass(versioncompare(&a, &b) == 0);
  59. test_pass(parseversion(&a, "0:0") == NULL);
  60. test_pass(versioncompare(&a, &b) == 0);
  61. test_pass(parseversion(&a, "0:0-") == NULL);
  62. test_pass(versioncompare(&a, &b) == 0);
  63. b = version(0, "0", "0");
  64. test_pass(parseversion(&a, "0:0-0") == NULL);
  65. test_pass(versioncompare(&a, &b) == 0);
  66. b = version(0, "0.0", "0.0");
  67. test_pass(parseversion(&a, "0:0.0-0.0") == NULL);
  68. test_pass(versioncompare(&a, &b) == 0);
  69. /* Test epoched versions. */
  70. b = version(1, "0", "");
  71. test_pass(parseversion(&a, "1:0") == NULL);
  72. test_pass(versioncompare(&a, &b) == 0);
  73. b = version(5, "1", "");
  74. test_pass(parseversion(&a, "5:1") == NULL);
  75. test_pass(versioncompare(&a, &b) == 0);
  76. /* Test multiple dashes. */
  77. b = version(0, "0-0", "0");
  78. test_pass(parseversion(&a, "0:0-0-0") == NULL);
  79. test_pass(versioncompare(&a, &b) == 0);
  80. b = version(0, "0-0-0", "0");
  81. test_pass(parseversion(&a, "0:0-0-0-0") == NULL);
  82. test_pass(versioncompare(&a, &b) == 0);
  83. /* Test multiple colons. */
  84. b = version(0, "0:0", "0");
  85. test_pass(parseversion(&a, "0:0:0-0") == NULL);
  86. test_pass(versioncompare(&a, &b) == 0);
  87. b = version(0, "0:0:0", "0");
  88. test_pass(parseversion(&a, "0:0:0:0-0") == NULL);
  89. test_pass(versioncompare(&a, &b) == 0);
  90. /* Test multiple dashes and colons. */
  91. b = version(0, "0:0-0", "0");
  92. test_pass(parseversion(&a, "0:0:0-0-0") == NULL);
  93. test_pass(versioncompare(&a, &b) == 0);
  94. b = version(0, "0-0:0", "0");
  95. test_pass(parseversion(&a, "0:0-0:0-0") == NULL);
  96. test_pass(versioncompare(&a, &b) == 0);
  97. /* Test valid characters in upstream version. */
  98. b = version(0, "azAZ09.-+~:", "0");
  99. test_pass(parseversion(&a, "0:azAZ09.-+~:-0") == NULL);
  100. test_pass(versioncompare(&a, &b) == 0);
  101. /* Test valid characters in revision. */
  102. b = version(0, "0", "azAZ09.+~");
  103. test_pass(parseversion(&a, "0:0-azAZ09.+~") == NULL);
  104. test_pass(versioncompare(&a, &b) == 0);
  105. /* Test invalid characters in epoch. */
  106. test_fail(parseversion(&a, "a:0-0") == NULL);
  107. test_fail(parseversion(&a, "A:0-0") == NULL);
  108. /* FIXME: parseversion() should validate input! */
  109. #if 0
  110. /* Test invalid characters in upstream version. */
  111. test_fail(parseversion(&a, "0:!#@$%&/|\\<>()[]{};,=*^'-0") == NULL);
  112. /* Test invalid characters in revision. */
  113. test_fail(parseversion(&a, "0:0-!#@$%&/|\\<>()[]{};,=*^'") == NULL);
  114. #endif
  115. /* FIXME: Complete. */
  116. }
  117. static void
  118. test(void)
  119. {
  120. test_version_compare();
  121. test_version_parse();
  122. }