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