]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add some DHCP unit tests
authorAlan T. DeKok <aland@freeradius.org>
Mon, 15 Jun 2015 15:40:29 +0000 (11:40 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 15 Jun 2015 15:40:48 +0000 (11:40 -0400)
src/main/radattr.c
src/main/radattr.mk
src/tests/unit/all.mk
src/tests/unit/dhcp.txt [new file with mode: 0644]

index c658a60bd609c019fc3138ec31f410c4f540a697..ec901c78e06ab206d18b493221a0878e15d51894 100644 (file)
@@ -30,6 +30,7 @@ typedef struct REQUEST REQUEST;
 #include <freeradius-devel/xlat.h>
 #include <freeradius-devel/conf.h>
 #include <freeradius-devel/radpaths.h>
+#include <freeradius-devel/dhcp.h>
 
 #include <ctype.h>
 
@@ -589,10 +590,11 @@ static void process_file(const char *root_dir, char const *filename)
 
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
                char *p = strchr(buffer, '\n');
-               VALUE_PAIR *vp, *head = NULL;
+               VALUE_PAIR *vp, *head;
                VALUE_PAIR **tail = &head;
 
                lineno++;
+               head = NULL;
 
                if (!p) {
                        if (!feof(fp)) {
@@ -759,6 +761,90 @@ static void process_file(const char *root_dir, char const *filename)
                        continue;
                }
 
+               /*
+                *      And some DHCP tests
+                */
+               if (strncmp(p, "encode-dhcp ", 12) == 0) {
+                       vp_cursor_t cursor;
+
+                       if (strcmp(p + 12, "-") == 0) {
+                               p = output;
+                       } else {
+                               p += 12;
+                       }
+
+                       if (userparse(NULL, p, &head) != T_EOL) {
+                               strlcpy(output, fr_strerror(), sizeof(output));
+                               continue;
+                       }
+
+                       fr_cursor_init(&cursor, &head);
+
+
+                       attr = data;
+                       vp = head;
+
+                       while ((vp = fr_cursor_current(&cursor))) {
+                               len = fr_dhcp_encode_option(NULL, attr, data + sizeof(data) - attr, &cursor);
+                               if (len < 0) {
+                                       fprintf(stderr, "Failed encoding %s: %s\n",
+                                               vp->da->name, fr_strerror());
+                                       exit(1);
+                               }
+                               if (len > 0) debug_pair(vp);
+                               attr += len;
+                       };
+
+                       pairfree(&head);
+                       outlen = attr - data;
+                       goto print_hex;
+               }
+
+               if (strncmp(p, "decode-dhcp ", 12) == 0) {
+                       ssize_t my_len;
+
+                       if (strcmp(p + 12, "-") == 0) {
+                               attr = data;
+                               len = data_len;
+                       } else {
+                               attr = data;
+                               len = encode_hex(p + 12, data, sizeof(data));
+                               if (len == 0) {
+                                       fprintf(stderr, "Failed decoding hex string at line %d of %s\n", lineno, directory);
+                                       exit(1);
+                               }
+                       }
+
+                       my_len = fr_dhcp_decode_options(NULL, &head, attr, len);
+
+                       /*
+                        *      Output may be an error, and we ignore
+                        *      it if so.
+                        */
+                       if (head) {
+                               vp_cursor_t cursor;
+                               p = output;
+                               for (vp = fr_cursor_init(&cursor, &head);
+                                    vp;
+                                    vp = fr_cursor_next(&cursor)) {
+                                       vp_prints(p, sizeof(output) - (p - output), vp);
+                                       p += strlen(p);
+
+                                       if (vp->next) {strcpy(p, ", ");
+                                               p += 2;
+                                       }
+                               }
+
+                               pairfree(&head);
+                       } else if (my_len < 0) {
+                               strlcpy(output, fr_strerror(), sizeof(output));
+
+                       } else { /* zero-length attribute */
+                               *output = '\0';
+                       }
+                       continue;
+               }
+
                if (strncmp(p, "attribute ", 10) == 0) {
                        p += 10;
 
index db880d2b6ecbb3ec2df98a1bdbb1f8d45732189c..7a8a034c7fb1c209d3958ef85e123882c134a6d5 100644 (file)
@@ -1,5 +1,5 @@
 TARGET         := radattr
 SOURCES                := radattr.c
 
-TGT_PREREQS    := libfreeradius-server.a libfreeradius-radius.a
+TGT_PREREQS    := libfreeradius-server.a libfreeradius-radius.a libfreeradius-dhcp.a
 TGT_LDLIBS     := $(LIBS)
index 3827842ec21e3d55d78262d2ea34d5e2971a4080..d54a0ce708eae75bf7eec353b24c4185c25d0091 100644 (file)
@@ -7,7 +7,7 @@
 #  functionality from earlier tests.
 #
 FILES  := rfc.txt errors.txt extended.txt lucent.txt wimax.txt \
-       escape.txt condition.txt xlat.txt vendor.txt
+       escape.txt condition.txt xlat.txt vendor.txt dhcp.txt
 
 #
 #  Create the output directory
@@ -16,13 +16,27 @@ FILES  := rfc.txt errors.txt extended.txt lucent.txt wimax.txt \
 $(BUILD_DIR)/tests/unit:
        @mkdir -p $@
 
+.PHONY: $(BUILD_DIR)/share
+$(BUILD_DIR)/share:
+       @mkdir -p $@
+
+#
+#  We need $INCLUDE in the output file, so we pass 2 parameters to 'echo'
+#  No idea how portable that is...
+#
+$(BUILD_DIR)/share/dictionary: $(top_srcdir)/share/dictionary $(top_srcdir)/share/dictionary.dhcp | $(BUILD_DIR)/share
+       @rm -f $@
+       @for x in $^; do \
+               echo '$$INCLUDE ' "$$x" >> $@; \
+       done
+
 #
 #  Files in the output dir depend on the unit tests
 #
-$(BUILD_DIR)/tests/unit/%: $(DIR)/% $(TESTBINDIR)/radattr | $(BUILD_DIR)/tests/unit
+$(BUILD_DIR)/tests/unit/%: $(DIR)/% $(TESTBINDIR)/radattr $(BUILD_DIR)/share | $(BUILD_DIR)/tests/unit
        @echo UNIT-TEST $(notdir $@)
-       @if ! $(TESTBIN)/radattr -D $(top_srcdir)/share $<; then \
-               echo "$(TESTBIN)/radattr -D $(top_srcdir)/share $<"; \
+       @if ! $(TESTBIN)/radattr -D $(BUILD_DIR)/share $<; then \
+               echo "$(TESTBIN)/radattr -D $(BUILD_DIR)/share $<"; \
                exit 1; \
        fi
        @touch $@
diff --git a/src/tests/unit/dhcp.txt b/src/tests/unit/dhcp.txt
new file mode 100644 (file)
index 0000000..be166e1
--- /dev/null
@@ -0,0 +1,15 @@
+#
+#  Test vectors for DHCP attributes
+#
+
+encode-dhcp DHCP-Subnet-Mask = 255.255.0.0
+data 01 04 ff ff 00 00
+
+decode-dhcp -
+data DHCP-Subnet-Mask = 255.255.0.0
+
+#
+#  A long one... with a weird DHCP-specific vendor ID.
+#
+decode-dhcp 3501013d0701001ceaadac1e37070103060f2c2e2f3c094d5346545f495054565232011c4c41424f4c54322065746820312f312f30312f30312f31302f312f3209120000197f0d050b4c4142373336304f4c5432
+data DHCP-Message-Type = DHCP-Discover, DHCP-Client-Identifier = 0x01001ceaadac1e, DHCP-Parameter-Request-List = DHCP-Subnet-Mask, DHCP-Parameter-Request-List = DHCP-Router-Address, DHCP-Parameter-Request-List = DHCP-Domain-Name-Server, DHCP-Parameter-Request-List = DHCP-Domain-Name, DHCP-Parameter-Request-List = DHCP-NETBIOS-Name-Servers, DHCP-Parameter-Request-List = DHCP-NETBIOS-Node-Type, DHCP-Parameter-Request-List = DHCP-NETBIOS, DHCP-Vendor-Class-Identifier = 0x4d5346545f49505456, DHCP-Relay-Circuit-Id = 0x4c41424f4c54322065746820312f312f30312f30312f31302f312f32, Attr-26.6527.0 = 0x050b4c4142373336304f4c5432