blob: e1b3278d73d6ae4c7c4d09f809504bf68f50e749 [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"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070024#include "util/string-helper.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070025
26#include "boost-test.hpp"
27
28namespace ndn {
29namespace util {
30namespace test {
31
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032BOOST_AUTO_TEST_SUITE(UtilDigest)
Yingdi Yude222c72014-08-15 16:06:52 -070033
34BOOST_AUTO_TEST_CASE(Sha256Digest)
35{
36 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
37 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
38
39 Sha256 statefulSha256;
40 statefulSha256.update(origin, 1);
41 statefulSha256.update(origin + 1, 1);
42 statefulSha256.update(origin + 2, 1);
43 statefulSha256.update(origin + 3, 1);
44 ConstBufferPtr digest2 = statefulSha256.computeDigest();
45
46 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
47 digest1->buf() + digest1->size(),
48 digest2->buf(),
49 digest2->buf() + digest2->size());
50}
51
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070052BOOST_AUTO_TEST_CASE(Compute)
53{
54 std::string input = "Hello, World!";
55 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<const uint8_t*>(input.data()),
56 input.size());
57
58 Sha256 hashObject;
59 hashObject << input;
60 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
61 ConstBufferPtr digest2 = hashObject.computeDigest();
62 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
63 digest1->buf() + digest1->size(),
64 digest2->buf(),
65 digest2->buf() + digest2->size());
66
67}
68
69BOOST_AUTO_TEST_CASE(ConstructFromStream)
70{
71 std::string input = "Hello, World!";
72 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<const uint8_t*>(input.data()),
73 input.size());
74
75 std::istringstream is(input);
76 Sha256 hashObject(is);
77 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
78 ConstBufferPtr digest2 = hashObject.computeDigest();
79 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
80 digest1->buf() + digest1->size(),
81 digest2->buf(),
82 digest2->buf() + digest2->size());
83}
84
Yingdi Yude222c72014-08-15 16:06:52 -070085BOOST_AUTO_TEST_CASE(Compare)
86{
87 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
88
89 Sha256 digest;
90 digest.update(origin, 4);
91 digest.computeDigest();
92
93 Sha256 digest2;
94 digest2.update(origin, 1);
95 digest2.update(origin + 1, 1);
96 digest2.update(origin + 2, 1);
97 digest2.update(origin + 3, 1);
98 digest2.computeDigest();
99
100 BOOST_CHECK(digest == digest2);
101 BOOST_CHECK_EQUAL(digest != digest2, false);
102}
103
104BOOST_AUTO_TEST_CASE(OperatorDigest)
105{
106 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
107 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
108 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
109 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
110 ConstBufferPtr digest1 = crypto::sha256(origin, 32);
111
112 std::string str("TEST");
113 Sha256 metaDigest;
114 metaDigest << str;
115
116 Sha256 statefulSha256;
117 statefulSha256 << metaDigest;
118 ConstBufferPtr digest2 = statefulSha256.computeDigest();
119
120 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
121 digest1->buf() + digest1->size(),
122 digest2->buf(),
123 digest2->buf() + digest2->size());
124}
125
126BOOST_AUTO_TEST_CASE(OperatorString)
127{
128 uint8_t origin[4] = {0x54, 0x45, 0x53, 0x54};
129 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
130
131 std::string str("TEST");
132 Sha256 statefulSha256;
133 statefulSha256 << str;
134 ConstBufferPtr digest2 = statefulSha256.computeDigest();
135
136 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
137 digest1->buf() + digest1->size(),
138 digest2->buf(),
139 digest2->buf() + digest2->size());
140}
141
142BOOST_AUTO_TEST_CASE(OperatorBlock)
143{
144 uint8_t origin[] = {
145 0x16, 0x1b, // SignatureInfo
146 0x1b, 0x01, // SignatureType
147 0x01, // Sha256WithRsa
148 0x1c, 0x16, // KeyLocator
149 0x07, 0x14, // Name
150 0x08, 0x04,
151 0x74, 0x65, 0x73, 0x74,
152 0x08, 0x03,
153 0x6b, 0x65, 0x79,
154 0x08, 0x07,
155 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
156 };
157 ConstBufferPtr digest1 = crypto::sha256(origin, sizeof(origin));
158
159 Sha256 statefulSha256;
160 Block block(origin, sizeof(origin));
161 statefulSha256 << block;
162 ConstBufferPtr digest2 = statefulSha256.computeDigest();
163
164 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
165 digest1->buf() + digest1->size(),
166 digest2->buf(),
167 digest2->buf() + digest2->size());
168}
169
170BOOST_AUTO_TEST_CASE(OperatorUint64t)
171{
172 uint64_t origin[4] = {1, 2, 3, 4};
173 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<uint8_t*>(origin), 32);
174
175 Sha256 statefulSha256;
176 statefulSha256 << origin[0];
177 statefulSha256 << origin[1];
178 statefulSha256 << origin[2];
179 statefulSha256 << origin[3];
180 ConstBufferPtr digest2 = statefulSha256.computeDigest();
181
182 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
183 digest1->buf() + digest1->size(),
184 digest2->buf(),
185 digest2->buf() + digest2->size());
186}
187
188
189BOOST_AUTO_TEST_CASE(Error)
190{
191 uint64_t origin = 256;
192
193 Sha256 digest;
194 BOOST_CHECK(digest.empty());
195
196 digest << origin;
197
198 BOOST_CHECK_NO_THROW(digest.computeDigest());
199 BOOST_CHECK_THROW(digest << origin, Sha256::Error);
200
201 digest.reset();
202}
203
204BOOST_AUTO_TEST_CASE(ComputeDigest)
205{
206 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
207 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
208
209 ConstBufferPtr digest2 = Sha256::computeDigest(origin, 4);
210
211 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
212 digest1->buf() + digest1->size(),
213 digest2->buf(),
214 digest2->buf() + digest2->size());
215}
216
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700217BOOST_AUTO_TEST_CASE(Print)
218{
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700219 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
220 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
221 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
222 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
223
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700224 std::string hexString = toHex(origin, 32);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700225
226 std::string str("TEST");
227 Sha256 digest;
228 digest << str;
229
230 std::ostringstream os;
231 os << digest;
232
233 BOOST_CHECK_EQUAL(os.str(), hexString);
234 BOOST_CHECK_EQUAL(digest.toString(), hexString);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700235}
236
Yingdi Yude222c72014-08-15 16:06:52 -0700237BOOST_AUTO_TEST_SUITE_END()
238
239} // namespace test
240} // namespace util
241} // namespace ndn