test: split unit tests (#42062)
This commit is contained in:
@@ -78,6 +78,12 @@ executables += [
|
||||
network_test_template + {
|
||||
'sources' : files('test-dhcp-client.c'),
|
||||
},
|
||||
network_test_template + {
|
||||
'sources' : files('test-dhcp-client-id.c'),
|
||||
},
|
||||
network_test_template + {
|
||||
'sources' : files('test-dhcp-duid.c'),
|
||||
},
|
||||
network_test_template + {
|
||||
'sources' : files('test-dhcp-message.c'),
|
||||
},
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "dhcp-client-id-internal.h"
|
||||
#include "hashmap.h"
|
||||
#include "siphash24.h"
|
||||
#include "tests.h"
|
||||
|
||||
static uint64_t client_id_hash_helper(sd_dhcp_client_id *id, uint8_t key[HASH_KEY_SIZE]) {
|
||||
struct siphash state;
|
||||
|
||||
siphash24_init(&state, key);
|
||||
client_id_hash_func(id, &state);
|
||||
|
||||
return htole64(siphash24_finalize(&state));
|
||||
}
|
||||
|
||||
TEST(client_id_hash) {
|
||||
sd_dhcp_client_id a = {
|
||||
.size = 4,
|
||||
}, b = {
|
||||
.size = 4,
|
||||
};
|
||||
uint8_t hash_key[HASH_KEY_SIZE] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
||||
};
|
||||
|
||||
log_debug("/* %s */", __func__);
|
||||
|
||||
memcpy(a.raw, "abcd", 4);
|
||||
memcpy(b.raw, "abcd", 4);
|
||||
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
a.size = 3;
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
a.size = 4;
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
|
||||
b.size = 3;
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
b.size = 4;
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
|
||||
memcpy(b.raw, "abce", 4);
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
@@ -108,23 +108,6 @@ TEST(dhcp_client_anonymize) {
|
||||
ASSERT_OK_ZERO(sd_dhcp_client_set_request_option(client, 101));
|
||||
}
|
||||
|
||||
TEST(dhcp_identifier_set_iaid) {
|
||||
uint32_t iaid_legacy;
|
||||
be32_t iaid;
|
||||
|
||||
ASSERT_OK(dhcp_identifier_set_iaid(NULL, &hw_addr, /* legacy_unstable_byteorder= */ true, &iaid_legacy));
|
||||
ASSERT_OK(dhcp_identifier_set_iaid(NULL, &hw_addr, /* legacy_unstable_byteorder= */ false, &iaid));
|
||||
|
||||
/* we expect, that the MAC address was hashed. The legacy value is in native endianness. */
|
||||
ASSERT_EQ(iaid_legacy, 0x8dde4ba8u);
|
||||
ASSERT_EQ(iaid, htole32(0x8dde4ba8u));
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
ASSERT_EQ(iaid, iaid_legacy);
|
||||
#else
|
||||
ASSERT_EQ(iaid, bswap_32(iaid_legacy));
|
||||
#endif
|
||||
}
|
||||
|
||||
static int check_options(uint8_t code, uint8_t len, const void *option, void *userdata) {
|
||||
switch (code) {
|
||||
case SD_DHCP_OPTION_CLIENT_IDENTIFIER: {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "dhcp-duid-internal.h"
|
||||
#include "ether-addr-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST(dhcp_identifier_set_iaid) {
|
||||
uint32_t iaid_legacy;
|
||||
be32_t iaid;
|
||||
|
||||
static struct hw_addr_data hw_addr = {
|
||||
.length = ETH_ALEN,
|
||||
.ether = {{ 'A', 'B', 'C', '1', '2', '3' }},
|
||||
};
|
||||
|
||||
ASSERT_OK(dhcp_identifier_set_iaid(/* dev= */ NULL, &hw_addr, /* legacy_unstable_byteorder= */ true, &iaid_legacy));
|
||||
ASSERT_OK(dhcp_identifier_set_iaid(/* dev= */ NULL, &hw_addr, /* legacy_unstable_byteorder= */ false, &iaid));
|
||||
|
||||
/* we expect, that the MAC address was hashed. The legacy value is in native endianness. */
|
||||
ASSERT_EQ(iaid_legacy, 0x8dde4ba8u);
|
||||
ASSERT_EQ(iaid, htole32(0x8dde4ba8u));
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
ASSERT_EQ(iaid, iaid_legacy);
|
||||
#else
|
||||
ASSERT_EQ(iaid, bswap_32(iaid_legacy));
|
||||
#endif
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
@@ -9,8 +9,6 @@
|
||||
#include "sd-event.h"
|
||||
|
||||
#include "dhcp-server-internal.h"
|
||||
#include "hashmap.h"
|
||||
#include "siphash24.h"
|
||||
#include "tests.h"
|
||||
|
||||
static void test_pool(struct in_addr *address, unsigned size, int ret) {
|
||||
@@ -285,49 +283,6 @@ static void test_message_handler(void) {
|
||||
ASSERT_OK_EQ(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test), NULL), DHCP_ACK);
|
||||
}
|
||||
|
||||
static uint64_t client_id_hash_helper(sd_dhcp_client_id *id, uint8_t key[HASH_KEY_SIZE]) {
|
||||
struct siphash state;
|
||||
|
||||
siphash24_init(&state, key);
|
||||
client_id_hash_func(id, &state);
|
||||
|
||||
return htole64(siphash24_finalize(&state));
|
||||
}
|
||||
|
||||
static void test_client_id_hash(void) {
|
||||
sd_dhcp_client_id a = {
|
||||
.size = 4,
|
||||
}, b = {
|
||||
.size = 4,
|
||||
};
|
||||
uint8_t hash_key[HASH_KEY_SIZE] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
||||
};
|
||||
|
||||
log_debug("/* %s */", __func__);
|
||||
|
||||
memcpy(a.raw, "abcd", 4);
|
||||
memcpy(b.raw, "abcd", 4);
|
||||
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
a.size = 3;
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
a.size = 4;
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
|
||||
b.size = 3;
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
b.size = 4;
|
||||
ASSERT_EQ(client_id_compare_func(&a, &b), 0);
|
||||
ASSERT_EQ(client_id_hash_helper(&a, hash_key), client_id_hash_helper(&b, hash_key));
|
||||
|
||||
memcpy(b.raw, "abce", 4);
|
||||
ASSERT_NE(client_id_compare_func(&a, &b), 0);
|
||||
}
|
||||
|
||||
static void test_static_lease(void) {
|
||||
_cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
|
||||
|
||||
@@ -452,7 +407,6 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
test_client_id_hash();
|
||||
test_static_lease();
|
||||
test_domain_name();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user