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