blob: 36a72e227564fa013cebf43c7ad5cadfea78ddef [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento94368312017-07-08 22:25:03 -04002/*
3 * Copyright (c) 2013-2017 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"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070023#include "util/string-helper.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070024
25#include "boost-test.hpp"
26
27namespace ndn {
28namespace util {
29namespace test {
30
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento94368312017-07-08 22:25:03 -040032BOOST_AUTO_TEST_SUITE(TestSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070033
Davide Pesavento94368312017-07-08 22:25:03 -040034BOOST_AUTO_TEST_CASE(Basic)
Yingdi Yude222c72014-08-15 16:06:52 -070035{
Davide Pesavento94368312017-07-08 22:25:03 -040036 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
37 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -070038
39 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040040 BOOST_CHECK_EQUAL(statefulSha256.empty(), true);
41
Yingdi Yude222c72014-08-15 16:06:52 -070042 statefulSha256.update(origin, 1);
43 statefulSha256.update(origin + 1, 1);
44 statefulSha256.update(origin + 2, 1);
45 statefulSha256.update(origin + 3, 1);
46 ConstBufferPtr digest2 = statefulSha256.computeDigest();
Davide Pesavento94368312017-07-08 22:25:03 -040047 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
48 digest2->buf(), digest2->buf() + digest2->size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070049}
50
51BOOST_AUTO_TEST_CASE(ConstructFromStream)
52{
Davide Pesavento94368312017-07-08 22:25:03 -040053 const std::string input = "Hello, World!";
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070054 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(input.data()),
55 input.size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070056
57 std::istringstream is(input);
Davide Pesavento94368312017-07-08 22:25:03 -040058 Sha256 sha(is);
59 BOOST_CHECK_EQUAL(sha.empty(), false);
60 BOOST_CHECK_EQUAL(sha.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
61
62 ConstBufferPtr digest2 = sha.computeDigest();
63 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
64 digest2->buf(), digest2->buf() + digest2->size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070065}
66
Yingdi Yude222c72014-08-15 16:06:52 -070067BOOST_AUTO_TEST_CASE(Compare)
68{
Davide Pesavento94368312017-07-08 22:25:03 -040069 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yude222c72014-08-15 16:06:52 -070070
71 Sha256 digest;
Davide Pesavento94368312017-07-08 22:25:03 -040072 digest.update(origin, sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -070073 digest.computeDigest();
74
75 Sha256 digest2;
76 digest2.update(origin, 1);
77 digest2.update(origin + 1, 1);
78 digest2.update(origin + 2, 1);
79 digest2.update(origin + 3, 1);
80 digest2.computeDigest();
81
Davide Pesavento94368312017-07-08 22:25:03 -040082 BOOST_CHECK_EQUAL(digest == digest2, true);
Yingdi Yude222c72014-08-15 16:06:52 -070083 BOOST_CHECK_EQUAL(digest != digest2, false);
84}
85
Davide Pesavento94368312017-07-08 22:25:03 -040086BOOST_AUTO_TEST_CASE(InsertionOperatorSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070087{
Davide Pesavento94368312017-07-08 22:25:03 -040088 const uint8_t origin[] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
89 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
90 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
91 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
92 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -070093
Davide Pesavento94368312017-07-08 22:25:03 -040094 Sha256 innerDigest;
95 innerDigest << "TEST";
Yingdi Yude222c72014-08-15 16:06:52 -070096
97 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040098 statefulSha256 << innerDigest;
Yingdi Yude222c72014-08-15 16:06:52 -070099 ConstBufferPtr digest2 = statefulSha256.computeDigest();
100
Davide Pesavento94368312017-07-08 22:25:03 -0400101 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
102 digest2->buf(), digest2->buf() + digest2->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700103}
104
Davide Pesavento94368312017-07-08 22:25:03 -0400105BOOST_AUTO_TEST_CASE(InsertionOperatorString)
Yingdi Yude222c72014-08-15 16:06:52 -0700106{
Davide Pesavento94368312017-07-08 22:25:03 -0400107 const std::string str = "Hello, World!";
108 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(str.data()),
109 str.size());
Yingdi Yude222c72014-08-15 16:06:52 -0700110
Yingdi Yude222c72014-08-15 16:06:52 -0700111 Sha256 statefulSha256;
112 statefulSha256 << str;
113 ConstBufferPtr digest2 = statefulSha256.computeDigest();
114
Davide Pesavento94368312017-07-08 22:25:03 -0400115 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
116 digest2->buf(), digest2->buf() + digest2->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700117}
118
Davide Pesavento94368312017-07-08 22:25:03 -0400119BOOST_AUTO_TEST_CASE(InsertionOperatorBlock)
Yingdi Yude222c72014-08-15 16:06:52 -0700120{
Davide Pesavento94368312017-07-08 22:25:03 -0400121 const uint8_t origin[] = {
Yingdi Yude222c72014-08-15 16:06:52 -0700122 0x16, 0x1b, // SignatureInfo
123 0x1b, 0x01, // SignatureType
124 0x01, // Sha256WithRsa
125 0x1c, 0x16, // KeyLocator
126 0x07, 0x14, // Name
127 0x08, 0x04,
128 0x74, 0x65, 0x73, 0x74,
129 0x08, 0x03,
130 0x6b, 0x65, 0x79,
131 0x08, 0x07,
132 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
133 };
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700134 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -0700135
136 Sha256 statefulSha256;
137 Block block(origin, sizeof(origin));
138 statefulSha256 << block;
139 ConstBufferPtr digest2 = statefulSha256.computeDigest();
140
Davide Pesavento94368312017-07-08 22:25:03 -0400141 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
142 digest2->buf(), digest2->buf() + digest2->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700143}
144
Davide Pesavento94368312017-07-08 22:25:03 -0400145BOOST_AUTO_TEST_CASE(InsertionOperatorUint64t)
Yingdi Yude222c72014-08-15 16:06:52 -0700146{
Davide Pesavento94368312017-07-08 22:25:03 -0400147 const uint64_t origin[] = {1, 2, 3, 4};
148 ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(origin),
149 sizeof(origin));
Yingdi Yude222c72014-08-15 16:06:52 -0700150
151 Sha256 statefulSha256;
152 statefulSha256 << origin[0];
153 statefulSha256 << origin[1];
154 statefulSha256 << origin[2];
155 statefulSha256 << origin[3];
156 ConstBufferPtr digest2 = statefulSha256.computeDigest();
157
Davide Pesavento94368312017-07-08 22:25:03 -0400158 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
159 digest2->buf(), digest2->buf() + digest2->size());
160}
161
162BOOST_AUTO_TEST_CASE(Reset)
163{
164 Sha256 sha;
165 BOOST_CHECK_EQUAL(sha.empty(), true);
166
167 sha << 42;
168 BOOST_CHECK_EQUAL(sha.empty(), false);
169
170 sha.computeDigest(); // finalize
171 sha.reset();
172 BOOST_CHECK_EQUAL(sha.empty(), true);
173 BOOST_CHECK_NO_THROW(sha << 42);
Yingdi Yude222c72014-08-15 16:06:52 -0700174}
175
Yingdi Yude222c72014-08-15 16:06:52 -0700176BOOST_AUTO_TEST_CASE(Error)
177{
Davide Pesavento94368312017-07-08 22:25:03 -0400178 Sha256 sha;
179 sha << 42;
180 sha.computeDigest(); // finalize
181 BOOST_CHECK_THROW(sha << 42, Sha256::Error);
Yingdi Yude222c72014-08-15 16:06:52 -0700182}
183
Davide Pesavento94368312017-07-08 22:25:03 -0400184BOOST_AUTO_TEST_CASE(StaticComputeDigest)
Yingdi Yude222c72014-08-15 16:06:52 -0700185{
Davide Pesavento94368312017-07-08 22:25:03 -0400186 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
187 ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
188 ConstBufferPtr digest2 = Sha256::computeDigest(origin, sizeof(origin));
189 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
190 digest2->buf(), digest2->buf() + digest2->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700191}
192
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700193BOOST_AUTO_TEST_CASE(Print)
194{
Davide Pesavento94368312017-07-08 22:25:03 -0400195 const uint8_t origin[] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
196 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
197 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
198 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
199 std::string hexString = toHex(origin, sizeof(origin));
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700200
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700201 Sha256 digest;
Davide Pesavento94368312017-07-08 22:25:03 -0400202 digest << "TEST";
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700203 std::ostringstream os;
204 os << digest;
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700205 BOOST_CHECK_EQUAL(os.str(), hexString);
206 BOOST_CHECK_EQUAL(digest.toString(), hexString);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700207}
208
Davide Pesavento94368312017-07-08 22:25:03 -0400209BOOST_AUTO_TEST_SUITE_END() // TestSha256
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100210BOOST_AUTO_TEST_SUITE_END() // Util
Yingdi Yude222c72014-08-15 16:06:52 -0700211
212} // namespace test
213} // namespace util
214} // namespace ndn