test_utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 apt_pkg
  19. import unittest
  20. from base_test import DakTestCase
  21. from daklib.utils import (is_in_debug_section,
  22. parse_built_using,
  23. extract_component_from_section,
  24. _gpg_get_addresses_from_listing,
  25. ArchKey)
  26. apt_pkg.init()
  27. class UtilsTest(DakTestCase):
  28. def test_utils_ArchKey(self):
  29. source = ArchKey('source')
  30. arch1 = ArchKey('amd64')
  31. arch2 = ArchKey('i386')
  32. assert source.__eq__(source)
  33. assert not source.__lt__(source)
  34. assert arch1.__eq__(arch1)
  35. assert not arch1.__lt__(arch1)
  36. assert not source.__eq__(arch1)
  37. assert not arch1.__eq__(source)
  38. assert source.__lt__(arch1)
  39. assert not arch1.__lt__(source)
  40. assert not arch1.__eq__(arch2)
  41. assert not arch2.__eq__(arch1)
  42. assert arch1.__lt__(arch2)
  43. assert not arch2.__lt__(arch1)
  44. def test_is_in_debug_section(self):
  45. data = [
  46. (
  47. {
  48. 'Section': 'debug',
  49. 'Auto-Built-Package': 'debug-symbols',
  50. },
  51. True,
  52. ),
  53. (
  54. {
  55. 'Section': 'non-free/debug',
  56. 'Auto-Built-Package': 'debug-symbols',
  57. },
  58. True
  59. ),
  60. (
  61. {
  62. 'Section': 'debug',
  63. 'Auto-Built-Package': 'other',
  64. },
  65. False
  66. ),
  67. (
  68. {
  69. 'Section': 'non-free/debug',
  70. 'Auto-Built-Package': 'other',
  71. },
  72. False
  73. ),
  74. (
  75. {
  76. 'Section': 'debug',
  77. },
  78. False
  79. ),
  80. (
  81. {
  82. 'Section': 'non-free/debug',
  83. },
  84. False
  85. ),
  86. ]
  87. for ctrl, r in data:
  88. self.assertEqual(is_in_debug_section(ctrl), r)
  89. def test_parse_built_using(self):
  90. field = 'binutils (= 1.0-1), gcc-6 (= 6.3-2)'
  91. expected = [('binutils', '1.0-1'), ('gcc-6', '6.3-2')]
  92. ctrl = {
  93. 'Built-Using': field,
  94. }
  95. self.assertEqual(parse_built_using(ctrl), expected)
  96. self.assertEqual(parse_built_using({}), [])
  97. def test_extract_component_from_section(self):
  98. data = [
  99. # Argument is passed through as first return value. There
  100. # is a comment in docs/TODO.old suggesting that it should
  101. # be changed.
  102. ('utils', ('utils', 'main')),
  103. ('main/utils', ('main/utils', 'main')),
  104. ('non-free/libs', ('non-free/libs', 'non-free')),
  105. ('contrib/net', ('contrib/net', 'contrib')),
  106. ('non-free/two/slashes', ('non-free/two/slashes', 'non-free'))
  107. ]
  108. for v, r in data:
  109. self.assertEqual(extract_component_from_section(v), r)
  110. def test_gpg_get_addresses_from_listing(self):
  111. # Test with output containing a uid encoded in Latin-1 and UTF-8 each
  112. data = (
  113. b"tru::1:1610798976:1673870895:3:1:5\n"
  114. b"pub:u:3072:1:4847924E3DEDDF1E:1610798895:1673870895::u:::scESC::::::23::0:\n"
  115. b"fpr:::::::::AC6DFC4B78223BFCC1C064004847924E3DEDDF1E:\n"
  116. b"uid:u::::1610798964::1D4AB6BD4AB8A446C196A1801F367B83B6141CC0::Markus Mustermann-S\xf6der <mustermann-soeder@example.org>::::::::::0:\n"
  117. b"uid:u::::1610798895::F60CF610557A38E2943DECEEA5B02EE57B1ECC24::Markus Mustermann-S\xc3\xb6der <mustermann-soeder@example.com>::::::::::0:\n"
  118. b"sub:u:3072:1:C3BCC6A42035DDB3:1610798895:1673870895:::::e::::::23:\n"
  119. b"fpr:::::::::C2CC5D5744746B5E7F5F7286C3BCC6A42035DDB3:\n"
  120. )
  121. addresses = _gpg_get_addresses_from_listing(data)
  122. expected = ['mustermann-soeder@example.org', 'mustermann-soeder@example.com']
  123. self.assertEqual(addresses, expected)
  124. if __name__ == '__main__':
  125. unittest.main()