blob: d7dfe9f933016d8c3f99e2895cd70e8fc93eaa3c [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 Pesaventodd0724b2022-04-18 00:30:05 -04003 * Copyright (c) 2013-2022 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"
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080026#include <boost/lexical_cast.hpp>
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070027
28namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070030
31BOOST_AUTO_TEST_SUITE(TestKeyLocator)
32
Junxiao Shibc5030d2014-09-01 11:53:12 -070033BOOST_AUTO_TEST_CASE(TypeNone)
34{
35 KeyLocator a;
Davide Pesavento51493502019-07-17 02:26:44 -040036 BOOST_CHECK_EQUAL(a.empty(), true);
37 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070038 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
39 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
40
Davide Pesavento51493502019-07-17 02:26:44 -040041 Block wire = a.wireEncode();
42 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070043 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050044 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070045 // 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
Junxiao Shibc5030d2014-09-01 11:53:12 -070053 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000054 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040055 BOOST_CHECK_EQUAL(a.empty(), true);
56 BOOST_CHECK_EQUAL(b.getType(), tlv::Invalid);
Junxiao Shibc5030d2014-09-01 11:53:12 -070057 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
58 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080059 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
Junxiao Shibc5030d2014-09-01 11:53:12 -070060}
61
62BOOST_AUTO_TEST_CASE(TypeName)
63{
64 KeyLocator a;
65 a.setName("/N");
Davide Pesavento51493502019-07-17 02:26:44 -040066 BOOST_CHECK_EQUAL(a.empty(), false);
67 BOOST_CHECK_EQUAL(a.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070068 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
69 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
70
Davide Pesavento51493502019-07-17 02:26:44 -040071 Block wire = a.wireEncode();
72 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -070073 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050074 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -070075 // 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
Junxiao Shibc5030d2014-09-01 11:53:12 -070083 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +000084 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -040085 BOOST_CHECK_EQUAL(b.getType(), tlv::Name);
Junxiao Shibc5030d2014-09-01 11:53:12 -070086 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
87 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080088 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
Davide Pesavento51493502019-07-17 02:26:44 -040089
90 KeyLocator c("/N");
91 BOOST_CHECK_EQUAL(a, c);
Junxiao Shibc5030d2014-09-01 11:53:12 -070092}
93
94BOOST_AUTO_TEST_CASE(TypeKeyDigest)
95{
Davide Pesavento765abc92021-12-27 00:44:04 -050096 static const uint8_t digestOctets[] = {
97 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
98 };
99 ConstBufferPtr digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets));
100 Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700101
102 KeyLocator a;
103 a.setKeyDigest(digestBuffer);
Davide Pesavento51493502019-07-17 02:26:44 -0400104 BOOST_CHECK_EQUAL(a.empty(), false);
105 BOOST_CHECK_EQUAL(a.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000106 BOOST_CHECK_EQUAL(a.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700107 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
108
Davide Pesavento51493502019-07-17 02:26:44 -0400109 Block wire = a.wireEncode();
110 // These octets are obtained from the snippet below.
Junxiao Shibc5030d2014-09-01 11:53:12 -0700111 // This check is intended to detect unexpected encoding change in the future.
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500112 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Junxiao Shibc5030d2014-09-01 11:53:12 -0700113 // printf("0x%02x, ", *it);
114 // }
115 static const uint8_t expected[] = {
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800116 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
Junxiao Shibc5030d2014-09-01 11:53:12 -0700117 };
118 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
119 wire.begin(), wire.end());
120
Junxiao Shibc5030d2014-09-01 11:53:12 -0700121 KeyLocator b(wire);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000122 BOOST_CHECK_EQUAL(a, b);
Davide Pesavento51493502019-07-17 02:26:44 -0400123 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000124 BOOST_CHECK_EQUAL(b.getKeyDigest(), expectedDigestBlock);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700125 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400126 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789ABCDEF1...");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800127
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400128 b.setKeyDigest("1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400129 BOOST_CHECK_EQUAL(b.getType(), tlv::KeyDigest);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400130 BOOST_CHECK_EQUAL(b.getKeyDigest(), "1D050123456789"_block);
Davide Pesavento51493502019-07-17 02:26:44 -0400131 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400132 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=0123456789");
Junxiao Shibc5030d2014-09-01 11:53:12 -0700133}
134
Davide Pesavento51493502019-07-17 02:26:44 -0400135BOOST_AUTO_TEST_CASE(TypeUnknown)
136{
137 const auto wire = "1C037F01CC"_block;
138 KeyLocator a(wire);
139 BOOST_CHECK_EQUAL(a.empty(), false);
140 BOOST_CHECK_EQUAL(a.getType(), 127);
141
142 KeyLocator b(wire);
143 BOOST_CHECK_EQUAL(a, b);
144 BOOST_CHECK_EQUAL(b.getType(), 127);
145 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown(127)");
146
147 b.setName("/N");
148 BOOST_CHECK_NE(a, b);
149}
150
151BOOST_AUTO_TEST_CASE(Clear)
152{
153 KeyLocator a("/foo");
154 BOOST_CHECK_EQUAL(a.empty(), false);
155
156 a.clear();
157 BOOST_CHECK_EQUAL(a.empty(), true);
158 BOOST_CHECK_EQUAL(a.getType(), tlv::Invalid);
159 BOOST_CHECK_EQUAL(a, KeyLocator{});
160}
161
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700162BOOST_AUTO_TEST_CASE(Equality)
163{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700164 KeyLocator a;
165 KeyLocator b;
166 BOOST_CHECK_EQUAL(a == b, true);
167 BOOST_CHECK_EQUAL(a != b, false);
168
169 a.setName("ndn:/A");
170 BOOST_CHECK_EQUAL(a == b, false);
171 BOOST_CHECK_EQUAL(a != b, true);
172
173 b.setName("ndn:/B");
174 BOOST_CHECK_EQUAL(a == b, false);
175 BOOST_CHECK_EQUAL(a != b, true);
176
177 b.setName("ndn:/A");
178 BOOST_CHECK_EQUAL(a == b, true);
179 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700180
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400181 const uint8_t digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
182 auto digestBuffer = std::make_shared<Buffer>(digestOctets, sizeof(digestOctets) - 1);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700183
184 a.setKeyDigest(digestBuffer);
185 BOOST_CHECK_EQUAL(a == b, false);
186 BOOST_CHECK_EQUAL(a != b, true);
187
188 b.setKeyDigest(digestBuffer);
189 BOOST_CHECK_EQUAL(a == b, true);
190 BOOST_CHECK_EQUAL(a != b, false);
191}
192
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100193BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700194
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800195} // namespace tests
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700196} // namespace ndn