Debian Codegraph @

src/libsystemd-network/test-dhcp-client-id.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "dhcp-client-id-internal.h"
4 #include "hashmap.h"
5 #include "siphash24.h"
6 #include "tests.h"
7
8 static uint64_t client_id_hash_helper(sd_dhcp_client_id *id, uint8_t key[HASH_KEY_SIZE]) {
9 struct siphash state;
10
11 siphash24_init(&state, key);
12 client_id_hash_func(id, &state);
13
14 return htole64(siphash24_finalize(&state));
15 }
16
17 TEST(client_id_hash) {
18 sd_dhcp_client_id a = {
19 .size = 4,
20 }, b = {
21 .size = 4,
22 };
23 uint8_t hash_key[HASH_KEY_SIZE] = {
24 '0', '1', '2', '3', '4', '5', '6', '7',
25 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
26 };
27
28 log_debug("/* %s */", __func__);
29
30 memcpy(a.raw, "abcd", 4);
31 memcpy(b.raw, "abcd", 4);
32
33 ASSERT_EQ(client_id_compare_func(&a, &b), 0);
34 ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
35 a.size = 3;
36 ASSERT_NE(client_id_compare_func(&a, &b), 0);
37 a.size = 4;
38 ASSERT_EQ(client_id_compare_func(&a, &b), 0);
39 ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
40
41 b.size = 3;
42 ASSERT_NE(client_id_compare_func(&a, &b), 0);
43 b.size = 4;
44 ASSERT_EQ(client_id_compare_func(&a, &b), 0);
45 ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
46
47 memcpy(b.raw, "abce", 4);
48 ASSERT_NE(client_id_compare_func(&a, &b), 0);
49 }
50
51 DEFINE_TEST_MAIN(LOG_DEBUG);