aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/test_wsutil.c
blob: 530b7f9db4a92d1f139564160834d144ccd5f53a (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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdio.h>
#include <glib.h>
#include <wsutil/utf8_entities.h>

#include "str_util.h"


static void test_format_size(void)
{
    char *str;

    str = format_size(10000, format_size_unit_bytes);
    g_assert_cmpstr(str, ==, "10 kB");
    g_free(str);

    str = format_size(100000, format_size_unit_bytes|format_size_prefix_iec);
    g_assert_cmpstr(str, ==, "97 KiB");
    g_free(str);

    str = format_size(20971520, format_size_unit_bits|format_size_prefix_iec);
    g_assert_cmpstr(str, ==, "20 Mib");
    g_free(str);
}

#include "to_str.h"

static void test_word_to_hex(void)
{
    static char buf[32];
    char *str;     /* String is not NULL terminated. */

    str = guint8_to_hex(buf, 0x34);
    g_assert_true(str == buf + 2);
    g_assert_cmpint(str[-1], ==, '4');
    g_assert_cmpint(str[-2], ==, '3');

    str = word_to_hex(buf, 0x1234);
    g_assert_true(str == buf + 4);
    g_assert_cmpint(str[-1], ==, '4');
    g_assert_cmpint(str[-2], ==, '3');
    g_assert_cmpint(str[-3], ==, '2');
    g_assert_cmpint(str[-4], ==, '1');

    str = dword_to_hex(buf, 0x1234);
    g_assert_true(str == buf + 8);
    g_assert_cmpint(str[-1], ==, '4');
    g_assert_cmpint(str[-2], ==, '3');
    g_assert_cmpint(str[-3], ==, '2');
    g_assert_cmpint(str[-4], ==, '1');
    g_assert_cmpint(str[-5], ==, '0');
    g_assert_cmpint(str[-6], ==, '0');
    g_assert_cmpint(str[-7], ==, '0');
    g_assert_cmpint(str[-8], ==, '0');

    str = qword_to_hex(buf, G_GUINT64_CONSTANT(0xFEDCBA987654321));
    g_assert_true(str == buf + 16);
    g_assert_cmpint(str[-1], ==, '1');
    g_assert_cmpint(str[-2], ==, '2');
    g_assert_cmpint(str[-3], ==, '3');
    g_assert_cmpint(str[-4], ==, '4');
    g_assert_cmpint(str[-5], ==, '5');
    g_assert_cmpint(str[-6], ==, '6');
    g_assert_cmpint(str[-7], ==, '7');
    g_assert_cmpint(str[-8], ==, '8');
    g_assert_cmpint(str[-9], ==, '9');
    g_assert_cmpint(str[-10], ==, 'a');
    g_assert_cmpint(str[-11], ==, 'b');
    g_assert_cmpint(str[-12], ==, 'c');
    g_assert_cmpint(str[-13], ==, 'd');
    g_assert_cmpint(str[-14], ==, 'e');
    g_assert_cmpint(str[-15], ==, 'f');
    g_assert_cmpint(str[-16], ==, '0');
}

static void test_bytes_to_str(void)
{
    char *str;

    const guint8 buf[] = { 1, 2, 3};

    str = bytes_to_str(NULL, buf, sizeof(buf));
    g_assert_cmpstr(str, ==, "010203");
    g_free(str);
}

static void test_bytes_to_str_punct(void)
{
    char *str;

    const guint8 buf[] = { 1, 2, 3};

    str = bytes_to_str_punct(NULL, buf, sizeof(buf), ':');
    g_assert_cmpstr(str, ==, "01:02:03");
    g_free(str);
}

static void test_bytes_to_string_trunc1(void)
{
    char *str;

    const guint8 buf[] = {
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA
    };
    const char *expect =
        "112233445566778899aa"
        "112233445566778899aa"
        "112233445566778899aa"
        "112233445566" UTF8_HORIZONTAL_ELLIPSIS;

    str = bytes_to_str(NULL, buf, sizeof(buf));
    g_assert_cmpstr(str, ==, expect);
    g_free(str);
}

static void test_bytes_to_string_punct_trunc1(void)
{
    char *str;

    const guint8 buf[] = {
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA
    };
    const char *expect =
        "11:22:33:44:55:66:77:88:99:aa:"
        "11:22:33:44:55:66:77:88:99:aa:"
        "11:22:33:44:" UTF8_HORIZONTAL_ELLIPSIS;

    str = bytes_to_str_punct(NULL, buf, sizeof(buf), ':');
    g_assert_cmpstr(str, ==, expect);
    g_free(str);
}

static char to_str_back_buf[32];
#define BACK_PTR (&to_str_back_buf[31]) /* pointer to NUL string terminator */

static void test_oct_to_str_back(void)
{
    char *str;

    str = oct_to_str_back(BACK_PTR, 958769886);
    g_assert_cmpstr(str,  ==, "07111325336");

    str = oct_to_str_back(BACK_PTR, 781499127);
    g_assert_cmpstr(str,  ==, "05645135367");

    str = oct_to_str_back(BACK_PTR, 1177329882);
    g_assert_cmpstr(str, ==, "010613120332");
}

static void test_oct64_to_str_back(void)
{
    char *str;

    str = oct64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(13873797580070999420));
    g_assert_cmpstr(str, ==, "01402115026217563452574");

    str = oct64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(7072159458371400691));
    g_assert_cmpstr(str, ==, "0610452670726711271763");

    str = oct64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(12453513102400590374));
    g_assert_cmpstr(str, ==, "01263236102754220511046");
}

static void test_hex_to_str_back_len(void)
{
    char *str;

    str = hex_to_str_back_len(BACK_PTR, 2481, 8);
    g_assert_cmpstr(str, ==, "0x000009b1");

    str = hex_to_str_back_len(BACK_PTR, 2457, 8);
    g_assert_cmpstr(str, ==, "0x00000999");

    str = hex_to_str_back_len(BACK_PTR, 16230, 8);
    g_assert_cmpstr(str, ==, "0x00003f66");
}

static void test_hex64_to_str_back_len(void)
{
    char *str;

    str = hex64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(1), 16);
    g_assert_cmpstr(str, ==, "0x0000000000000001");

    str = hex64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(4294967295), 16);
    g_assert_cmpstr(str, ==, "0x00000000ffffffff");

    str = hex64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(18446744073709551615), 16);
    g_assert_cmpstr(str, ==, "0xffffffffffffffff");
}

static void test_uint_to_str_back(void)
{
    char *str;

    str = uint_to_str_back(BACK_PTR, 873735883);
    g_assert_cmpstr(str, ==, "873735883");

    str = uint_to_str_back(BACK_PTR, 1801148094);
    g_assert_cmpstr(str, ==, "1801148094");

    str = uint_to_str_back(BACK_PTR, 181787997);
    g_assert_cmpstr(str, ==, "181787997");
}

static void test_uint64_to_str_back(void)
{
    char *str;

    str = uint64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(585143757104211265));
    g_assert_cmpstr(str, ==, "585143757104211265");

    str = uint64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(7191580247919484847));
    g_assert_cmpstr(str, ==, "7191580247919484847");

    str = uint64_to_str_back(BACK_PTR, G_GUINT64_CONSTANT(95778573911934485));
    g_assert_cmpstr(str, ==, "95778573911934485");
}

static void test_uint_to_str_back_len(void)
{
    char *str;

    str = uint_to_str_back_len(BACK_PTR, 26630, 8);
    g_assert_cmpstr(str, ==, "00026630");

    str = uint_to_str_back_len(BACK_PTR, 25313, 8);
    g_assert_cmpstr(str, ==, "00025313");

    str = uint_to_str_back_len(BACK_PTR, 18750000, 8);
    g_assert_cmpstr(str, ==, "18750000");
}

static void test_uint64_to_str_back_len(void)
{
    char *str;

    str = uint64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(1), 16);
    g_assert_cmpstr(str, ==, "0000000000000001");

    str = uint64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(4294967295), 16);
    g_assert_cmpstr(str, ==, "0000004294967295");

    str = uint64_to_str_back_len(BACK_PTR, G_GUINT64_CONSTANT(18446744073709551615), 16);
    g_assert_cmpstr(str, ==, "18446744073709551615");
}

static void test_int_to_str_back(void)
{
    char *str;

    str = int_to_str_back(BACK_PTR, -763689611);
    g_assert_cmpstr(str, ==, "-763689611");

    str = int_to_str_back(BACK_PTR, -296015954);
    g_assert_cmpstr(str, ==, "-296015954");

    str = int_to_str_back(BACK_PTR, 898901469);
    g_assert_cmpstr(str, ==, "898901469");
}

static void test_int64_to_str_back(void)
{
    char *str;

    str = int64_to_str_back(BACK_PTR, G_GINT64_CONSTANT(-9223372036854775807));
    g_assert_cmpstr(str, ==, "-9223372036854775807");

    str = int64_to_str_back(BACK_PTR, G_GINT64_CONSTANT(1));
    g_assert_cmpstr(str, ==, "1");

    str = int64_to_str_back(BACK_PTR, G_GINT64_CONSTANT(9223372036854775807));
    g_assert_cmpstr(str, ==, "9223372036854775807");
}

#include "ws_getopt.h"

#define ARGV_MAX 31

static char **new_argv(int *argc_ptr, const char *args, ...)
{
    char **argv;
    int argc = 0;
    va_list ap;

    argv = g_malloc((ARGV_MAX + 1) * sizeof(char *));

    va_start(ap, args);
    while (args != NULL) {
        /* Increase ARGV_MAX or use a dynamic size if this assertion fails. */
        g_assert_true(argc < ARGV_MAX);
        argv[argc++] = g_strdup(args);
        args = va_arg(ap, const char *);
    }
    argv[argc] = NULL;
    va_end(ap);

    *argc_ptr = argc;
    return argv;
}

static void free_argv(char **argv)
{
    for (char **p = argv; *p != NULL; p++) {
        g_free(*p);
    }
    g_free(argv);
}

static void test_getopt_long_basic1(void)
{
    char **argv;
    int argc;

    const char *optstring = "ab:c";
    argv = new_argv(&argc, "/bin/ls", "-a", "-b", "arg1", "-c", "path", (char *)NULL);

    ws_optind = 1;
    int opt;

    opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
    g_assert_cmpint(opt, ==, 'a');
    g_assert_null(ws_optarg);

    opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
    g_assert_cmpint(opt, ==, 'b');
    g_assert_cmpstr(ws_optarg, ==, "arg1");

    opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
    g_assert_cmpint(opt, ==, 'c');
    g_assert_null(ws_optarg);

    opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
    g_assert_cmpint(opt, ==, -1);

    free_argv(argv);
}

static void test_getopt_long_basic2(void)
{
    char **argv;
    int argc;

    struct option longopts[] = {
        { "opt1", no_argument, NULL, '1' },
        { "opt2", required_argument, NULL, '2' },
        { "opt3", required_argument, NULL, '3' },
        { 0, 0, 0, 0 }
    };
    argv = new_argv(&argc, "/bin/ls", "--opt1", "--opt2", "arg1", "--opt3=arg2", "path", (char *)NULL);

    ws_optind = 1;
    int opt;

    opt = ws_getopt_long(argc, argv, "", longopts, NULL);
    g_assert_cmpint(opt, ==, '1');
    g_assert_null(ws_optarg);

    opt = ws_getopt_long(argc, argv, "", longopts, NULL);
    g_assert_cmpint(opt, ==, '2');
    g_assert_cmpstr(ws_optarg, ==, "arg1");

    opt = ws_getopt_long(argc, argv, "", longopts, NULL);
    g_assert_cmpint(opt, ==, '3');
    g_assert_cmpstr(ws_optarg, ==, "arg2");

    opt = ws_getopt_long(argc, argv, "", longopts, NULL);
    g_assert_cmpint(opt, ==, -1);

    free_argv(argv);
}


static void test_getopt_opterr1(void)
{
    char **argv;
    int argc;

#ifdef _WIN32
    g_test_skip("Not supported on Windows");
    return;
#endif

    if (g_test_subprocess()) {
        const char *optstring = "ab";
        argv = new_argv(&argc, "/bin/ls", "-a", "-z", "path", (char *)NULL);

        ws_optind = 0;
        ws_opterr = 1;
        int opt;

        opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
        g_assert_cmpint(opt, ==, 'a');

        opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
        g_assert_cmpint(opt, ==, '?');
        g_assert_cmpint(ws_optopt, ==, 'z');

        opt = ws_getopt_long(argc, argv, optstring, NULL, NULL);
        g_assert_cmpint(opt, ==, -1);

        free_argv(argv);

        return;
    }

    g_test_trap_subprocess(NULL, 0, 0);
    g_test_trap_assert_passed();
    g_test_trap_assert_stderr("/bin/ls: unrecognized option: z\n");
}

int main(int argc, char **argv)
{
    int ret;

    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/str_util/format_size", test_format_size);

    g_test_add_func("/to_str/word_to_hex", test_word_to_hex);
    g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str);
    g_test_add_func("/to_str/bytes_to_str_punct", test_bytes_to_str_punct);
    g_test_add_func("/to_str/bytes_to_str_trunc1", test_bytes_to_string_trunc1);
    g_test_add_func("/to_str/bytes_to_str_punct_trunc1", test_bytes_to_string_punct_trunc1);
    g_test_add_func("/to_str/oct_to_str_back", test_oct_to_str_back);
    g_test_add_func("/to_str/oct64_to_str_back", test_oct64_to_str_back);
    g_test_add_func("/to_str/hex_to_str_back_len", test_hex_to_str_back_len);
    g_test_add_func("/to_str/hex64_to_str_back_len", test_hex64_to_str_back_len);
    g_test_add_func("/to_str/uint_to_str_back", test_uint_to_str_back);
    g_test_add_func("/to_str/uint64_to_str_back", test_uint64_to_str_back);
    g_test_add_func("/to_str/uint_to_str_back_len", test_uint_to_str_back_len);
    g_test_add_func("/to_str/uint64_to_str_back_len", test_uint64_to_str_back_len);
    g_test_add_func("/to_str/int_to_str_back", test_int_to_str_back);
    g_test_add_func("/to_str/int64_to_str_back", test_int64_to_str_back);

    g_test_add_func("/ws_getopt/basic1", test_getopt_long_basic1);
    g_test_add_func("/ws_getopt/basic2", test_getopt_long_basic2);
    g_test_add_func("/ws_getopt/opterr1", test_getopt_opterr1);

    ret = g_test_run();

    return ret;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */