]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd-network/test-dhcp-client.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / libsystemd-network / test-dhcp-client.c
index 831ec190592985c5ed01e440dbc93c705a6d673e..1200a7c251a526aef0189051dfd94a238847e5e5 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <stdlib.h>
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
-#include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>
 
-#include "util.h"
-#include "socket-util.h"
+#include "sd-dhcp-client.h"
 #include "sd-event.h"
-#include "event-util.h"
 
+#include "alloc-util.h"
 #include "dhcp-identifier.h"
-#include "dhcp-protocol.h"
 #include "dhcp-internal.h"
-#include "sd-dhcp-client.h"
+#include "dhcp-protocol.h"
+#include "event-util.h"
+#include "fd-util.h"
+#include "util.h"
 
 static uint8_t mac_addr[] = {'A', 'B', 'C', '1', '2', '3'};
 
@@ -47,16 +45,13 @@ static test_callback_recv_t callback_recv;
 static be32_t xid;
 static sd_event_source *test_hangcheck;
 
-static int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec,
-                               void *userdata)
-{
+static int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec, void *userdata) {
         assert_not_reached("Test case should have completed in 2 seconds");
 
         return 0;
 }
 
-static void test_request_basic(sd_event *e)
-{
+static void test_request_basic(sd_event *e) {
         int r;
 
         sd_dhcp_client *client;
@@ -91,10 +86,7 @@ static void test_request_basic(sd_event *e)
         assert_se(sd_dhcp_client_set_request_option(client,
                                         DHCP_OPTION_DOMAIN_NAME) == -EEXIST);
         assert_se(sd_dhcp_client_set_request_option(client,
-                                        DHCP_OPTION_DOMAIN_NAME_SERVER)
-                        == -EEXIST);
-        assert_se(sd_dhcp_client_set_request_option(client,
-                                        DHCP_OPTION_NTP_SERVER) == -EEXIST);
+                                        DHCP_OPTION_DOMAIN_NAME_SERVER) == -EEXIST);
 
         assert_se(sd_dhcp_client_set_request_option(client,
                                         DHCP_OPTION_PAD) == -EINVAL);
@@ -116,8 +108,7 @@ static void test_request_basic(sd_event *e)
         sd_dhcp_client_unref(client);
 }
 
-static void test_checksum(void)
-{
+static void test_checksum(void) {
         uint8_t buf[20] = {
                 0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
                 0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -130,9 +121,7 @@ static void test_checksum(void)
         assert_se(dhcp_packet_checksum((uint8_t*)&buf, 20) == be16toh(0x78ae));
 }
 
-static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
-                void *user_data)
-{
+static int check_options(uint8_t code, uint8_t len, const void *option, void *userdata) {
         switch(code) {
         case DHCP_OPTION_CLIENT_IDENTIFIER:
         {
@@ -145,10 +134,10 @@ static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
 
                 assert_se(len == sizeof(uint8_t) + sizeof(uint32_t) + duid_len);
                 assert_se(len == 19);
-                assert_se(option[0] == 0xff);
+                assert_se(((uint8_t*) option)[0] == 0xff);
 
-                assert_se(memcmp(&option[1], &iaid, sizeof(iaid)) == 0);
-                assert_se(memcmp(&option[5], &duid, duid_len) == 0);
+                assert_se(memcmp((uint8_t*) option + 1, &iaid, sizeof(iaid)) == 0);
+                assert_se(memcmp((uint8_t*) option + 5, &duid, duid_len) == 0);
                 break;
         }
 
@@ -159,9 +148,7 @@ static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
         return 0;
 }
 
-int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
-                                 const void *packet, size_t len)
-{
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, const void *packet, size_t len) {
         size_t size;
         _cleanup_free_ DHCPPacket *discover;
         uint16_t ip_check, udp_check;
@@ -206,18 +193,20 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
         return 575;
 }
 
-int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link,
-                                 uint32_t id, const uint8_t *addr,
-                                 size_t addr_len, uint16_t arp_type)
-{
+int dhcp_network_bind_raw_socket(
+                int index,
+                union sockaddr_union *link,
+                uint32_t id,
+                const uint8_t *addr, size_t addr_len,
+                uint16_t arp_type) {
+
         if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0)
                 return -errno;
 
         return test_fd[0];
 }
 
-int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
-{
+int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
         int fd;
 
         fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
@@ -227,14 +216,11 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
         return fd;
 }
 
-int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
-                                 const void *packet, size_t len)
-{
+int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port, const void *packet, size_t len) {
         return 0;
 }
 
-static int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp)
-{
+static int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp) {
         int res;
 
         res = dhcp_option_parse(dhcp, size, check_options, NULL);
@@ -246,8 +232,7 @@ static int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp)
         return 0;
 }
 
-static void test_discover_message(sd_event *e)
-{
+static void test_discover_message(sd_event *e) {
         sd_dhcp_client *client;
         int res, r;
 
@@ -377,7 +362,7 @@ static void test_addr_acq_acquired(sd_dhcp_client *client, int event,
         struct in_addr addr;
 
         assert_se(client);
-        assert_se(event == DHCP_EVENT_IP_ACQUIRE);
+        assert_se(event == SD_DHCP_CLIENT_EVENT_IP_ACQUIRE);
 
         assert_se(sd_dhcp_client_get_lease(client, &lease) >= 0);
         assert_se(lease);
@@ -397,7 +382,6 @@ static void test_addr_acq_acquired(sd_dhcp_client *client, int event,
         if (verbose)
                 printf("  DHCP address acquired\n");
 
-        sd_dhcp_lease_unref(lease);
         sd_event_exit(e, 0);
 }
 
@@ -521,5 +505,12 @@ int main(int argc, char *argv[]) {
         test_discover_message(e);
         test_addr_acq(e);
 
+#ifdef VALGRIND
+        /* Make sure the async_close thread has finished.
+         * valgrind would report some of the phread_* structures
+         * as not cleaned up properly. */
+        sleep(1);
+#endif
+
         return 0;
 }