]>
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());
169 hashes
.AddFD(fd
.Fd());
170 HashStringList list
= hashes
.GetHashStringList();
171 EXPECT_FALSE(list
.empty());
172 EXPECT_EQ(4, list
.size());
173 EXPECT_EQ(md5
.Value(), list
.find("MD5Sum")->HashValue());
174 EXPECT_EQ(sha1
.Value(), list
.find("SHA1")->HashValue());
175 EXPECT_EQ(sha256
.Value(), list
.find("SHA256")->HashValue());
176 EXPECT_EQ(sha512
.Value(), list
.find("SHA512")->HashValue());
178 unsigned long sz
= fd
.FileSize();
182 hashes
.AddFD(fd
.Fd(), sz
);
183 HashStringList list
= hashes
.GetHashStringList();
184 EXPECT_FALSE(list
.empty());
185 EXPECT_EQ(4, list
.size());
186 EXPECT_EQ(md5
.Value(), list
.find("MD5Sum")->HashValue());
187 EXPECT_EQ(sha1
.Value(), list
.find("SHA1")->HashValue());
188 EXPECT_EQ(sha256
.Value(), list
.find("SHA256")->HashValue());
189 EXPECT_EQ(sha512
.Value(), list
.find("SHA512")->HashValue());
195 EXPECT_EQ(md5
.Value(), MD5
.Result().Value());
201 EXPECT_EQ(sha1
.Value(), SHA1
.Result().Value());
205 SHA256Summation SHA2
;
207 EXPECT_EQ(sha256
.Value(), SHA2
.Result().Value());
211 SHA512Summation SHA2
;
213 EXPECT_EQ(sha512
.Value(), SHA2
.Result().Value());
217 HashString
sha2file("SHA512", sha512
.Value());
218 EXPECT_TRUE(sha2file
.VerifyFile(__FILE__
));
219 HashString
sha2wrong("SHA512", "00000000000");
220 EXPECT_FALSE(sha2wrong
.VerifyFile(__FILE__
));
221 EXPECT_EQ(sha2file
, sha2file
);
222 EXPECT_TRUE(sha2file
== sha2file
);
223 EXPECT_NE(sha2file
, sha2wrong
);
224 EXPECT_TRUE(sha2file
!= sha2wrong
);
226 HashString
sha2big("SHA256", sha256
.Value());
227 EXPECT_TRUE(sha2big
.VerifyFile(__FILE__
));
228 HashString
sha2small("sha256:" + sha256
.Value());
229 EXPECT_TRUE(sha2small
.VerifyFile(__FILE__
));
230 EXPECT_EQ(sha2big
, sha2small
);
231 EXPECT_TRUE(sha2big
== sha2small
);
232 EXPECT_FALSE(sha2big
!= sha2small
);
234 HashStringList hashes
;
235 EXPECT_TRUE(hashes
.empty());
236 EXPECT_TRUE(hashes
.push_back(sha2file
));
237 EXPECT_FALSE(hashes
.empty());
238 EXPECT_EQ(1, hashes
.size());
240 HashStringList wrong
;
241 EXPECT_TRUE(wrong
.push_back(sha2wrong
));
242 EXPECT_NE(wrong
, hashes
);
243 EXPECT_FALSE(wrong
== hashes
);
244 EXPECT_TRUE(wrong
!= hashes
);
246 HashStringList similar
;
247 EXPECT_TRUE(similar
.push_back(sha2big
));
248 EXPECT_NE(similar
, hashes
);
249 EXPECT_FALSE(similar
== hashes
);
250 EXPECT_TRUE(similar
!= hashes
);
252 EXPECT_TRUE(hashes
.push_back(sha2big
));
253 EXPECT_EQ(2, hashes
.size());
254 EXPECT_TRUE(hashes
.push_back(sha2small
));
255 EXPECT_EQ(2, hashes
.size());
256 EXPECT_FALSE(hashes
.push_back(sha2wrong
));
257 EXPECT_EQ(2, hashes
.size());
258 EXPECT_TRUE(hashes
.VerifyFile(__FILE__
));
260 EXPECT_EQ(similar
, hashes
);
261 EXPECT_TRUE(similar
== hashes
);
262 EXPECT_FALSE(similar
!= hashes
);
264 EXPECT_TRUE(similar
.empty());
265 EXPECT_EQ(0, similar
.size());
266 EXPECT_NE(similar
, hashes
);
267 EXPECT_FALSE(similar
== hashes
);
268 EXPECT_TRUE(similar
!= hashes
);
270 TEST(HashSumsTest
, HashStringList
)
272 _config
->Clear("Acquire::ForceHash");
275 EXPECT_TRUE(list
.empty());
276 EXPECT_FALSE(list
.usable());
277 EXPECT_EQ(0, list
.size());
278 EXPECT_EQ(NULL
, list
.find(NULL
));
279 EXPECT_EQ(NULL
, list
.find(""));
280 EXPECT_EQ(NULL
, list
.find("MD5Sum"));
282 HashStringList list2
;
283 EXPECT_FALSE(list
== list2
);
284 EXPECT_TRUE(list
!= list2
);
287 hashes
.Add("The quick brown fox jumps over the lazy dog");
288 list
= hashes
.GetHashStringList();
289 EXPECT_FALSE(list
.empty());
290 EXPECT_TRUE(list
.usable());
291 EXPECT_EQ(4, list
.size());
292 EXPECT_TRUE(NULL
!= list
.find(NULL
));
293 EXPECT_TRUE(NULL
!= list
.find(""));
294 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
295 EXPECT_TRUE(NULL
== list
.find("ROT26"));
297 _config
->Set("Acquire::ForceHash", "MD5Sum");
298 EXPECT_FALSE(list
.empty());
299 EXPECT_TRUE(list
.usable());
300 EXPECT_EQ(4, list
.size());
301 EXPECT_TRUE(NULL
!= list
.find(NULL
));
302 EXPECT_TRUE(NULL
!= list
.find(""));
303 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
304 EXPECT_TRUE(NULL
== list
.find("ROT26"));
306 _config
->Set("Acquire::ForceHash", "ROT26");
307 EXPECT_FALSE(list
.empty());
308 EXPECT_FALSE(list
.usable());
309 EXPECT_EQ(4, list
.size());
310 EXPECT_TRUE(NULL
== list
.find(NULL
));
311 EXPECT_TRUE(NULL
== list
.find(""));
312 EXPECT_TRUE(NULL
!= list
.find("MD5Sum"));
313 EXPECT_TRUE(NULL
== list
.find("ROT26"));
315 _config
->Clear("Acquire::ForceHash");
317 list2
.push_back(*list
.find("MD5Sum"));
318 EXPECT_TRUE(list
== list2
);
319 EXPECT_FALSE(list
!= list2
);
321 // introduce a mismatch to the list
322 list2
.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc"));
323 EXPECT_FALSE(list
== list2
);
324 EXPECT_TRUE(list
!= list2
);
326 _config
->Set("Acquire::ForceHash", "MD5Sum");
327 EXPECT_TRUE(list
== list2
);
328 EXPECT_FALSE(list
!= list2
);
330 _config
->Clear("Acquire::ForceHash");