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