]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd-network/test-dhcp-option.c
Merge pull request #1527 from keszybz/lz4
[thirdparty/systemd.git] / src / libsystemd-network / test-dhcp-option.c
index eaf6a535e6fe21bcdd7e48e4c99ae7e5fbfae785..b1ef1748490732865efc4a457534ad0f37bab630 100644 (file)
@@ -1,9 +1,9 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 #include <stdio.h>
 #include <stdbool.h>
 #include <errno.h>
 #include <string.h>
-#include <assert.h>
 
 #include "util.h"
 #include "macro.h"
@@ -51,8 +51,7 @@ static struct option_desc option_tests[] = {
             DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
 };
 
-static const char *dhcp_type(int type)
-{
+static const char *dhcp_type(int type) {
         switch(type) {
         case DHCP_DISCOVER:
                 return "DHCPDISCOVER";
@@ -73,8 +72,7 @@ static const char *dhcp_type(int type)
         }
 }
 
-static void test_invalid_buffer_length(void)
-{
+static void test_invalid_buffer_length(void) {
         DHCPMessage message;
 
         assert_se(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL);
@@ -82,17 +80,16 @@ static void test_invalid_buffer_length(void)
                == -EINVAL);
 }
 
-static void test_message_init(void)
-{
+static void test_message_init(void) {
         _cleanup_free_ DHCPMessage *message = NULL;
-        size_t optlen = 3, optoffset;
+        size_t optlen = 4, optoffset;
         size_t len = sizeof(DHCPMessage) + optlen;
         uint8_t *magic;
 
         message = malloc0(len);
 
         assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
-                  DHCP_DISCOVER, message->options, optlen, &optoffset) >= 0);
+                  DHCP_DISCOVER, ARPHRD_ETHER, optlen, &optoffset) >= 0);
 
         assert_se(message->xid == htobe32(0x12345678));
         assert_se(message->op == BOOTREQUEST);
@@ -100,21 +97,21 @@ static void test_message_init(void)
         magic = (uint8_t*)&message->magic;
 
         assert_se(magic[0] == 99);
-        assert_se(magic[1] = 130);
-        assert_se(magic[2] = 83);
-        assert_se(magic[3] = 99);
+        assert_se(magic[1] == 130);
+        assert_se(magic[2] == 83);
+        assert_se(magic[3] == 99);
 
         assert_se(dhcp_option_parse(message, len, NULL, NULL) >= 0);
 }
 
 static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
                 uint8_t *file, uint8_t filelen,
-                uint8_t *sname, uint8_t snamelen)
-{
+                uint8_t *sname, uint8_t snamelen) {
         DHCPMessage *message;
         size_t len = sizeof(DHCPMessage) + optlen;
 
         message = malloc0(len);
+        assert_se(message);
 
         if (options && optlen)
                 memcpy(&message->options, options, optlen);
@@ -128,8 +125,9 @@ static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
         return message;
 }
 
-static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen)
-{
+static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen) {
+        assert(*descpos >= 0);
+
         while (*descpos < *desclen) {
                 switch(descoption[*descpos]) {
                 case DHCP_OPTION_PAD:
@@ -147,10 +145,8 @@ static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen)
         }
 }
 
-static int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option,
-                           void *user_data)
-{
-        struct option_desc *desc = user_data;
+static int test_options_cb(uint8_t code, uint8_t len, const void *option, void *userdata) {
+        struct option_desc *desc = userdata;
         uint8_t *descoption = NULL;
         int *desclen = NULL, *descpos = NULL;
         uint8_t optcode = 0;
@@ -211,10 +207,10 @@ static int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option,
         for (i = 0; i < len; i++) {
 
                 if (verbose)
-                        printf("0x%02x(0x%02x) ", option[i],
+                        printf("0x%02x(0x%02x) ", ((uint8_t*) option)[i],
                                         descoption[*descpos + 2 + i]);
 
-                assert_se(option[i] == descoption[*descpos + 2 + i]);
+                assert_se(((uint8_t*) option)[i] == descoption[*descpos + 2 + i]);
         }
 
         if (verbose)
@@ -236,8 +232,7 @@ static int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option,
         return 0;
 }
 
-static void test_options(struct option_desc *desc)
-{
+static void test_options(struct option_desc *desc) {
         uint8_t *options = NULL;
         uint8_t *file = NULL;
         uint8_t *sname = NULL;
@@ -287,10 +282,6 @@ static void test_options(struct option_desc *desc)
                 printf("DHCP type %s\n", dhcp_type(res));
 }
 
-static uint8_t result[64] = {
-        'A', 'B', 'C', 'D',
-};
-
 static uint8_t options[64] = {
         'A', 'B', 'C', 'D',
         160, 2, 0x11, 0x12,
@@ -302,26 +293,35 @@ static uint8_t options[64] = {
         255
 };
 
-static void test_option_set(void)
-{
+static void test_option_set(void) {
+        _cleanup_free_ DHCPMessage *result = NULL;
         size_t offset = 0, len, pos;
         unsigned i;
 
-        assert_se(dhcp_option_append(result, 0, &offset, DHCP_OPTION_PAD,
-                                  0, NULL) == -ENOBUFS);
+        result = malloc0(sizeof(DHCPMessage) + 11);
+        assert_se(result);
+
+        result->options[0] = 'A';
+        result->options[1] = 'B';
+        result->options[2] = 'C';
+        result->options[3] = 'D';
+
+        assert_se(dhcp_option_append(result, 0, &offset, 0, DHCP_OPTION_PAD,
+                                     0, NULL) == -ENOBUFS);
         assert_se(offset == 0);
 
         offset = 4;
-        assert_se(dhcp_option_append(result, 1, &offset, DHCP_OPTION_PAD,
-                                    0, NULL) >= 0);
+        assert_se(dhcp_option_append(result, 5, &offset, 0, DHCP_OPTION_PAD,
+                                     0, NULL) == -ENOBUFS);
+        assert_se(offset == 4);
+        assert_se(dhcp_option_append(result, 6, &offset, 0, DHCP_OPTION_PAD,
+                                     0, NULL) >= 0);
         assert_se(offset == 5);
 
         offset = pos = 4;
-        len = 60;
-        while (pos < 64 && options[pos] != DHCP_OPTION_END) {
-                offset = pos;
-
-                assert_se(dhcp_option_append(result, len, &offset,
+        len = 11;
+        while (pos < len && options[pos] != DHCP_OPTION_END) {
+                assert_se(dhcp_option_append(result, len, &offset, DHCP_OVERLOAD_SNAME,
                                              options[pos],
                                              options[pos + 1],
                                              &options[pos + 2]) >= 0);
@@ -331,23 +331,42 @@ static void test_option_set(void)
                 else
                         pos += 2 + options[pos + 1];
 
-                assert_se(offset == pos);
+                if (pos < len)
+                        assert_se(offset == pos);
         }
 
-        for (i = 0; i < pos; i++) {
+        for (i = 0; i < 9; i++) {
                 if (verbose)
-                        printf("%2d: 0x%02x(0x%02x)\n", i, result[i],
+                        printf("%2u: 0x%02x(0x%02x) (options)\n", i, result->options[i],
                                options[i]);
-                assert_se(result[i] == options[i]);
+                assert_se(result->options[i] == options[i]);
+        }
+
+        if (verbose)
+                printf("%2d: 0x%02x(0x%02x) (options)\n", 9, result->options[9],
+                       DHCP_OPTION_END);
+
+        assert_se(result->options[9] == DHCP_OPTION_END);
+
+        if (verbose)
+                printf("%2d: 0x%02x(0x%02x) (options)\n", 10, result->options[10],
+                       DHCP_OPTION_PAD);
+
+        assert_se(result->options[10] == DHCP_OPTION_PAD);
+
+        for (i = 0; i < pos - 8; i++) {
+                if (verbose)
+                        printf("%2u: 0x%02x(0x%02x) (sname)\n", i, result->sname[i],
+                               options[i + 9]);
+                assert_se(result->sname[i] == options[i + 9]);
         }
 
         if (verbose)
                 printf ("\n");
 }
 
-int main(int argc, char *argv[])
-{
-        unsigned int i;
+int main(int argc, char *argv[]) {
+        unsigned i;
 
         test_invalid_buffer_length();
         test_message_init();