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