]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-dhcp-client: add test case for anonymized mode
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 27 Apr 2026 05:37:29 +0000 (14:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 May 2026 06:18:41 +0000 (15:18 +0900)
src/libsystemd-network/test-dhcp-client.c

index 7a6c163e28fb5a6fada451238f53fc66e03cee6a..0dd5623486071d44e5d28cd09dcbfa428bfcc66c 100644 (file)
@@ -326,6 +326,30 @@ static void verify_request(sd_dhcp_message *m, uint8_t type) {
         ASSERT_STREQ(str, extra_option_164);
 }
 
+static void verify_anonymized_request(sd_dhcp_message *m, uint8_t type) {
+        verify_header(m);
+        verify_basic_options(m, type);
+
+        _cleanup_set_free_ Set *prl = NULL;
+        ASSERT_OK(dhcp_message_get_option_parameter_request_list(m, &prl));
+
+        for (uint8_t i = 178; i <= 207; i++)
+                ASSERT_FALSE(set_contains(prl, UINT_TO_PTR(i)));
+
+        uint8_t code;
+        FOREACH_ARGUMENT(code,
+                         SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
+                         SD_DHCP_OPTION_MUD_URL,
+                         SD_DHCP_OPTION_HOST_NAME,
+                         SD_DHCP_OPTION_FQDN,
+                         SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER,
+                         SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION,
+                         SD_DHCP_OPTION_USER_CLASS,
+                         163,
+                         164)
+                ASSERT_FALSE(dhcp_message_has_option(m, code));
+}
+
 static void verify_request_server_address(sd_dhcp_message *m) {
         struct in_addr a;
         ASSERT_OK(dhcp_message_get_option_address(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, &a));
@@ -594,6 +618,79 @@ TEST(basic) {
         ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client)));
 }
 
+static int anonymize_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        sd_dhcp_client *client = ASSERT_PTR(userdata);
+        static unsigned count = 0;
+
+        count++;
+        log_debug("%s: count=%u", __func__, count);
+
+        switch (count) {
+        case 1: {
+                _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL;
+                receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request);
+
+                verify_anonymized_request(request, DHCP_DISCOVER);
+
+                _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL;
+                create_reply(client, request, DHCP_OFFER, &reply);
+
+                send_message(fd, /* raw= */ true, client, reply);
+                break;
+        }
+        case 2: {
+                _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL;
+                receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request);
+
+                /* REQUEST (selecting) */
+                verify_anonymized_request(request, DHCP_REQUEST);
+                verify_request_server_address(request);
+
+                _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL;
+                create_reply(client, request, DHCP_ACK, &reply);
+
+                send_message(fd, /* raw= */ true, client, reply);
+                break;
+        }
+        default:
+                assert_not_reached();
+        }
+
+        return 0;
+}
+
+static int anonymize_client_handler(sd_dhcp_client *client, int event, void *userdata) {
+        sd_event *e = ASSERT_PTR(userdata);
+        static unsigned count = 0;
+
+        count++;
+        log_debug("%s: count=%u, event=%i", __func__, count, event);
+
+        switch (count) {
+        case 1:
+                ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING);
+                verify_reply(client, DHCP_STATE_SELECTING);
+                break;
+        case 2:
+                ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE);
+                verify_reply(client, DHCP_STATE_BOUND);
+                ASSERT_OK(sd_event_exit(e, 0));
+                break;
+        default:
+                assert_not_reached();
+        }
+
+        return 0;
+}
+
+TEST(anonymize) {
+        _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
+        setup(anonymize_io_handler, anonymize_client_handler, &client);
+        ASSERT_OK(sd_dhcp_client_set_anonymize(client, true));
+        ASSERT_OK(sd_dhcp_client_start(client));
+        ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client)));
+}
+
 static int intro(void) {
         ASSERT_OK_ERRNO(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", /* overwrite= */ true));
         return 0;