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