blob: 531868fb1fd42dadf34db940a87c2091947f82fb [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Yingdi Yude222c72014-08-15 16:06:52 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/digest.hpp"
23#include "util/crypto.hpp"
24
25#include "boost-test.hpp"
26
27namespace ndn {
28namespace util {
29namespace test {
30
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031BOOST_AUTO_TEST_SUITE(UtilDigest)
Yingdi Yude222c72014-08-15 16:06:52 -070032
33BOOST_AUTO_TEST_CASE(Sha256Digest)
34{
35 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
36 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
37
38 Sha256 statefulSha256;
39 statefulSha256.update(origin, 1);
40 statefulSha256.update(origin + 1, 1);
41 statefulSha256.update(origin + 2, 1);
42 statefulSha256.update(origin + 3, 1);
43 ConstBufferPtr digest2 = statefulSha256.computeDigest();
44
45 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
46 digest1->buf() + digest1->size(),
47 digest2->buf(),
48 digest2->buf() + digest2->size());
49}
50
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070051BOOST_AUTO_TEST_CASE(Compute)
52{
53 std::string input = "Hello, World!";
54 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<const uint8_t*>(input.data()),
55 input.size());
56
57 Sha256 hashObject;
58 hashObject << input;
59 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
60 ConstBufferPtr digest2 = hashObject.computeDigest();
61 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
62 digest1->buf() + digest1->size(),
63 digest2->buf(),
64 digest2->buf() + digest2->size());
65
66}
67
68BOOST_AUTO_TEST_CASE(ConstructFromStream)
69{
70 std::string input = "Hello, World!";
71 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<const uint8_t*>(input.data()),
72 input.size());
73
74 std::istringstream is(input);
75 Sha256 hashObject(is);
76 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
77 ConstBufferPtr digest2 = hashObject.computeDigest();
78 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
79 digest1->buf() + digest1->size(),
80 digest2->buf(),
81 digest2->buf() + digest2->size());
82}
83
Yingdi Yude222c72014-08-15 16:06:52 -070084BOOST_AUTO_TEST_CASE(Compare)
85{
86 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
87
88 Sha256 digest;
89 digest.update(origin, 4);
90 digest.computeDigest();
91
92 Sha256 digest2;
93 digest2.update(origin, 1);
94 digest2.update(origin + 1, 1);
95 digest2.update(origin + 2, 1);
96 digest2.update(origin + 3, 1);
97 digest2.computeDigest();
98
99 BOOST_CHECK(digest == digest2);
100 BOOST_CHECK_EQUAL(digest != digest2, false);
101}
102
103BOOST_AUTO_TEST_CASE(OperatorDigest)
104{
105 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
106 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
107 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
108 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
109 ConstBufferPtr digest1 = crypto::sha256(origin, 32);
110
111 std::string str("TEST");
112 Sha256 metaDigest;
113 metaDigest << str;
114
115 Sha256 statefulSha256;
116 statefulSha256 << metaDigest;
117 ConstBufferPtr digest2 = statefulSha256.computeDigest();
118
119 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
120 digest1->buf() + digest1->size(),
121 digest2->buf(),
122 digest2->buf() + digest2->size());
123}
124
125BOOST_AUTO_TEST_CASE(OperatorString)
126{
127 uint8_t origin[4] = {0x54, 0x45, 0x53, 0x54};
128 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
129
130 std::string str("TEST");
131 Sha256 statefulSha256;
132 statefulSha256 << str;
133 ConstBufferPtr digest2 = statefulSha256.computeDigest();
134
135 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
136 digest1->buf() + digest1->size(),
137 digest2->buf(),
138 digest2->buf() + digest2->size());
139}
140
141BOOST_AUTO_TEST_CASE(OperatorBlock)
142{
143 uint8_t origin[] = {
144 0x16, 0x1b, // SignatureInfo
145 0x1b, 0x01, // SignatureType
146 0x01, // Sha256WithRsa
147 0x1c, 0x16, // KeyLocator
148 0x07, 0x14, // Name
149 0x08, 0x04,
150 0x74, 0x65, 0x73, 0x74,
151 0x08, 0x03,
152 0x6b, 0x65, 0x79,
153 0x08, 0x07,
154 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
155 };
156 ConstBufferPtr digest1 = crypto::sha256(origin, sizeof(origin));
157
158 Sha256 statefulSha256;
159 Block block(origin, sizeof(origin));
160 statefulSha256 << block;
161 ConstBufferPtr digest2 = statefulSha256.computeDigest();
162
163 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
164 digest1->buf() + digest1->size(),
165 digest2->buf(),
166 digest2->buf() + digest2->size());
167}
168
169BOOST_AUTO_TEST_CASE(OperatorUint64t)
170{
171 uint64_t origin[4] = {1, 2, 3, 4};
172 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<uint8_t*>(origin), 32);
173
174 Sha256 statefulSha256;
175 statefulSha256 << origin[0];
176 statefulSha256 << origin[1];
177 statefulSha256 << origin[2];
178 statefulSha256 << origin[3];
179 ConstBufferPtr digest2 = statefulSha256.computeDigest();
180
181 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
182 digest1->buf() + digest1->size(),
183 digest2->buf(),
184 digest2->buf() + digest2->size());
185}
186
187
188BOOST_AUTO_TEST_CASE(Error)
189{
190 uint64_t origin = 256;
191
192 Sha256 digest;
193 BOOST_CHECK(digest.empty());
194
195 digest << origin;
196
197 BOOST_CHECK_NO_THROW(digest.computeDigest());
198 BOOST_CHECK_THROW(digest << origin, Sha256::Error);
199
200 digest.reset();
201}
202
203BOOST_AUTO_TEST_CASE(ComputeDigest)
204{
205 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
206 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
207
208 ConstBufferPtr digest2 = Sha256::computeDigest(origin, 4);
209
210 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
211 digest1->buf() + digest1->size(),
212 digest2->buf(),
213 digest2->buf() + digest2->size());
214}
215
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700216BOOST_AUTO_TEST_CASE(Print)
217{
218 using namespace CryptoPP;
219
220 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
221 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
222 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
223 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
224
225 std::string hexString;
226 StringSource(origin, 32, true, new HexEncoder(new StringSink(hexString)));
227
228 std::string str("TEST");
229 Sha256 digest;
230 digest << str;
231
232 std::ostringstream os;
233 os << digest;
234
235 BOOST_CHECK_EQUAL(os.str(), hexString);
236 BOOST_CHECK_EQUAL(digest.toString(), hexString);
237
238}
239
Yingdi Yude222c72014-08-15 16:06:52 -0700240BOOST_AUTO_TEST_SUITE_END()
241
242} // namespace test
243} // namespace util
244} // namespace ndn