blob: 50daa8c468c779fe5fce5c2d92affd696956f6bb [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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070030
Davide Pesavento152ef442023-04-22 02:02:29 -040031BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
32BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
33BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<KeyLocator>));
34BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
35static_assert(std::is_convertible_v<KeyLocator::Error*, tlv::Error*>,
36 "KeyLocator::Error must inherit from tlv::Error");
37
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070038BOOST_AUTO_TEST_SUITE(TestKeyLocator)
39
Junxiao Shibc5030d2014-09-01 11:53:12 -070040BOOST_AUTO_TEST_CASE(TypeNone)
41{
42 KeyLocator a;
Davide Pesavento51493502019-07-17 02:26:44 -040043 BOOST_CHECK_EQUAL(a.empty(), true);
44 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070045 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
46 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
47
Davide Pesavento51493502019-07-17 02:26:44 -040048 Block wire = a.wireEncode();
49 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070050 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050051 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070052 // printf("0x%02x, ", *it);
53 // }
54 static const uint8_t expected[] = {
55 0x1c, 0x00
56 };
57 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
58 wire.begin(), wire.end());
59
Junxiao Shibc5030d2014-09-01 11:53:12 -070060 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000061 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040062 BOOST_CHECK_EQUAL(a.empty(), true);
63 BOOST_CHECK_EQUAL(b.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070064 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
65 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080066 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
Junxiao Shibc5030d2014-09-01 11:53:12 -070067}
68
69BOOST_AUTO_TEST_CASE(TypeName)
70{
71 KeyLocator a;
72 a.setName("/N");
Davide Pesavento51493502019-07-17 02:26:44 -040073 BOOST_CHECK_EQUAL(a.empty(), false);
74 BOOST_CHECK_EQUAL(a.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070075 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
76 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
77
Davide Pesavento51493502019-07-17 02:26:44 -040078 Block wire = a.wireEncode();
79 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070080 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050081 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070082 // printf("0x%02x, ", *it);
83 // }
84 static const uint8_t expected[] = {
85 0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e
86 };
87 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
88 wire.begin(), wire.end());
89
Junxiao Shibc5030d2014-09-01 11:53:12 -070090 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000091 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040092 BOOST_CHECK_EQUAL(b.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070093 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
94 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080095 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
Davide Pesavento51493502019-07-17 02:26:44 -040096
97 KeyLocator c("/N");
98 BOOST_CHECK_EQUAL(a, c);
Junxiao Shibc5030d2014-09-01 11:53:12 -070099}
100
101BOOST_AUTO_TEST_CASE(TypeKeyDigest)
102{
Davide Pesavento765abc92021-12-27 00:44:04 -0500103 static const uint8_t digestOctets[] = {
104 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
105 };
106 ConstBufferPtr digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets));
107 Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700108
109 KeyLocator a;
110 a.setKeyDigest(digestBuffer);
Davide Pesavento51493502019-07-17 02:26:44 -0400111 BOOST_CHECK_EQUAL(a.empty(), false);
112 BOOST_CHECK_EQUAL(a.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000113 BOOST_CHECK_EQUAL(a.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700114 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
115
Davide Pesavento51493502019-07-17 02:26:44 -0400116 Block wire = a.wireEncode();
117 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -0700118 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500119 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -0700120 // printf("0x%02x, ", *it);
121 // }
122 static const uint8_t expected[] = {
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800123 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
Junxiao Shibc5030d2014-09-01 11:53:12 -0700124 };
125 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
126 wire.begin(), wire.end());
127
Junxiao Shibc5030d2014-09-01 11:53:12 -0700128 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000129 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -0400130 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000131 BOOST_CHECK_EQUAL(b.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700132 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400133 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789ABCDEF1...");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800134
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400135 b.setKeyDigest("1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400136 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400137 BOOST_CHECK_EQUAL(b.getKeyDigest(), "1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400138 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400139 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=0123456789");
Junxiao Shibc5030d2014-09-01 11:53:12 -0700140}
141
Davide Pesavento51493502019-07-17 02:26:44 -0400142BOOST_AUTO_TEST_CASE(TypeUnknown)
143{
144 const auto wire = "1C037F01CC"_block;
145 KeyLocator a(wire);
146 BOOST_CHECK_EQUAL(a.empty(), false);
147 BOOST_CHECK_EQUAL(a.getType(), 127);
148
149 KeyLocator b(wire);
150 BOOST_CHECK_EQUAL(a, b);
151 BOOST_CHECK_EQUAL(b.getType(), 127);
152 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown(127)");
153
154 b.setName("/N");
155 BOOST_CHECK_NE(a, b);
156}
157
158BOOST_AUTO_TEST_CASE(Clear)
159{
160 KeyLocator a("/foo");
161 BOOST_CHECK_EQUAL(a.empty(), false);
162
163 a.clear();
164 BOOST_CHECK_EQUAL(a.empty(), true);
165 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
166 BOOST_CHECK_EQUAL(a, KeyLocator{});
167}
168
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700169BOOST_AUTO_TEST_CASE(Equality)
170{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700171 KeyLocator a;
172 KeyLocator b;
173 BOOST_CHECK_EQUAL(a == b, true);
174 BOOST_CHECK_EQUAL(a != b, false);
175
176 a.setName("ndn:/A");
177 BOOST_CHECK_EQUAL(a == b, false);
178 BOOST_CHECK_EQUAL(a != b, true);
179
180 b.setName("ndn:/B");
181 BOOST_CHECK_EQUAL(a == b, false);
182 BOOST_CHECK_EQUAL(a != b, true);
183
184 b.setName("ndn:/A");
185 BOOST_CHECK_EQUAL(a == b, true);
186 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700187
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400188 const uint8_t digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
189 auto digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets) - 1);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700190
191 a.setKeyDigest(digestBuffer);
192 BOOST_CHECK_EQUAL(a == b, false);
193 BOOST_CHECK_EQUAL(a != b, true);
194
195 b.setKeyDigest(digestBuffer);
196 BOOST_CHECK_EQUAL(a == b, true);
197 BOOST_CHECK_EQUAL(a != b, false);
198}
199
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100200BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700201
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400202} // namespace ndn::tests