aboutsummaryrefslogtreecommitdiff
path: root/tests/test-xdr.c
blob: 6f4eadcb0ec6332dc798c0ec1c4b760d1ed73555 (plain) (blame)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * xdr.c
 * -----
 * Serialization/deserialization routines, using XDR (RFC 4506) encoding.
 * These functions are not part of the public libhal API.
 *
 * Copyright (c) 2016-2018, NORDUnet A/S All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the NORDUnet nor the names of its contributors may
 *   be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>		/* ptrdiff_t */
#include <string.h>             /* memcpy, memset */

#include "hal.h"
#include "hal_internal.h"	/* htonl/ntohl */
#include "xdr_internal.h"

static int verbose = 0;

/* suppress assert() error messages */
void hal_log(const hal_log_level_t level, const char *format, ...)
{
}

#define DEFINE_HAL_ERROR(_code_,_text_)  #_code_,
static char *err_name[] = { HAL_ERROR_LIST };
#undef  DEFINE_HAL_ERROR

static void hexdump(uint8_t *buf, uint32_t len)
{
    for (uint32_t i = 0; i < len; ++i) {
        printf("%02x", buf[i]);
        if (i < len - 1)
            printf(" ");
    }
}

#define test(name, result, expected)                            \
    if ((err = result) != expected) {                           \
        if (!verbose) printf("%s: ", func);                     \
        printf("%s test returned %s, expected %s\n",            \
               name, err_name[err], err_name[expected]);        \
        ++nerr;                                                 \
    }

#define test_value(val, exp)                            \
    if (val != exp) {                                   \
        if (!verbose) printf("%s: ", func);             \
        printf("value = %u, expected %u\n", val, exp);  \
        ++nerr;                                         \
    }

#define test_len(len, exp)                              \
    if (len != exp) {                                   \
        if (!verbose) printf("%s: ", func);             \
        printf("len = %lu, expected %u\n", len, exp);   \
        ++nerr;                                         \
    }

#define test_memcmp(buf, exp, len)              \
    if (memcmp(buf, exp, len) != 0) {           \
        if (!verbose) printf("%s: ", func);     \
        printf("buffer = [");                   \
        hexdump(buf, len);                      \
        printf("], expected [");                \
        hexdump(exp, len);                      \
        printf("]\n");                          \
        ++nerr;                                 \
    }

#define test_bufptr(bfp, exp)                                   \
    if (bfp != exp) {                                           \
        if (!verbose) printf("%s: ", func);                     \
        printf("buf ptr = %p, expected %p\n", bfp, exp);        \
        ++nerr;                                                 \
    }

static int test_hal_xdr_encode_int(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    uint8_t *null = NULL;
    uint8_t buf[4] = {0};
    uint8_t *bufptr = buf;
    uint8_t *limit = buf + sizeof(buf);
    uint8_t exp[4] = { 0x00, 0x11, 0x22, 0x33 };
    uint32_t value = 0x00112233;
    const char *func = "hal_xdr_encode_int";

    if (verbose)
        printf("%s... ", func);

    test("null outbuf", hal_xdr_encode_int(NULL, limit, value), HAL_ERROR_ASSERTION_FAILED);
    test("null outbuf", hal_xdr_encode_int(&null, limit, value), HAL_ERROR_ASSERTION_FAILED);
    test("null limit", hal_xdr_encode_int(&bufptr, NULL, value), HAL_ERROR_ASSERTION_FAILED);
    test("outbuf overflow", hal_xdr_encode_int(&bufptr, buf, value), HAL_ERROR_XDR_BUFFER_OVERFLOW);
    test("outbuf overflow", hal_xdr_encode_int(&bufptr, buf + 3, value), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    test("valid write", hal_xdr_encode_int(&bufptr, limit, value), HAL_OK);
    test_memcmp(buf, exp, sizeof(exp));
    test_bufptr(bufptr, buf + 4);

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_decode_int(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    const uint8_t *null = NULL;
    const uint8_t buf[4] = { 0x00, 0x11, 0x22, 0x33 };
    const uint8_t *bufptr = buf;
    const uint8_t * const limit = buf + sizeof(buf);
    uint32_t value = 0;
    const uint32_t exp = 0x00112233;
    const char *func = "hal_xdr_decode_int"; 

    if (verbose)
        printf("%s... ", func);

    test("null inbuf", hal_xdr_decode_int(NULL, limit, &value), HAL_ERROR_ASSERTION_FAILED);
    test("null inbuf", hal_xdr_decode_int(&null, limit, &value), HAL_ERROR_ASSERTION_FAILED);
    test("null limit", hal_xdr_decode_int(&bufptr, NULL, &value), HAL_ERROR_ASSERTION_FAILED);
    test("null value", hal_xdr_decode_int(&bufptr, limit, NULL), HAL_ERROR_ASSERTION_FAILED);

    test("inbuf overflow", hal_xdr_decode_int(&bufptr, buf, &value), HAL_ERROR_XDR_BUFFER_OVERFLOW);
    test("inbuf overflow", hal_xdr_decode_int(&bufptr, buf + 3, &value), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    test("valid read", hal_xdr_decode_int(&bufptr, limit, &value), HAL_OK);
    test_value(value, exp);
    test_bufptr(bufptr, buf + 4);

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_decode_int_peek(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    const uint8_t buf[4] = { 0x00, 0x11, 0x22, 0x33 };
    const uint8_t *bufptr = buf;
    const uint8_t * const limit = buf + sizeof(buf);
    uint32_t value = 0;
    const uint32_t exp = 0x00112233;
    const char *func = "hal_xdr_decode_int_peek";

    if (verbose)
        printf("%s... ", func);

    test("valid read", hal_xdr_decode_int_peek(&bufptr, limit, &value), HAL_OK);
    test_value(value, exp);
    test_bufptr(bufptr, buf);

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_encode_fixed_opaque(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    uint8_t *null = NULL;
    uint8_t buf[8];
    uint8_t *bufptr = buf;
    uint8_t *limit = buf + sizeof(buf);
    uint8_t src[8]  = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    uint8_t exp3[4] = { 0x00, 0x11, 0x22, 0x00 };
    uint8_t exp4[4] = { 0x00, 0x11, 0x22, 0x33 };
    uint8_t exp5[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00 };
    const char *func = "hal_xdr_encode_fixed_opaque";

    if (verbose)
        printf("%s... ", func);

    test("null outbuf", hal_xdr_encode_fixed_opaque(NULL, limit, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
    test("null outbuf", hal_xdr_encode_fixed_opaque(&null, limit, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED); 
    test("null limit", hal_xdr_encode_fixed_opaque(&bufptr, NULL, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
   test("null value", hal_xdr_encode_fixed_opaque(&bufptr, limit, NULL, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
    test("outbuf overflow", hal_xdr_encode_fixed_opaque(&bufptr, buf, src, sizeof(src)), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    memset(buf, 0xff, sizeof(buf));
    test("write 3", hal_xdr_encode_fixed_opaque(&bufptr, limit, src, 3), HAL_OK);
    test_memcmp(buf, exp3, sizeof(exp3));
    test_bufptr(bufptr, buf + 4);

    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 4", hal_xdr_encode_fixed_opaque(&bufptr, limit, src, 4), HAL_OK);
    test_memcmp(buf, exp4, sizeof(exp4));
    test_bufptr(bufptr, buf + 4)
    
    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 5", hal_xdr_encode_fixed_opaque(&bufptr, limit, src, 5), HAL_OK);
    test_memcmp(buf, exp5, sizeof(exp5));
    test_bufptr(bufptr, buf + 8);

    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 8", hal_xdr_encode_fixed_opaque(&bufptr, limit, src, 8), HAL_OK);
    test_memcmp(buf, src, sizeof(src));
    test_bufptr(bufptr, buf + 8)

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_decode_fixed_opaque(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    const uint8_t *null = NULL;
    const uint8_t buf[8]  = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    const uint8_t *bufptr = buf;
    const uint8_t *limit = buf + sizeof(buf);
    uint8_t value[8];
    uint8_t exp3[4] = { 0x00, 0x11, 0x22, 0x00 };
    uint8_t exp4[4] = { 0x00, 0x11, 0x22, 0x33 };
    uint8_t exp5[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00 };
    const char *func = "hal_xdr_decode_fixed_opaque";

    if (verbose)
        printf("%s... ", func);

    test("null inbuf", hal_xdr_decode_fixed_opaque(NULL, limit, value, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null inbuf", hal_xdr_decode_fixed_opaque(&null, limit, value, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null limit", hal_xdr_decode_fixed_opaque(&bufptr, NULL, value, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null value", hal_xdr_decode_fixed_opaque(&bufptr, limit, NULL, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("inbuf overflow", hal_xdr_decode_fixed_opaque(&bufptr, buf, value, sizeof(value)), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    memset(value, 0xff, sizeof(value));
    test("read 3", hal_xdr_decode_fixed_opaque(&bufptr, limit, value, 3), HAL_OK);
    test_memcmp(value, exp3, sizeof(exp3));
    test_bufptr(bufptr, buf + 4);

    bufptr = buf;
    memset(value, 0xff, sizeof(value));
    test("read 4", hal_xdr_decode_fixed_opaque(&bufptr, limit, value, 4), HAL_OK);
    test_memcmp(value, exp4, sizeof(exp4));
    test_bufptr(bufptr, buf + 4);

    bufptr = buf;
    memset(value, 0xff, sizeof(value));
    test("read 5", hal_xdr_decode_fixed_opaque(&bufptr, limit, value, 5), HAL_OK);
    test_memcmp(value, exp5, sizeof(exp5));
    test_bufptr(bufptr, buf + 8);

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_encode_variable_opaque(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    uint8_t *null = NULL;
    uint8_t buf[12];
    uint8_t *bufptr = buf;
    uint8_t *limit = buf + sizeof(buf);
    uint8_t src[8]  = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    uint8_t exp3[8] = { 0x00, 0x00, 0x00, 0x03, 0x00, 0x11, 0x22, 0x00 };
    uint8_t exp4[8] = { 0x00, 0x00, 0x00, 0x04, 0x00, 0x11, 0x22, 0x33 };
    uint8_t exp5[12] = { 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00 };
    uint8_t exp8[12] = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    const char *func = "hal_xdr_encode_variable_opaque";

    if (verbose)
        printf("%s... ", func);

    test("null outbuf", hal_xdr_encode_variable_opaque(NULL, limit, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
    test("null outbuf", hal_xdr_encode_variable_opaque(&null, limit, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED); 
    test("null limit", hal_xdr_encode_variable_opaque(&bufptr, NULL, src, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
   test("null value", hal_xdr_encode_variable_opaque(&bufptr, limit, NULL, sizeof(src)), HAL_ERROR_ASSERTION_FAILED);
    test("outbuf overflow", hal_xdr_encode_variable_opaque(&bufptr, buf, src, sizeof(src)), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    memset(buf, 0xff, sizeof(buf));
    test("write 3", hal_xdr_encode_variable_opaque(&bufptr, limit, src, 3), HAL_OK);
    test_memcmp(buf, exp3, sizeof(exp3));
    test_bufptr(bufptr, buf + 8);

    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 4", hal_xdr_encode_variable_opaque(&bufptr, limit, src, 4), HAL_OK);
    test_memcmp(buf, exp4, sizeof(exp4));
    test_bufptr(bufptr, buf + 8)
    
    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 5", hal_xdr_encode_variable_opaque(&bufptr, limit, src, 5), HAL_OK);
    test_memcmp(buf, exp5, sizeof(exp5));
    test_bufptr(bufptr, buf + 12);

    bufptr = buf;
    memset(buf, 0xff, sizeof(buf));
    test("write 8", hal_xdr_encode_variable_opaque(&bufptr, limit, src, 8), HAL_OK);
    test_memcmp(buf, exp8, sizeof(exp8));
    test_bufptr(bufptr, buf + 12)

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}

static int test_hal_xdr_decode_variable_opaque(void)
{
    hal_error_t err = HAL_OK;
    int nerr = 0;
    const uint8_t *null = NULL;
    uint8_t buf[12]  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    const uint8_t *bufptr = buf;
    const uint8_t *limit = buf + sizeof(buf);
    uint8_t value[12];
    size_t len;
    uint8_t exp3[4] = { 0x00, 0x11, 0x22, 0x00 };
    uint8_t exp4[4] = { 0x00, 0x11, 0x22, 0x33 };
    uint8_t exp5[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00 };
    uint8_t exp8[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
    const char *func = "hal_xdr_decode_variable_opaque";

    if (verbose)
        printf("%s... ", func);

    test("null inbuf", hal_xdr_decode_variable_opaque(NULL, limit, value, &len, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null inbuf", hal_xdr_decode_variable_opaque(&null, limit, value, &len, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null limit", hal_xdr_decode_variable_opaque(&bufptr, NULL, value, &len, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("null value", hal_xdr_decode_variable_opaque(&bufptr, limit, NULL, &len, sizeof(value)), HAL_ERROR_ASSERTION_FAILED);
    test("inbuf overflow", hal_xdr_decode_variable_opaque(&bufptr, buf, value, &len, sizeof(value)), HAL_ERROR_XDR_BUFFER_OVERFLOW);

    buf[3] = 0x03;
    memset(value, 0xff, sizeof(value));
    test("read 3", hal_xdr_decode_variable_opaque(&bufptr, limit, value, &len, sizeof(value)), HAL_OK);
    test_len(len, 3);
    test_memcmp(value, exp3, sizeof(exp3));
    test_bufptr(bufptr, buf + 8);

    bufptr = buf;
    buf[3] = 0x04;
    memset(value, 0xff, sizeof(value));
    test("read 4", hal_xdr_decode_variable_opaque(&bufptr, limit, value, &len, sizeof(value)), HAL_OK);
    test_len(len, 4);
    test_memcmp(value, exp4, sizeof(exp4));
    test_bufptr(bufptr, buf + 8);

    bufptr = buf;
    buf[3] = 0x05;
    memset(value, 0xff, sizeof(value));
    test("read 5", hal_xdr_decode_variable_opaque(&bufptr, limit, value, &len, sizeof(value)), HAL_OK);
    test_len(len, 5);
    test_memcmp(value, exp5, sizeof(exp5));
    test_bufptr(bufptr, buf + 12);

    bufptr = buf;
    buf[3] = 0x08;
    memset(value, 0xff, sizeof(value));
    test("read 8", hal_xdr_decode_variable_opaque(&bufptr, limit, value, &len, sizeof(value)), HAL_OK);
    test_len(len, 8);
    test_memcmp(value, exp8, sizeof(exp8));
    test_bufptr(bufptr, buf + 12);

    if (verbose && nerr == 0)
        printf("PASS\n");

    return nerr;
}


int main(int argc, char *argv[])
{
    if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v')
        verbose = 1;

    int nerr = 0;
    nerr += test_hal_xdr_encode_int();
    nerr += test_hal_xdr_decode_int();
    nerr += test_hal_xdr_decode_int_peek();
    nerr += test_hal_xdr_encode_fixed_opaque();
    nerr += test_hal_xdr_decode_fixed_opaque();
    nerr += test_hal_xdr_encode_variable_opaque();
    nerr += test_hal_xdr_decode_variable_opaque();

    if (nerr)
        printf("%d failures\n", nerr);

    return nerr;
}