Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8a8c01b | 2018-03-11 00:07:52 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/key-locator.hpp" |
| 23 | #include "ndn-cxx/encoding/block-helpers.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestKeyLocator) |
| 32 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(TypeNone) |
| 34 | { |
| 35 | KeyLocator a; |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 36 | BOOST_CHECK_EQUAL(a.empty(), true); |
| 37 | BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 38 | BOOST_CHECK_THROW(a.getName(), KeyLocator::Error); |
| 39 | BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error); |
| 40 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 41 | Block wire = a.wireEncode(); |
| 42 | // These octets are obtained from the snippet below. |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 43 | // This check is intended to detect unexpected encoding change in the future. |
Davide Pesavento | 8a8c01b | 2018-03-11 00:07:52 -0500 | [diff] [blame] | 44 | // for (auto it = wire.begin(); it != wire.end(); ++it) { |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 45 | // printf("0x%02x, ", *it); |
| 46 | // } |
| 47 | static const uint8_t expected[] = { |
| 48 | 0x1c, 0x00 |
| 49 | }; |
| 50 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 51 | wire.begin(), wire.end()); |
| 52 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 53 | KeyLocator b(wire); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(a, b); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(a.empty(), true); |
| 56 | BOOST_CHECK_EQUAL(b.getType(), tlv::Invalid); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 57 | BOOST_CHECK_THROW(b.getName(), KeyLocator::Error); |
| 58 | BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None"); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE(TypeName) |
| 63 | { |
| 64 | KeyLocator a; |
| 65 | a.setName("/N"); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(a.empty(), false); |
| 67 | BOOST_CHECK_EQUAL(a.getType(), tlv::Name); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(a.getName(), Name("/N")); |
| 69 | BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error); |
| 70 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 71 | Block wire = a.wireEncode(); |
| 72 | // These octets are obtained from the snippet below. |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 73 | // This check is intended to detect unexpected encoding change in the future. |
Davide Pesavento | 8a8c01b | 2018-03-11 00:07:52 -0500 | [diff] [blame] | 74 | // for (auto it = wire.begin(); it != wire.end(); ++it) { |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 75 | // printf("0x%02x, ", *it); |
| 76 | // } |
| 77 | static const uint8_t expected[] = { |
| 78 | 0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e |
| 79 | }; |
| 80 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 81 | wire.begin(), wire.end()); |
| 82 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 83 | KeyLocator b(wire); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(a, b); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(b.getType(), tlv::Name); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(b.getName(), Name("/N")); |
| 87 | BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N"); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 89 | |
| 90 | KeyLocator c("/N"); |
| 91 | BOOST_CHECK_EQUAL(a, c); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(TypeKeyDigest) |
| 95 | { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 96 | static const uint8_t digestOctets[] = { |
| 97 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45 |
| 98 | }; |
| 99 | ConstBufferPtr digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets)); |
| 100 | Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 101 | |
| 102 | KeyLocator a; |
| 103 | a.setKeyDigest(digestBuffer); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(a.empty(), false); |
| 105 | BOOST_CHECK_EQUAL(a.getType(), tlv::KeyDigest); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(a.getKeyDigest(), expectedDigestBlock); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 107 | BOOST_CHECK_THROW(a.getName(), KeyLocator::Error); |
| 108 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 109 | Block wire = a.wireEncode(); |
| 110 | // These octets are obtained from the snippet below. |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 111 | // This check is intended to detect unexpected encoding change in the future. |
Davide Pesavento | 8a8c01b | 2018-03-11 00:07:52 -0500 | [diff] [blame] | 112 | // for (auto it = wire.begin(); it != wire.end(); ++it) { |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 113 | // printf("0x%02x, ", *it); |
| 114 | // } |
| 115 | static const uint8_t expected[] = { |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 116 | 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45 |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 117 | }; |
| 118 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 119 | wire.begin(), wire.end()); |
| 120 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 121 | KeyLocator b(wire); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(a, b); |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(b.getKeyDigest(), expectedDigestBlock); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 125 | BOOST_CHECK_THROW(b.getName(), KeyLocator::Error); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789A..."); |
| 127 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 128 | b.setKeyDigest("1D03BCDEF1"_block); |
| 129 | BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest); |
| 130 | BOOST_CHECK_EQUAL(b.getKeyDigest(), "1D03BCDEF1"_block); |
| 131 | BOOST_CHECK_THROW(b.getName(), KeyLocator::Error); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=BCDEF1"); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 135 | BOOST_AUTO_TEST_CASE(TypeUnknown) |
| 136 | { |
| 137 | const auto wire = "1C037F01CC"_block; |
| 138 | KeyLocator a(wire); |
| 139 | BOOST_CHECK_EQUAL(a.empty(), false); |
| 140 | BOOST_CHECK_EQUAL(a.getType(), 127); |
| 141 | |
| 142 | KeyLocator b(wire); |
| 143 | BOOST_CHECK_EQUAL(a, b); |
| 144 | BOOST_CHECK_EQUAL(b.getType(), 127); |
| 145 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown(127)"); |
| 146 | |
| 147 | b.setName("/N"); |
| 148 | BOOST_CHECK_NE(a, b); |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(Clear) |
| 152 | { |
| 153 | KeyLocator a("/foo"); |
| 154 | BOOST_CHECK_EQUAL(a.empty(), false); |
| 155 | |
| 156 | a.clear(); |
| 157 | BOOST_CHECK_EQUAL(a.empty(), true); |
| 158 | BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid); |
| 159 | BOOST_CHECK_EQUAL(a, KeyLocator{}); |
| 160 | } |
| 161 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE(Equality) |
| 163 | { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 164 | KeyLocator a; |
| 165 | KeyLocator b; |
| 166 | BOOST_CHECK_EQUAL(a == b, true); |
| 167 | BOOST_CHECK_EQUAL(a != b, false); |
| 168 | |
| 169 | a.setName("ndn:/A"); |
| 170 | BOOST_CHECK_EQUAL(a == b, false); |
| 171 | BOOST_CHECK_EQUAL(a != b, true); |
| 172 | |
| 173 | b.setName("ndn:/B"); |
| 174 | BOOST_CHECK_EQUAL(a == b, false); |
| 175 | BOOST_CHECK_EQUAL(a != b, true); |
| 176 | |
| 177 | b.setName("ndn:/A"); |
| 178 | BOOST_CHECK_EQUAL(a == b, true); |
| 179 | BOOST_CHECK_EQUAL(a != b, false); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 180 | |
Davide Pesavento | 5149350 | 2019-07-17 02:26:44 -0400 | [diff] [blame] | 181 | const char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"; |
| 182 | auto digestBuffer = make_shared<Buffer>(digestOctets, 8); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 183 | |
| 184 | a.setKeyDigest(digestBuffer); |
| 185 | BOOST_CHECK_EQUAL(a == b, false); |
| 186 | BOOST_CHECK_EQUAL(a != b, true); |
| 187 | |
| 188 | b.setKeyDigest(digestBuffer); |
| 189 | BOOST_CHECK_EQUAL(a == b, true); |
| 190 | BOOST_CHECK_EQUAL(a != b, false); |
| 191 | } |
| 192 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 193 | BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 194 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 195 | } // namespace tests |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 196 | } // namespace ndn |