Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/sha256.hpp" |
| 23 | #include "ndn-cxx/util/string-helper.hpp" |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 27 | #include <boost/endian/conversion.hpp> |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 28 | |
| 29 | #include <array> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 30 | #include <sstream> |
| 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | namespace ndn::tests { |
| 33 | |
| 34 | using ndn::util::Sha256; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Util) |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestSha256) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(Basic) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 41 | const uint8_t buf[] = {0x01, 0x02, 0x03, 0x04}; |
| 42 | auto input = make_span(buf); |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 43 | auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a"); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 44 | |
| 45 | Sha256 statefulSha256; |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(statefulSha256.empty(), true); |
| 47 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 48 | statefulSha256.update(input.subspan(0, 1)); |
| 49 | statefulSha256.update(input.subspan(1, 1)); |
| 50 | statefulSha256.update(input.subspan(2, 1)); |
| 51 | statefulSha256.update(input.subspan(3, 1)); |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 52 | ConstBufferPtr digest = statefulSha256.computeDigest(); |
| 53 | BOOST_CHECK_EQUAL(digest->size(), Sha256::DIGEST_SIZE); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 54 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(ConstructFromStream) |
| 58 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 59 | const std::string input = "Hello, world!"; |
| 60 | auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3"); |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 61 | |
| 62 | std::istringstream is(input); |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 63 | Sha256 sha(is); |
| 64 | BOOST_CHECK_EQUAL(sha.empty(), false); |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(sha.toString(), "315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3"); |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 66 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 67 | ConstBufferPtr digest = sha.computeDigest(); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 68 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(Compare) |
| 72 | { |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 73 | const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 74 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 75 | Sha256 digest1; |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 76 | digest1.update(origin); |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 77 | digest1.computeDigest(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 78 | |
| 79 | Sha256 digest2; |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 80 | auto in = make_span(origin); |
| 81 | digest2.update(in.first(2)); |
| 82 | digest2.update(in.last(2)); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 83 | digest2.computeDigest(); |
| 84 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(digest1 == digest2, true); |
| 86 | BOOST_CHECK_EQUAL(digest1 != digest2, false); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(InsertionOperatorSha256) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 90 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 91 | auto expected = fromHex("d7bd34bfe44a18d2aa755a344fe3e6b06ed0473772e6dfce16ac71ba0b0a241c"); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 92 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 93 | Sha256 innerDigest; |
| 94 | innerDigest << "TEST"; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 95 | |
| 96 | Sha256 statefulSha256; |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 97 | statefulSha256 << innerDigest; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 98 | ConstBufferPtr digest = statefulSha256.computeDigest(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 99 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(statefulSha256.empty(), false); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 101 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(InsertionOperatorString) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 105 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 106 | const std::string input = "Hello, world!"; |
| 107 | auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3"); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 108 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 109 | Sha256 statefulSha256; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 110 | statefulSha256 << input; |
| 111 | ConstBufferPtr digest = statefulSha256.computeDigest(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 112 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 113 | BOOST_CHECK_EQUAL(statefulSha256.empty(), false); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 114 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(InsertionOperatorUnsignedInt) |
| 118 | { |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 119 | const std::array input{1, 2, 3, 4}; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 120 | auto expected = fromHex("7236c00c170036c6de133a878210ddd58567aa1d0619a0f70f69e38ae6f916e9"); |
| 121 | |
| 122 | Sha256 statefulSha256; |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 123 | for (auto i : input) { |
| 124 | statefulSha256 << boost::endian::native_to_big(static_cast<uint64_t>(i)); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 125 | } |
| 126 | ConstBufferPtr digest = statefulSha256.computeDigest(); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(statefulSha256.empty(), false); |
| 129 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_CASE(InsertionOperatorSpan) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 133 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 134 | const uint8_t input[] = { |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 135 | 0x16, 0x1b, // SignatureInfo |
| 136 | 0x1b, 0x01, // SignatureType |
| 137 | 0x01, // Sha256WithRsa |
| 138 | 0x1c, 0x16, // KeyLocator |
| 139 | 0x07, 0x14, // Name |
| 140 | 0x08, 0x04, |
| 141 | 0x74, 0x65, 0x73, 0x74, |
| 142 | 0x08, 0x03, |
| 143 | 0x6b, 0x65, 0x79, |
| 144 | 0x08, 0x07, |
| 145 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 146 | }; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 147 | auto expected = fromHex("b372edfd4d6a4db2cfeaeead6c34fdee9b9e759f7b8d799cf8067e39e7f2886c"); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 148 | |
| 149 | Sha256 statefulSha256; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 150 | statefulSha256 << input; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 151 | ConstBufferPtr digest = statefulSha256.computeDigest(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 152 | |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(statefulSha256.empty(), false); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 154 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(Reset) |
| 158 | { |
| 159 | Sha256 sha; |
| 160 | BOOST_CHECK_EQUAL(sha.empty(), true); |
| 161 | |
| 162 | sha << 42; |
| 163 | BOOST_CHECK_EQUAL(sha.empty(), false); |
| 164 | |
| 165 | sha.computeDigest(); // finalize |
| 166 | sha.reset(); |
| 167 | BOOST_CHECK_EQUAL(sha.empty(), true); |
| 168 | BOOST_CHECK_NO_THROW(sha << 42); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 171 | BOOST_AUTO_TEST_CASE(StaticComputeDigest) |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 172 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 173 | auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a"); |
| 174 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 175 | ConstBufferPtr digest = Sha256::computeDigest({0x01, 0x02, 0x03, 0x04}); |
| 176 | BOOST_TEST(*digest == *expected, boost::test_tools::per_element()); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 179 | BOOST_AUTO_TEST_CASE(Error) |
| 180 | { |
| 181 | Sha256 sha; |
| 182 | sha << 42; |
| 183 | sha.computeDigest(); // finalize |
| 184 | BOOST_CHECK_THROW(sha << 42, Sha256::Error); |
| 185 | } |
| 186 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 187 | BOOST_AUTO_TEST_CASE(Print) |
| 188 | { |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 189 | const uint8_t origin[] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5, |
| 190 | 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81, |
| 191 | 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48, |
| 192 | 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2}; |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 193 | const auto expected = toHex(origin); |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 194 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 195 | Sha256 digest; |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 196 | digest << "TEST"; |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 197 | std::ostringstream os; |
| 198 | os << digest; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(os.str(), expected); |
| 200 | BOOST_CHECK_EQUAL(digest.toString(), expected); |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 203 | BOOST_AUTO_TEST_SUITE_END() // TestSha256 |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 204 | BOOST_AUTO_TEST_SUITE_END() // Util |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 205 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 206 | } // namespace ndn::tests |