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