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