blob: 72e4d66224483cd6827fc27203aacf7e388475b4 [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/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
27namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080028namespace tests {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070029
30BOOST_AUTO_TEST_SUITE(TestKeyLocator)
31
Junxiao Shibc5030d2014-09-01 11:53:12 -070032BOOST_AUTO_TEST_CASE(TypeNone)
33{
34 KeyLocator a;
35 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_None);
36 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
37 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
38
39 Block wire;
40 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
41
42 // These octets are obtained by the snippet below.
43 // This check is intended to detect unexpected encoding change in the future.
44 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
45 // 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
53 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
54 KeyLocator b(wire);
55 BOOST_CHECK(a == b);
56 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
57 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
58 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
59}
60
61BOOST_AUTO_TEST_CASE(TypeName)
62{
63 KeyLocator a;
64 a.setName("/N");
65 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Name);
66 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
67 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
68
69 Block wire;
70 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
71
72 // These octets are obtained by the snippet below.
73 // This check is intended to detect unexpected encoding change in the future.
74 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
75 // 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
83 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
84 KeyLocator b(wire);
85 BOOST_CHECK(a == b);
86 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
87 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
88 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
89}
90
91BOOST_AUTO_TEST_CASE(TypeKeyDigest)
92{
93 char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
94 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070095 Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets, 8);
Junxiao Shibc5030d2014-09-01 11:53:12 -070096
97 KeyLocator a;
98 a.setKeyDigest(digestBuffer);
99 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_KeyDigest);
100 BOOST_CHECK(a.getKeyDigest() == expectedDigestBlock);
101 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
102
103 Block wire;
104 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
105
106 // These octets are obtained by the snippet below.
107 // This check is intended to detect unexpected encoding change in the future.
108 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
109 // printf("0x%02x, ", *it);
110 // }
111 static const uint8_t expected[] = {
112 0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
113 };
114 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
115 wire.begin(), wire.end());
116
117 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
118 KeyLocator b(wire);
119 BOOST_CHECK(a == b);
120 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
121 BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
122 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
123}
124
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700125BOOST_AUTO_TEST_CASE(Equality)
126{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700127 KeyLocator a;
128 KeyLocator b;
129 BOOST_CHECK_EQUAL(a == b, true);
130 BOOST_CHECK_EQUAL(a != b, false);
131
132 a.setName("ndn:/A");
133 BOOST_CHECK_EQUAL(a == b, false);
134 BOOST_CHECK_EQUAL(a != b, true);
135
136 b.setName("ndn:/B");
137 BOOST_CHECK_EQUAL(a == b, false);
138 BOOST_CHECK_EQUAL(a != b, true);
139
140 b.setName("ndn:/A");
141 BOOST_CHECK_EQUAL(a == b, true);
142 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700143
144 char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
145 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
146
147 a.setKeyDigest(digestBuffer);
148 BOOST_CHECK_EQUAL(a == b, false);
149 BOOST_CHECK_EQUAL(a != b, true);
150
151 b.setKeyDigest(digestBuffer);
152 BOOST_CHECK_EQUAL(a == b, true);
153 BOOST_CHECK_EQUAL(a != b, false);
154}
155
156BOOST_AUTO_TEST_CASE(UnknownType)
157{
158 static const uint8_t wireOctets[] = {
159 0x1c, 0x03, 0x7f, 0x01, 0xcc
160 };
161 Block wire(wireOctets, sizeof(wireOctets));
162 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
163 KeyLocator a(wire);
164 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Unknown);
165
166 KeyLocator b(wire);
167 BOOST_CHECK_EQUAL(a == b, true);
168 BOOST_CHECK_EQUAL(a != b, false);
169
170 b.setName("/N");
171 BOOST_CHECK_EQUAL(a == b, false);
172 BOOST_CHECK_EQUAL(a != b, true);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700173}
174
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100175BOOST_AUTO_TEST_SUITE_END() // TestKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700176
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800177} // namespace tests
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700178} // namespace ndn