]> git.saurik.com Git - apt.git/blame - test/libapt/hashsums_test.cc
Merge remote-tracking branch 'donkult/debian/experimental' into debian/experimental
[apt.git] / test / libapt / hashsums_test.cc
CommitLineData
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
19template <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
28TEST(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}
81TEST(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 105static 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}
144TEST(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());
67dc3830 166
96c9fb17 167 {
f00832cc
DK
168 Hashes hashes;
169 hashes.AddFD(fd.Fd());
b3501edb
DK
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());
96c9fb17 177 }
163d39cc
DK
178 unsigned long sz = fd.FileSize();
179 fd.Seek(0);
96c9fb17 180 {
f00832cc
DK
181 Hashes hashes;
182 hashes.AddFD(fd.Fd(), sz);
b3501edb
DK
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());
96c9fb17 190 }
163d39cc 191 fd.Seek(0);
96c9fb17 192 {
f00832cc
DK
193 MD5Summation MD5;
194 MD5.AddFD(fd.Fd());
195 EXPECT_EQ(md5.Value(), MD5.Result().Value());
96c9fb17 196 }
163d39cc 197 fd.Seek(0);
96c9fb17 198 {
f00832cc
DK
199 SHA1Summation SHA1;
200 SHA1.AddFD(fd.Fd());
201 EXPECT_EQ(sha1.Value(), SHA1.Result().Value());
96c9fb17 202 }
163d39cc 203 fd.Seek(0);
96c9fb17 204 {
f00832cc
DK
205 SHA256Summation SHA2;
206 SHA2.AddFD(fd.Fd());
207 EXPECT_EQ(sha256.Value(), SHA2.Result().Value());
96c9fb17 208 }
163d39cc 209 fd.Seek(0);
96c9fb17 210 {
f00832cc
DK
211 SHA512Summation SHA2;
212 SHA2.AddFD(fd.Fd());
213 EXPECT_EQ(sha512.Value(), SHA2.Result().Value());
96c9fb17 214 }
163d39cc 215 fd.Close();
e99a544c 216
f4c3850e
DK
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);
225
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);
233
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());
239
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);
245
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);
251
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__));
259
260 EXPECT_EQ(similar, hashes);
261 EXPECT_TRUE(similar == hashes);
262 EXPECT_FALSE(similar != hashes);
263 similar.clear();
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);
67dc3830 269}
b3501edb
DK
270TEST(HashSumsTest, HashStringList)
271{
272 _config->Clear("Acquire::ForceHash");
273
274 HashStringList list;
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"));
281
282 HashStringList list2;
283 EXPECT_FALSE(list == list2);
284 EXPECT_TRUE(list != list2);
285
286 Hashes hashes;
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"));
296
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"));
305
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"));
314
315 _config->Clear("Acquire::ForceHash");
316
317 list2.push_back(*list.find("MD5Sum"));
318 EXPECT_TRUE(list == list2);
319 EXPECT_FALSE(list != list2);
320
321 // introduce a mismatch to the list
322 list2.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc"));
323 EXPECT_FALSE(list == list2);
324 EXPECT_TRUE(list != list2);
325
326 _config->Set("Acquire::ForceHash", "MD5Sum");
327 EXPECT_TRUE(list == list2);
328 EXPECT_FALSE(list != list2);
329
330 _config->Clear("Acquire::ForceHash");
331}