]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dhcp: add test for dhcp_identifier_set_iaid()
authorThomas Haller <thaller@redhat.com>
Thu, 1 Nov 2018 13:46:27 +0000 (14:46 +0100)
committerThomas Haller <thaller@redhat.com>
Mon, 12 Nov 2018 18:08:35 +0000 (19:08 +0100)
src/libsystemd-network/test-dhcp-client.c

index 8770d9d7e7a3755000e880fd93c091281e8d627e..e92d4afac0905628f44b2c1d61c1fd88a1631779 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <sys/socket.h>
 #include <unistd.h>
+#include <net/if.h>
 
 #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);