test_utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /usr/bin/env python3
  2. #
  3. # Copyright (C) 2017, Niels Thykier <niels@thykier.net>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. import unittest
  19. import apt_pkg
  20. from base_test import DakTestCase
  21. from daklib.utils import (
  22. ArchKey,
  23. _gpg_get_addresses_from_listing,
  24. extract_component_from_section,
  25. is_in_debug_section,
  26. parse_built_using,
  27. )
  28. apt_pkg.init()
  29. class UtilsTest(DakTestCase):
  30. def test_utils_ArchKey(self):
  31. source = ArchKey("source")
  32. arch1 = ArchKey("amd64")
  33. arch2 = ArchKey("i386")
  34. assert source.__eq__(source)
  35. assert not source.__lt__(source)
  36. assert arch1.__eq__(arch1)
  37. assert not arch1.__lt__(arch1)
  38. assert not source.__eq__(arch1)
  39. assert not arch1.__eq__(source)
  40. assert source.__lt__(arch1)
  41. assert not arch1.__lt__(source)
  42. assert not arch1.__eq__(arch2)
  43. assert not arch2.__eq__(arch1)
  44. assert arch1.__lt__(arch2)
  45. assert not arch2.__lt__(arch1)
  46. def test_is_in_debug_section(self):
  47. data = [
  48. (
  49. {
  50. "Section": "debug",
  51. "Auto-Built-Package": "debug-symbols",
  52. },
  53. True,
  54. ),
  55. (
  56. {
  57. "Section": "non-free/debug",
  58. "Auto-Built-Package": "debug-symbols",
  59. },
  60. True,
  61. ),
  62. (
  63. {
  64. "Section": "debug",
  65. "Auto-Built-Package": "other",
  66. },
  67. False,
  68. ),
  69. (
  70. {
  71. "Section": "non-free/debug",
  72. "Auto-Built-Package": "other",
  73. },
  74. False,
  75. ),
  76. (
  77. {
  78. "Section": "debug",
  79. },
  80. False,
  81. ),
  82. (
  83. {
  84. "Section": "non-free/debug",
  85. },
  86. False,
  87. ),
  88. ]
  89. for ctrl, r in data:
  90. self.assertEqual(is_in_debug_section(ctrl), r)
  91. def test_parse_built_using(self):
  92. field = "binutils (= 1.0-1), gcc-6 (= 6.3-2)"
  93. expected = [("binutils", "1.0-1"), ("gcc-6", "6.3-2")]
  94. ctrl = {
  95. "Built-Using": field,
  96. }
  97. self.assertEqual(parse_built_using(ctrl), expected)
  98. self.assertEqual(parse_built_using({}), [])
  99. def test_extract_component_from_section(self):
  100. data = [
  101. # Argument is passed through as first return value. There
  102. # is a comment in docs/TODO.old suggesting that it should
  103. # be changed.
  104. ("utils", ("utils", "main")),
  105. ("main/utils", ("main/utils", "main")),
  106. ("non-free/libs", ("non-free/libs", "non-free")),
  107. ("contrib/net", ("contrib/net", "contrib")),
  108. ("non-free/two/slashes", ("non-free/two/slashes", "non-free")),
  109. ]
  110. for v, r in data:
  111. self.assertEqual(extract_component_from_section(v), r)
  112. def test_gpg_get_addresses_from_listing(self):
  113. # Test with output containing a uid encoded in Latin-1 and UTF-8 each
  114. data = (
  115. b"tru::1:1610798976:1673870895:3:1:5\n"
  116. b"pub:u:3072:1:4847924E3DEDDF1E:1610798895:1673870895::u:::scESC::::::23::0:\n"
  117. b"fpr:::::::::AC6DFC4B78223BFCC1C064004847924E3DEDDF1E:\n"
  118. b"uid:u::::1610798964::1D4AB6BD4AB8A446C196A1801F367B83B6141CC0::Markus Mustermann-S\xf6der <mustermann-soeder@example.org>::::::::::0:\n"
  119. b"uid:u::::1610798895::F60CF610557A38E2943DECEEA5B02EE57B1ECC24::Markus Mustermann-S\xc3\xb6der <mustermann-soeder@example.com>::::::::::0:\n"
  120. b"sub:u:3072:1:C3BCC6A42035DDB3:1610798895:1673870895:::::e::::::23:\n"
  121. b"fpr:::::::::C2CC5D5744746B5E7F5F7286C3BCC6A42035DDB3:\n"
  122. )
  123. addresses = _gpg_get_addresses_from_listing(data)
  124. expected = ["mustermann-soeder@example.org", "mustermann-soeder@example.com"]
  125. self.assertEqual(addresses, expected)
  126. if __name__ == "__main__":
  127. unittest.main()