From: Alan T. DeKok Date: Mon, 15 Jun 2015 15:40:29 +0000 (-0400) Subject: Add some DHCP unit tests X-Git-Tag: release_3_0_9~171 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b2bccaa1d83315894f2a7c7a5c43f7443e9c65e;p=thirdparty%2Ffreeradius-server.git Add some DHCP unit tests --- diff --git a/src/main/radattr.c b/src/main/radattr.c index c658a60bd60..ec901c78e06 100644 --- a/src/main/radattr.c +++ b/src/main/radattr.c @@ -30,6 +30,7 @@ typedef struct REQUEST REQUEST; #include #include #include +#include #include @@ -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; diff --git a/src/main/radattr.mk b/src/main/radattr.mk index db880d2b6ec..7a8a034c7fb 100644 --- a/src/main/radattr.mk +++ b/src/main/radattr.mk @@ -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) diff --git a/src/tests/unit/all.mk b/src/tests/unit/all.mk index 3827842ec21..d54a0ce708e 100644 --- a/src/tests/unit/all.mk +++ b/src/tests/unit/all.mk @@ -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 index 00000000000..be166e18bb5 --- /dev/null +++ b/src/tests/unit/dhcp.txt @@ -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