blob: 3e4823337562b8a342531e73f775c776385c0cae [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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 "util/concepts.hpp"
24#include "encoding/block-helpers.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070027
28namespace ndn {
29
Junxiao Shibc5030d2014-09-01 11:53:12 -070030BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
31BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
32BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
33
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070034BOOST_AUTO_TEST_SUITE(TestKeyLocator)
35
Junxiao Shibc5030d2014-09-01 11:53:12 -070036BOOST_AUTO_TEST_CASE(TypeNone)
37{
38 KeyLocator a;
39 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_None);
40 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
41 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
42
43 Block wire;
44 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
45
46 // These octets are obtained by the snippet below.
47 // This check is intended to detect unexpected encoding change in the future.
48 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
49 // printf("0x%02x, ", *it);
50 // }
51 static const uint8_t expected[] = {
52 0x1c, 0x00
53 };
54 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
55 wire.begin(), wire.end());
56
57 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
58 KeyLocator b(wire);
59 BOOST_CHECK(a == b);
60 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
61 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
62 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
63}
64
65BOOST_AUTO_TEST_CASE(TypeName)
66{
67 KeyLocator a;
68 a.setName("/N");
69 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Name);
70 BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
71 BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
72
73 Block wire;
74 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
75
76 // These octets are obtained by the snippet below.
77 // This check is intended to detect unexpected encoding change in the future.
78 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
79 // printf("0x%02x, ", *it);
80 // }
81 static const uint8_t expected[] = {
82 0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e
83 };
84 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
85 wire.begin(), wire.end());
86
87 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
88 KeyLocator b(wire);
89 BOOST_CHECK(a == b);
90 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
91 BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
92 BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
93}
94
95BOOST_AUTO_TEST_CASE(TypeKeyDigest)
96{
97 char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
98 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
99 Block expectedDigestBlock = dataBlock(tlv::KeyDigest, digestOctets, 8);
100
101 KeyLocator a;
102 a.setKeyDigest(digestBuffer);
103 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_KeyDigest);
104 BOOST_CHECK(a.getKeyDigest() == expectedDigestBlock);
105 BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
106
107 Block wire;
108 BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
109
110 // These octets are obtained by the snippet below.
111 // This check is intended to detect unexpected encoding change in the future.
112 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
113 // printf("0x%02x, ", *it);
114 // }
115 static const uint8_t expected[] = {
116 0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
117 };
118 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
119 wire.begin(), wire.end());
120
121 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
122 KeyLocator b(wire);
123 BOOST_CHECK(a == b);
124 BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
125 BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
126 BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
127}
128
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700129BOOST_AUTO_TEST_CASE(Equality)
130{
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700131 KeyLocator a;
132 KeyLocator b;
133 BOOST_CHECK_EQUAL(a == b, true);
134 BOOST_CHECK_EQUAL(a != b, false);
135
136 a.setName("ndn:/A");
137 BOOST_CHECK_EQUAL(a == b, false);
138 BOOST_CHECK_EQUAL(a != b, true);
139
140 b.setName("ndn:/B");
141 BOOST_CHECK_EQUAL(a == b, false);
142 BOOST_CHECK_EQUAL(a != b, true);
143
144 b.setName("ndn:/A");
145 BOOST_CHECK_EQUAL(a == b, true);
146 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shibc5030d2014-09-01 11:53:12 -0700147
148 char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
149 ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
150
151 a.setKeyDigest(digestBuffer);
152 BOOST_CHECK_EQUAL(a == b, false);
153 BOOST_CHECK_EQUAL(a != b, true);
154
155 b.setKeyDigest(digestBuffer);
156 BOOST_CHECK_EQUAL(a == b, true);
157 BOOST_CHECK_EQUAL(a != b, false);
158}
159
160BOOST_AUTO_TEST_CASE(UnknownType)
161{
162 static const uint8_t wireOctets[] = {
163 0x1c, 0x03, 0x7f, 0x01, 0xcc
164 };
165 Block wire(wireOctets, sizeof(wireOctets));
166 BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
167 KeyLocator a(wire);
168 BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Unknown);
169
170 KeyLocator b(wire);
171 BOOST_CHECK_EQUAL(a == b, true);
172 BOOST_CHECK_EQUAL(a != b, false);
173
174 b.setName("/N");
175 BOOST_CHECK_EQUAL(a == b, false);
176 BOOST_CHECK_EQUAL(a != b, true);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700177}
178
179BOOST_AUTO_TEST_SUITE_END()
180
181} // namespace ndn