blob: 8ae8d48e53e05ce66b08c7f55980498fd5db669c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shiaf8eeea2014-03-31 20:10:56 -07002/**
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -08003 * Copyright (c) 2013-2017 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
22#include "key-locator.hpp"
Junxiao Shibc5030d2014-09-01 11:53:12 -070023#include "encoding/block-helpers.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "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;
36 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_None);
37 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
38 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
39
40 Block wire;
41 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
42
43 // These octets are obtained by the snippet below.
44 // This check is intended to detect unexpected encoding change in the future.
45 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
46 // printf("0x%02x, ", *it);
47 // }
48 static const uint8_t expected[] = {
49 0x1c, 0x00
50 };
51 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
52 wire.begin(), wire.end());
53
54 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
55 KeyLocator b(wire);
56 BOOST_CHECK(a == b);
57 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
58 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
59 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080060
61 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
Junxiao Shibc5030d2014-09-01 11:53:12 -070062}
63
64BOOST_AUTO_TEST_CASE(TypeName)
65{
66 KeyLocator a;
67 a.setName("/N");
68 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Name);
69 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
70 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
71
72 Block wire;
73 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
74
75 // These octets are obtained by the snippet below.
76 // This check is intended to detect unexpected encoding change in the future.
77 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
78 // printf("0x%02x, ", *it);
79 // }
80 static const uint8_t expected[] = {
81 0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e
82 };
83 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
84 wire.begin(), wire.end());
85
86 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
87 KeyLocator b(wire);
88 BOOST_CHECK(a == b);
89 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
90 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
91 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080092
93 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
Junxiao Shibc5030d2014-09-01 11:53:12 -070094}
95
96BOOST_AUTO_TEST_CASE(TypeKeyDigest)
97{
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080098 std::string digestOctets = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45";
99 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets.c_str(), digestOctets.size());
100 Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets.c_str(), digestOctets.size());
Junxiao Shibc5030d2014-09-01 11:53:12 -0700101
102 KeyLocator a;
103 a.setKeyDigest(digestBuffer);
104 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_KeyDigest);
105 BOOST_CHECK(a.getKeyDigest() == expectedDigestBlock);
106 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
107
108 Block wire;
109 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
110
111 // These octets are obtained by the snippet below.
112 // This check is intended to detect unexpected encoding change in the future.
113 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
114 // printf("0x%02x, ", *it);
115 // }
116 static const uint8_t expected[] = {
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800117 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
Junxiao Shibc5030d2014-09-01 11:53:12 -0700118 };
119 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
120 wire.begin(), wire.end());
121
122 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
123 KeyLocator b(wire);
124 BOOST_CHECK(a == b);
125 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
126 BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
127 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800128
129 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789A...");
130
131 std::string shortDigest = "\xbc\xde\xf1";
132 b.setKeyDigest(make_shared<Buffer>(shortDigest.c_str(), shortDigest.size()));
133 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=BCDEF1");
Junxiao Shibc5030d2014-09-01 11:53:12 -0700134}
135
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700136BOOST_AUTO_TEST_CASE(Equality)
137{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700138 KeyLocator a;
139 KeyLocator b;
140 BOOST_CHECK_EQUAL(a == b, true);
141 BOOST_CHECK_EQUAL(a != b, false);
142
143 a.setName("ndn:/A");
144 BOOST_CHECK_EQUAL(a == b, false);
145 BOOST_CHECK_EQUAL(a != b, true);
146
147 b.setName("ndn:/B");
148 BOOST_CHECK_EQUAL(a == b, false);
149 BOOST_CHECK_EQUAL(a != b, true);
150
151 b.setName("ndn:/A");
152 BOOST_CHECK_EQUAL(a == b, true);
153 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700154
155 char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
156 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
157
158 a.setKeyDigest(digestBuffer);
159 BOOST_CHECK_EQUAL(a == b, false);
160 BOOST_CHECK_EQUAL(a != b, true);
161
162 b.setKeyDigest(digestBuffer);
163 BOOST_CHECK_EQUAL(a == b, true);
164 BOOST_CHECK_EQUAL(a != b, false);
165}
166
167BOOST_AUTO_TEST_CASE(UnknownType)
168{
169 static const uint8_t wireOctets[] = {
170 0x1c, 0x03, 0x7f, 0x01, 0xcc
171 };
172 Block wire(wireOctets, sizeof(wireOctets));
173 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
174 KeyLocator a(wire);
175 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Unknown);
176
177 KeyLocator b(wire);
178 BOOST_CHECK_EQUAL(a == b, true);
179 BOOST_CHECK_EQUAL(a != b, false);
180
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800181 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown");
182
Junxiao Shibc5030d2014-09-01 11:53:12 -0700183 b.setName("/N");
184 BOOST_CHECK_EQUAL(a == b, false);
185 BOOST_CHECK_EQUAL(a != b, true);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700186}
187
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100188BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700189
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800190} // namespace tests
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700191} // namespace ndn