blob: 25f75dda4dae8aaaa9f479cb04cd21e93a6b1e67 [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032BOOST_AUTO_TEST_SUITE(Util)
33BOOST_AUTO_TEST_SUITE(TestDigest)
Yingdi Yude222c72014-08-15 16:06:52 -070034
35BOOST_AUTO_TEST_CASE(Sha256Digest)
36{
37 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070038 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, 4);
Yingdi Yude222c72014-08-15 16:06:52 -070039
40 Sha256 statefulSha256;
41 statefulSha256.update(origin, 1);
42 statefulSha256.update(origin + 1, 1);
43 statefulSha256.update(origin + 2, 1);
44 statefulSha256.update(origin + 3, 1);
45 ConstBufferPtr digest2 = statefulSha256.computeDigest();
46
47 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
48 digest1->buf() + digest1->size(),
49 digest2->buf(),
50 digest2->buf() + digest2->size());
51}
52
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070053BOOST_AUTO_TEST_CASE(Compute)
54{
55 std::string input = "Hello, World!";
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070056 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(input.data()),
57 input.size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070058
59 Sha256 hashObject;
60 hashObject << input;
61 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
62 ConstBufferPtr digest2 = hashObject.computeDigest();
63 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
64 digest1->buf() + digest1->size(),
65 digest2->buf(),
66 digest2->buf() + digest2->size());
67
68}
69
70BOOST_AUTO_TEST_CASE(ConstructFromStream)
71{
72 std::string input = "Hello, World!";
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070073 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(input.data()),
74 input.size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070075
76 std::istringstream is(input);
77 Sha256 hashObject(is);
78 BOOST_CHECK_EQUAL(hashObject.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
79 ConstBufferPtr digest2 = hashObject.computeDigest();
80 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
81 digest1->buf() + digest1->size(),
82 digest2->buf(),
83 digest2->buf() + digest2->size());
84}
85
Yingdi Yude222c72014-08-15 16:06:52 -070086BOOST_AUTO_TEST_CASE(Compare)
87{
88 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
89
90 Sha256 digest;
91 digest.update(origin, 4);
92 digest.computeDigest();
93
94 Sha256 digest2;
95 digest2.update(origin, 1);
96 digest2.update(origin + 1, 1);
97 digest2.update(origin + 2, 1);
98 digest2.update(origin + 3, 1);
99 digest2.computeDigest();
100
101 BOOST_CHECK(digest == digest2);
102 BOOST_CHECK_EQUAL(digest != digest2, false);
103}
104
105BOOST_AUTO_TEST_CASE(OperatorDigest)
106{
107 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
108 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
109 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
110 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700111 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, 32);
Yingdi Yude222c72014-08-15 16:06:52 -0700112
113 std::string str("TEST");
114 Sha256 metaDigest;
115 metaDigest << str;
116
117 Sha256 statefulSha256;
118 statefulSha256 << metaDigest;
119 ConstBufferPtr digest2 = statefulSha256.computeDigest();
120
121 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
122 digest1->buf() + digest1->size(),
123 digest2->buf(),
124 digest2->buf() + digest2->size());
125}
126
127BOOST_AUTO_TEST_CASE(OperatorString)
128{
129 uint8_t origin[4] = {0x54, 0x45, 0x53, 0x54};
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700130 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, 4);
Yingdi Yude222c72014-08-15 16:06:52 -0700131
132 std::string str("TEST");
133 Sha256 statefulSha256;
134 statefulSha256 << str;
135 ConstBufferPtr digest2 = statefulSha256.computeDigest();
136
137 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
138 digest1->buf() + digest1->size(),
139 digest2->buf(),
140 digest2->buf() + digest2->size());
141}
142
143BOOST_AUTO_TEST_CASE(OperatorBlock)
144{
145 uint8_t origin[] = {
146 0x16, 0x1b, // SignatureInfo
147 0x1b, 0x01, // SignatureType
148 0x01, // Sha256WithRsa
149 0x1c, 0x16, // KeyLocator
150 0x07, 0x14, // Name
151 0x08, 0x04,
152 0x74, 0x65, 0x73, 0x74,
153 0x08, 0x03,
154 0x6b, 0x65, 0x79,
155 0x08, 0x07,
156 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
157 };
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700158 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -0700159
160 Sha256 statefulSha256;
161 Block block(origin, sizeof(origin));
162 statefulSha256 << block;
163 ConstBufferPtr digest2 = statefulSha256.computeDigest();
164
165 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
166 digest1->buf() + digest1->size(),
167 digest2->buf(),
168 digest2->buf() + digest2->size());
169}
170
171BOOST_AUTO_TEST_CASE(OperatorUint64t)
172{
173 uint64_t origin[4] = {1, 2, 3, 4};
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700174 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<uint8_t*>(origin), 32);
Yingdi Yude222c72014-08-15 16:06:52 -0700175
176 Sha256 statefulSha256;
177 statefulSha256 << origin[0];
178 statefulSha256 << origin[1];
179 statefulSha256 << origin[2];
180 statefulSha256 << origin[3];
181 ConstBufferPtr digest2 = statefulSha256.computeDigest();
182
183 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
184 digest1->buf() + digest1->size(),
185 digest2->buf(),
186 digest2->buf() + digest2->size());
187}
188
Yingdi Yude222c72014-08-15 16:06:52 -0700189BOOST_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};
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700207 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, 4);
Yingdi Yude222c72014-08-15 16:06:52 -0700208
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
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100237BOOST_AUTO_TEST_SUITE_END() // TestDigest
238BOOST_AUTO_TEST_SUITE_END() // Util
Yingdi Yude222c72014-08-15 16:06:52 -0700239
240} // namespace test
241} // namespace util
242} // namespace ndn