aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/test/test_prefix.cc
blob: 5de7375662c19efd061e3eb89e0edb9012b8b8b2 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (c) 2021 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 <glog/logging.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hicn/transport/core/prefix.h>
#include <hicn/transport/errors/invalid_ip_address_exception.h>
#include <hicn/transport/portability/endianess.h>

#include <cstring>
#include <memory>
#include <vector>

namespace transport {
namespace core {

namespace {
class PrefixTest : public ::testing::Test {
 protected:
  static inline const char prefix_str0[] = "2001:db8:1::/64";
  static inline const char prefix_str1[] = "10.11.12.0/24";
  static inline const char prefix_str2[] = "2001:db8:1::abcd/64";
  static inline const char prefix_str3[] = "10.11.12.245/27";
  static inline const char wrong_prefix_str0[] = "10.11.12.245/45";
  static inline const char wrong_prefix_str1[] = "10.400.12.13/8";
  static inline const char wrong_prefix_str2[] = "2001:db8:1::/640";
  static inline const char wrong_prefix_str3[] = "20011::db8:1::/16";
  static inline const char wrong_prefix_str4[] = "2001::db8:1::fffff/96";

  PrefixTest() = default;

  ~PrefixTest() override = default;

  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:

  void SetUp() override {
    // Code here will be called immediately after the constructor (right
    // before each test).
  }

  void TearDown() override {
    // Code here will be called immediately after each test (right
    // before the destructor).
  }
};

TEST_F(PrefixTest, ConstructorRightString) {
  // Create empty prefix
  Prefix p;

  // Create prefix from string
  Prefix p0(prefix_str0);
  // Reconstruct string and check it is equal to original address
  std::string network = p0.getNetwork();
  std::uint16_t prefix_length = p0.getPrefixLength();
  EXPECT_THAT(network + "/" + std::to_string(prefix_length),
              ::testing::StrEq(prefix_str0));

  // Create prefix from string
  Prefix p1(prefix_str1);
  // Reconstruct string and check it is equal to original address
  network = p1.getNetwork();
  prefix_length = p1.getPrefixLength();
  EXPECT_THAT(network + "/" + std::to_string(prefix_length),
              ::testing::StrEq(prefix_str1));

  // Create prefix from string
  Prefix p2(prefix_str2);
  // Reconstruct string and check it is equal to original address
  network = p2.getNetwork();
  prefix_length = p2.getPrefixLength();
  EXPECT_THAT(network + "/" + std::to_string(prefix_length),
              ::testing::StrEq(prefix_str2));

  // Create prefix from string
  Prefix p3(prefix_str3);
  // Reconstruct string and check it is equal to original address
  network = p3.getNetwork();
  prefix_length = p3.getPrefixLength();
  EXPECT_THAT(network + "/" + std::to_string(prefix_length),
              ::testing::StrEq(prefix_str3));

  // Create prefix from string and prefix length
  Prefix p4("2001::1234", 66);
  // Reconstruct string and check it is equal to original address
  network = p4.getNetwork();
  prefix_length = p4.getPrefixLength();
  auto af = p4.getAddressFamily();
  EXPECT_THAT(network, ::testing::StrEq("2001::1234"));
  EXPECT_THAT(prefix_length, ::testing::Eq(66));
  EXPECT_THAT(af, ::testing::Eq(AF_INET6));
}

TEST_F(PrefixTest, ConstructorWrongString) {
  try {
    Prefix p0(wrong_prefix_str0);
    FAIL() << "Expected exception";
  } catch (const errors::InvalidIpAddressException &) {
    // Expected exception
  }

  try {
    Prefix p1(wrong_prefix_str1);
    FAIL() << "Expected exception";
  } catch (const errors::InvalidIpAddressException &) {
    // Expected exception
  }

  try {
    Prefix p2(wrong_prefix_str2);
    FAIL() << "Expected exception";
  } catch (const errors::InvalidIpAddressException &) {
    // Expected exception
  }

  try {
    Prefix p3(wrong_prefix_str3);
    FAIL() << "Expected exception";
  } catch (const errors::InvalidIpAddressException &) {
    // Expected exception
  }

  try {
    Prefix p4(wrong_prefix_str4);
    FAIL() << "Expected exception";
  } catch (const errors::InvalidIpAddressException &) {
    // Expected exception
  }
}

TEST_F(PrefixTest, Comparison) {
  Prefix p0(prefix_str0);
  Prefix p1(prefix_str1);

  // Expect they are different
  EXPECT_THAT(p0, ::testing::Ne(p1));

  auto p2 = p1;
  // Expect they are equal
  EXPECT_THAT(p1, ::testing::Eq(p2));
}

TEST_F(PrefixTest, ToSockAddress) {
  Prefix p0(prefix_str3);

  auto ret = p0.toSockaddr();
  auto sockaddr = reinterpret_cast<sockaddr_in *>(ret.get());

  EXPECT_THAT(sockaddr->sin_family, ::testing::Eq(AF_INET));
  EXPECT_THAT(sockaddr->sin_addr.s_addr, portability::host_to_net(0x0a0b0cf5));
}

TEST_F(PrefixTest, GetPrefixLength) {
  Prefix p0(prefix_str3);
  EXPECT_THAT(p0.getPrefixLength(), ::testing::Eq(27));
}

TEST_F(PrefixTest, SetPrefixLength) {
  Prefix p0(prefix_str3);
  EXPECT_THAT(p0.getPrefixLength(), ::testing::Eq(27));
  p0.setPrefixLength(20);
  EXPECT_THAT(p0.getPrefixLength(), ::testing::Eq(20));

  try {
    p0.setPrefixLength(33);
    FAIL() << "Expected exception";
  } catch ([[maybe_unused]] const errors::InvalidIpAddressException &) {
    // Expected exception
  }
}

TEST_F(PrefixTest, SetGetNetwork) {
  Prefix p0(prefix_str0);
  EXPECT_THAT(p0.getPrefixLength(), ::testing::Eq(64));
  p0.setNetwork("b001::1234");
  EXPECT_THAT(p0.getNetwork(), ::testing::StrEq("b001::1234"));
  EXPECT_THAT(p0.getPrefixLength(), ::testing::Eq(64));
}

TEST_F(PrefixTest, Contains) {
  // IPv6 prefix
  Prefix p0(prefix_str0);
  ip_address_t ip0, ip1;

  ip_address_pton("2001:db8:1::1234", &ip0);
  ip_address_pton("2001:db9:1::1234", &ip1);

  EXPECT_TRUE(p0.contains(ip0));
  EXPECT_FALSE(p0.contains(ip1));

  Prefix p1(prefix_str1);
  ip_address_pton("10.11.12.12", &ip0);
  ip_address_pton("10.12.12.13", &ip1);

  EXPECT_TRUE(p1.contains(ip0));
  EXPECT_FALSE(p1.contains(ip1));

  Prefix p2(prefix_str2);
  ip_address_pton("2001:db8:1::dbca", &ip0);
  ip_address_pton("10.12.12.12", &ip1);

  EXPECT_TRUE(p2.contains(ip0));
  EXPECT_FALSE(p2.contains(ip1));

  Prefix p3(prefix_str3);
  ip_address_pton("10.11.12.245", &ip0);
  ip_address_pton("10.11.12.1", &ip1);

  EXPECT_TRUE(p3.contains(ip0));
  EXPECT_FALSE(p3.contains(ip1));

  // Corner cases
  Prefix p4("::/0");
  ip_address_pton("7001:db8:1::1234", &ip0);
  ip_address_pton("8001:db8:1::1234", &ip1);

  EXPECT_TRUE(p4.contains(ip0));
  EXPECT_TRUE(p4.contains(ip1));

  // Corner cases
  Prefix p5("b001:a:b:c:d:e:f:1/128");
  ip_address_pton("b001:a:b:c:d:e:f:1", &ip0);
  ip_address_pton("b001:a:b:c:d:e:f:2", &ip1);

  EXPECT_TRUE(p5.contains(ip0));
  EXPECT_FALSE(p5.contains(ip1));
}

TEST_F(PrefixTest, GetAddressFamily) {
  Prefix p0(prefix_str0);
  auto af = p0.getAddressFamily();
  EXPECT_THAT(af, ::testing::Eq(AF_INET6));

  Prefix p1(prefix_str1);
  af = p1.getAddressFamily();
  EXPECT_THAT(af, ::testing::Eq(AF_INET));
}

TEST_F(PrefixTest, MakeName) {
  Prefix p0(prefix_str0);
  auto name0 = p0.makeName();
  EXPECT_THAT(name0.toString(), ::testing::StrEq("2001:db8:1::|0"));

  Prefix p1(prefix_str1);
  auto name1 = p1.makeName();
  EXPECT_THAT(name1.toString(), ::testing::StrEq("10.11.12.0|0"));

  Prefix p2(prefix_str2);
  auto name2 = p2.makeName();
  EXPECT_THAT(name2.toString(), ::testing::StrEq("2001:db8:1::|0"));

  Prefix p3(prefix_str3);
  auto name3 = p3.makeName();
  EXPECT_THAT(name3.toString(), ::testing::StrEq("10.11.12.224|0"));

  Prefix p4("b001:a:b:c:d:e:f:1/128");
  auto name4 = p4.makeName();
  EXPECT_THAT(name4.toString(), ::testing::StrEq("b001:a:b:c:d:e:f:1|0"));
}

TEST_F(PrefixTest, MakeRandomName) {
  Prefix p0(prefix_str0);
  auto name0 = p0.makeRandomName();
  auto name1 = p0.makeRandomName();
  auto name2 = p0.makeRandomName();
  auto name3 = p0.makeRandomName();

  EXPECT_THAT(name0, ::testing::Not(::testing::Eq(name1)));
  EXPECT_THAT(name0, ::testing::Not(::testing::Eq(name2)));
  EXPECT_THAT(name0, ::testing::Not(::testing::Eq(name3)));
  EXPECT_THAT(name1, ::testing::Not(::testing::Eq(name2)));
  EXPECT_THAT(name1, ::testing::Not(::testing::Eq(name3)));
  EXPECT_THAT(name2, ::testing::Not(::testing::Eq(name3)));

  // Corner case
  Prefix p2("b001:a:b:c:d:e:f:1/128");
  name0 = p2.makeRandomName();
  name1 = p2.makeRandomName();
  name2 = p2.makeRandomName();
  name3 = p2.makeRandomName();

  EXPECT_THAT(name0, ::testing::Eq(name1));
  EXPECT_THAT(name0, ::testing::Eq(name2));
  EXPECT_THAT(name0, ::testing::Eq(name3));
  EXPECT_THAT(name1, ::testing::Eq(name2));
  EXPECT_THAT(name1, ::testing::Eq(name3));
  EXPECT_THAT(name2, ::testing::Eq(name3));
}

TEST_F(PrefixTest, MakeNameWithIndex) {
  Prefix p0(prefix_str0);
  auto name0 = p0.makeNameWithIndex(0);
  EXPECT_THAT(name0.toString(), ::testing::StrEq("2001:db8:1::|0"));
  auto name1 = p0.makeNameWithIndex(1);
  EXPECT_THAT(name1.toString(), ::testing::StrEq("2001:db8:1::1|0"));
  auto name2 = p0.makeNameWithIndex(2);
  EXPECT_THAT(name2.toString(), ::testing::StrEq("2001:db8:1::2|0"));
  auto name3 = p0.makeNameWithIndex(3);
  EXPECT_THAT(name3.toString(), ::testing::StrEq("2001:db8:1::3|0"));

  Prefix p1(prefix_str1);
  name0 = p1.makeNameWithIndex(0);
  EXPECT_THAT(name0.toString(), ::testing::StrEq("10.11.12.0|0"));
  name1 = p1.makeNameWithIndex(1);
  EXPECT_THAT(name1.toString(), ::testing::StrEq("10.11.12.1|0"));
  name2 = p1.makeNameWithIndex(2);
  EXPECT_THAT(name2.toString(), ::testing::StrEq("10.11.12.2|0"));
  name3 = p1.makeNameWithIndex(3);
  EXPECT_THAT(name3.toString(), ::testing::StrEq("10.11.12.3|0"));

  // Test truncation
  Prefix p2("b001::/96");
  name0 = p2.makeNameWithIndex(0xffffffffffffffff);
  EXPECT_THAT(name0.toString(), ::testing::StrEq("b001::ffff:ffff|0"));
}

}  // namespace

}  // namespace core
}  // namespace transport