blob: 6e4220174e1e5d12eabd66517a09536eb43e8f59 [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
28namespace ndn {
29namespace util {
30namespace test {
31
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento94368312017-07-08 22:25:03 -040033BOOST_AUTO_TEST_SUITE(TestSha256)
Yingdi Yude222c72014-08-15 16:06:52 -070034
Davide Pesavento94368312017-07-08 22:25:03 -040035BOOST_AUTO_TEST_CASE(Basic)
Yingdi Yude222c72014-08-15 16:06:52 -070036{
Davide Pesavento10b24be2017-07-12 23:23:46 -040037 const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
38 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
Yingdi Yude222c72014-08-15 16:06:52 -070039
40 Sha256 statefulSha256;
Davide Pesavento94368312017-07-08 22:25:03 -040041 BOOST_CHECK_EQUAL(statefulSha256.empty(), true);
42
Davide Pesavento10b24be2017-07-12 23:23:46 -040043 statefulSha256.update(input, 1);
44 statefulSha256.update(input + 1, 1);
45 statefulSha256.update(input + 2, 1);
46 statefulSha256.update(input + 3, 1);
47 ConstBufferPtr digest = statefulSha256.computeDigest();
48 BOOST_CHECK_EQUAL(digest->size(), Sha256::DIGEST_SIZE);
49 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
50 digest->buf(), digest->buf() + digest->size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070051}
52
53BOOST_AUTO_TEST_CASE(ConstructFromStream)
54{
Davide Pesavento10b24be2017-07-12 23:23:46 -040055 const std::string input = "Hello, world!";
56 auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070057
58 std::istringstream is(input);
Davide Pesavento94368312017-07-08 22:25:03 -040059 Sha256 sha(is);
60 BOOST_CHECK_EQUAL(sha.empty(), false);
Davide Pesavento10b24be2017-07-12 23:23:46 -040061 BOOST_CHECK_EQUAL(sha.toString(), "315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3");
Davide Pesavento94368312017-07-08 22:25:03 -040062
Davide Pesavento10b24be2017-07-12 23:23:46 -040063 ConstBufferPtr digest = sha.computeDigest();
64 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
65 digest->buf(), digest->buf() + digest->size());
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070066}
67
Yingdi Yude222c72014-08-15 16:06:52 -070068BOOST_AUTO_TEST_CASE(Compare)
69{
Davide Pesavento94368312017-07-08 22:25:03 -040070 const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yude222c72014-08-15 16:06:52 -070071
Davide Pesavento10b24be2017-07-12 23:23:46 -040072 Sha256 digest1;
73 digest1.update(origin, sizeof(origin));
74 digest1.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -070075
76 Sha256 digest2;
77 digest2.update(origin, 1);
78 digest2.update(origin + 1, 1);
79 digest2.update(origin + 2, 1);
80 digest2.update(origin + 3, 1);
81 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);
99 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
100 digest->buf(), digest->buf() + digest->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700101}
102
Davide Pesavento94368312017-07-08 22:25:03 -0400103BOOST_AUTO_TEST_CASE(InsertionOperatorString)
Yingdi Yude222c72014-08-15 16:06:52 -0700104{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400105 const std::string input = "Hello, world!";
106 auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Yingdi Yude222c72014-08-15 16:06:52 -0700107
Yingdi Yude222c72014-08-15 16:06:52 -0700108 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400109 statefulSha256 << input;
110 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700111
Davide Pesavento10b24be2017-07-12 23:23:46 -0400112 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
113 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
114 digest->buf(), digest->buf() + digest->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700115}
116
Davide Pesavento94368312017-07-08 22:25:03 -0400117BOOST_AUTO_TEST_CASE(InsertionOperatorBlock)
Yingdi Yude222c72014-08-15 16:06:52 -0700118{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400119 const uint8_t input[] = {
Yingdi Yude222c72014-08-15 16:06:52 -0700120 0x16, 0x1b, // SignatureInfo
121 0x1b, 0x01, // SignatureType
122 0x01, // Sha256WithRsa
123 0x1c, 0x16, // KeyLocator
124 0x07, 0x14, // Name
125 0x08, 0x04,
126 0x74, 0x65, 0x73, 0x74,
127 0x08, 0x03,
128 0x6b, 0x65, 0x79,
129 0x08, 0x07,
130 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
131 };
Davide Pesavento10b24be2017-07-12 23:23:46 -0400132 auto expected = fromHex("b372edfd4d6a4db2cfeaeead6c34fdee9b9e759f7b8d799cf8067e39e7f2886c");
Yingdi Yude222c72014-08-15 16:06:52 -0700133
134 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400135 statefulSha256 << Block{input, sizeof(input)};
136 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700137
Davide Pesavento10b24be2017-07-12 23:23:46 -0400138 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
139 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
140 digest->buf(), digest->buf() + digest->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700141}
142
Davide Pesavento94368312017-07-08 22:25:03 -0400143BOOST_AUTO_TEST_CASE(InsertionOperatorUint64t)
Yingdi Yude222c72014-08-15 16:06:52 -0700144{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400145 const uint64_t input[] = {1, 2, 3, 4};
146 auto expected = fromHex("7236c00c170036c6de133a878210ddd58567aa1d0619a0f70f69e38ae6f916e9");
Yingdi Yude222c72014-08-15 16:06:52 -0700147
148 Sha256 statefulSha256;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400149 for (size_t i = 0; i < sizeof(input) / sizeof(uint64_t); ++i) {
150 statefulSha256 << htobe64(input[i]);
151 }
152 ConstBufferPtr digest = statefulSha256.computeDigest();
Yingdi Yude222c72014-08-15 16:06:52 -0700153
Davide Pesavento10b24be2017-07-12 23:23:46 -0400154 BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
155 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
156 digest->buf(), digest->buf() + digest->size());
Davide Pesavento94368312017-07-08 22:25:03 -0400157}
158
159BOOST_AUTO_TEST_CASE(Reset)
160{
161 Sha256 sha;
162 BOOST_CHECK_EQUAL(sha.empty(), true);
163
164 sha << 42;
165 BOOST_CHECK_EQUAL(sha.empty(), false);
166
167 sha.computeDigest(); // finalize
168 sha.reset();
169 BOOST_CHECK_EQUAL(sha.empty(), true);
170 BOOST_CHECK_NO_THROW(sha << 42);
Yingdi Yude222c72014-08-15 16:06:52 -0700171}
172
Yingdi Yude222c72014-08-15 16:06:52 -0700173BOOST_AUTO_TEST_CASE(Error)
174{
Davide Pesavento94368312017-07-08 22:25:03 -0400175 Sha256 sha;
176 sha << 42;
177 sha.computeDigest(); // finalize
178 BOOST_CHECK_THROW(sha << 42, Sha256::Error);
Yingdi Yude222c72014-08-15 16:06:52 -0700179}
180
Davide Pesavento94368312017-07-08 22:25:03 -0400181BOOST_AUTO_TEST_CASE(StaticComputeDigest)
Yingdi Yude222c72014-08-15 16:06:52 -0700182{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400183 const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
184 auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
185
186 ConstBufferPtr digest = Sha256::computeDigest(input, sizeof(input));
187 BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
188 digest->buf(), digest->buf() + digest->size());
Yingdi Yude222c72014-08-15 16:06:52 -0700189}
190
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700191BOOST_AUTO_TEST_CASE(Print)
192{
Davide Pesavento94368312017-07-08 22:25:03 -0400193 const uint8_t origin[] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
194 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
195 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
196 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
Davide Pesavento10b24be2017-07-12 23:23:46 -0400197 std::string expected = toHex(origin, sizeof(origin));
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700198
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700199 Sha256 digest;
Davide Pesavento94368312017-07-08 22:25:03 -0400200 digest << "TEST";
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700201 std::ostringstream os;
202 os << digest;
Davide Pesavento10b24be2017-07-12 23:23:46 -0400203 BOOST_CHECK_EQUAL(os.str(), expected);
204 BOOST_CHECK_EQUAL(digest.toString(), expected);
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700205}
206
Davide Pesavento94368312017-07-08 22:25:03 -0400207BOOST_AUTO_TEST_SUITE_END() // TestSha256
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100208BOOST_AUTO_TEST_SUITE_END() // Util
Yingdi Yude222c72014-08-15 16:06:52 -0700209
210} // namespace test
211} // namespace util
212} // namespace ndn