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