[systemd-devel] [PATCH 06/28] dhcp: Add buffer length and invalid cookie tests for DHCP options

Patrik Flykt patrik.flykt at linux.intel.com
Wed Nov 13 13:22:34 PST 2013


Create an initial simple test program for these two cases.
---
 src/dhcp/test-dhcp-option.c |   54 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 src/dhcp/test-dhcp-option.c

diff --git a/src/dhcp/test-dhcp-option.c b/src/dhcp/test-dhcp-option.c
new file mode 100644
index 0000000..25cb17c
--- /dev/null
+++ b/src/dhcp/test-dhcp-option.c
@@ -0,0 +1,54 @@
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+#include <assert.h>
+
+#include <util.h>
+
+#include "protocol.h"
+#include "internal.h"
+
+static void test_invalid_buffer_length(void)
+{
+        struct dhcp_message message;
+
+        assert(__dhcp_option_parse(&message, INT32_MIN, NULL, NULL) ==
+                        -EINVAL);
+        assert(__dhcp_option_parse(&message, sizeof(struct dhcp_message),
+                                        NULL, NULL) == -EINVAL);
+}
+
+static void test_cookie(void)
+{
+        struct dhcp_message *message;
+        uint32_t len = sizeof(struct dhcp_message) + 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[])
+{
+        unsigned int i;
+
+        test_invalid_buffer_length();
+        test_cookie();
+
+        return 0;
+}
-- 
1.7.10.4



More information about the systemd-devel mailing list