summaryrefslogtreecommitdiff
path: root/src/libsystemd/test-dhcp-option.c
blob: ac0b0a435a93986b9ac5e3cdbbf551e873e2e277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>

#include "util.h"
#include "macro.h"

#include "dhcp-protocol.h"
#include "dhcp-internal.h"

struct option_desc {
        uint8_t sname[64];
        int snamelen;
        uint8_t file[128];
        int filelen;
        uint8_t options[128];
        int len;
        bool success;
        int filepos;
        int snamepos;
        int pos;
};

static bool verbose = false;

static struct option_desc option_tests[] = {
        { {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69 }, 7, false, },
        { {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69, 0, 0,
                          DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 12, true, },
        { {}, 0, {}, 0, { 8, 255, 70, 71, 72 }, 5, false, },
        { {}, 0, {}, 0, { 0x35, 0x01, 0x05, 0x36, 0x04, 0x01, 0x00, 0xa8,
                          0xc0, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x01,
                          0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0xc0,
                          0xa8, 0x00, 0x01, 0x06, 0x04, 0xc0, 0xa8, 0x00,
                          0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
          40, true, },
        { {}, 0, {}, 0, { DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_OFFER,
                          42, 3, 0, 0, 0 }, 8, true, },
        { {}, 0, {}, 0, { 42, 2, 1, 2, 44 }, 5, false, },

        { {}, 0,
          { 222, 3, 1, 2, 3, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_NAK }, 8,
          { DHCP_OPTION_OVERLOAD, 1, DHCP_OVERLOAD_FILE }, 3, true, },

        { { 1, 4, 1, 2, 3, 4, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 9,
          { 222, 3, 1, 2, 3 }, 5,
          { DHCP_OPTION_OVERLOAD, 1,
            DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
};

static const char *dhcp_type(int type)
{
        switch(type) {
        case DHCP_DISCOVER:
                return "DHCPDISCOVER";
        case DHCP_OFFER:
                return "DHCPOFFER";
        case DHCP_REQUEST:
                return "DHCPREQUEST";
        case DHCP_DECLINE:
                return "DHCPDECLINE";
        case DHCP_ACK:
                return "DHCPACK";
        case DHCP_NAK:
                return "DHCPNAK";
        case DHCP_RELEASE:
                return "DHCPRELEASE";
        default:
                return "unknown";
        }
}

static void test_invalid_buffer_length(void)
{
        DHCPMessage message;

        assert(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL);
        assert(dhcp_option_parse(&message, sizeof(DHCPMessage), NULL, NULL)
               == -EINVAL);
}

static void test_cookie(void)
{
        _cleanup_free_ DHCPMessage *message;
        size_t len = sizeof(DHCPMessage) + 4;
        uint8_t *opt;

        message = malloc0(len);

        opt = (uint8_t *)(message + 1);
        opt[0] = 0xff;

        assert(dhcp_option_parse(message, len, NULL, NULL) == -EINVAL);

        opt[0] = 99;
        opt[1] = 130;
        opt[2] = 83;
        opt[3] = 99;

        assert(dhcp_option_parse(message, len, NULL, NULL) == -ENOMSG);
}

static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
                uint8_t *file, uint8_t filelen,
                uint8_t *sname, uint8_t snamelen)
{
        DHCPMessage *message;
        size_t len = sizeof(DHCPMessage) + 4 + optlen;
        uint8_t *opt;

        message = malloc0(len);
        opt = (uint8_t *)(message + 1);

        opt[0] = 99;
        opt[1] = 130;
        opt[2] = 83;
        opt[3] = 99;

        if (options && optlen)
                memcpy(&opt[4], options, optlen);

        if (file && filelen <= 128)
                memcpy(&message->file, file, filelen);

        if (sname && snamelen <= 64)
                memcpy(&message->sname, sname, snamelen);

        return message;
}

static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen)
{
        while (*descpos < *desclen) {
                switch(descoption[*descpos]) {
                case DHCP_OPTION_PAD:
                        *descpos += 1;
                        break;

                case DHCP_OPTION_MESSAGE_TYPE:
                case DHCP_OPTION_OVERLOAD:
                        *descpos += 3;
                        break;

                default:
                        return;
                }
        }
}

static int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option,
                           void *user_data)
{
        struct option_desc *desc = user_data;
        uint8_t *descoption = NULL;
        int *desclen = NULL, *descpos = NULL;
        uint8_t optcode = 0;
        uint8_t optlen = 0;
        uint8_t i;

        assert((!desc && !code && !len) || desc);

        if (!desc)
                return -EINVAL;

        assert(code != DHCP_OPTION_PAD);
        assert(code != DHCP_OPTION_END);
        assert(code != DHCP_OPTION_MESSAGE_TYPE);
        assert(code != DHCP_OPTION_OVERLOAD);

        while (desc->pos >= 0 || desc->filepos >= 0 || desc->snamepos >= 0) {

                if (desc->pos >= 0) {
                        descoption = &desc->options[0];
                        desclen = &desc->len;
                        descpos = &desc->pos;
                } else if (desc->filepos >= 0) {
                        descoption = &desc->file[0];
                        desclen = &desc->filelen;
                        descpos = &desc->filepos;
                } else if (desc->snamepos >= 0) {
                        descoption = &desc->sname[0];
                        desclen = &desc->snamelen;
                        descpos = &desc->snamepos;
                }

                assert(descoption && desclen && descpos);

                if (*desclen)
                        test_ignore_opts(descoption, descpos, desclen);

                if (*descpos < *desclen)
                        break;

                if (*descpos == *desclen)
                        *descpos = -1;
        }

        assert(descpos);
        assert(*descpos != -1);

        optcode = descoption[*descpos];
        optlen = descoption[*descpos + 1];

        if (verbose)
                printf("DHCP code %2d(%2d) len %2d(%2d) ", code, optcode,
                                len, optlen);

        assert(code == optcode);
        assert(len == optlen);

        for (i = 0; i < len; i++) {

                if (verbose)
                        printf("0x%02x(0x%02x) ", option[i],
                                        descoption[*descpos + 2 + i]);

                assert(option[i] == descoption[*descpos + 2 + i]);
        }

        if (verbose)
                printf("\n");

        *descpos += optlen + 2;

        test_ignore_opts(descoption, descpos, desclen);

        if (desc->pos != -1 && desc->pos == desc->len)
                desc->pos = -1;

        if (desc->filepos != -1 && desc->filepos == desc->filelen)
                desc->filepos = -1;

        if (desc->snamepos != -1 && desc->snamepos == desc->snamelen)
                desc->snamepos = -1;

        return 0;
}

static void test_options(struct option_desc *desc)
{
        uint8_t *options = NULL;
        uint8_t *file = NULL;
        uint8_t *sname = NULL;
        int optlen = 0;
        int filelen = 0;
        int snamelen = 0;
        int buflen = 0;
        _cleanup_free_ DHCPMessage *message;
        int res;

        if (desc) {
                file = &desc->file[0];
                filelen = desc->filelen;
                if (!filelen)
                        desc->filepos = -1;

                sname = &desc->sname[0];
                snamelen = desc->snamelen;
                if (!snamelen)
                        desc->snamepos = -1;

                options = &desc->options[0];
                optlen = desc->len;
                desc->pos = 0;
        }
        message = create_message(options, optlen, file, filelen,
                        sname, snamelen);

        buflen = sizeof(DHCPMessage) + 4 + optlen;

        if (!desc) {
                assert((res = dhcp_option_parse(message, buflen,
                                                test_options_cb,
                                                NULL)) == -ENOMSG);
        } else if (desc->success) {
                assert((res = dhcp_option_parse(message, buflen,
                                                test_options_cb,
                                                desc)) >= 0);
                assert(desc->pos == -1 && desc->filepos == -1 &&
                                desc->snamepos == -1);
        } else
                assert((res = dhcp_option_parse(message, buflen,
                                                test_options_cb,
                                                desc)) < 0);

        if (verbose)
                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;

        test_invalid_buffer_length();
        test_cookie();

        test_options(NULL);

        for (i = 0; i < ELEMENTSOF(option_tests); i++)
                test_options(&option_tests[i]);

        test_option_set();

        return 0;
}