blob: 8a99b2fe45085c8dddbee19a5508cfa333b42284 [file] [log] [blame]
Junxiao Shidf4b24e2016-07-14 21:41:43 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "name-component.hpp"
23
24#include "boost-test.hpp"
25#include <boost/mpl/vector.hpp>
26
27namespace ndn {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TestNameComponent)
31
32static const uint8_t NAME_COMPONENT_WIRE[] = {
33 0x08, 0x03, // NameComponent
34 0x6e, 0x64, 0x6e};
35
36static const uint8_t NAME_COMPONENT2_WIRE[] = {
37 0x08, 0x20, // ImplicitSha256DigestComponent
38 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
39 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
40 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
41 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
42
43static const uint8_t DIGEST_COMPONENT_WIRE[] = {
44 0x01, 0x20, // ImplicitSha256DigestComponent
45 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
46 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
47 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
48 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
49
50static const uint8_t DIGEST_COMPONENT2_WIRE[] = {
51 0x01, 0x20, // ImplicitSha256DigestComponent
52 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
53 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
54 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
55 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
56
57static const uint8_t INVALID_COMPONENT_WIRE[] = {
58 0x07, 0x03, // unknown component type
59 0x6e, 0x64, 0x6e};
60
61BOOST_AUTO_TEST_SUITE(Decode)
62
63BOOST_AUTO_TEST_CASE(Generic)
64{
65 Block block(NAME_COMPONENT_WIRE, sizeof(NAME_COMPONENT_WIRE));
66 name::Component comp;
67 BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
68 BOOST_CHECK_EQUAL(comp.toUri(), "ndn");
69}
70
71BOOST_AUTO_TEST_CASE(Digest)
72{
73 Block block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE));
74 name::Component comp;
75 BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
76 BOOST_REQUIRE_EQUAL(comp.toUri(), "sha256digest=28bad4b5275bd392dbb670c75cf0b66f"
77 "13f7942b21e80f55c0e86b374753a548");
78}
79
80BOOST_AUTO_TEST_CASE(Invalid)
81{
82 Block block(INVALID_COMPONENT_WIRE, sizeof(INVALID_COMPONENT_WIRE));
83 name::Component comp;
84 BOOST_REQUIRE_THROW(comp.wireDecode(block), name::Component::Error);
85}
86
87BOOST_AUTO_TEST_SUITE_END() // Decode
88
89BOOST_AUTO_TEST_SUITE(Compare)
90
91BOOST_AUTO_TEST_CASE(Generic)
92{
93 name::Component compD("D");
94 name::Component compD2("D");
95 name::Component compF("F");
96 name::Component compAA("AA");
97
98 BOOST_CHECK_EQUAL(compD == compD2, true);
99 BOOST_CHECK_EQUAL(compD != compD2, false);
100 BOOST_CHECK_EQUAL(compD < compD2, false);
101 BOOST_CHECK_EQUAL(compD <= compD2, true);
102 BOOST_CHECK_EQUAL(compD > compD2, false);
103 BOOST_CHECK_EQUAL(compD >= compD2, true);
104
105 BOOST_CHECK_EQUAL(compD == compF, false);
106 BOOST_CHECK_EQUAL(compD != compF, true);
107 BOOST_CHECK_EQUAL(compD < compF, true);
108 BOOST_CHECK_EQUAL(compD <= compF, true);
109 BOOST_CHECK_EQUAL(compD > compF, false);
110 BOOST_CHECK_EQUAL(compD >= compF, false);
111
112 BOOST_CHECK_EQUAL(compF == compAA, false);
113 BOOST_CHECK_EQUAL(compF != compAA, true);
114 BOOST_CHECK_EQUAL(compF < compAA, true);
115 BOOST_CHECK_EQUAL(compF <= compAA, true);
116 BOOST_CHECK_EQUAL(compF > compAA, false);
117 BOOST_CHECK_EQUAL(compF >= compAA, false);
118}
119
120BOOST_AUTO_TEST_CASE(ZeroLength)
121{
122 name::Component comp0("");
123 BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
124
125 BOOST_CHECK_EQUAL(comp0, comp0);
126 BOOST_CHECK_EQUAL(comp0, name::Component(""));
127 BOOST_CHECK_LT(comp0, name::Component("A"));
128 BOOST_CHECK_LE(comp0, name::Component("A"));
129 BOOST_CHECK_NE(comp0, name::Component("A"));
130 BOOST_CHECK_GT(name::Component("A"), comp0);
131 BOOST_CHECK_GE(name::Component("A"), comp0);
132}
133
134BOOST_AUTO_TEST_CASE(Digest)
135{
136 name::Component digest1(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
137 name::Component digest1b(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
138 name::Component digest2(Block(DIGEST_COMPONENT2_WIRE, sizeof(DIGEST_COMPONENT2_WIRE)));
139 name::Component generic0;
140 name::Component generic2(Block(NAME_COMPONENT2_WIRE, sizeof(NAME_COMPONENT2_WIRE)));
141
142 BOOST_CHECK_EQUAL(digest1 == digest1b, true);
143 BOOST_CHECK_EQUAL(digest1 != digest1b, false);
144 BOOST_CHECK_EQUAL(digest1 < digest1b, false);
145 BOOST_CHECK_EQUAL(digest1 <= digest1b, true);
146 BOOST_CHECK_EQUAL(digest1 > digest1b, false);
147 BOOST_CHECK_EQUAL(digest1 >= digest1b, true);
148
149 BOOST_CHECK_EQUAL(digest1 == digest2, false);
150 BOOST_CHECK_EQUAL(digest1 != digest2, true);
151 BOOST_CHECK_EQUAL(digest1 < digest2, true);
152 BOOST_CHECK_EQUAL(digest1 <= digest2, true);
153 BOOST_CHECK_EQUAL(digest1 > digest2, false);
154 BOOST_CHECK_EQUAL(digest1 >= digest2, false);
155
156 BOOST_CHECK_EQUAL(digest1 == generic0, false);
157 BOOST_CHECK_EQUAL(digest1 != generic0, true);
158 BOOST_CHECK_EQUAL(digest1 < generic0, true);
159 BOOST_CHECK_EQUAL(digest1 <= generic0, true);
160 BOOST_CHECK_EQUAL(digest1 > generic0, false);
161 BOOST_CHECK_EQUAL(digest1 >= generic0, false);
162
163 BOOST_CHECK_EQUAL(digest2 == generic2, false);
164 BOOST_CHECK_EQUAL(digest2 != generic2, true);
165 BOOST_CHECK_EQUAL(digest2 < generic2, true);
166 BOOST_CHECK_EQUAL(digest2 <= generic2, true);
167 BOOST_CHECK_EQUAL(digest2 > generic2, false);
168 BOOST_CHECK_EQUAL(digest2 >= generic2, false);
169}
170
171BOOST_AUTO_TEST_SUITE_END() // Compare
172
173BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
174
175typedef boost::mpl::vector<
176 std::vector<uint8_t>,
177 std::list<uint8_t>,
178 std::vector<int8_t>,
179 std::list<int8_t>
180> ContainerTypes;
181
182BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
183{
184 T bytes;
185 name::Component c(bytes.begin(), bytes.end());
186 BOOST_CHECK_EQUAL(c.value_size(), 0);
187 BOOST_CHECK_EQUAL(c.size(), 2);
188}
189
190BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
191{
192 T bytes{1};
193 name::Component c(bytes.begin(), bytes.end());
194 BOOST_CHECK_EQUAL(c.value_size(), 1);
195 BOOST_CHECK_EQUAL(c.size(), 3);
196}
197
198BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
199{
200 T bytes{1, 2, 3, 4};
201 name::Component c(bytes.begin(), bytes.end());
202 BOOST_CHECK_EQUAL(c.value_size(), 4);
203 BOOST_CHECK_EQUAL(c.size(), 6);
204}
205
206BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
207
208BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
209
210} // namespace tests
211} // namespace ndn