/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
  Copyright © 2013 Intel Corporation. All rights reserved.
***/

#include <net/if_arp.h>

#include "sd-dhcp-server.h"
#include "sd-event.h"

#include "dhcp-server-internal.h"
#include "dhcp-server-request.h"
#include "fd-util.h"
#include "iovec-util.h"
#include "tests.h"

TEST(basic) {
        struct in_addr address_lo = {
                .s_addr = htobe32(INADDR_LOOPBACK),
        };
        struct in_addr address_any = {
                .s_addr = htobe32(INADDR_ANY),
        };

        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
        ASSERT_OK(sd_event_new(&event));

        _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
        ASSERT_OK(sd_dhcp_server_new(&server, 4242));
        ASSERT_NOT_NULL(server);

        ASSERT_OK(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL), EBUSY));
        ASSERT_PTR_EQ(sd_dhcp_server_get_event(server), event);
        ASSERT_OK(sd_dhcp_server_detach_event(server));
        ASSERT_NULL(sd_dhcp_server_get_event(server));
        ASSERT_OK(sd_dhcp_server_attach_event(server, NULL, SD_EVENT_PRIORITY_NORMAL));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_attach_event(server, NULL, SD_EVENT_PRIORITY_NORMAL), EBUSY));

        ASSERT_TRUE(sd_dhcp_server_ref(server) == server);
        ASSERT_NULL(sd_dhcp_server_unref(server));

        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_start(server), EUNATCH));

        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_any, 28, 0, 0), EINVAL));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_lo, 38, 0, 0), ERANGE));
        ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
        ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_any, 8, 0, 1), EINVAL));
        ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 1));

        _cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd));

        server->socket_fd = TAKE_FD(socket_fd[0]);

        ASSERT_OK(sd_dhcp_server_start(server));
        ASSERT_OK(sd_dhcp_server_start(server));
        ASSERT_OK(sd_dhcp_server_stop(server));
        ASSERT_OK(sd_dhcp_server_stop(server));
        ASSERT_OK(sd_dhcp_server_start(server));
}

TEST(dhcp_server_process_message) {
        struct {
                DHCPMessageHeader header;
                struct {
                        uint8_t code;
                        uint8_t length;
                        uint8_t type;
                } _packed_ option_type;
                struct {
                        uint8_t code;
                        uint8_t length;
                        be32_t address;
                } _packed_ option_requested_ip;
                struct {
                        uint8_t code;
                        uint8_t length;
                        be32_t address;
                } _packed_ option_server_id;
                struct {
                        uint8_t code;
                        uint8_t length;
                        uint8_t id[7];
                } _packed_ option_client_id;
                struct {
                        uint8_t code;
                        uint8_t length;
                        uint8_t hostname[6];
                } _packed_ option_hostname;
                uint8_t end;
        } _packed_ test = {
                .header.op = BOOTREQUEST,
                .header.htype = ARPHRD_ETHER,
                .header.hlen = ETHER_ADDR_LEN,
                .header.xid = htobe32(0x12345678),
                .header.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
                .header.magic = htobe32(DHCP_MAGIC_COOKIE),
                .option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE,
                .option_type.length = 1,
                .option_type.type = DHCP_DISCOVER,
                .option_hostname.code = SD_DHCP_OPTION_HOST_NAME,
                .option_hostname.length = 6,
                .option_hostname.hostname = { 'T', 'E', 'S', 'T', 'H', 'N' },
                .end = SD_DHCP_OPTION_END,
        };
        struct in_addr address_lo = {
                .s_addr = htobe32(INADDR_LOOPBACK),
        };
        struct in_addr static_lease_address = {
                .s_addr = htobe32(INADDR_LOOPBACK + 42),
        };
        static uint8_t static_lease_client_id[7] = {0x01, 'A', 'B', 'C', 'D', 'E', 'G' };

        _cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd));

        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
        ASSERT_OK(sd_event_new(&event));

        _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
        ASSERT_OK(sd_dhcp_server_new(&server, 4242));
        ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &static_lease_address,
                        static_lease_client_id,
                        ELEMENTSOF(static_lease_client_id),
                        /* hostname= */ NULL));
        ASSERT_OK(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL));
        server->socket_fd = TAKE_FD(socket_fd[0]);
        ASSERT_OK(sd_dhcp_server_start(server));

        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        test.end = 0;
        /* TODO, shouldn't this fail? */
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
        test.end = SD_DHCP_OPTION_END;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        test.option_type.code = 0;
        test.option_type.length = 0;
        test.option_type.type = 0;
        ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), ENODATA);
        test.option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE;
        test.option_type.length = 1;
        test.option_type.type = DHCP_DISCOVER;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        test.header.op = 0;
        ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
        test.header.op = BOOTREQUEST;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        /* Neither client ID nor hardware type is set. There is no way to manage the bound lease for the request. */
        test.header.htype = 0;
        ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
        test.header.htype = ARPHRD_ETHER;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        test.header.hlen = 0;
        ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
        test.header.hlen = ETHER_ADDR_LEN;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        /* DHCPREQUEST (init-boot) without requested IP */
        test.option_type.type = DHCP_REQUEST;
        ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), ENODATA);

        test.option_requested_ip.code = SD_DHCP_OPTION_REQUESTED_IP_ADDRESS;
        test.option_requested_ip.length = 4;
        test.option_requested_ip.address = htobe32(0x12345678);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
        test.option_server_id.code = SD_DHCP_OPTION_SERVER_IDENTIFIER;
        test.option_server_id.length = 4;
        test.option_server_id.address = htobe32(INADDR_LOOPBACK);
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        test.option_server_id.address = htobe32(0x12345678);
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
        ASSERT_OK_ZERO(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL));
        test.option_server_id.address = htobe32(INADDR_LOOPBACK);
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        test.option_client_id.code = SD_DHCP_OPTION_CLIENT_IDENTIFIER;
        test.option_client_id.length = 7;
        test.option_client_id.id[0] = 0x01;
        test.option_client_id.id[1] = 'A';
        test.option_client_id.id[2] = 'B';
        test.option_client_id.id[3] = 'C';
        test.option_client_id.id[4] = 'D';
        test.option_client_id.id[5] = 'E';
        test.option_client_id.id[6] = 'F';
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        /* add the static lease for the client ID */
        ASSERT_OK(sd_dhcp_server_stop(server));
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = htobe32(INADDR_LOOPBACK + 31) },
                        (uint8_t[7]) { 0x01, 'A', 'B', 'C', 'D', 'E', 'F' },
                        7,
                        /* hostname= */ NULL));
        ASSERT_OK(sd_dhcp_server_start(server));

        /* discover */
        test.option_type.type = DHCP_DISCOVER;
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);

        /* request neither bound nor static address */
        test.option_type.type = DHCP_REQUEST;
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 29);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);

        /* request the currently assigned address */
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);

        /* request the new static address */
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 31);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        /* release the bound static lease */
        test.header.ciaddr = htobe32(INADDR_LOOPBACK + 31);
        test.option_type.type = DHCP_RELEASE;
        ASSERT_OK_ZERO(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL));

        /* drop the static lease for the client ID */
        ASSERT_OK(sd_dhcp_server_stop(server));
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        /* address= */ NULL,
                        (uint8_t[7]) { 0x01, 'A', 'B', 'C', 'D', 'E', 'F' },
                        7,
                        /* hostname= */ NULL));
        ASSERT_OK(sd_dhcp_server_start(server));

        /* request a new non-static address */
        test.header.ciaddr = 0;
        test.option_type.type = DHCP_REQUEST;
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 29);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        /* request address reserved for static lease (unmatching client ID) */
        test.option_client_id.id[6] = 'H';
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);

        /* request unmatching address */
        test.option_client_id.id[6] = 'G';
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 41);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);

        /* request matching address */
        test.option_client_id.id[6] = 'G';
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);

        /* try again */
        test.option_client_id.id[6] = 'G';
        test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
        ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
}

TEST(sd_dhcp_server_set_static_lease) {
        _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
        ASSERT_OK(sd_dhcp_server_new(&server, 4242));

        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x01020304 },
                        (uint8_t *) &(uint32_t) { 0x01020304 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Duplicated entry. */
        ASSERT_ERROR(sd_dhcp_server_set_static_lease(
                                     server,
                                     &(struct in_addr) { .s_addr = 0x01020304 },
                                     (uint8_t *) &(uint32_t) { 0x01020304 },
                                     sizeof(uint32_t),
                                     /* hostname= */ NULL),
                     EEXIST);
        /* Address is conflicted. */
        ASSERT_ERROR(sd_dhcp_server_set_static_lease(
                                     server,
                                     &(struct in_addr) { .s_addr = 0x01020304 },
                                     (uint8_t *) &(uint32_t) { 0x01020305 },
                                     sizeof(uint32_t),
                                     /* hostname= */ NULL),
                     EEXIST);
        /* Client ID is conflicted. */
        ASSERT_ERROR(sd_dhcp_server_set_static_lease(
                                     server,
                                     &(struct in_addr) { .s_addr = 0x01020305 },
                                     (uint8_t *) &(uint32_t) { 0x01020304 },
                                     sizeof(uint32_t),
                                     /* hostname= */ NULL),
                     EEXIST);

        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x01020305 },
                        (uint8_t *) &(uint32_t) { 0x01020305 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Remove the previous entry. */
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x00000000 },
                        (uint8_t *) &(uint32_t) { 0x01020305 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Then, set a different address. */
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x01020306 },
                        (uint8_t *) &(uint32_t) { 0x01020305 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Remove again. */
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x00000000 },
                        (uint8_t *) &(uint32_t) { 0x01020305 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Try to remove non-existent entry. */
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x00000000 },
                        (uint8_t *) &(uint32_t) { 0x01020305 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
        /* Try to remove non-existent entry. */
        ASSERT_OK(sd_dhcp_server_set_static_lease(
                        server,
                        &(struct in_addr) { .s_addr = 0x00000000 },
                        (uint8_t *) &(uint32_t) { 0x01020306 },
                        sizeof(uint32_t),
                        /* hostname= */ NULL));
}

TEST(sd_dhcp_server_set_domain_name) {
        _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
        ASSERT_OK(sd_dhcp_server_new(&server, 4242));

        /* Test setting domain name */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "example.com"));

        /* Test setting same domain name (should return 0 - no change) */
        ASSERT_OK_ZERO(sd_dhcp_server_set_domain_name(server, "example.com"));

        /* Test changing domain name */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "test.local"));

        /* Test clearing domain name */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, NULL));

        /* Test clearing again (should return 0 - already cleared) */
        ASSERT_OK_ZERO(sd_dhcp_server_set_domain_name(server, NULL));

        /* Test invalid domain name */
        ASSERT_ERROR(sd_dhcp_server_set_domain_name(server, "invalid..domain"), EINVAL);

        /* Test empty string (treated differently from NULL) */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, ""));

        /* Test clearing domain name with NULL */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, NULL));

        /* Test valid domain with subdomain */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "sub.example.com"));

        /* Test single-label domain */
        ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "local"));
}

DEFINE_TEST_MAIN(LOG_DEBUG);
