From a0ae95c9be347c1e7fbfe4977f35bfd92775eeb6 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Mon, 9 Dec 2013 23:43:12 +0200 Subject: [PATCH] dhcp: Add buffer length and invalid cookie tests for DHCP options Create an initial simple test program for these two cases. --- src/libsystemd-dhcp/test-dhcp-option.c | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/libsystemd-dhcp/test-dhcp-option.c diff --git a/src/libsystemd-dhcp/test-dhcp-option.c b/src/libsystemd-dhcp/test-dhcp-option.c new file mode 100644 index 000000000..cde8c2922 --- /dev/null +++ b/src/libsystemd-dhcp/test-dhcp-option.c @@ -0,0 +1,51 @@ + +#include +#include +#include +#include +#include + +#include + +#include "dhcp-protocol.h" +#include "dhcp-internal.h" + +static void test_invalid_buffer_length(void) +{ + DHCPMessage message; + + assert(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL); + assert(dhcp_option_parse(&message, sizeof(DHCPMessage), NULL, NULL) + == -EINVAL); +} + +static void test_cookie(void) +{ + DHCPMessage *message; + size_t len = sizeof(DHCPMessage) + 4; + uint8_t *opt; + + message = malloc0(len); + + opt = (uint8_t *)(message + 1); + opt[0] = 0xff; + + assert(dhcp_option_parse(message, len, NULL, NULL) == -EINVAL); + + opt[0] = 99; + opt[1] = 130; + opt[2] = 83; + opt[3] = 99; + + assert(dhcp_option_parse(message, len, NULL, NULL) == -ENOMSG); + + free(message); +} + +int main(int argc, char *argv[]) +{ + test_invalid_buffer_length(); + test_cookie(); + + return 0; +} -- 2.30.2