test-dhcp-option.c revision 89ca10c6a61309d84d54c5dc5a295387ce39e610
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic bool verbose = false;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic struct option_desc option_tests[] = {
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering { {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69 }, 7, false, },
b4bbcaa9c44260e88402cb8f9a5fb8ac7f35e123Thomas Hindoe Paaboel Andersen { {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69, 0, 0,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 12, true, },
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering { {}, 0, {}, 0, { 8, 255, 70, 71, 72 }, 5, false, },
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering { {}, 0, {}, 0, { 0x35, 0x01, 0x05, 0x36, 0x04, 0x01, 0x00, 0xa8,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 0xc0, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x01,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0xc0,
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering 0xa8, 0x00, 0x01, 0x06, 0x04, 0xc0, 0xa8, 0x00,
430f0182b72373145c839dbfe99d2382855cb8f8Lennart Poettering 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { {}, 0, {}, 0, { DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_OFFER,
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen { {}, 0, {}, 0, { 42, 2, 1, 2, 44 }, 5, false, },
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering { 222, 3, 1, 2, 3, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_NAK }, 8,
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poettering { DHCP_OPTION_OVERLOAD, 1, DHCP_OVERLOAD_FILE }, 3, true, },
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering { { 1, 4, 1, 2, 3, 4, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 9,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "DHCPDISCOVER";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "DHCPOFFER";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPREQUEST";
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering return "DHCPDECLINE";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPACK";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPNAK";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPRELEASE";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "unknown";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_invalid_buffer_length(void) {
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering assert_se(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL);
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz assert_se(dhcp_option_parse(&message, sizeof(DHCPMessage) - 1, NULL, NULL)
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poetteringstatic void test_message_init(void) {
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering _cleanup_free_ DHCPMessage *message = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t len = sizeof(DHCPMessage) + optlen;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_DISCOVER, ARPHRD_ETHER, optlen, &optoffset) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(message->xid == htobe32(0x12345678));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_option_parse(message, len, NULL, NULL) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t len = sizeof(DHCPMessage) + optlen;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering memcpy(&message->options, options, optlen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(&message->file, file, filelen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(&message->sname, sname, snamelen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int *desclen = NULL, *descpos = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se((!desc && !code && !len) || desc);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(code != DHCP_OPTION_PAD);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert_se(code != DHCP_OPTION_END);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert_se(code != DHCP_OPTION_MESSAGE_TYPE);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek while (desc->pos >= 0 || desc->filepos >= 0 || desc->snamepos >= 0) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek } else if (desc->filepos >= 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(descoption && desclen && descpos);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_ignore_opts(descoption, descpos, desclen);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering printf("DHCP code %2d(%2d) len %2d(%2d) ", code, optcode,
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek for (i = 0; i < len; i++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek printf("0x%02x(0x%02x) ", option[i],
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(option[i] == descoption[*descpos + 2 + i]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek test_ignore_opts(descoption, descpos, desclen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->pos != -1 && desc->pos == desc->len)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->filepos != -1 && desc->filepos == desc->filelen)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->snamepos != -1 && desc->snamepos == desc->snamelen)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_options(struct option_desc *desc) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ DHCPMessage *message = NULL;
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek message = create_message(options, optlen, file, filelen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek buflen = sizeof(DHCPMessage) + optlen;
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering assert_se((res = dhcp_option_parse(message, buflen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se((res = dhcp_option_parse(message, buflen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(desc->pos == -1 && desc->filepos == -1 &&
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert_se((res = dhcp_option_parse(message, buflen,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering printf("DHCP type %s\n", dhcp_type(res));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 31, 8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering 17, 7, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmekstatic void test_option_set(void) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering _cleanup_free_ DHCPMessage *result = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result = malloc0(sizeof(DHCPMessage) + 11);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_option_append(result, 0, &offset, 0, DHCP_OPTION_PAD,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(dhcp_option_append(result, 5, &offset, 0, DHCP_OPTION_PAD,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(dhcp_option_append(result, 6, &offset, 0, DHCP_OPTION_PAD,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk while (pos < len && options[pos] != DHCP_OPTION_END) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_se(dhcp_option_append(result, len, &offset, DHCP_OVERLOAD_SNAME,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk for (i = 0; i < 9; i++) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering printf("%2u: 0x%02x(0x%02x) (options)\n", i, result->options[i],
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk printf("%2d: 0x%02x(0x%02x) (options)\n", 9, result->options[9],
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert_se(result->options[9] == DHCP_OPTION_END);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering printf("%2d: 0x%02x(0x%02x) (options)\n", 10, result->options[10],
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(result->options[10] == DHCP_OPTION_PAD);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek printf("%2u: 0x%02x(0x%02x) (sname)\n", i, result->sname[i],
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(result->sname[i] == options[i + 9]);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering for (i = 0; i < ELEMENTSOF(option_tests); i++)