summaryrefslogtreecommitdiffstats
path: root/lib/src/test/test_name.cc
blob: 35e636b639901b38a854e2ee1b721548253e5a8f (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
335
/*
 * 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>

extern "C"
{
#include <hicn/name.h>
#include <hicn/common.h>
#include <hicn/error.h>
}

class NameTest : public ::testing::Test
{
protected:
  const char *ipv6_prefix = "b001::abcd:1234:abcd:1234";
  const char *ipv4_prefix = "12.13.14.15";
  const uint32_t suffix = 12345;

  NameTest () : name_{}, name4_{}, name6_{}
  {
    int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6);
    EXPECT_EQ (rc, 1);

    rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4);
    EXPECT_EQ (rc, 1);

    rc = hicn_name_create (ipv4_prefix, suffix, &name4_);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
    rc = hicn_name_create (ipv6_prefix, suffix, &name6_);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
  }

  virtual ~NameTest () {}

  void
  nameHashTest (const char *prefix)
  {
    // Create 2 names
    uint32_t suffix = 13579;
    hicn_name_t name_a, name_b;
    int rc = hicn_name_create (prefix, suffix, &name_a);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
    rc = hicn_name_create (prefix, suffix, &name_b);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

    // The hash should be equal, with and without considering the suffix
    uint32_t hash_a, hash_b;
    hash_a = hicn_name_get_hash (&name_a);
    hash_b = hicn_name_get_hash (&name_b);
    EXPECT_EQ (hash_a, hash_b);

    hash_a = hicn_name_get_prefix_hash (&name_a);
    hash_b = hicn_name_get_prefix_hash (&name_b);
    EXPECT_EQ (hash_a, hash_b);

    // Now let's change the suffix
    rc = hicn_name_set_suffix (&name_a, 97531);
    // They should result equal if we do not consider the suffix
    hash_a = hicn_name_get_prefix_hash (&name_a);
    hash_b = hicn_name_get_prefix_hash (&name_b);
    EXPECT_EQ (hash_a, hash_b);

    // And different if we consider it
    hash_a = hicn_name_get_hash (&name_a);
    hash_b = hicn_name_get_hash (&name_b);
    EXPECT_NE (hash_a, hash_b);
  }

  void
  nameCopyTest (const char *prefix)
  {
    uint32_t suffix = 13579;
    hicn_name_t name_a, name_b;
    int rc = hicn_name_create (prefix, suffix, &name_a);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

    rc = hicn_name_copy (&name_b, &name_a);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

    rc = hicn_name_compare (&name_a, &name_b, 1);
    EXPECT_EQ (rc, 0);
  }

  void
  nameCompareTest (const char *prefix)
  {
    // Create 2 names
    uint32_t suffix = 13579;
    hicn_name_t name_a, name_b;
    int rc = hicn_name_create (prefix, suffix, &name_a);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
    rc = hicn_name_create (prefix, suffix, &name_b);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

    // They should be equal, with and without considering the suffix
    rc = hicn_name_compare (&name_a, &name_b, 1);
    EXPECT_EQ (rc, 0);
    rc = hicn_name_compare (&name_a, &name_b, 0);
    EXPECT_EQ (rc, 0);

    // Now let's change the suffix
    rc = hicn_name_set_suffix (&name_a, 97531);
    // They should result equal if we do not consider the suffix
    rc = hicn_name_compare (&name_a, &name_b, 0);
    EXPECT_EQ (rc, 0);
    // And different if we consider the suffix
    rc = hicn_name_compare (&name_a, &name_b, 1);
    EXPECT_NE (rc, 0);
  }

  void
  nameFromIpPrefixTest (const hicn_ip_prefix_t &hicn_ip_prefix)
  {
    uint32_t suffix = 54321;
    hicn_name_t name;
    int rc = hicn_name_create_from_ip_prefix (&hicn_ip_prefix, suffix, &name);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
    rc = memcmp (hicn_ip_prefix.address.v6.as_u8, name.prefix.v6.as_u8,
		 sizeof (name.prefix.v6));
    EXPECT_EQ (rc, 0);
    EXPECT_EQ (suffix, name.suffix);
  }

  void
  nameToIpPrefixTest (const char *prefix)
  {
    uint32_t suffix = 54321;
    hicn_name_t name;
    int rc = hicn_name_create (prefix, suffix, &name);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

    // Get family
    int family;
    rc = hicn_name_get_family (&name, &family);

    hicn_ip_prefix_t hicn_ip_prefix;
    rc = hicn_name_to_hicn_ip_prefix (&name, &hicn_ip_prefix);
    EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
    EXPECT_EQ (hicn_ip_prefix.family, family);
    rc = hicn_ip_address_cmp (&hicn_ip_prefix.address, &name.prefix);
    EXPECT_EQ (rc, 0);
  }

  hicn_name_t name_, name4_, name6_;
  hicn_ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes;
};

/**
 * Name Initialization
 */
TEST_F (NameTest, NameInitialization)
{
  EXPECT_TRUE (_is_unspec (&name_));
  uint32_t suffix = 12345;

  // Initialize ipv6 name
  hicn_name_t name6;
  int rc = hicn_name_create (ipv6_prefix, suffix, &name6);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

  // Check name is correctly created
  rc = hicn_ip_address_cmp (&name6.prefix, &ipv6_prefix_bytes);
  EXPECT_EQ (rc, 0);
  EXPECT_EQ (name6.suffix, suffix);

  // Initialize ipv4 name
  hicn_name_t name4;
  rc = hicn_name_create (ipv4_prefix, suffix, &name4);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

  // Check name is correctly created
  rc = hicn_ip_address_cmp (&name4.prefix, &ipv4_prefix_bytes);
  EXPECT_EQ (name4.prefix.pad[0], 0UL);
  EXPECT_EQ (name4.prefix.pad[1], 0UL);
  EXPECT_EQ (name4.prefix.pad[2], 0UL);
  EXPECT_EQ (rc, 0);
  EXPECT_EQ (name4.suffix, suffix);

  // Try also to reuse previously initialized name
  rc = hicn_name_create (ipv4_prefix, suffix, &name6);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

  // Check name is correctly created
  rc = hicn_ip_address_cmp (&name6.prefix, &ipv4_prefix_bytes);
  EXPECT_EQ (name6.prefix.pad[0], 0UL);
  EXPECT_EQ (name6.prefix.pad[1], 0UL);
  EXPECT_EQ (name6.prefix.pad[2], 0UL);
  EXPECT_EQ (rc, 0);
  EXPECT_EQ (name6.suffix, suffix);
}

/**
 * Name from ip prefix
 */
TEST_F (NameTest, NameFromIpPrefix6)
{
  hicn_ip_prefix_t hicn_ip_prefix = { .family = AF_INET6,
				      .address = {},
				      .len = 64 };

  hicn_ip_prefix.address.v6.as_u64[0] = ipv6_prefix_bytes.v6.as_u64[0];
  hicn_ip_prefix.address.v6.as_u64[1] = ipv6_prefix_bytes.v6.as_u64[1];

  nameFromIpPrefixTest (hicn_ip_prefix);
}

TEST_F (NameTest, NameFromIpPrefix4)
{
  hicn_ip_prefix_t hicn_ip_prefix = { .family = AF_INET,
				      .address = {},
				      .len = 64 };
  hicn_ip_prefix.address.v4.as_u32 = ipv4_prefix_bytes.v4.as_u32;
  hicn_ip_prefix.address.pad[0] = 0;
  hicn_ip_prefix.address.pad[1] = 0;
  hicn_ip_prefix.address.pad[2] = 0;
  nameFromIpPrefixTest (hicn_ip_prefix);
}

TEST_F (NameTest, NameCompare6) { nameCompareTest (ipv6_prefix); }

TEST_F (NameTest, NameCompare4) { nameCompareTest (ipv4_prefix); }

TEST_F (NameTest, NameHash6) { nameHashTest (ipv6_prefix); }

TEST_F (NameTest, NameHash4) { nameHashTest (ipv4_prefix); }

TEST_F (NameTest, NameEmpty)
{
  int rc = hicn_name_empty (&name_);
  EXPECT_EQ (rc, 1);

  name_.prefix.v6 = ipv6_prefix_bytes.v6;
  rc = hicn_name_empty (&name_);
  EXPECT_EQ (rc, 0);
}

TEST_F (NameTest, NameCopy6) { nameCopyTest (ipv6_prefix); }

TEST_F (NameTest, NameCopy4) { nameCopyTest (ipv4_prefix); }

TEST_F (NameTest, NameCopyToDestination)
{
  ipv4_address_t dst4;
  ipv6_address_t dst6;

  // Copy names to destination
  int rc = hicn_name_copy_prefix_to_destination (dst4.as_u8, &name4_);
  EXPECT_EQ (rc, 0);
  rc = hicn_name_copy_prefix_to_destination (dst6.as_u8, &name6_);
  EXPECT_EQ (rc, 0);

  // Check copy succeeded
  EXPECT_TRUE (dst4.as_u32 == name4_.prefix.v4.as_u32);
  EXPECT_TRUE (dst6.as_u64[0] == name6_.prefix.v6.as_u64[0]);
  EXPECT_TRUE (dst6.as_u64[1] == name6_.prefix.v6.as_u64[1]);
}

TEST_F (NameTest, SetGetSuffix)
{
  uint32_t suffix2 = 55555, suffix_ret;

  // Check if suffix is correct
  int rc = hicn_name_get_seq_number (&name6_, &suffix_ret);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
  EXPECT_EQ (suffix, suffix_ret);

  // Set new suffix
  rc = hicn_name_set_suffix (&name6_, suffix2);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

  // Check suffix was set
  rc = hicn_name_get_seq_number (&name6_, &suffix_ret);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
  EXPECT_EQ (suffix2, suffix_ret);
}

TEST_F (NameTest, NameToSockAddr)
{
  struct sockaddr_in saddr4;
  struct sockaddr_in6 saddr6;

  int rc =
    hicn_name_to_sockaddr_address (&name6_, (struct sockaddr *) (&saddr6));
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
  rc = memcmp (name6_.prefix.v6.as_u8, saddr6.sin6_addr.s6_addr,
	       sizeof (name6_.prefix.v6));
  EXPECT_EQ (rc, 0);

  rc = hicn_name_to_sockaddr_address (&name4_, (struct sockaddr *) (&saddr4));
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);
  EXPECT_EQ (name4_.prefix.v4.as_u32, saddr4.sin_addr.s_addr);
}

TEST_F (NameTest, NameToIpPrefix)
{
  nameToIpPrefixTest (ipv4_prefix);
  nameToIpPrefixTest (ipv6_prefix);
}

TEST_F (NameTest, NameNToP)
{
  char dst[128];

  // V6
  int rc = hicn_name_ntop (&name6_, dst, 128);
  EXPECT_EQ (rc, HICN_LIB_ERROR_NONE);

  // Build expected name
  std::stringstream expected6;
  expected6 << ipv6_prefix << "|" << suffix;

  rc = strcmp (dst, expected6.str ().c_str ());
  EXPECT_EQ (rc, 0);

  // V4
  rc = hicn_name_ntop (&name4_, dst, 128);
  std::stringstream expected4;
  expected4 << ipv4_prefix << "|" << suffix;

  rc = strcmp (dst, expected4.str ().c_str ());
  EXPECT_EQ (rc, 0);
}