diff options
author | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-12-09 23:43:15 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-12-12 11:43:34 -0500 |
commit | d8b61a1dc9153c6f22c923b303b6235ff55122a3 (patch) | |
tree | 874fd09bfcf5c90032c9bbdf477cc1a0e3282acc | |
parent | a10c375e02da66efec40e28142bc22fd8955e968 (diff) |
dhcp: Add option append tests
Add checks for invalid lengths and parameters when using the option
appending function. Add also checks for adding options, see to it
that the resulting array is identical to the array of options added.
-rw-r--r-- | src/libsystemd-dhcp/test-dhcp-option.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/libsystemd-dhcp/test-dhcp-option.c b/src/libsystemd-dhcp/test-dhcp-option.c index df717af06d..e2a680064c 100644 --- a/src/libsystemd-dhcp/test-dhcp-option.c +++ b/src/libsystemd-dhcp/test-dhcp-option.c @@ -289,6 +289,76 @@ static void test_options(struct option_desc *desc) printf("DHCP type %s\n", dhcp_type(res)); } +static uint8_t result[64] = { + 'A', 'B', 'C', 'D', +}; + +static uint8_t options[64] = { + 'A', 'B', 'C', 'D', + 160, 2, 0x11, 0x12, + 0, + 31, 8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, + 0, + 55, 3, 0x51, 0x52, 0x53, + 17, 7, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 255 +}; + +static void test_option_set(void) +{ + size_t len, oldlen; + int pos, i; + uint8_t *opt; + + assert(dhcp_option_append(NULL, NULL, 0, 0, NULL) == -EINVAL); + + len = 0; + opt = &result[0]; + assert(dhcp_option_append(&opt, NULL, 0, 0, NULL) == -EINVAL); + assert(opt == &result[0] && len == 0); + + assert(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD, + 0, NULL) == -ENOBUFS); + assert(opt == &result[0] && len == 0); + + opt = &result[4]; + len = 1; + assert(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD, + 0, NULL) >= 0); + assert(opt == &result[5] && len == 0); + + pos = 4; + len = 60; + while (pos < 64 && options[pos] != DHCP_OPTION_END) { + opt = &result[pos]; + oldlen = len; + + assert(dhcp_option_append(&opt, &len, options[pos], + options[pos + 1], + &options[pos + 2]) >= 0); + + if (options[pos] == DHCP_OPTION_PAD) { + assert(opt == &result[pos + 1]); + assert(len == oldlen - 1); + pos++; + } else { + assert(opt == &result[pos + 2 + options[pos + 1]]); + assert(len == oldlen - 2 - options[pos + 1]); + pos += 2 + options[pos + 1]; + } + } + + for (i = 0; i < pos; i++) { + if (verbose) + printf("%2d: 0x%02x(0x%02x)\n", i, result[i], + options[i]); + assert(result[i] == options[i]); + } + + if (verbose) + printf ("\n"); +} + int main(int argc, char *argv[]) { unsigned int i; @@ -301,5 +371,7 @@ int main(int argc, char *argv[]) for (i = 0; i < ELEMENTSOF(option_tests); i++) test_options(&option_tests[i]); + test_option_set(); + return 0; } |