]>
Commit | Line | Data |
---|---|---|
453b82a3 DK |
1 | #include <config.h> |
2 | ||
b3501edb | 3 | #include <apt-pkg/configuration.h> |
67dc3830 DK |
4 | #include <apt-pkg/md5.h> |
5 | #include <apt-pkg/sha1.h> | |
6 | #include <apt-pkg/sha2.h> | |
7 | #include <apt-pkg/strutl.h> | |
8 | #include <apt-pkg/hashes.h> | |
163d39cc | 9 | #include <apt-pkg/fileutl.h> |
67dc3830 | 10 | |
453b82a3 DK |
11 | #include <iostream> |
12 | #include <stdlib.h> | |
13 | #include <string> | |
67dc3830 | 14 | |
f00832cc DK |
15 | #include <gtest/gtest.h> |
16 | ||
17 | #include "file-helpers.h" | |
67dc3830 DK |
18 | |
19 | template <class T> void Test(const char *In,const char *Out) | |
20 | { | |
21 | T Sum; | |
22 | Sum.Add(In); | |
23 | equals(Sum.Result().Value(), Out); | |
24 | } | |
25 | ||
f00832cc DK |
26 | |
27 | ||
28 | TEST(HashSumsTest,SummationStrings) | |
67dc3830 | 29 | { |
f00832cc DK |
30 | #define EXPECT_SUM(Summation, In, Out) \ |
31 | { \ | |
32 | Summation Sum; \ | |
33 | Sum.Add(In); \ | |
34 | EXPECT_EQ(Sum.Result().Value(), Out) << #Summation << " for '" << In << "'"; \ | |
35 | } | |
36 | ||
37 | // From FIPS PUB 180-1 | |
38 | EXPECT_SUM(SHA1Summation, "","da39a3ee5e6b4b0d3255bfef95601890afd80709"); | |
39 | EXPECT_SUM(SHA1Summation, "abc","a9993e364706816aba3e25717850c26c9cd0d89d"); | |
40 | EXPECT_SUM(SHA1Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
41 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); | |
42 | ||
43 | // MD5 tests from RFC 1321 | |
44 | EXPECT_SUM(MD5Summation, "","d41d8cd98f00b204e9800998ecf8427e"); | |
45 | EXPECT_SUM(MD5Summation, "a","0cc175b9c0f1b6a831c399e269772661"); | |
46 | EXPECT_SUM(MD5Summation, "abc","900150983cd24fb0d6963f7d28e17f72"); | |
47 | EXPECT_SUM(MD5Summation, "message digest","f96b697d7cb7938d525a2f31aaf161d0"); | |
48 | EXPECT_SUM(MD5Summation, "abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b"); | |
49 | EXPECT_SUM(MD5Summation, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | |
50 | "d174ab98d277d9f5a5611c2c9f419d9f"); | |
51 | EXPECT_SUM(MD5Summation, "12345678901234567890123456789012345678901234567890123456789012345678901234567890", | |
52 | "57edf4a22be3c955ac49da2e2107b67a"); | |
67dc3830 | 53 | |
f00832cc DK |
54 | // SHA-256, From FIPS 180-2 |
55 | EXPECT_SUM(SHA256Summation, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); | |
56 | EXPECT_SUM(SHA256Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
57 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); | |
58 | ||
59 | // SHA-512 | |
60 | EXPECT_SUM(SHA512Summation, "", | |
61 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" | |
62 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); | |
63 | EXPECT_SUM(SHA512Summation, "abc", | |
64 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" | |
65 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); | |
66 | ||
67 | ||
68 | EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6"); | |
69 | EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0"); | |
70 | EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"); | |
71 | EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"); | |
72 | EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"); | |
73 | EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"); | |
74 | EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64" | |
75 | "2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"); | |
76 | EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog.", "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb" | |
77 | "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed"); | |
78 | ||
79 | #undef EXPECT_SUM | |
80 | } | |
81 | TEST(HashSumsTest, Mill) | |
82 | { | |
83 | SHA1Summation Sum1; | |
84 | ||
85 | const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
86 | size_t const AsCount = sizeof(As)/sizeof(As[0]) - 1; | |
87 | size_t Count = 1000000; | |
88 | while (Count != 0) | |
67dc3830 | 89 | { |
f00832cc | 90 | if (Count >= AsCount) |
67dc3830 | 91 | { |
f00832cc DK |
92 | Sum1.Add(As, AsCount); |
93 | Count -= AsCount; | |
67dc3830 DK |
94 | } |
95 | else | |
96 | { | |
f00832cc | 97 | Sum1.Add(As,Count); |
67dc3830 DK |
98 | Count = 0; |
99 | } | |
100 | } | |
101 | ||
f00832cc | 102 | EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sum1.Result().Value()); |
67dc3830 DK |
103 | } |
104 | ||
f00832cc | 105 | static void getSummationString(char const * const type, std::string &sum) |
67dc3830 | 106 | { |
f00832cc DK |
107 | /* to compare our result with an independent source we call the specific binaries |
108 | and read their result back. We do this with a little trick by claiming that the | |
109 | summation is a compressor – and open the 'compressed' file later on directly to | |
110 | read out the summation sum calculated by it */ | |
111 | APT::Configuration::Compressor compress(type, ".ext", type, NULL, NULL, 99); | |
112 | std::string name("apt-test-"); | |
113 | name.append("hashsums").append(".XXXXXX"); | |
114 | char * tempfile = strdup(name.c_str()); | |
115 | int tempfile_fd = mkstemp(tempfile); | |
116 | close(tempfile_fd); | |
117 | ASSERT_NE(-1, tempfile_fd); | |
118 | ||
119 | FileFd fd; | |
120 | ASSERT_TRUE(fd.Open(tempfile, FileFd::WriteOnly | FileFd::Empty, compress)); | |
121 | ASSERT_TRUE(fd.IsOpen()); | |
122 | FileFd input(__FILE__, FileFd::ReadOnly); | |
123 | ASSERT_TRUE(input.IsOpen()); | |
124 | ASSERT_NE(0, input.FileSize()); | |
125 | ASSERT_TRUE(CopyFile(input, fd)); | |
126 | ASSERT_TRUE(input.IsOpen()); | |
127 | ASSERT_TRUE(fd.IsOpen()); | |
128 | ASSERT_FALSE(fd.Failed()); | |
129 | input.Close(); | |
130 | fd.Close(); | |
131 | ASSERT_TRUE(fd.Open(tempfile, FileFd::ReadOnly, FileFd::None)); | |
132 | ASSERT_TRUE(fd.IsOpen()); | |
133 | ASSERT_NE(0, fd.FileSize()); | |
134 | ASSERT_FALSE(fd.Failed()); | |
135 | unlink(tempfile); | |
136 | free(tempfile); | |
137 | char readback[2000]; | |
138 | unsigned long long actual; | |
139 | ASSERT_TRUE(fd.Read(readback, sizeof(readback)/sizeof(readback[0]), &actual)); | |
140 | actual -= 4; | |
141 | readback[actual] = '\0'; | |
142 | sum = readback; | |
143 | } | |
144 | TEST(HashSumsTest, FileBased) | |
145 | { | |
146 | std::string summation; | |
65512241 | 147 | |
f00832cc DK |
148 | getSummationString("md5sum", summation); |
149 | MD5SumValue md5(summation); | |
150 | EXPECT_EQ(md5.Value(), summation); | |
9a4ffe57 | 151 | |
f00832cc DK |
152 | getSummationString("sha1sum", summation); |
153 | SHA1SumValue sha1(summation); | |
154 | EXPECT_EQ(sha1.Value(), summation); | |
67dc3830 | 155 | |
f00832cc DK |
156 | getSummationString("sha256sum", summation); |
157 | SHA256SumValue sha256(summation); | |
158 | EXPECT_EQ(sha256.Value(), summation); | |
67dc3830 | 159 | |
f00832cc DK |
160 | getSummationString("sha512sum", summation); |
161 | SHA512SumValue sha512(summation); | |
162 | EXPECT_EQ(sha512.Value(), summation); | |
163 | ||
164 | FileFd fd(__FILE__, FileFd::ReadOnly); | |
165 | EXPECT_TRUE(fd.IsOpen()); | |
23397c9d DK |
166 | std::string FileSize; |
167 | strprintf(FileSize, "%llu", fd.FileSize()); | |
67dc3830 | 168 | |
96c9fb17 | 169 | { |
f00832cc DK |
170 | Hashes hashes; |
171 | hashes.AddFD(fd.Fd()); | |
b3501edb DK |
172 | HashStringList list = hashes.GetHashStringList(); |
173 | EXPECT_FALSE(list.empty()); | |
23397c9d | 174 | EXPECT_EQ(5, list.size()); |
b3501edb DK |
175 | EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); |
176 | EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue()); | |
177 | EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue()); | |
178 | EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); | |
23397c9d | 179 | EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); |
96c9fb17 | 180 | } |
23397c9d | 181 | unsigned long long sz = fd.FileSize(); |
163d39cc | 182 | fd.Seek(0); |
96c9fb17 | 183 | { |
f00832cc DK |
184 | Hashes hashes; |
185 | hashes.AddFD(fd.Fd(), sz); | |
b3501edb DK |
186 | HashStringList list = hashes.GetHashStringList(); |
187 | EXPECT_FALSE(list.empty()); | |
23397c9d | 188 | EXPECT_EQ(5, list.size()); |
b3501edb DK |
189 | EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); |
190 | EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue()); | |
191 | EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue()); | |
192 | EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); | |
23397c9d | 193 | EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); |
96c9fb17 | 194 | } |
163d39cc | 195 | fd.Seek(0); |
96c9fb17 | 196 | { |
f00832cc DK |
197 | MD5Summation MD5; |
198 | MD5.AddFD(fd.Fd()); | |
199 | EXPECT_EQ(md5.Value(), MD5.Result().Value()); | |
96c9fb17 | 200 | } |
163d39cc | 201 | fd.Seek(0); |
96c9fb17 | 202 | { |
f00832cc DK |
203 | SHA1Summation SHA1; |
204 | SHA1.AddFD(fd.Fd()); | |
205 | EXPECT_EQ(sha1.Value(), SHA1.Result().Value()); | |
96c9fb17 | 206 | } |
163d39cc | 207 | fd.Seek(0); |
96c9fb17 | 208 | { |
f00832cc DK |
209 | SHA256Summation SHA2; |
210 | SHA2.AddFD(fd.Fd()); | |
211 | EXPECT_EQ(sha256.Value(), SHA2.Result().Value()); | |
96c9fb17 | 212 | } |
163d39cc | 213 | fd.Seek(0); |
96c9fb17 | 214 | { |
f00832cc DK |
215 | SHA512Summation SHA2; |
216 | SHA2.AddFD(fd.Fd()); | |
217 | EXPECT_EQ(sha512.Value(), SHA2.Result().Value()); | |
96c9fb17 | 218 | } |
163d39cc | 219 | fd.Close(); |
e99a544c | 220 | |
f4c3850e DK |
221 | HashString sha2file("SHA512", sha512.Value()); |
222 | EXPECT_TRUE(sha2file.VerifyFile(__FILE__)); | |
223 | HashString sha2wrong("SHA512", "00000000000"); | |
224 | EXPECT_FALSE(sha2wrong.VerifyFile(__FILE__)); | |
225 | EXPECT_EQ(sha2file, sha2file); | |
226 | EXPECT_TRUE(sha2file == sha2file); | |
227 | EXPECT_NE(sha2file, sha2wrong); | |
228 | EXPECT_TRUE(sha2file != sha2wrong); | |
229 | ||
230 | HashString sha2big("SHA256", sha256.Value()); | |
231 | EXPECT_TRUE(sha2big.VerifyFile(__FILE__)); | |
232 | HashString sha2small("sha256:" + sha256.Value()); | |
233 | EXPECT_TRUE(sha2small.VerifyFile(__FILE__)); | |
234 | EXPECT_EQ(sha2big, sha2small); | |
235 | EXPECT_TRUE(sha2big == sha2small); | |
236 | EXPECT_FALSE(sha2big != sha2small); | |
237 | ||
238 | HashStringList hashes; | |
239 | EXPECT_TRUE(hashes.empty()); | |
240 | EXPECT_TRUE(hashes.push_back(sha2file)); | |
241 | EXPECT_FALSE(hashes.empty()); | |
242 | EXPECT_EQ(1, hashes.size()); | |
243 | ||
244 | HashStringList wrong; | |
245 | EXPECT_TRUE(wrong.push_back(sha2wrong)); | |
246 | EXPECT_NE(wrong, hashes); | |
247 | EXPECT_FALSE(wrong == hashes); | |
248 | EXPECT_TRUE(wrong != hashes); | |
249 | ||
250 | HashStringList similar; | |
251 | EXPECT_TRUE(similar.push_back(sha2big)); | |
252 | EXPECT_NE(similar, hashes); | |
253 | EXPECT_FALSE(similar == hashes); | |
254 | EXPECT_TRUE(similar != hashes); | |
255 | ||
256 | EXPECT_TRUE(hashes.push_back(sha2big)); | |
257 | EXPECT_EQ(2, hashes.size()); | |
258 | EXPECT_TRUE(hashes.push_back(sha2small)); | |
259 | EXPECT_EQ(2, hashes.size()); | |
260 | EXPECT_FALSE(hashes.push_back(sha2wrong)); | |
261 | EXPECT_EQ(2, hashes.size()); | |
262 | EXPECT_TRUE(hashes.VerifyFile(__FILE__)); | |
263 | ||
264 | EXPECT_EQ(similar, hashes); | |
265 | EXPECT_TRUE(similar == hashes); | |
266 | EXPECT_FALSE(similar != hashes); | |
267 | similar.clear(); | |
268 | EXPECT_TRUE(similar.empty()); | |
269 | EXPECT_EQ(0, similar.size()); | |
270 | EXPECT_NE(similar, hashes); | |
271 | EXPECT_FALSE(similar == hashes); | |
272 | EXPECT_TRUE(similar != hashes); | |
67dc3830 | 273 | } |
b3501edb DK |
274 | TEST(HashSumsTest, HashStringList) |
275 | { | |
276 | _config->Clear("Acquire::ForceHash"); | |
277 | ||
278 | HashStringList list; | |
279 | EXPECT_TRUE(list.empty()); | |
280 | EXPECT_FALSE(list.usable()); | |
281 | EXPECT_EQ(0, list.size()); | |
282 | EXPECT_EQ(NULL, list.find(NULL)); | |
283 | EXPECT_EQ(NULL, list.find("")); | |
284 | EXPECT_EQ(NULL, list.find("MD5Sum")); | |
285 | ||
23397c9d | 286 | // empty lists aren't equal |
b3501edb DK |
287 | HashStringList list2; |
288 | EXPECT_FALSE(list == list2); | |
289 | EXPECT_TRUE(list != list2); | |
290 | ||
23397c9d DK |
291 | // some hashes don't really contribute to usability |
292 | list.push_back(HashString("Checksum-FileSize", "29")); | |
293 | EXPECT_FALSE(list.empty()); | |
294 | EXPECT_FALSE(list.usable()); | |
295 | ||
b3501edb DK |
296 | Hashes hashes; |
297 | hashes.Add("The quick brown fox jumps over the lazy dog"); | |
298 | list = hashes.GetHashStringList(); | |
299 | EXPECT_FALSE(list.empty()); | |
300 | EXPECT_TRUE(list.usable()); | |
23397c9d | 301 | EXPECT_EQ(5, list.size()); |
b3501edb DK |
302 | EXPECT_TRUE(NULL != list.find(NULL)); |
303 | EXPECT_TRUE(NULL != list.find("")); | |
304 | EXPECT_TRUE(NULL != list.find("MD5Sum")); | |
23397c9d | 305 | EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); |
b3501edb DK |
306 | EXPECT_TRUE(NULL == list.find("ROT26")); |
307 | ||
308 | _config->Set("Acquire::ForceHash", "MD5Sum"); | |
309 | EXPECT_FALSE(list.empty()); | |
310 | EXPECT_TRUE(list.usable()); | |
23397c9d | 311 | EXPECT_EQ(5, list.size()); |
b3501edb DK |
312 | EXPECT_TRUE(NULL != list.find(NULL)); |
313 | EXPECT_TRUE(NULL != list.find("")); | |
314 | EXPECT_TRUE(NULL != list.find("MD5Sum")); | |
23397c9d | 315 | EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); |
b3501edb DK |
316 | EXPECT_TRUE(NULL == list.find("ROT26")); |
317 | ||
318 | _config->Set("Acquire::ForceHash", "ROT26"); | |
319 | EXPECT_FALSE(list.empty()); | |
320 | EXPECT_FALSE(list.usable()); | |
23397c9d | 321 | EXPECT_EQ(5, list.size()); |
b3501edb DK |
322 | EXPECT_TRUE(NULL == list.find(NULL)); |
323 | EXPECT_TRUE(NULL == list.find("")); | |
324 | EXPECT_TRUE(NULL != list.find("MD5Sum")); | |
23397c9d | 325 | EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); |
b3501edb DK |
326 | EXPECT_TRUE(NULL == list.find("ROT26")); |
327 | ||
328 | _config->Clear("Acquire::ForceHash"); | |
329 | ||
330 | list2.push_back(*list.find("MD5Sum")); | |
331 | EXPECT_TRUE(list == list2); | |
332 | EXPECT_FALSE(list != list2); | |
333 | ||
334 | // introduce a mismatch to the list | |
335 | list2.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc")); | |
336 | EXPECT_FALSE(list == list2); | |
337 | EXPECT_TRUE(list != list2); | |
338 | ||
339 | _config->Set("Acquire::ForceHash", "MD5Sum"); | |
340 | EXPECT_TRUE(list == list2); | |
341 | EXPECT_FALSE(list != list2); | |
342 | ||
343 | _config->Clear("Acquire::ForceHash"); | |
344 | } |