1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* Copyright (c) 2021-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <hicn/transport/core/interest.h>
#include <hicn/transport/errors/not_implemented_exception.h>
#include <test/packet_samples.h>
#include <climits>
#include <random>
#include <vector>
namespace transport {
namespace core {
namespace {
// The fixture for testing class Foo.
class InterestTest : public ::testing::Test {
protected:
InterestTest()
: name_("b001::123|321"), interest_(HICN_PACKET_FORMAT_IPV6_TCP) {
// You can do set-up work for each test here.
}
virtual ~InterestTest() {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
virtual void SetUp() {
// Code here will be called immediately after the constructor (right
// before each test).
}
virtual void TearDown() {
// Code here will be called immediately after each test (right
// before the destructor).
}
Name name_;
Interest interest_;
std::vector<uint8_t> buffer_ = {// IPv6 src=b001::ab:cdab:cdef, dst=b002::ca
IPV6_HEADER(TCP_PROTO, 20 + PAYLOAD_SIZE),
// ICMP6 echo request
TCP_HEADER(0x00),
// Payload
PAYLOAD};
};
void testFormatConstructor(
hicn_packet_format_t format = HICN_PACKET_FORMAT_NONE) {
try {
Interest interest(format, 0);
} catch (...) {
char buf[MAXSZ_HICN_PACKET_FORMAT];
FAIL() << "ERROR: Unexpected exception thrown for " << buf;
}
}
void testFormatConstructorException(
Packet::Format format = HICN_PACKET_FORMAT_NONE) {
try {
Interest interest(format, 0);
FAIL() << "We expected an exception here";
} catch (errors::MalformedPacketException &exc) {
// Ok right exception
} catch (...) {
FAIL() << "Wrong exception thrown";
}
}
} // namespace
TEST_F(InterestTest, ConstructorWithFormat) {
/**
* Without arguments it should be format = HICN_PACKET_FORMAT_NONE.
* We expect a crash.
*/
testFormatConstructor(HICN_PACKET_FORMAT_IPV4_TCP);
testFormatConstructor(HICN_PACKET_FORMAT_IPV6_TCP);
testFormatConstructorException(HICN_PACKET_FORMAT_IPV4_ICMP);
testFormatConstructorException(HICN_PACKET_FORMAT_IPV6_ICMP);
testFormatConstructor(HICN_PACKET_FORMAT_IPV4_TCP_AH);
testFormatConstructor(HICN_PACKET_FORMAT_IPV6_TCP_AH);
testFormatConstructorException(HICN_PACKET_FORMAT_IPV4_ICMP_AH);
testFormatConstructorException(HICN_PACKET_FORMAT_IPV6_ICMP_AH);
}
TEST_F(InterestTest, ConstructorWithName) {
/**
* Without arguments it should be format = HICN_PACKET_FORMAT_NONE.
* We expect a crash.
*/
Name n("b001::1|123");
try {
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP, n);
} catch (...) {
FAIL() << "ERROR: Unexpected exception thrown";
}
}
TEST_F(InterestTest, ConstructorWithBuffer) {
// Ensure buffer is interest
#if 0
auto ret = Interest::isInterest(&buffer_[0]);
EXPECT_TRUE(ret);
#endif
// Create interest from buffer
try {
Interest interest(Interest::COPY_BUFFER, &buffer_[0], buffer_.size());
} catch (...) {
FAIL() << "ERROR: Unexpected exception thrown";
}
std::vector<uint8_t> buffer2{// IPv6 src=b001::ab:cdab:cdef, dst=b002::ca
IPV6_HEADER(ICMP6_PROTO, 60 + 44),
// ICMP6 echo request
TCP_HEADER(0x00),
// Payload
PAYLOAD};
// Ensure this throws an exception
try {
Interest interest(Interest::COPY_BUFFER, &buffer2[0], buffer2.size());
FAIL() << "We expected an exception here";
} catch (errors::MalformedPacketException &exc) {
// Ok right exception
} catch (...) {
FAIL() << "Wrong exception thrown";
}
}
TEST_F(InterestTest, SetGetName) {
// Create interest from buffer
Interest interest(Interest::COPY_BUFFER, &buffer_[0], buffer_.size());
// Get name
auto n = interest.getName();
// ensure name is b002::ca|1
Name n2("b002::ca|1");
auto ret = (n == n2);
EXPECT_TRUE(ret);
Name n3("b003::1234|1234");
// Change name to b003::1234|1234
interest.setName(n3);
// Check name was set
n = interest.getName();
ret = (n == n3);
EXPECT_TRUE(ret);
}
TEST_F(InterestTest, SetGetLocator) {
// Create interest from buffer
Interest interest(Interest::COPY_BUFFER, &buffer_[0], buffer_.size());
// Get locator
auto l = interest.getLocator();
hicn_ip_address_t address;
inet_pton(AF_INET6, "b006::ab:cdab:cdef", &address);
auto ret = !hicn_ip_address_cmp(&l, &address);
EXPECT_TRUE(ret);
// Set different locator
inet_pton(AF_INET6, "2001::1234::4321::abcd::", &address);
// Set it on interest
interest.setLocator(address);
// Check it was set
l = interest.getLocator();
ret = !hicn_ip_address_cmp(&l, &address);
EXPECT_TRUE(ret);
}
TEST_F(InterestTest, SetGetLifetime) {
// Create interest from buffer
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP);
const constexpr uint32_t lifetime = 10000;
// Set lifetime
interest.setLifetime(lifetime);
// Get lifetime
auto l = interest.getLifetime();
// Ensure they are the same
EXPECT_EQ(l, lifetime);
}
TEST_F(InterestTest, HasManifest) {
// Create interest from buffer
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP);
// Let's expect anexception here
try {
interest.setPayloadType(PayloadType::UNSPECIFIED);
FAIL() << "We expect an esception here";
} catch (errors::RuntimeException &exc) {
// Ok right exception
} catch (...) {
FAIL() << "Wrong exception thrown";
}
interest.setPayloadType(PayloadType::DATA);
EXPECT_FALSE(interest.hasManifest());
interest.setPayloadType(PayloadType::MANIFEST);
EXPECT_TRUE(interest.hasManifest());
}
TEST_F(InterestTest, AppendSuffixesEncodeAndIterate) {
// Create interest from buffer
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP);
// Appenad some suffixes, with some duplicates
interest.appendSuffix(1);
interest.appendSuffix(2);
interest.appendSuffix(5);
interest.appendSuffix(3);
interest.appendSuffix(4);
interest.appendSuffix(5);
interest.appendSuffix(5);
interest.appendSuffix(5);
interest.appendSuffix(5);
interest.appendSuffix(5);
// Encode them in wire format
interest.encodeSuffixes();
// Decode suffixes from wire format
interest.decodeSuffixes();
// Iterate over them. They should be in order and without repetitions
auto suffix = interest.firstSuffix();
auto n_suffixes = interest.numberOfSuffixes();
for (uint32_t i = 0; i < n_suffixes; i++) {
EXPECT_EQ(*(suffix + i), (i + 1));
}
}
TEST_F(InterestTest, AppendSuffixesWithGaps) {
// Create interest from buffer
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP);
// Appenad some suffixes, out of order and with gaps
interest.appendSuffix(6);
interest.appendSuffix(2);
interest.appendSuffix(5);
interest.appendSuffix(1);
// Encode them in wire format
interest.encodeSuffixes();
EXPECT_TRUE(interest.hasManifest());
// Decode suffixes from wire format
interest.decodeSuffixes();
// Check first suffix correctness
auto suffix = interest.firstSuffix();
EXPECT_NE(suffix, nullptr);
EXPECT_EQ(*suffix, 1U);
// Iterate over them. They should be in order and without repetitions
std::vector<uint32_t> expected = {1, 2, 5, 6};
EXPECT_EQ(interest.numberOfSuffixes(), expected.size());
for (uint32_t seq : expected) {
EXPECT_EQ(*suffix, seq);
suffix++;
}
}
TEST_F(InterestTest, InterestWithoutManifest) {
// Create interest without manifest
Interest interest(HICN_PACKET_FORMAT_IPV6_TCP);
auto suffix = interest.firstSuffix();
EXPECT_FALSE(interest.hasManifest());
EXPECT_EQ(interest.numberOfSuffixes(), 0U);
EXPECT_EQ(suffix, nullptr);
}
} // namespace core
} // namespace transport
|