]>
git.saurik.com Git - apt.git/blob - test/libapt/hashsums_test.cc
3 #include <apt-pkg/configuration.h>
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>
9 #include <apt-pkg/fileutl.h>
15 #include <gtest/gtest.h>
17 #include "file-helpers.h"
19 template <class T
> void Test(const char *In
,const char *Out
)
23 equals(Sum
.Result().Value(), Out
);
28 TEST(HashSumsTest
,SummationStrings
)
30 #define EXPECT_SUM(Summation, In, Out) \
34 EXPECT_EQ(Sum.Result().Value(), Out) << #Summation << " for '" << In << "'"; \
37 // From FIPS PUB 180-1
38 EXPECT_SUM(SHA1Summation
, "","da39a3ee5e6b4b0d3255bfef95601890afd80709");
39 EXPECT_SUM(SHA1Summation
, "abc","a9993e364706816aba3e25717850c26c9cd0d89d");
40 EXPECT_SUM(SHA1Summation
, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
41 "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
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");
54 // SHA-256, From FIPS 180-2
55 EXPECT_SUM(SHA256Summation
, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
56 EXPECT_SUM(SHA256Summation
, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
57 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
60 EXPECT_SUM(SHA512Summation
, "",
61 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
62 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
63 EXPECT_SUM(SHA512Summation
, "abc",
64 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
65 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
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");
81 TEST(HashSumsTest
, Mill
)
85 const unsigned char As
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
86 size_t const AsCount
= sizeof(As
)/sizeof(As
[0]) - 1;
87 size_t Count
= 1000000;
92 Sum1
.Add(As
, AsCount
);
102 EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sum1
.Result().Value());
105 static void getSummationString(char const * const type
, std::string
&sum
)
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
);
117 ASSERT_NE(-1, tempfile_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());
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());
138 unsigned long long actual
;
139 ASSERT_TRUE(fd
.Read(readback
, sizeof(readback
)/sizeof(readback
[0]), &actual
));
141 readback
[actual
] = '\0';
144 TEST(HashSumsTest
, FileBased
)
146 std::string summation
;
148 getSummationString("md5sum", summation
);
149 MD5SumValue
md5(summation
);
150 EXPECT_EQ(md5
.Value(), summation
);
152 getSummationString("sha1sum", summation
);
153 SHA1SumValue
sha1(summation
);
154 EXPECT_EQ(sha1
.Value(), summation
);
156 getSummationString("sha256sum", summation
);
157 SHA256SumValue
sha256(summation
);
158 EXPECT_EQ(sha256
.Value(), summation
);
160 getSummationString("sha512sum", summation
);
161 SHA512SumValue
sha512(summation
);
162 EXPECT_EQ(sha512
.Value(), summation
);
164 FileFd
fd(__FILE__
, FileFd::ReadOnly
);
165 EXPECT_TRUE(fd
.IsOpen());
166 std::string FileSize
;
167 strprintf(FileSize
, "%llu", fd
.FileSize());
171 hashes
.AddFD(fd
.Fd());
172 HashStringList list
= hashes
.GetHashStringList();
173 EXPECT_FALSE(list
.empty());
174 EXPECT_EQ(5, list
.size());
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());
179 EXPECT_EQ(FileSize
, list
.find("Checksum-FileSize")->HashValue());
181 unsigned long long sz
= fd
.FileSize();
185 hashes
.AddFD(fd
.Fd(), sz
);
186 HashStringList list
= hashes
.GetHashStringList();
187 EXPECT_FALSE(list
.empty());
188 EXPECT_EQ(5, list
.size());
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());
193 EXPECT_EQ(FileSize
, list
.find("Checksum-FileSize")->HashValue());
197 Hashes
hashes(Hashes::MD5SUM
| Hashes::SHA512SUM
);
199 HashStringList list
= hashes
.GetHashStringList();
200 EXPECT_FALSE(list
.empty());
201 EXPECT_EQ(3, list
.size());
202 EXPECT_EQ(md5
.Value(), list
.find("MD5Sum")->HashValue());
203 EXPECT_EQ(NULL
, list
.find("SHA1"));
204 EXPECT_EQ(NULL
, list
.find("SHA256"));
205 EXPECT_EQ(sha512
.Value(), list
.find("SHA512")->HashValue());
206 EXPECT_EQ(FileSize
, list
.find("Checksum-FileSize")->HashValue());
208 Hashes
hashes2(list
);
210 list
= hashes2
.GetHashStringList();
211 EXPECT_FALSE(list
.empty());
212 EXPECT_EQ(3, list
.size());
213 EXPECT_EQ(md5
.Value(), list
.find("MD5Sum")->HashValue());
214 EXPECT_EQ(NULL
, list
.find("SHA1"));
215 EXPECT_EQ(NULL
, list
.find("SHA256"));
216 EXPECT_EQ(sha512
.Value(), list
.find("SHA512")->HashValue());
217 EXPECT_EQ(FileSize
, list
.find("Checksum-FileSize")->HashValue());
223 EXPECT_EQ(md5
.Value(), MD5
.Result().Value());
229 EXPECT_EQ(sha1
.Value(), SHA1
.Result().Value());
233 SHA256Summation SHA2
;
235 EXPECT_EQ(sha256
.Value(), SHA2
.Result().Value());
239 SHA512Summation SHA2
;
241 EXPECT_EQ(sha512
.Value(), SHA2
.Result().Value());
245 HashString
sha2file("SHA512", sha512
.Value());
246 EXPECT_TRUE(sha2file
.VerifyFile(__FILE__
));
247 HashString
sha2wrong("SHA512", "00000000000");
248 EXPECT_FALSE(sha2wrong
.VerifyFile(__FILE__
));
249 EXPECT_EQ(sha2file
, sha2file
);
250 EXPECT_TRUE(sha2file
== sha2file
);
251 EXPECT_NE(sha2file
, sha2wrong
);
252 EXPECT_TRUE(sha2file
!= sha2wrong
);
254 HashString
sha2big("SHA256", sha256
.Value());
255 EXPECT_TRUE(sha2big
.VerifyFile(__FILE__
));
256 HashString
sha2small("sha256:" + sha256
.Value());
257 EXPECT_TRUE(sha2small
.VerifyFile(__FILE__
));
258 EXPECT_EQ(sha2big
, sha2small
);
259 EXPECT_TRUE(sha2big
== sha2small
);
260 EXPECT_FALSE(sha2big
!= sha2small
);
262 HashStringList hashes
;
263 EXPECT_TRUE(hashes
.empty());
264 EXPECT_TRUE(hashes
.push_back(sha2file
));
265 EXPECT_FALSE(hashes
.empty());
266 EXPECT_EQ(1, hashes
.size());
268 HashStringList wrong
;
269 EXPECT_TRUE(wrong
.push_back(sha2wrong
));
270 EXPECT_NE(wrong
, hashes
);
271 EXPECT_FALSE(wrong
== hashes
);
272 EXPECT_TRUE(wrong
!= hashes
);
274 HashStringList similar
;
275 EXPECT_TRUE(similar
.push_back(sha2big
));
276 EXPECT_NE(similar
, hashes
);
277 EXPECT_FALSE(similar
== hashes
);
278 EXPECT_TRUE(similar
!= hashes
);
280 EXPECT_TRUE(hashes
.push_back(sha2big
));
281 EXPECT_EQ(2, hashes
.size());
282 EXPECT_TRUE(hashes
.push_back(sha2small
));
283 EXPECT_EQ(2, hashes
.size());
284 EXPECT_FALSE(hashes
.push_back(sha2wrong
));
285 EXPECT_EQ(2, hashes
.size());
286 EXPECT_TRUE(hashes
.VerifyFile(__FILE__
));
288 EXPECT_EQ(similar
, hashes
);
289 EXPECT_TRUE(similar
== hashes
);
290 EXPECT_FALSE(similar
!= hashes
);
292 EXPECT_TRUE(similar
.empty());
293 EXPECT_EQ(0, similar
.size());
294 EXPECT_NE(similar
, hashes
);
295 EXPECT_FALSE(similar
== hashes
);
296 EXPECT_TRUE(similar
!= hashes
);
298 TEST(HashSumsTest
, HashStringList
)
300 _config
->Clear("Acquire::ForceHash");
303 EXPECT_TRUE(list
.empty());
304 EXPECT_FALSE(list
.usable());
305 EXPECT_EQ(0, list
.size());
306 EXPECT_EQ(NULL
, list
.find(NULL
));
307 EXPECT_EQ(NULL
, list
.find(""));
308 EXPECT_EQ(NULL
, list
.find("MD5Sum"));
309 EXPECT_EQ(0, list
.FileSize());
311 // empty lists aren't equal
312 HashStringList list2
;
313 EXPECT_FALSE(list
== list2
);
314 EXPECT_TRUE(list
!= list2
);
316 // some hashes don't really contribute to usability
317 list
.push_back(HashString("Checksum-FileSize", "29"));
318 EXPECT_FALSE(list
.empty());
319 EXPECT_FALSE(list
.usable());
320 EXPECT_EQ(1, list
.size());
321 EXPECT_EQ(29, list
.FileSize());
324 hashes
.Add("The quick brown fox jumps over the lazy dog");
325 list
= hashes
.GetHashStringList();
326 EXPECT_FALSE(list
.empty());
327 EXPECT_TRUE(list
.usable());
328 EXPECT_EQ(5, list
.size());
329 EXPECT_TRUE(NULL
!= list
.find(NULL
));
330 EXPECT_TRUE(NULL
!= list
.find(""));
331 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
332 EXPECT_TRUE(NULL
!= list
.find("Checksum-FileSize"));
333 EXPECT_TRUE(NULL
== list
.find("ROT26"));
335 _config
->Set("Acquire::ForceHash", "MD5Sum");
336 EXPECT_FALSE(list
.empty());
337 EXPECT_TRUE(list
.usable());
338 EXPECT_EQ(5, list
.size());
339 EXPECT_TRUE(NULL
!= list
.find(NULL
));
340 EXPECT_TRUE(NULL
!= list
.find(""));
341 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
342 EXPECT_TRUE(NULL
!= list
.find("Checksum-FileSize"));
343 EXPECT_TRUE(NULL
== list
.find("ROT26"));
345 _config
->Set("Acquire::ForceHash", "ROT26");
346 EXPECT_FALSE(list
.empty());
347 EXPECT_FALSE(list
.usable());
348 EXPECT_EQ(5, list
.size());
349 EXPECT_TRUE(NULL
== list
.find(NULL
));
350 EXPECT_TRUE(NULL
== list
.find(""));
351 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
352 EXPECT_TRUE(NULL
!= list
.find("Checksum-FileSize"));
353 EXPECT_TRUE(NULL
== list
.find("ROT26"));
355 _config
->Clear("Acquire::ForceHash");
357 list2
.push_back(*list
.find("MD5Sum"));
358 EXPECT_TRUE(list
== list2
);
359 EXPECT_FALSE(list
!= list2
);
361 // introduce a mismatch to the list
362 list2
.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc"));
363 EXPECT_FALSE(list
== list2
);
364 EXPECT_TRUE(list
!= list2
);
366 _config
->Set("Acquire::ForceHash", "MD5Sum");
367 EXPECT_TRUE(list
== list2
);
368 EXPECT_FALSE(list
!= list2
);
370 _config
->Clear("Acquire::ForceHash");