2
1

test_lib_hash.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import pytest
  2. import checkpackagelib.test_util as util
  3. import checkpackagelib.lib_hash as m
  4. HashNumberOfFields = [
  5. ('empty file',
  6. 'any',
  7. '',
  8. []),
  9. ('empty line',
  10. 'any',
  11. '\n',
  12. []),
  13. ('ignore whitespace',
  14. 'any',
  15. '\t\n',
  16. []),
  17. ('ignore comments',
  18. 'any',
  19. '# text\n',
  20. []),
  21. ('1 field',
  22. 'any',
  23. 'field1\n',
  24. [['any:1: expected three fields (url#adding-packages-hash)',
  25. 'field1\n']]),
  26. ('2 fields',
  27. 'any',
  28. 'field1 field2\n',
  29. [['any:1: expected three fields (url#adding-packages-hash)',
  30. 'field1 field2\n']]),
  31. ('4 fields',
  32. 'any',
  33. 'field1 field2 field3 field4\n',
  34. [['any:1: expected three fields (url#adding-packages-hash)',
  35. 'field1 field2 field3 field4\n']]),
  36. ('with 1 space',
  37. 'any',
  38. 'field1 field2 field3\n',
  39. []),
  40. ('many spaces',
  41. 'any',
  42. ' field1 field2 field3\n',
  43. []),
  44. ('tabs',
  45. 'any',
  46. 'field1\tfield2\tfield3\n',
  47. []),
  48. ('mix of tabs and spaces',
  49. 'any',
  50. '\tfield1\t field2\t field3 \n',
  51. []),
  52. ]
  53. @pytest.mark.parametrize('testname,filename,string,expected', HashNumberOfFields)
  54. def test_HashNumberOfFields(testname, filename, string, expected):
  55. warnings = util.check_file(m.HashNumberOfFields, filename, string)
  56. assert warnings == expected
  57. HashType = [
  58. ('ignore empty files',
  59. 'any',
  60. '',
  61. []),
  62. ('ignore 1 field',
  63. 'any',
  64. 'text\n',
  65. []),
  66. ('wrong type',
  67. 'any',
  68. 'text text\n',
  69. [['any:1: unexpected type of hash (url#adding-packages-hash)',
  70. 'text text\n']]),
  71. ('md5 (good)',
  72. 'any',
  73. 'md5 12345678901234567890123456789012\n',
  74. []),
  75. ('md5 (short)',
  76. 'any',
  77. 'md5 123456\n',
  78. [['any:1: hash size does not match type (url#adding-packages-hash)',
  79. 'md5 123456\n',
  80. 'expected 32 hex digits']]),
  81. ('ignore space before',
  82. 'any',
  83. ' md5 12345678901234567890123456789012\n',
  84. []),
  85. ('2 spaces',
  86. 'any',
  87. 'md5 12345678901234567890123456789012\n',
  88. []),
  89. ('ignore tabs',
  90. 'any',
  91. 'md5\t12345678901234567890123456789012\n',
  92. []),
  93. ('common typo',
  94. 'any',
  95. 'md5sum 12345678901234567890123456789012\n',
  96. [['any:1: unexpected type of hash (url#adding-packages-hash)',
  97. 'md5sum 12345678901234567890123456789012\n']]),
  98. ('md5 (too long)',
  99. 'any',
  100. 'md5 123456789012345678901234567890123\n',
  101. [['any:1: hash size does not match type (url#adding-packages-hash)',
  102. 'md5 123456789012345678901234567890123\n',
  103. 'expected 32 hex digits']]),
  104. ('sha1 (good)',
  105. 'any',
  106. 'sha1 1234567890123456789012345678901234567890\n',
  107. []),
  108. ('sha256',
  109. 'any',
  110. 'sha256 1234567890123456789012345678901234567890123456789012345678901234\n',
  111. []),
  112. ('sha384',
  113. 'any',
  114. 'sha384 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n',
  115. []),
  116. ('sha512',
  117. 'any',
  118. 'sha512 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678'
  119. '9012345678\n',
  120. []),
  121. ]
  122. @pytest.mark.parametrize('testname,filename,string,expected', HashType)
  123. def test_HashType(testname, filename, string, expected):
  124. warnings = util.check_file(m.HashType, filename, string)
  125. assert warnings == expected
  126. HashSpaces = [
  127. ('ignore empty files',
  128. 'any',
  129. '',
  130. []),
  131. ('ignore 1 field',
  132. 'any',
  133. 'text\n',
  134. []),
  135. ('ignore comments',
  136. 'any',
  137. '# type 1234567890123456789012345678901234567890 file\n',
  138. []),
  139. ('ignore trailing space',
  140. 'any',
  141. 'type 1234567890123456789012345678901234567890 file\t \n',
  142. []),
  143. ('2 spaces',
  144. 'any',
  145. 'type 1234567890123456789012345678901234567890 file\n',
  146. []),
  147. ('1 space',
  148. 'any',
  149. 'type 1234567890123456789012345678901234567890 file\n',
  150. [['any:1: separation does not match expectation (url#adding-packages-hash)',
  151. 'type 1234567890123456789012345678901234567890 file\n']]),
  152. ('3 spaces',
  153. 'any',
  154. 'type 1234567890123456789012345678901234567890 file\n',
  155. [['any:1: separation does not match expectation (url#adding-packages-hash)',
  156. 'type 1234567890123456789012345678901234567890 file\n']]),
  157. ('tabs',
  158. 'any',
  159. 'type\t1234567890123456789012345678901234567890\tfile\n',
  160. [['any:1: separation does not match expectation (url#adding-packages-hash)',
  161. 'type\t1234567890123456789012345678901234567890\tfile\n']]),
  162. ('mixed tabs and spaces',
  163. 'any',
  164. 'type\t 1234567890123456789012345678901234567890 \tfile\n',
  165. [['any:1: separation does not match expectation (url#adding-packages-hash)',
  166. 'type\t 1234567890123456789012345678901234567890 \tfile\n']]),
  167. ]
  168. @pytest.mark.parametrize('testname,filename,string,expected', HashSpaces)
  169. def test_HashSpaces(testname, filename, string, expected):
  170. warnings = util.check_file(m.HashSpaces, filename, string)
  171. assert warnings == expected