blob: 7a8b6362e5a3757aec9384b97085790511b6331c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8a8c01b2018-03-11 00:07:52 -05002/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Shiaf8eeea2014-03-31 20:10:56 -070020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/key-locator.hpp"
23#include "ndn-cxx/encoding/block-helpers.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040026
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080027#include <boost/lexical_cast.hpp>
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070028
29namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030namespace tests {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070031
Davide Pesavento152ef442023-04-22 02:02:29 -040032BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
33BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
34BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<KeyLocator>));
35BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
36static_assert(std::is_convertible_v<KeyLocator::Error*, tlv::Error*>,
37 "KeyLocator::Error must inherit from tlv::Error");
38
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070039BOOST_AUTO_TEST_SUITE(TestKeyLocator)
40
Junxiao Shibc5030d2014-09-01 11:53:12 -070041BOOST_AUTO_TEST_CASE(TypeNone)
42{
43 KeyLocator a;
Davide Pesavento51493502019-07-17 02:26:44 -040044 BOOST_CHECK_EQUAL(a.empty(), true);
45 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070046 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
47 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
48
Davide Pesavento51493502019-07-17 02:26:44 -040049 Block wire = a.wireEncode();
50 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070051 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050052 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070053 // printf("0x%02x, ", *it);
54 // }
55 static const uint8_t expected[] = {
56 0x1c, 0x00
57 };
58 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
59 wire.begin(), wire.end());
60
Junxiao Shibc5030d2014-09-01 11:53:12 -070061 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000062 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040063 BOOST_CHECK_EQUAL(a.empty(), true);
64 BOOST_CHECK_EQUAL(b.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070065 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
66 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080067 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
Junxiao Shibc5030d2014-09-01 11:53:12 -070068}
69
70BOOST_AUTO_TEST_CASE(TypeName)
71{
72 KeyLocator a;
73 a.setName("/N");
Davide Pesavento51493502019-07-17 02:26:44 -040074 BOOST_CHECK_EQUAL(a.empty(), false);
75 BOOST_CHECK_EQUAL(a.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070076 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
77 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
78
Davide Pesavento51493502019-07-17 02:26:44 -040079 Block wire = a.wireEncode();
80 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070081 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050082 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070083 // printf("0x%02x, ", *it);
84 // }
85 static const uint8_t expected[] = {
86 0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e
87 };
88 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
89 wire.begin(), wire.end());
90
Junxiao Shibc5030d2014-09-01 11:53:12 -070091 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000092 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040093 BOOST_CHECK_EQUAL(b.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070094 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
95 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080096 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
Davide Pesavento51493502019-07-17 02:26:44 -040097
98 KeyLocator c("/N");
99 BOOST_CHECK_EQUAL(a, c);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700100}
101
102BOOST_AUTO_TEST_CASE(TypeKeyDigest)
103{
Davide Pesavento765abc92021-12-27 00:44:04 -0500104 static const uint8_t digestOctets[] = {
105 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
106 };
107 ConstBufferPtr digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets));
108 Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700109
110 KeyLocator a;
111 a.setKeyDigest(digestBuffer);
Davide Pesavento51493502019-07-17 02:26:44 -0400112 BOOST_CHECK_EQUAL(a.empty(), false);
113 BOOST_CHECK_EQUAL(a.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000114 BOOST_CHECK_EQUAL(a.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700115 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
116
Davide Pesavento51493502019-07-17 02:26:44 -0400117 Block wire = a.wireEncode();
118 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -0700119 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500120 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -0700121 // printf("0x%02x, ", *it);
122 // }
123 static const uint8_t expected[] = {
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800124 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
Junxiao Shibc5030d2014-09-01 11:53:12 -0700125 };
126 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
127 wire.begin(), wire.end());
128
Junxiao Shibc5030d2014-09-01 11:53:12 -0700129 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000130 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -0400131 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000132 BOOST_CHECK_EQUAL(b.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700133 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400134 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789ABCDEF1...");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800135
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400136 b.setKeyDigest("1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400137 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400138 BOOST_CHECK_EQUAL(b.getKeyDigest(), "1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400139 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400140 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=0123456789");
Junxiao Shibc5030d2014-09-01 11:53:12 -0700141}
142
Davide Pesavento51493502019-07-17 02:26:44 -0400143BOOST_AUTO_TEST_CASE(TypeUnknown)
144{
145 const auto wire = "1C037F01CC"_block;
146 KeyLocator a(wire);
147 BOOST_CHECK_EQUAL(a.empty(), false);
148 BOOST_CHECK_EQUAL(a.getType(), 127);
149
150 KeyLocator b(wire);
151 BOOST_CHECK_EQUAL(a, b);
152 BOOST_CHECK_EQUAL(b.getType(), 127);
153 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown(127)");
154
155 b.setName("/N");
156 BOOST_CHECK_NE(a, b);
157}
158
159BOOST_AUTO_TEST_CASE(Clear)
160{
161 KeyLocator a("/foo");
162 BOOST_CHECK_EQUAL(a.empty(), false);
163
164 a.clear();
165 BOOST_CHECK_EQUAL(a.empty(), true);
166 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
167 BOOST_CHECK_EQUAL(a, KeyLocator{});
168}
169
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700170BOOST_AUTO_TEST_CASE(Equality)
171{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700172 KeyLocator a;
173 KeyLocator b;
174 BOOST_CHECK_EQUAL(a == b, true);
175 BOOST_CHECK_EQUAL(a != b, false);
176
177 a.setName("ndn:/A");
178 BOOST_CHECK_EQUAL(a == b, false);
179 BOOST_CHECK_EQUAL(a != b, true);
180
181 b.setName("ndn:/B");
182 BOOST_CHECK_EQUAL(a == b, false);
183 BOOST_CHECK_EQUAL(a != b, true);
184
185 b.setName("ndn:/A");
186 BOOST_CHECK_EQUAL(a == b, true);
187 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700188
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400189 const uint8_t digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
190 auto digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets) - 1);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700191
192 a.setKeyDigest(digestBuffer);
193 BOOST_CHECK_EQUAL(a == b, false);
194 BOOST_CHECK_EQUAL(a != b, true);
195
196 b.setKeyDigest(digestBuffer);
197 BOOST_CHECK_EQUAL(a == b, true);
198 BOOST_CHECK_EQUAL(a != b, false);
199}
200
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100201BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700202
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800203} // namespace tests
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700204} // namespace ndn