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 | /** |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 28 | namespace tests { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestKeyLocator) |
| 31 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 32 | BOOST_AUTO_TEST_CASE(TypeNone) |
| 33 | { |
| 34 | KeyLocator a; |
| 35 | BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_None); |
| 36 | BOOST_CHECK_THROW(a.getName(), KeyLocator::Error); |
| 37 | BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error); |
| 38 | |
| 39 | Block wire; |
| 40 | BOOST_REQUIRE_NO_THROW(wire = a.wireEncode()); |
| 41 | |
| 42 | // These octets are obtained by the snippet below. |
| 43 | // This check is intended to detect unexpected encoding change in the future. |
| 44 | // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) { |
| 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 | |
| 53 | BOOST_REQUIRE_NO_THROW(KeyLocator(wire)); |
| 54 | KeyLocator b(wire); |
| 55 | BOOST_CHECK(a == b); |
| 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); |
| 59 | } |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(TypeName) |
| 62 | { |
| 63 | KeyLocator a; |
| 64 | a.setName("/N"); |
| 65 | BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Name); |
| 66 | BOOST_CHECK_EQUAL(a.getName(), Name("/N")); |
| 67 | BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error); |
| 68 | |
| 69 | Block wire; |
| 70 | BOOST_REQUIRE_NO_THROW(wire = a.wireEncode()); |
| 71 | |
| 72 | // These octets are obtained by the snippet below. |
| 73 | // This check is intended to detect unexpected encoding change in the future. |
| 74 | // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) { |
| 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 | |
| 83 | BOOST_REQUIRE_NO_THROW(KeyLocator(wire)); |
| 84 | KeyLocator b(wire); |
| 85 | BOOST_CHECK(a == b); |
| 86 | BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name); |
| 87 | BOOST_CHECK_EQUAL(b.getName(), Name("/N")); |
| 88 | BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error); |
| 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_CASE(TypeKeyDigest) |
| 92 | { |
| 93 | char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"; |
| 94 | ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 95 | Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets, 8); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 96 | |
| 97 | KeyLocator a; |
| 98 | a.setKeyDigest(digestBuffer); |
| 99 | BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_KeyDigest); |
| 100 | BOOST_CHECK(a.getKeyDigest() == expectedDigestBlock); |
| 101 | BOOST_CHECK_THROW(a.getName(), KeyLocator::Error); |
| 102 | |
| 103 | Block wire; |
| 104 | BOOST_REQUIRE_NO_THROW(wire = a.wireEncode()); |
| 105 | |
| 106 | // These octets are obtained by the snippet below. |
| 107 | // This check is intended to detect unexpected encoding change in the future. |
| 108 | // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) { |
| 109 | // printf("0x%02x, ", *it); |
| 110 | // } |
| 111 | static const uint8_t expected[] = { |
| 112 | 0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 113 | }; |
| 114 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 115 | wire.begin(), wire.end()); |
| 116 | |
| 117 | BOOST_REQUIRE_NO_THROW(KeyLocator(wire)); |
| 118 | KeyLocator b(wire); |
| 119 | BOOST_CHECK(a == b); |
| 120 | BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest); |
| 121 | BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock); |
| 122 | BOOST_CHECK_THROW(b.getName(), KeyLocator::Error); |
| 123 | } |
| 124 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 125 | BOOST_AUTO_TEST_CASE(Equality) |
| 126 | { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 127 | KeyLocator a; |
| 128 | KeyLocator b; |
| 129 | BOOST_CHECK_EQUAL(a == b, true); |
| 130 | BOOST_CHECK_EQUAL(a != b, false); |
| 131 | |
| 132 | a.setName("ndn:/A"); |
| 133 | BOOST_CHECK_EQUAL(a == b, false); |
| 134 | BOOST_CHECK_EQUAL(a != b, true); |
| 135 | |
| 136 | b.setName("ndn:/B"); |
| 137 | BOOST_CHECK_EQUAL(a == b, false); |
| 138 | BOOST_CHECK_EQUAL(a != b, true); |
| 139 | |
| 140 | b.setName("ndn:/A"); |
| 141 | BOOST_CHECK_EQUAL(a == b, true); |
| 142 | BOOST_CHECK_EQUAL(a != b, false); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 143 | |
| 144 | char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"; |
| 145 | ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8); |
| 146 | |
| 147 | a.setKeyDigest(digestBuffer); |
| 148 | BOOST_CHECK_EQUAL(a == b, false); |
| 149 | BOOST_CHECK_EQUAL(a != b, true); |
| 150 | |
| 151 | b.setKeyDigest(digestBuffer); |
| 152 | BOOST_CHECK_EQUAL(a == b, true); |
| 153 | BOOST_CHECK_EQUAL(a != b, false); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(UnknownType) |
| 157 | { |
| 158 | static const uint8_t wireOctets[] = { |
| 159 | 0x1c, 0x03, 0x7f, 0x01, 0xcc |
| 160 | }; |
| 161 | Block wire(wireOctets, sizeof(wireOctets)); |
| 162 | BOOST_REQUIRE_NO_THROW(KeyLocator(wire)); |
| 163 | KeyLocator a(wire); |
| 164 | BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Unknown); |
| 165 | |
| 166 | KeyLocator b(wire); |
| 167 | BOOST_CHECK_EQUAL(a == b, true); |
| 168 | BOOST_CHECK_EQUAL(a != b, false); |
| 169 | |
| 170 | b.setName("/N"); |
| 171 | BOOST_CHECK_EQUAL(a == b, false); |
| 172 | BOOST_CHECK_EQUAL(a != b, true); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 176 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 177 | } // namespace tests |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 178 | } // namespace ndn |