blob: 577578568c7c4573e8edf93dc47f04fb394aba5c [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
Junxiao Shi6938e342017-07-25 21:56:58 +000022#include "util/sha256.hpp"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070023#include "util/string-helper.hpp"
Davide Pesavento10b24be2017-07-12 23:23:46 -040024#include "encoding/endian.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070025
26#include "boost-test.hpp"
27
Davide Pesaventoa84f4642017-08-23 16:14:51 -040028#include <sstream>
29
Yingdi Yude222c72014-08-15 16:06:52 -070030namespace ndn {
31namespace util {
32namespace test {
33
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 Pesavento10b24be2017-07-12 23:23:46 -040039 const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
40 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
Yingdi Yude222c72014-08-15 16:06:52 -070041
42 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040043 BOOST_CHECK_EQUAL(statefulSha256.empty(), true);
44
Davide Pesavento10b24be2017-07-12 23:23:46 -040045 statefulSha256.update(input, 1);
46 statefulSha256.update(input + 1, 1);
47 statefulSha256.update(input + 2, 1);
48 statefulSha256.update(input + 3, 1);
49 ConstBufferPtr digest = statefulSha256.computeDigest();
50 BOOST_CHECK_EQUAL(digest->size(), Sha256::DIGEST_SIZE);
Davide Pesavento5d0b0102017-10-07 13:43:16 -040051 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
52 digest->data(), digest->data() + digest->size());
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 Pesavento5d0b0102017-10-07 13:43:16 -040066 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
67 digest->data(), digest->data() + digest->size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070068}
69
Yingdi Yude222c72014-08-15 16:06:52 -070070BOOST_AUTO_TEST_CASE(Compare)
71{
Davide Pesavento94368312017-07-08 22:25:03 -040072 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yude222c72014-08-15 16:06:52 -070073
Davide Pesavento10b24be2017-07-12 23:23:46 -040074 Sha256 digest1;
75 digest1.update(origin, sizeof(origin));
76 digest1.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -070077
78 Sha256 digest2;
79 digest2.update(origin, 1);
80 digest2.update(origin + 1, 1);
81 digest2.update(origin + 2, 1);
82 digest2.update(origin + 3, 1);
83 digest2.computeDigest();
84
Davide Pesavento10b24be2017-07-12 23:23:46 -040085 BOOST_CHECK_EQUAL(digest1 == digest2, true);
86 BOOST_CHECK_EQUAL(digest1 != digest2, false);
Yingdi Yude222c72014-08-15 16:06:52 -070087}
88
Davide Pesavento94368312017-07-08 22:25:03 -040089BOOST_AUTO_TEST_CASE(InsertionOperatorSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070090{
Davide Pesavento10b24be2017-07-12 23:23:46 -040091 auto expected = fromHex("d7bd34bfe44a18d2aa755a344fe3e6b06ed0473772e6dfce16ac71ba0b0a241c");
Yingdi Yude222c72014-08-15 16:06:52 -070092
Davide Pesavento94368312017-07-08 22:25:03 -040093 Sha256 innerDigest;
94 innerDigest << "TEST";
Yingdi Yude222c72014-08-15 16:06:52 -070095
96 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040097 statefulSha256 << innerDigest;
Davide Pesavento10b24be2017-07-12 23:23:46 -040098 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -070099
Davide Pesavento10b24be2017-07-12 23:23:46 -0400100 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400101 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
102 digest->data(), digest->data() + digest->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 Pesavento10b24be2017-07-12 23:23:46 -0400107 const std::string input = "Hello, world!";
108 auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Yingdi Yude222c72014-08-15 16:06:52 -0700109
Yingdi Yude222c72014-08-15 16:06:52 -0700110 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400111 statefulSha256 << input;
112 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700113
Davide Pesavento10b24be2017-07-12 23:23:46 -0400114 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400115 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
116 digest->data(), digest->data() + digest->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 Pesavento10b24be2017-07-12 23:23:46 -0400121 const uint8_t input[] = {
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 };
Davide Pesavento10b24be2017-07-12 23:23:46 -0400134 auto expected = fromHex("b372edfd4d6a4db2cfeaeead6c34fdee9b9e759f7b8d799cf8067e39e7f2886c");
Yingdi Yude222c72014-08-15 16:06:52 -0700135
136 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400137 statefulSha256 << Block{input, sizeof(input)};
138 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700139
Davide Pesavento10b24be2017-07-12 23:23:46 -0400140 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400141 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
142 digest->data(), digest->data() + digest->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 Pesavento10b24be2017-07-12 23:23:46 -0400147 const uint64_t input[] = {1, 2, 3, 4};
148 auto expected = fromHex("7236c00c170036c6de133a878210ddd58567aa1d0619a0f70f69e38ae6f916e9");
Yingdi Yude222c72014-08-15 16:06:52 -0700149
150 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400151 for (size_t i = 0; i < sizeof(input) / sizeof(uint64_t); ++i) {
152 statefulSha256 << htobe64(input[i]);
153 }
154 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700155
Davide Pesavento10b24be2017-07-12 23:23:46 -0400156 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400157 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
158 digest->data(), digest->data() + digest->size());
Davide Pesavento94368312017-07-08 22:25:03 -0400159}
160
161BOOST_AUTO_TEST_CASE(Reset)
162{
163 Sha256 sha;
164 BOOST_CHECK_EQUAL(sha.empty(), true);
165
166 sha << 42;
167 BOOST_CHECK_EQUAL(sha.empty(), false);
168
169 sha.computeDigest(); // finalize
170 sha.reset();
171 BOOST_CHECK_EQUAL(sha.empty(), true);
172 BOOST_CHECK_NO_THROW(sha << 42);
Yingdi Yude222c72014-08-15 16:06:52 -0700173}
174
Yingdi Yude222c72014-08-15 16:06:52 -0700175BOOST_AUTO_TEST_CASE(Error)
176{
Davide Pesavento94368312017-07-08 22:25:03 -0400177 Sha256 sha;
178 sha << 42;
179 sha.computeDigest(); // finalize
180 BOOST_CHECK_THROW(sha << 42, Sha256::Error);
Yingdi Yude222c72014-08-15 16:06:52 -0700181}
182
Davide Pesavento94368312017-07-08 22:25:03 -0400183BOOST_AUTO_TEST_CASE(StaticComputeDigest)
Yingdi Yude222c72014-08-15 16:06:52 -0700184{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400185 const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
186 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
187
188 ConstBufferPtr digest = Sha256::computeDigest(input, sizeof(input));
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400189 BOOST_CHECK_EQUAL_COLLECTIONS(expected->data(), expected->data() + expected->size(),
190 digest->data(), digest->data() + digest->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};
Davide Pesavento10b24be2017-07-12 23:23:46 -0400199 std::string expected = 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;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400205 BOOST_CHECK_EQUAL(os.str(), expected);
206 BOOST_CHECK_EQUAL(digest.toString(), expected);
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