test-dhcp-option.c revision 89ca10c6a61309d84d54c5dc5a295387ce39e610
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdio.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdbool.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <errno.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <string.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "macro.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "dhcp-protocol.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "dhcp-internal.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstruct option_desc {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t sname[64];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int snamelen;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t file[128];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int filelen;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t options[128];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int len;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool success;
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering int filepos;
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering int snamepos;
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering int pos;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering};
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic bool verbose = false;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 40, true, },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { {}, 0, {}, 0, { DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_OFFER,
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering 42, 3, 0, 0, 0 }, 8, true, },
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen { {}, 0, {}, 0, { 42, 2, 1, 2, 44 }, 5, false, },
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
33d52ab92f2f0bfd706e6f343d172618d1e03f3dLennart Poettering { {}, 0,
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, },
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering { { 1, 4, 1, 2, 3, 4, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 9,
afc5dbf37fd2399d37976388d9dd9ab470ecf446Lennart Poettering { 222, 3, 1, 2, 3 }, 5,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering { DHCP_OPTION_OVERLOAD, 1,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek};
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic const char *dhcp_type(int type) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek switch(type) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case DHCP_DISCOVER:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "DHCPDISCOVER";
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering case DHCP_OFFER:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "DHCPOFFER";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case DHCP_REQUEST:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPREQUEST";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering case DHCP_DECLINE:
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering return "DHCPDECLINE";
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering case DHCP_ACK:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPACK";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering case DHCP_NAK:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPNAK";
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering case DHCP_RELEASE:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "DHCPRELEASE";
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering default:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return "unknown";
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_invalid_buffer_length(void) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk DHCPMessage message;
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering
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)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek == -EINVAL);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poetteringstatic void test_message_init(void) {
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering _cleanup_free_ DHCPMessage *message = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t optlen = 4, optoffset;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t len = sizeof(DHCPMessage) + optlen;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t *magic;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering message = malloc0(len);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_DISCOVER, ARPHRD_ETHER, optlen, &optoffset) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(message->xid == htobe32(0x12345678));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(message->op == BOOTREQUEST);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering magic = (uint8_t*)&message->magic;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(magic[0] == 99);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(magic[1] == 130);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(magic[2] == 83);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(magic[3] == 99);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_option_parse(message, len, NULL, NULL) >= 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *file, uint8_t filelen,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t *sname, uint8_t snamelen) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DHCPMessage *message;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t len = sizeof(DHCPMessage) + optlen;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering message = malloc0(len);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(message);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (options && optlen)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering memcpy(&message->options, options, optlen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (file && filelen <= 128)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(&message->file, file, filelen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (sname && snamelen <= 64)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(&message->sname, sname, snamelen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return message;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(*descpos >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (*descpos < *desclen) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek switch(descoption[*descpos]) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case DHCP_OPTION_PAD:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *descpos += 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case DHCP_OPTION_MESSAGE_TYPE:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case DHCP_OPTION_OVERLOAD:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *descpos += 3;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek default:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option, void *userdata) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct option_desc *desc = userdata;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t *descoption = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int *desclen = NULL, *descpos = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t optcode = 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t optlen = 0;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek uint8_t i;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se((!desc && !code && !len) || desc);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!desc)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -EINVAL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(code != DHCP_OPTION_OVERLOAD);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek while (desc->pos >= 0 || desc->filepos >= 0 || desc->snamepos >= 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering if (desc->pos >= 0) {
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering descoption = &desc->options[0];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering desclen = &desc->len;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering descpos = &desc->pos;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek } else if (desc->filepos >= 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering descoption = &desc->file[0];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering desclen = &desc->filelen;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek descpos = &desc->filepos;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering } else if (desc->snamepos >= 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering descoption = &desc->sname[0];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering desclen = &desc->snamelen;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering descpos = &desc->snamepos;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(descoption && desclen && descpos);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (*desclen)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_ignore_opts(descoption, descpos, desclen);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (*descpos < *desclen)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (*descpos == *desclen)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *descpos = -1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(descpos);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(*descpos != -1);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering optcode = descoption[*descpos];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering optlen = descoption[*descpos + 1];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (verbose)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering printf("DHCP code %2d(%2d) len %2d(%2d) ", code, optcode,
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek len, optlen);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(code == optcode);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(len == optlen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek for (i = 0; i < len; i++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek if (verbose)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek printf("0x%02x(0x%02x) ", option[i],
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek descoption[*descpos + 2 + i]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(option[i] == descoption[*descpos + 2 + i]);
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (verbose)
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek printf("\n");
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek *descpos += optlen + 2;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek test_ignore_opts(descoption, descpos, desclen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->pos != -1 && desc->pos == desc->len)
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek desc->pos = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->filepos != -1 && desc->filepos == desc->filelen)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc->filepos = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (desc->snamepos != -1 && desc->snamepos == desc->snamelen)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc->snamepos = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_options(struct option_desc *desc) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *options = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *file = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *sname = NULL;
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering int optlen = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int filelen = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int snamelen = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int buflen = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ DHCPMessage *message = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int res;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering if (desc) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek file = &desc->file[0];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek filelen = desc->filelen;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!filelen)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek desc->filepos = -1;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sname = &desc->sname[0];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek snamelen = desc->snamelen;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (!snamelen)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc->snamepos = -1;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek options = &desc->options[0];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek optlen = desc->len;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc->pos = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek message = create_message(options, optlen, file, filelen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sname, snamelen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek buflen = sizeof(DHCPMessage) + optlen;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!desc) {
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering assert_se((res = dhcp_option_parse(message, buflen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek test_options_cb,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek NULL)) == -ENOMSG);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (desc->success) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se((res = dhcp_option_parse(message, buflen,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek test_options_cb,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc)) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(desc->pos == -1 && desc->filepos == -1 &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek desc->snamepos == -1);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering } else
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert_se((res = dhcp_option_parse(message, buflen,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering test_options_cb,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering desc)) < 0);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (verbose)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering printf("DHCP type %s\n", dhcp_type(res));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic uint8_t options[64] = {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 'A', 'B', 'C', 'D',
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 160, 2, 0x11, 0x12,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 0,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 31, 8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 0,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 55, 3, 0x51, 0x52, 0x53,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering 17, 7, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek 255
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering};
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmekstatic void test_option_set(void) {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering _cleanup_free_ DHCPMessage *result = NULL;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek size_t offset = 0, len, pos;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek unsigned i;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result = malloc0(sizeof(DHCPMessage) + 11);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(result);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result->options[0] = 'A';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result->options[1] = 'B';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result->options[2] = 'C';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek result->options[3] = 'D';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(dhcp_option_append(result, 0, &offset, 0, DHCP_OPTION_PAD,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering 0, NULL) == -ENOBUFS);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(offset == 0);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering offset = 4;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(dhcp_option_append(result, 5, &offset, 0, DHCP_OPTION_PAD,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt 0, NULL) == -ENOBUFS);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(offset == 4);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(dhcp_option_append(result, 6, &offset, 0, DHCP_OPTION_PAD,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt 0, NULL) >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(offset == 5);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek offset = pos = 4;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk len = 11;
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 options[pos],
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk options[pos + 1],
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk &options[pos + 2]) >= 0);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (options[pos] == DHCP_OPTION_PAD)
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering pos++;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk else
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk pos += 2 + options[pos + 1];
65c1d46b0923771955519329160a1e4c7cd027b0Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (pos < len)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_se(offset == pos);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk for (i = 0; i < 9; i++) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (verbose)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering printf("%2u: 0x%02x(0x%02x) (options)\n", i, result->options[i],
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering options[i]);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert_se(result->options[i] == options[i]);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (verbose)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk printf("%2d: 0x%02x(0x%02x) (options)\n", 9, result->options[9],
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk DHCP_OPTION_END);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert_se(result->options[9] == DHCP_OPTION_END);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (verbose)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering printf("%2d: 0x%02x(0x%02x) (options)\n", 10, result->options[10],
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DHCP_OPTION_PAD);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(result->options[10] == DHCP_OPTION_PAD);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering for (i = 0; i < pos - 8; i++) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (verbose)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek printf("%2u: 0x%02x(0x%02x) (sname)\n", i, result->sname[i],
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek options[i + 9]);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_se(result->sname[i] == options[i + 9]);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (verbose)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering printf ("\n");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek}
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering unsigned i;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_invalid_buffer_length();
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_message_init();
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_options(NULL);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering for (i = 0; i < ELEMENTSOF(option_tests); i++)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek test_options(&option_tests[i]);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering test_option_set();
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek