]> git.saurik.com Git - apt.git/blob - test/libapt/hashsums_test.cc
* lp:~mvo/apt/add-glob-function:
[apt.git] / test / libapt / hashsums_test.cc
1 #include <apt-pkg/md5.h>
2 #include <apt-pkg/sha1.h>
3 #include <apt-pkg/sha2.h>
4 #include <apt-pkg/strutl.h>
5 #include <apt-pkg/hashes.h>
6 #include <iostream>
7
8 #include <stdio.h>
9
10 #include "assert.h"
11
12 template <class T> void Test(const char *In,const char *Out)
13 {
14 T Sum;
15 Sum.Add(In);
16 equals(Sum.Result().Value(), Out);
17 }
18
19 template <class T> void TestMill(const char *Out)
20 {
21 T Sum;
22
23 const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
24 unsigned Count = 1000000;
25 for (; Count != 0;)
26 {
27 if (Count >= 64)
28 {
29 Sum.Add(As,64);
30 Count -= 64;
31 }
32 else
33 {
34 Sum.Add(As,Count);
35 Count = 0;
36 }
37 }
38
39 if (stringcasecmp(Sum.Result().Value(), Out) != 0)
40 abort();
41 }
42
43 int main(int argc, char** argv)
44 {
45 // test HashSumValue which doesn't calculate but just stores sums
46 {
47 string md5sum = argv[2];
48 MD5SumValue md5(md5sum);
49 equals(md5.Value(), md5sum);
50 }
51 {
52 string sha1sum = argv[3];
53 SHA1SumValue sha1(sha1sum);
54 equals(sha1.Value(), sha1sum);
55 }
56 {
57 string sha2sum = argv[4];
58 SHA256SumValue sha2(sha2sum);
59 equals(sha2.Value(), sha2sum);
60 }
61 {
62 string sha2sum = argv[5];
63 SHA512SumValue sha2(sha2sum);
64 equals(sha2.Value(), sha2sum);
65 }
66
67 // From FIPS PUB 180-1
68 Test<SHA1Summation>("","da39a3ee5e6b4b0d3255bfef95601890afd80709");
69 Test<SHA1Summation>("abc","a9993e364706816aba3e25717850c26c9cd0d89d");
70 Test<SHA1Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
71 "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
72 TestMill<SHA1Summation>("34aa973cd4c4daa4f61eeb2bdbad27316534016f");
73
74 // MD5 tests from RFC 1321
75 Test<MD5Summation>("","d41d8cd98f00b204e9800998ecf8427e");
76 Test<MD5Summation>("a","0cc175b9c0f1b6a831c399e269772661");
77 Test<MD5Summation>("abc","900150983cd24fb0d6963f7d28e17f72");
78 Test<MD5Summation>("message digest","f96b697d7cb7938d525a2f31aaf161d0");
79 Test<MD5Summation>("abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b");
80 Test<MD5Summation>("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
81 "d174ab98d277d9f5a5611c2c9f419d9f");
82 Test<MD5Summation>("12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 "57edf4a22be3c955ac49da2e2107b67a");
84
85 // SHA-256, From FIPS 180-2
86 Test<SHA256Summation>("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
87 Test<SHA256Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
88 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
89
90 // SHA-512
91 Test<SHA512Summation>("",
92 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
93 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
94 Test<SHA512Summation>(
95 "abc",
96 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
97 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
98
99
100 Test<MD5Summation>("The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
101 Test<MD5Summation>("The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0");
102 Test<SHA1Summation>("The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
103 Test<SHA1Summation>("The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3");
104 Test<SHA256Summation>("The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
105 Test<SHA256Summation>("The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
106 Test<SHA512Summation>("The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64"
107 "2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
108 Test<SHA512Summation>("The quick brown fox jumps over the lazy dog.", "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb"
109 "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed");
110
111 FILE* fd = fopen(argv[1], "r");
112 if (fd == NULL) {
113 std::cerr << "Can't open file for 1. testing: " << argv[1] << std::endl;
114 return 1;
115 }
116 {
117 Hashes hashes;
118 hashes.AddFD(fileno(fd));
119 equals(argv[2], hashes.MD5.Result().Value());
120 equals(argv[3], hashes.SHA1.Result().Value());
121 equals(argv[4], hashes.SHA256.Result().Value());
122 equals(argv[5], hashes.SHA512.Result().Value());
123 }
124 fseek(fd, 0L, SEEK_END);
125 unsigned long sz = ftell(fd);
126 fseek(fd, 0L, SEEK_SET);
127 {
128 Hashes hashes;
129 hashes.AddFD(fileno(fd), sz);
130 equals(argv[2], hashes.MD5.Result().Value());
131 equals(argv[3], hashes.SHA1.Result().Value());
132 equals(argv[4], hashes.SHA256.Result().Value());
133 equals(argv[5], hashes.SHA512.Result().Value());
134 }
135 fseek(fd, 0L, SEEK_SET);
136 {
137 MD5Summation md5;
138 md5.AddFD(fileno(fd));
139 equals(argv[2], md5.Result().Value());
140 }
141 fseek(fd, 0L, SEEK_SET);
142 {
143 SHA1Summation sha1;
144 sha1.AddFD(fileno(fd));
145 equals(argv[3], sha1.Result().Value());
146 }
147 fseek(fd, 0L, SEEK_SET);
148 {
149 SHA256Summation sha2;
150 sha2.AddFD(fileno(fd));
151 equals(argv[4], sha2.Result().Value());
152 }
153 fseek(fd, 0L, SEEK_SET);
154 {
155 SHA512Summation sha2;
156 sha2.AddFD(fileno(fd));
157 equals(argv[5], sha2.Result().Value());
158 }
159 fclose(fd);
160
161 // test HashString code
162 {
163 HashString sha2("SHA256", argv[4]);
164 equals(sha2.VerifyFile(argv[1]), true);
165 }
166 {
167 HashString sha2("SHA512", argv[5]);
168 equals(sha2.VerifyFile(argv[1]), true);
169 }
170 {
171 HashString sha2("SHA256:" + std::string(argv[4]));
172 equals(sha2.VerifyFile(argv[1]), true);
173 }
174
175 return 0;
176 }
177
178