diff options
-rw-r--r-- | src/libsystemd-network/test-dhcp-client.c | 4 | ||||
-rw-r--r-- | src/libsystemd-network/test-dhcp-option.c | 47 |
2 files changed, 26 insertions, 25 deletions
diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index a208b0d1e8..71b06b17a9 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -152,7 +152,7 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, assert_se(s >= 0); assert_se(packet); - size = sizeof(DHCPPacket) + 4; + size = sizeof(DHCPPacket); assert_se(len > size); discover = memdup(packet, len); @@ -338,7 +338,7 @@ static uint8_t test_addr_acq_ack[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00, - 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff, + 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f, 0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01, diff --git a/src/libsystemd-network/test-dhcp-option.c b/src/libsystemd-network/test-dhcp-option.c index 8659fd5025..35db8c1c0a 100644 --- a/src/libsystemd-network/test-dhcp-option.c +++ b/src/libsystemd-network/test-dhcp-option.c @@ -78,29 +78,35 @@ static void test_invalid_buffer_length(void) DHCPMessage message; assert_se(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL); - assert_se(dhcp_option_parse(&message, sizeof(DHCPMessage), NULL, NULL) + assert_se(dhcp_option_parse(&message, sizeof(DHCPMessage) - 1, NULL, NULL) == -EINVAL); } -static void test_cookie(void) +static void test_message_init(void) { - _cleanup_free_ DHCPMessage *message; - size_t len = sizeof(DHCPMessage) + 4; - uint8_t *opt; + _cleanup_free_ DHCPMessage *message = NULL; + size_t optlen = 3; + size_t len = sizeof(DHCPMessage) + optlen; + uint8_t *opt, *magic; message = malloc0(len); opt = (uint8_t *)(message + 1); - opt[0] = 0xff; - assert_se(dhcp_option_parse(message, len, NULL, NULL) == -EINVAL); + assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678, + DHCP_DISCOVER, &opt, &optlen) >= 0); + + assert_se(message->xid == htobe32(0x12345678)); + assert_se(message->op == BOOTREQUEST); - opt[0] = 99; - opt[1] = 130; - opt[2] = 83; - opt[3] = 99; + magic = (uint8_t*)&message->magic; - assert_se(dhcp_option_parse(message, len, NULL, NULL) == -ENOMSG); + assert_se(magic[0] == 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, @@ -108,19 +114,14 @@ static DHCPMessage *create_message(uint8_t *options, uint16_t optlen, uint8_t *sname, uint8_t snamelen) { DHCPMessage *message; - size_t len = sizeof(DHCPMessage) + 4 + optlen; + size_t len = sizeof(DHCPMessage) + optlen; uint8_t *opt; message = malloc0(len); opt = (uint8_t *)(message + 1); - opt[0] = 99; - opt[1] = 130; - opt[2] = 83; - opt[3] = 99; - if (options && optlen) - memcpy(&opt[4], options, optlen); + memcpy(opt, options, optlen); if (file && filelen <= 128) memcpy(&message->file, file, filelen); @@ -248,7 +249,7 @@ static void test_options(struct option_desc *desc) int filelen = 0; int snamelen = 0; int buflen = 0; - _cleanup_free_ DHCPMessage *message; + _cleanup_free_ DHCPMessage *message = NULL; int res; if (desc) { @@ -267,9 +268,9 @@ static void test_options(struct option_desc *desc) desc->pos = 0; } message = create_message(options, optlen, file, filelen, - sname, snamelen); + sname, snamelen); - buflen = sizeof(DHCPMessage) + 4 + optlen; + buflen = sizeof(DHCPMessage) + optlen; if (!desc) { assert_se((res = dhcp_option_parse(message, buflen, @@ -365,7 +366,7 @@ int main(int argc, char *argv[]) unsigned int i; test_invalid_buffer_length(); - test_cookie(); + test_message_init(); test_options(NULL); |