blob: ae4fea365bc2fc1ee63bef3b368204e7c1836ca6 [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/*
Davide Pesavento35c63792022-01-17 02:06:03 -05003 * Copyright (c) 2013-2022 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/sha256.hpp"
23#include "ndn-cxx/util/string-helper.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070026
Davide Pesavento14883ad2018-07-14 16:31:39 -040027#include <boost/endian/conversion.hpp>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040028#include <sstream>
29
Yingdi Yude222c72014-08-15 16:06:52 -070030namespace ndn {
31namespace util {
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050032namespace tests {
Yingdi Yude222c72014-08-15 16:06:52 -070033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento94368312017-07-08 22:25:03 -040035BOOST_AUTO_TEST_SUITE(TestSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070036
Davide Pesavento94368312017-07-08 22:25:03 -040037BOOST_AUTO_TEST_CASE(Basic)
Yingdi Yude222c72014-08-15 16:06:52 -070038{
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050039 const uint8_t buf[] = {0x01, 0x02, 0x03, 0x04};
40 auto input = make_span(buf);
Davide Pesavento10b24be2017-07-12 23:23:46 -040041 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
Yingdi Yude222c72014-08-15 16:06:52 -070042
43 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040044 BOOST_CHECK_EQUAL(statefulSha256.empty(), true);
45
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050046 statefulSha256.update(input.subspan(0, 1));
47 statefulSha256.update(input.subspan(1, 1));
48 statefulSha256.update(input.subspan(2, 1));
49 statefulSha256.update(input.subspan(3, 1));
Davide Pesavento10b24be2017-07-12 23:23:46 -040050 ConstBufferPtr digest = statefulSha256.computeDigest();
51 BOOST_CHECK_EQUAL(digest->size(), Sha256::DIGEST_SIZE);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050052 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070053}
54
55BOOST_AUTO_TEST_CASE(ConstructFromStream)
56{
Davide Pesavento10b24be2017-07-12 23:23:46 -040057 const std::string input = "Hello, world!";
58 auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070059
60 std::istringstream is(input);
Davide Pesavento94368312017-07-08 22:25:03 -040061 Sha256 sha(is);
62 BOOST_CHECK_EQUAL(sha.empty(), false);
Davide Pesavento10b24be2017-07-12 23:23:46 -040063 BOOST_CHECK_EQUAL(sha.toString(), "315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3");
Davide Pesavento94368312017-07-08 22:25:03 -040064
Davide Pesavento10b24be2017-07-12 23:23:46 -040065 ConstBufferPtr digest = sha.computeDigest();
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050066 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070067}
68
Yingdi Yude222c72014-08-15 16:06:52 -070069BOOST_AUTO_TEST_CASE(Compare)
70{
Davide Pesavento94368312017-07-08 22:25:03 -040071 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yude222c72014-08-15 16:06:52 -070072
Davide Pesavento10b24be2017-07-12 23:23:46 -040073 Sha256 digest1;
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050074 digest1.update(origin);
Davide Pesavento10b24be2017-07-12 23:23:46 -040075 digest1.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -070076
77 Sha256 digest2;
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050078 auto in = make_span(origin);
79 digest2.update(in.first(2));
80 digest2.update(in.last(2));
Yingdi Yude222c72014-08-15 16:06:52 -070081 digest2.computeDigest();
82
Davide Pesavento10b24be2017-07-12 23:23:46 -040083 BOOST_CHECK_EQUAL(digest1 == digest2, true);
84 BOOST_CHECK_EQUAL(digest1 != digest2, false);
Yingdi Yude222c72014-08-15 16:06:52 -070085}
86
Davide Pesavento94368312017-07-08 22:25:03 -040087BOOST_AUTO_TEST_CASE(InsertionOperatorSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070088{
Davide Pesavento10b24be2017-07-12 23:23:46 -040089 auto expected = fromHex("d7bd34bfe44a18d2aa755a344fe3e6b06ed0473772e6dfce16ac71ba0b0a241c");
Yingdi Yude222c72014-08-15 16:06:52 -070090
Davide Pesavento94368312017-07-08 22:25:03 -040091 Sha256 innerDigest;
92 innerDigest << "TEST";
Yingdi Yude222c72014-08-15 16:06:52 -070093
94 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040095 statefulSha256 << innerDigest;
Davide Pesavento10b24be2017-07-12 23:23:46 -040096 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -070097
Davide Pesavento10b24be2017-07-12 23:23:46 -040098 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050099 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Yingdi Yude222c72014-08-15 16:06:52 -0700100}
101
Davide Pesavento94368312017-07-08 22:25:03 -0400102BOOST_AUTO_TEST_CASE(InsertionOperatorString)
Yingdi Yude222c72014-08-15 16:06:52 -0700103{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400104 const std::string input = "Hello, world!";
105 auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Yingdi Yude222c72014-08-15 16:06:52 -0700106
Yingdi Yude222c72014-08-15 16:06:52 -0700107 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400108 statefulSha256 << input;
109 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700110
Davide Pesavento10b24be2017-07-12 23:23:46 -0400111 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500112 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Yingdi Yude222c72014-08-15 16:06:52 -0700113}
114
Davide Pesavento94368312017-07-08 22:25:03 -0400115BOOST_AUTO_TEST_CASE(InsertionOperatorBlock)
Yingdi Yude222c72014-08-15 16:06:52 -0700116{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400117 const uint8_t input[] = {
Yingdi Yude222c72014-08-15 16:06:52 -0700118 0x16, 0x1b, // SignatureInfo
119 0x1b, 0x01, // SignatureType
120 0x01, // Sha256WithRsa
121 0x1c, 0x16, // KeyLocator
122 0x07, 0x14, // Name
123 0x08, 0x04,
124 0x74, 0x65, 0x73, 0x74,
125 0x08, 0x03,
126 0x6b, 0x65, 0x79,
127 0x08, 0x07,
128 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
129 };
Davide Pesavento10b24be2017-07-12 23:23:46 -0400130 auto expected = fromHex("b372edfd4d6a4db2cfeaeead6c34fdee9b9e759f7b8d799cf8067e39e7f2886c");
Yingdi Yude222c72014-08-15 16:06:52 -0700131
132 Sha256 statefulSha256;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500133 statefulSha256 << Block{input};
Davide Pesavento10b24be2017-07-12 23:23:46 -0400134 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700135
Davide Pesavento10b24be2017-07-12 23:23:46 -0400136 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500137 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Yingdi Yude222c72014-08-15 16:06:52 -0700138}
139
Davide Pesavento94368312017-07-08 22:25:03 -0400140BOOST_AUTO_TEST_CASE(InsertionOperatorUint64t)
Yingdi Yude222c72014-08-15 16:06:52 -0700141{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400142 const uint64_t input[] = {1, 2, 3, 4};
143 auto expected = fromHex("7236c00c170036c6de133a878210ddd58567aa1d0619a0f70f69e38ae6f916e9");
Yingdi Yude222c72014-08-15 16:06:52 -0700144
145 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400146 for (size_t i = 0; i < sizeof(input) / sizeof(uint64_t); ++i) {
Davide Pesavento14883ad2018-07-14 16:31:39 -0400147 statefulSha256 << boost::endian::native_to_big(input[i]);
Davide Pesavento10b24be2017-07-12 23:23:46 -0400148 }
149 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700150
Davide Pesavento10b24be2017-07-12 23:23:46 -0400151 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500152 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Davide Pesavento94368312017-07-08 22:25:03 -0400153}
154
155BOOST_AUTO_TEST_CASE(Reset)
156{
157 Sha256 sha;
158 BOOST_CHECK_EQUAL(sha.empty(), true);
159
160 sha << 42;
161 BOOST_CHECK_EQUAL(sha.empty(), false);
162
163 sha.computeDigest(); // finalize
164 sha.reset();
165 BOOST_CHECK_EQUAL(sha.empty(), true);
166 BOOST_CHECK_NO_THROW(sha << 42);
Yingdi Yude222c72014-08-15 16:06:52 -0700167}
168
Yingdi Yude222c72014-08-15 16:06:52 -0700169BOOST_AUTO_TEST_CASE(Error)
170{
Davide Pesavento94368312017-07-08 22:25:03 -0400171 Sha256 sha;
172 sha << 42;
173 sha.computeDigest(); // finalize
174 BOOST_CHECK_THROW(sha << 42, Sha256::Error);
Yingdi Yude222c72014-08-15 16:06:52 -0700175}
176
Davide Pesavento94368312017-07-08 22:25:03 -0400177BOOST_AUTO_TEST_CASE(StaticComputeDigest)
Yingdi Yude222c72014-08-15 16:06:52 -0700178{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400179 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
180
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500181 ConstBufferPtr digest = Sha256::computeDigest({0x01, 0x02, 0x03, 0x04});
182 BOOST_TEST(*digest == *expected, boost::test_tools::per_element());
Yingdi Yude222c72014-08-15 16:06:52 -0700183}
184
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700185BOOST_AUTO_TEST_CASE(Print)
186{
Davide Pesavento94368312017-07-08 22:25:03 -0400187 const uint8_t origin[] = {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};
Davide Pesavento35c63792022-01-17 02:06:03 -0500191 const auto expected = toHex(origin);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700192
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700193 Sha256 digest;
Davide Pesavento94368312017-07-08 22:25:03 -0400194 digest << "TEST";
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700195 std::ostringstream os;
196 os << digest;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400197 BOOST_CHECK_EQUAL(os.str(), expected);
198 BOOST_CHECK_EQUAL(digest.toString(), expected);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700199}
200
Davide Pesavento94368312017-07-08 22:25:03 -0400201BOOST_AUTO_TEST_SUITE_END() // TestSha256
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100202BOOST_AUTO_TEST_SUITE_END() // Util
Yingdi Yude222c72014-08-15 16:06:52 -0700203
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500204} // namespace tests
Yingdi Yude222c72014-08-15 16:06:52 -0700205} // namespace util
206} // namespace ndn