blob: 252a0e704ec235b20bfefa287690eb04baa6c1a0 [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
51BOOST_AUTO_TEST_CASE(Compare)
52{
53 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
54
55 Sha256 digest;
56 digest.update(origin, 4);
57 digest.computeDigest();
58
59 Sha256 digest2;
60 digest2.update(origin, 1);
61 digest2.update(origin + 1, 1);
62 digest2.update(origin + 2, 1);
63 digest2.update(origin + 3, 1);
64 digest2.computeDigest();
65
66 BOOST_CHECK(digest == digest2);
67 BOOST_CHECK_EQUAL(digest != digest2, false);
68}
69
70BOOST_AUTO_TEST_CASE(OperatorDigest)
71{
72 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
73 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
74 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
75 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
76 ConstBufferPtr digest1 = crypto::sha256(origin, 32);
77
78 std::string str("TEST");
79 Sha256 metaDigest;
80 metaDigest << str;
81
82 Sha256 statefulSha256;
83 statefulSha256 << metaDigest;
84 ConstBufferPtr digest2 = statefulSha256.computeDigest();
85
86 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
87 digest1->buf() + digest1->size(),
88 digest2->buf(),
89 digest2->buf() + digest2->size());
90}
91
92BOOST_AUTO_TEST_CASE(OperatorString)
93{
94 uint8_t origin[4] = {0x54, 0x45, 0x53, 0x54};
95 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
96
97 std::string str("TEST");
98 Sha256 statefulSha256;
99 statefulSha256 << str;
100 ConstBufferPtr digest2 = statefulSha256.computeDigest();
101
102 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
103 digest1->buf() + digest1->size(),
104 digest2->buf(),
105 digest2->buf() + digest2->size());
106}
107
108BOOST_AUTO_TEST_CASE(OperatorBlock)
109{
110 uint8_t origin[] = {
111 0x16, 0x1b, // SignatureInfo
112 0x1b, 0x01, // SignatureType
113 0x01, // Sha256WithRsa
114 0x1c, 0x16, // KeyLocator
115 0x07, 0x14, // Name
116 0x08, 0x04,
117 0x74, 0x65, 0x73, 0x74,
118 0x08, 0x03,
119 0x6b, 0x65, 0x79,
120 0x08, 0x07,
121 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
122 };
123 ConstBufferPtr digest1 = crypto::sha256(origin, sizeof(origin));
124
125 Sha256 statefulSha256;
126 Block block(origin, sizeof(origin));
127 statefulSha256 << block;
128 ConstBufferPtr digest2 = statefulSha256.computeDigest();
129
130 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
131 digest1->buf() + digest1->size(),
132 digest2->buf(),
133 digest2->buf() + digest2->size());
134}
135
136BOOST_AUTO_TEST_CASE(OperatorUint64t)
137{
138 uint64_t origin[4] = {1, 2, 3, 4};
139 ConstBufferPtr digest1 = crypto::sha256(reinterpret_cast<uint8_t*>(origin), 32);
140
141 Sha256 statefulSha256;
142 statefulSha256 << origin[0];
143 statefulSha256 << origin[1];
144 statefulSha256 << origin[2];
145 statefulSha256 << origin[3];
146 ConstBufferPtr digest2 = statefulSha256.computeDigest();
147
148 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
149 digest1->buf() + digest1->size(),
150 digest2->buf(),
151 digest2->buf() + digest2->size());
152}
153
154
155BOOST_AUTO_TEST_CASE(Error)
156{
157 uint64_t origin = 256;
158
159 Sha256 digest;
160 BOOST_CHECK(digest.empty());
161
162 digest << origin;
163
164 BOOST_CHECK_NO_THROW(digest.computeDigest());
165 BOOST_CHECK_THROW(digest << origin, Sha256::Error);
166
167 digest.reset();
168}
169
170BOOST_AUTO_TEST_CASE(ComputeDigest)
171{
172 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
173 ConstBufferPtr digest1 = crypto::sha256(origin, 4);
174
175 ConstBufferPtr digest2 = Sha256::computeDigest(origin, 4);
176
177 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(),
178 digest1->buf() + digest1->size(),
179 digest2->buf(),
180 digest2->buf() + digest2->size());
181}
182
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700183BOOST_AUTO_TEST_CASE(Print)
184{
185 using namespace CryptoPP;
186
187 uint8_t origin[32] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
188 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
189 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
190 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
191
192 std::string hexString;
193 StringSource(origin, 32, true, new HexEncoder(new StringSink(hexString)));
194
195 std::string str("TEST");
196 Sha256 digest;
197 digest << str;
198
199 std::ostringstream os;
200 os << digest;
201
202 BOOST_CHECK_EQUAL(os.str(), hexString);
203 BOOST_CHECK_EQUAL(digest.toString(), hexString);
204
205}
206
Yingdi Yude222c72014-08-15 16:06:52 -0700207BOOST_AUTO_TEST_SUITE_END()
208
209} // namespace test
210} // namespace util
211} // namespace ndn