From: Thomas Haller Date: Thu, 1 Nov 2018 13:46:27 +0000 (+0100) Subject: dhcp: add test for dhcp_identifier_set_iaid() X-Git-Tag: v240~340^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43fc095532899121030750204f47ab8566514d90;p=thirdparty%2Fsystemd.git dhcp: add test for dhcp_identifier_set_iaid() --- diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 8770d9d7e7a..e92d4afac09 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "sd-dhcp-client.h" #include "sd-event.h" @@ -16,6 +17,7 @@ #include "dhcp-internal.h" #include "dhcp-protocol.h" #include "fd-util.h" +#include "random-util.h" #include "tests.h" #include "util.h" @@ -153,6 +155,27 @@ static void test_checksum(void) { assert_se(dhcp_packet_checksum((uint8_t*)&buf, 20) == be16toh(0x78ae)); } +static void test_dhcp_identifier_set_iaid(void) { + uint32_t iaid; + int ifindex; + + for (;;) { + char ifname[IFNAMSIZ]; + + /* try to find an ifindex which does not exist. I causes dhcp_identifier_set_iaid() + * to hash the MAC address. */ + pseudo_random_bytes(&ifindex, sizeof(ifindex)); + if (ifindex > 0 && !if_indextoname(ifindex, ifname)) + break; + } + + assert_se(dhcp_identifier_set_iaid(ifindex, mac_addr, sizeof(mac_addr), &iaid) >= 0); + + /* we expect, that the MAC address was hashed. Note that the value is in native + * endianness. */ + assert_se(iaid == 0x8dde4ba8u); +} + static int check_options(uint8_t code, uint8_t len, const void *option, void *userdata) { switch(code) { case SD_DHCP_OPTION_CLIENT_IDENTIFIER: @@ -532,6 +555,7 @@ int main(int argc, char *argv[]) { test_request_basic(e); test_request_anonymize(e); test_checksum(); + test_dhcp_identifier_set_iaid(); test_discover_message(e); test_addr_acq(e);