2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 1998 Gerald Combs
6 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include <wsutil/utf8_entities.h>
14 #include <wsutil/time_util.h>
15 #include <wsutil/to_str.h>
17 #include "inet_addr.h"
19 #define PROGNAME "test_wsutil"
21 static void test_inet_pton4_test1(void)
25 ws_in4_addr result
, expect
;
27 str
= "198.51.100.200";
28 expect
= g_htonl(3325256904);
29 ok
= ws_inet_pton4(str
, &result
);
31 g_assert_cmpint(result
, ==, expect
);
34 static void test_inet_ntop4_test1(void)
36 char result
[WS_INET_ADDRSTRLEN
];
37 const char *expect
, *ptr
;
40 addr
= g_htonl(3325256904);
41 expect
= "198.51.100.200";
42 ptr
= ws_inet_ntop4(&addr
, result
, sizeof(result
));
43 g_assert_true(ptr
== result
);
44 g_assert_cmpstr(result
, ==, expect
);
48 char str
[WS_INET6_ADDRSTRLEN
];
52 static const struct in6_test in6_test1
= {
53 .str
= "2001:db8:ffaa:ddbb:1199:2288:3377:1",
54 .addr
= { { 0x20, 0x01, 0x0d, 0xb8, 0xff, 0xaa, 0xdd, 0xbb,
55 0x11, 0x99, 0x22, 0x88, 0x33, 0x77, 0x00, 0x01 } }
58 static void test_inet_pton6_test1(void)
63 ok
= ws_inet_pton6(in6_test1
.str
, &result
);
65 g_assert_cmpmem(&result
, sizeof(result
), &in6_test1
.addr
, sizeof(in6_test1
.addr
));
68 static void test_inet_ntop6_test1(void)
70 char result
[WS_INET6_ADDRSTRLEN
];
73 ptr
= ws_inet_ntop6(&in6_test1
.addr
, result
, sizeof(result
));
74 g_assert_true(ptr
== result
);
75 g_assert_cmpstr(result
, ==, in6_test1
.str
);
78 static void test_ip_addr_to_str_test1(void)
80 char result
[WS_INET_ADDRSTRLEN
];
84 addr
= g_htonl(3325256904);
85 expect
= "198.51.100.200";
86 ip_addr_to_str_buf(&addr
, result
, sizeof(result
));
88 g_assert_cmpstr(result
, ==, expect
);
93 static void test_format_size(void)
97 str
= format_size(10000, FORMAT_SIZE_UNIT_BYTES
, FORMAT_SIZE_PREFIX_SI
);
98 g_assert_cmpstr(str
, ==, "10 kB");
101 str
= format_size(100000, FORMAT_SIZE_UNIT_BYTES
, FORMAT_SIZE_PREFIX_IEC
);
102 g_assert_cmpstr(str
, ==, "97 KiB");
105 str
= format_size(20971520, FORMAT_SIZE_UNIT_BITS
, FORMAT_SIZE_PREFIX_IEC
);
106 g_assert_cmpstr(str
, ==, "20 Mib");
110 static void test_escape_string(void)
114 buf
= ws_escape_string(NULL
, "quoted \"\\\" backslash", true);
115 g_assert_cmpstr(buf
, ==, "\"quoted \\\"\\\\\\\" backslash\"");
116 wmem_free(NULL
, buf
);
118 buf
= ws_escape_string(NULL
, "whitespace \t \n \r \f \v", true);
119 g_assert_cmpstr(buf
, ==, "\"whitespace \\t \\n \\r \\f \\v""\"");
120 wmem_free(NULL
, buf
);
122 const char s1
[] = { 'a', 'b', 'c', '\0', 'e', 'f', 'g'};
123 buf
= ws_escape_null(NULL
, s1
, sizeof(s1
), true);
124 g_assert_cmpstr(buf
, ==, "\"abc\\0efg\"");
125 wmem_free(NULL
, buf
);
127 const char s2
[] = { 'a', 'b', 'c', '\0', '"', 'e', 'f', 'g'};
128 buf
= ws_escape_null(NULL
, s2
, sizeof(s2
), true);
129 g_assert_cmpstr(buf
, ==, "\"abc\\0\\\"efg\"");
130 wmem_free(NULL
, buf
);
132 buf
= ws_escape_csv(NULL
, "CSV-style \" escape", true, '"', true, false);
133 g_assert_cmpstr(buf
, ==, "\"CSV-style \"\" escape\"");
134 wmem_free(NULL
, buf
);
137 static void test_strconcat(void)
139 wmem_allocator_t
*allocator
;
142 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
144 new_str
= wmem_strconcat(allocator
, "ABC", NULL
);
145 g_assert_cmpstr(new_str
, ==, "ABC");
147 new_str
= wmem_strconcat(allocator
, "ABC", "DEF", NULL
);
148 g_assert_cmpstr(new_str
, ==, "ABCDEF");
150 new_str
= wmem_strconcat(allocator
, "", "", "ABCDEF", "", "GH", NULL
);
151 g_assert_cmpstr(new_str
, ==, "ABCDEFGH");
153 wmem_destroy_allocator(allocator
);
156 static void test_strsplit(void)
158 wmem_allocator_t
*allocator
;
161 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
163 split_str
= wmem_strsplit(allocator
, "A-C", "-", 2);
164 g_assert_cmpstr(split_str
[0], ==, "A");
165 g_assert_cmpstr(split_str
[1], ==, "C");
166 g_assert_null(split_str
[2]);
168 split_str
= wmem_strsplit(allocator
, "A-C", "-", 0);
169 g_assert_cmpstr(split_str
[0], ==, "A");
170 g_assert_cmpstr(split_str
[1], ==, "C");
171 g_assert_null(split_str
[2]);
173 split_str
= wmem_strsplit(allocator
, "--aslkf-asio--asfj-as--", "-", 10);
174 g_assert_cmpstr(split_str
[0], ==, "");
175 g_assert_cmpstr(split_str
[1], ==, "");
176 g_assert_cmpstr(split_str
[2], ==, "aslkf");
177 g_assert_cmpstr(split_str
[3], ==, "asio");
178 g_assert_cmpstr(split_str
[4], ==, "");
179 g_assert_cmpstr(split_str
[5], ==, "asfj");
180 g_assert_cmpstr(split_str
[6], ==, "as");
181 g_assert_cmpstr(split_str
[7], ==, "");
182 g_assert_cmpstr(split_str
[8], ==, "");
183 g_assert_null(split_str
[9]);
185 split_str
= wmem_strsplit(allocator
, "--aslkf-asio--asfj-as--", "-", 5);
186 g_assert_cmpstr(split_str
[0], ==, "");
187 g_assert_cmpstr(split_str
[1], ==, "");
188 g_assert_cmpstr(split_str
[2], ==, "aslkf");
189 g_assert_cmpstr(split_str
[3], ==, "asio");
190 g_assert_cmpstr(split_str
[4], ==, "-asfj-as--");
191 g_assert_null(split_str
[5]);
193 split_str
= wmem_strsplit(allocator
, "", "-", -1);
194 g_assert_null(split_str
[0]);
196 wmem_destroy_allocator(allocator
);
199 static void test_str_ascii(void)
201 wmem_allocator_t
*allocator
;
202 const char *orig_str
;
205 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
207 orig_str
= "TeStAsCiIsTrDoWn";
208 new_str
= wmem_ascii_strdown(allocator
, orig_str
, -1);
209 g_assert_cmpstr(new_str
, ==, "testasciistrdown");
211 wmem_destroy_allocator(allocator
);
214 static void test_format_text(void)
216 const char *have
, *want
;
222 res
= format_text_string(NULL
, have
);
223 g_assert_cmpstr(res
, ==, want
);
226 /* ASCII with special escape characters. */
227 have
= "abc\td\fe\nf";
228 want
= "abc\\td\\fe\\nf";
229 res
= format_text_string(NULL
, have
);
230 g_assert_cmpstr(res
, ==, want
);
233 /* ASCII with non-printable characters. */
234 have
= "abc \004 def";
235 want
= "abc \\004 def";
236 res
= format_text_string(NULL
, have
);
237 g_assert_cmpstr(res
, ==, want
);
241 have
= u8
"Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο";
242 want
= u8
"Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο";
243 res
= format_text_string(NULL
, have
);
244 g_assert_cmpstr(res
, ==, want
);
247 /* UTF-8 with non-ASCII non-printable characters. */
248 have
= u8
"String with BOM \ufeff";
249 want
= u8
"String with BOM \\uFEFF";
250 res
= format_text_string(NULL
, have
);
251 g_assert_cmpstr(res
, ==, want
);
256 #define RESOURCE_USAGE_START get_resource_usage(&start_utime, &start_stime)
258 #define RESOURCE_USAGE_END \
259 get_resource_usage(&end_utime, &end_stime); \
260 utime_ms = (end_utime - start_utime) * 1000.0; \
261 stime_ms = (end_stime - start_stime) * 1000.0
263 static void test_format_text_perf(void)
265 #define LOOP_COUNT (1 * 1000 * 1000)
268 double start_utime
, start_stime
, end_utime
, end_stime
, utime_ms
, stime_ms
;
270 const char *text
= "The quick brown fox\tjumps over the lazy \001dog"UTF8_HORIZONTAL_ELLIPSIS
"\n";
272 RESOURCE_USAGE_START
;
273 for (i
= 0; i
< LOOP_COUNT
; i
++) {
274 str
= format_text_string(NULL
, text
);
278 g_test_minimized_result(utime_ms
+ stime_ms
,
279 "format_text_string(): u %.3f ms s %.3f ms", utime_ms
, stime_ms
);
284 static void test_word_to_hex(void)
287 char *str
; /* String is not NULL terminated. */
289 str
= uint8_to_hex(buf
, 0x34);
290 g_assert_true(str
== buf
+ 2);
291 g_assert_cmpint(str
[-1], ==, '4');
292 g_assert_cmpint(str
[-2], ==, '3');
294 str
= word_to_hex(buf
, 0x1234);
295 g_assert_true(str
== buf
+ 4);
296 g_assert_cmpint(str
[-1], ==, '4');
297 g_assert_cmpint(str
[-2], ==, '3');
298 g_assert_cmpint(str
[-3], ==, '2');
299 g_assert_cmpint(str
[-4], ==, '1');
301 str
= dword_to_hex(buf
, 0x1234);
302 g_assert_true(str
== buf
+ 8);
303 g_assert_cmpint(str
[-1], ==, '4');
304 g_assert_cmpint(str
[-2], ==, '3');
305 g_assert_cmpint(str
[-3], ==, '2');
306 g_assert_cmpint(str
[-4], ==, '1');
307 g_assert_cmpint(str
[-5], ==, '0');
308 g_assert_cmpint(str
[-6], ==, '0');
309 g_assert_cmpint(str
[-7], ==, '0');
310 g_assert_cmpint(str
[-8], ==, '0');
312 str
= qword_to_hex(buf
, UINT64_C(0xFEDCBA987654321));
313 g_assert_true(str
== buf
+ 16);
314 g_assert_cmpint(str
[-1], ==, '1');
315 g_assert_cmpint(str
[-2], ==, '2');
316 g_assert_cmpint(str
[-3], ==, '3');
317 g_assert_cmpint(str
[-4], ==, '4');
318 g_assert_cmpint(str
[-5], ==, '5');
319 g_assert_cmpint(str
[-6], ==, '6');
320 g_assert_cmpint(str
[-7], ==, '7');
321 g_assert_cmpint(str
[-8], ==, '8');
322 g_assert_cmpint(str
[-9], ==, '9');
323 g_assert_cmpint(str
[-10], ==, 'a');
324 g_assert_cmpint(str
[-11], ==, 'b');
325 g_assert_cmpint(str
[-12], ==, 'c');
326 g_assert_cmpint(str
[-13], ==, 'd');
327 g_assert_cmpint(str
[-14], ==, 'e');
328 g_assert_cmpint(str
[-15], ==, 'f');
329 g_assert_cmpint(str
[-16], ==, '0');
332 static void test_bytes_to_str(void)
336 const uint8_t buf
[] = { 1, 2, 3};
338 str
= bytes_to_str(NULL
, buf
, sizeof(buf
));
339 g_assert_cmpstr(str
, ==, "010203");
343 static void test_bytes_to_str_punct(void)
347 const uint8_t buf
[] = { 1, 2, 3};
349 str
= bytes_to_str_punct(NULL
, buf
, sizeof(buf
), ':');
350 g_assert_cmpstr(str
, ==, "01:02:03");
354 static void test_bytes_to_str_punct_maxlen(void)
358 const uint8_t buf
[] = { 1, 2, 3};
360 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 4);
361 g_assert_cmpstr(str
, ==, "01:02:03");
364 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 3);
365 g_assert_cmpstr(str
, ==, "01:02:03");
368 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 2);
369 g_assert_cmpstr(str
, ==, "01:02:" UTF8_HORIZONTAL_ELLIPSIS
);
372 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 1);
373 g_assert_cmpstr(str
, ==, "01:" UTF8_HORIZONTAL_ELLIPSIS
);
376 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 0);
377 g_assert_cmpstr(str
, ==, "01:02:03");
381 static void test_bytes_to_str_maxlen(void)
385 const uint8_t buf
[] = { 1, 2, 3};
387 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 4);
388 g_assert_cmpstr(str
, ==, "010203");
391 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 3);
392 g_assert_cmpstr(str
, ==, "010203");
395 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 2);
396 g_assert_cmpstr(str
, ==, "0102" UTF8_HORIZONTAL_ELLIPSIS
);
399 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 1);
400 g_assert_cmpstr(str
, ==, "01" UTF8_HORIZONTAL_ELLIPSIS
);
403 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 0);
404 g_assert_cmpstr(str
, ==, "010203");
408 static void test_bytes_to_string_trunc1(void)
412 const uint8_t buf
[] = {
413 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
414 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
415 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
416 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
417 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
418 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
419 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA
422 "112233445566778899aa"
423 "112233445566778899aa"
424 "112233445566778899aa"
425 "112233445566" UTF8_HORIZONTAL_ELLIPSIS
;
427 str
= bytes_to_str(NULL
, buf
, sizeof(buf
));
428 g_assert_cmpstr(str
, ==, expect
);
432 static void test_bytes_to_string_punct_trunc1(void)
436 const uint8_t buf
[] = {
437 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
438 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
439 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
440 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
441 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
442 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
443 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA
446 "11:22:33:44:55:66:77:88:99:aa:"
447 "11:22:33:44:55:66:77:88:99:aa:"
448 "11:22:33:44:" UTF8_HORIZONTAL_ELLIPSIS
;
450 str
= bytes_to_str_punct(NULL
, buf
, sizeof(buf
), ':');
451 g_assert_cmpstr(str
, ==, expect
);
455 static char to_str_back_buf
[32];
456 #define BACK_PTR (&to_str_back_buf[31]) /* pointer to NUL string terminator */
458 static void test_oct_to_str_back(void)
462 str
= oct_to_str_back(BACK_PTR
, 958769886);
463 g_assert_cmpstr(str
, ==, "07111325336");
465 str
= oct_to_str_back(BACK_PTR
, 781499127);
466 g_assert_cmpstr(str
, ==, "05645135367");
468 str
= oct_to_str_back(BACK_PTR
, 1177329882);
469 g_assert_cmpstr(str
, ==, "010613120332");
472 static void test_oct64_to_str_back(void)
476 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(13873797580070999420));
477 g_assert_cmpstr(str
, ==, "01402115026217563452574");
479 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(7072159458371400691));
480 g_assert_cmpstr(str
, ==, "0610452670726711271763");
482 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(12453513102400590374));
483 g_assert_cmpstr(str
, ==, "01263236102754220511046");
486 static void test_hex_to_str_back_len(void)
490 str
= hex_to_str_back_len(BACK_PTR
, 2481, 8);
491 g_assert_cmpstr(str
, ==, "0x000009b1");
493 str
= hex_to_str_back_len(BACK_PTR
, 2457, 8);
494 g_assert_cmpstr(str
, ==, "0x00000999");
496 str
= hex_to_str_back_len(BACK_PTR
, 16230, 8);
497 g_assert_cmpstr(str
, ==, "0x00003f66");
500 static void test_hex64_to_str_back_len(void)
504 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(1), 16);
505 g_assert_cmpstr(str
, ==, "0x0000000000000001");
507 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(4294967295), 16);
508 g_assert_cmpstr(str
, ==, "0x00000000ffffffff");
510 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(18446744073709551615), 16);
511 g_assert_cmpstr(str
, ==, "0xffffffffffffffff");
514 static void test_uint_to_str_back(void)
518 str
= uint_to_str_back(BACK_PTR
, 873735883);
519 g_assert_cmpstr(str
, ==, "873735883");
521 str
= uint_to_str_back(BACK_PTR
, 1801148094);
522 g_assert_cmpstr(str
, ==, "1801148094");
524 str
= uint_to_str_back(BACK_PTR
, 181787997);
525 g_assert_cmpstr(str
, ==, "181787997");
528 static void test_uint64_to_str_back(void)
532 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(585143757104211265));
533 g_assert_cmpstr(str
, ==, "585143757104211265");
535 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(7191580247919484847));
536 g_assert_cmpstr(str
, ==, "7191580247919484847");
538 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(95778573911934485));
539 g_assert_cmpstr(str
, ==, "95778573911934485");
542 static void test_uint_to_str_back_len(void)
546 str
= uint_to_str_back_len(BACK_PTR
, 26630, 8);
547 g_assert_cmpstr(str
, ==, "00026630");
549 str
= uint_to_str_back_len(BACK_PTR
, 25313, 8);
550 g_assert_cmpstr(str
, ==, "00025313");
552 str
= uint_to_str_back_len(BACK_PTR
, 18750000, 8);
553 g_assert_cmpstr(str
, ==, "18750000");
556 static void test_uint64_to_str_back_len(void)
560 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(1), 16);
561 g_assert_cmpstr(str
, ==, "0000000000000001");
563 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(4294967295), 16);
564 g_assert_cmpstr(str
, ==, "0000004294967295");
566 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(18446744073709551615), 16);
567 g_assert_cmpstr(str
, ==, "18446744073709551615");
570 static void test_int_to_str_back(void)
574 str
= int_to_str_back(BACK_PTR
, -763689611);
575 g_assert_cmpstr(str
, ==, "-763689611");
577 str
= int_to_str_back(BACK_PTR
, -296015954);
578 g_assert_cmpstr(str
, ==, "-296015954");
580 str
= int_to_str_back(BACK_PTR
, 898901469);
581 g_assert_cmpstr(str
, ==, "898901469");
584 static void test_int64_to_str_back(void)
588 str
= int64_to_str_back(BACK_PTR
, INT64_C(-9223372036854775807));
589 g_assert_cmpstr(str
, ==, "-9223372036854775807");
591 str
= int64_to_str_back(BACK_PTR
, INT64_C(1));
592 g_assert_cmpstr(str
, ==, "1");
594 str
= int64_to_str_back(BACK_PTR
, INT64_C(9223372036854775807));
595 g_assert_cmpstr(str
, ==, "9223372036854775807");
599 #include "time_util.h"
601 void test_nstime_from_iso8601(void)
605 nstime_t result
, expect
;
608 memset(&tm1
, 0, sizeof(tm1
));
613 tm1
.tm_mon
= 4; /* starts at zero */
614 tm1
.tm_year
= 2013 - 1900;
617 /* Date and time with local time. */
618 str
= "2013-05-30T23:45:25.349124";
619 expect
.secs
= mktime(&tm1
);
620 expect
.nsecs
= 349124 * 1000;
621 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
622 g_assert_nonnull(endp
);
623 g_assert(*endp
== '\0');
624 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
625 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
627 /* Date and time with UTC timezone. */
628 str
= "2013-05-30T23:45:25.349124Z";
629 expect
.secs
= mktime_utc(&tm1
);
630 expect
.nsecs
= 349124 * 1000;
631 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
632 g_assert_nonnull(endp
);
633 g_assert(*endp
== '\0');
634 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
635 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
637 /* Date and time with timezone offset with separator. */
638 str
= "2013-05-30T23:45:25.349124+01:00";
639 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
640 expect
.nsecs
= 349124 * 1000;
641 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
642 g_assert_nonnull(endp
);
643 g_assert(*endp
== '\0');
644 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
645 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
647 /* Date and time with timezone offset without separator. */
648 str
= "2013-05-30T23:45:25.349124+0100";
649 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
650 expect
.nsecs
= 349124 * 1000;
651 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
652 g_assert_nonnull(endp
);
653 g_assert(*endp
== '\0');
654 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
655 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
657 /* Date and time with timezone offset with hours only. */
658 str
= "2013-05-30T23:45:25.349124+01";
659 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
660 expect
.nsecs
= 349124 * 1000;
661 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
662 g_assert_nonnull(endp
);
663 g_assert(*endp
== '\0');
664 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
665 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
668 #include "ws_getopt.h"
672 static char **new_argv(int *argc_ptr
, const char *args
, ...)
678 argv
= g_malloc((ARGV_MAX
+ 1) * sizeof(char *));
681 while (args
!= NULL
) {
682 /* Increase ARGV_MAX or use a dynamic size if this assertion fails. */
683 g_assert_true(argc
< ARGV_MAX
);
684 argv
[argc
++] = g_strdup(args
);
685 args
= va_arg(ap
, const char *);
694 static void free_argv(char **argv
)
696 for (char **p
= argv
; *p
!= NULL
; p
++) {
702 static void test_getopt_long_basic1(void)
707 const char *optstring
= "ab:c";
708 argv
= new_argv(&argc
, "/bin/ls", "-a", "-b", "arg1", "-c", "path", (char *)NULL
);
713 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
714 g_assert_cmpint(opt
, ==, 'a');
715 g_assert_null(ws_optarg
);
717 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
718 g_assert_cmpint(opt
, ==, 'b');
719 g_assert_cmpstr(ws_optarg
, ==, "arg1");
721 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
722 g_assert_cmpint(opt
, ==, 'c');
723 g_assert_null(ws_optarg
);
725 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
726 g_assert_cmpint(opt
, ==, -1);
731 static void test_getopt_long_basic2(void)
736 struct ws_option longopts
[] = {
737 { "opt1", ws_no_argument
, NULL
, '1' },
738 { "opt2", ws_required_argument
, NULL
, '2' },
739 { "opt3", ws_required_argument
, NULL
, '3' },
742 argv
= new_argv(&argc
, "/bin/ls", "--opt1", "--opt2", "arg1", "--opt3=arg2", "path", (char *)NULL
);
747 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
748 g_assert_cmpint(opt
, ==, '1');
749 g_assert_null(ws_optarg
);
751 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
752 g_assert_cmpint(opt
, ==, '2');
753 g_assert_cmpstr(ws_optarg
, ==, "arg1");
755 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
756 g_assert_cmpint(opt
, ==, '3');
757 g_assert_cmpstr(ws_optarg
, ==, "arg2");
759 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
760 g_assert_cmpint(opt
, ==, -1);
765 static void test_getopt_optional_argument1(void)
771 struct ws_option longopts_optional
[] = {
772 { "optional", ws_optional_argument
, NULL
, '1' },
776 argv
= new_argv(&argc
, "/bin/ls", "--optional=arg1", (char *)NULL
);
779 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
780 g_assert_cmpint(opt
, ==, '1');
781 g_assert_cmpstr(ws_optarg
, ==, "arg1");
784 argv
= new_argv(&argc
, "/bin/ls", "--optional", "arg1", (char *)NULL
);
787 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
788 g_assert_cmpint(opt
, ==, '1');
789 /* Optional argument does not recognize the form "--arg param" (it's ambiguous). */
790 g_assert_null(ws_optarg
);
793 argv
= new_argv(&argc
, "/bin/ls", "--optional", (char *)NULL
);
796 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
797 g_assert_cmpint(opt
, ==, '1');
798 g_assert_null(ws_optarg
);
803 static void test_getopt_opterr1(void)
809 g_test_skip("Not supported on Windows");
813 if (g_test_subprocess()) {
814 const char *optstring
= "ab";
817 * Yes, this has a different command name from this
818 * program's name - and from the name in the error
819 * message that we're testing. ws_getopt() and
820 * ws_getopt_long() use the name set by g_set_prgname(),
821 * not the argv[0] string, as the program name in the
822 * error message, just as other error message routines do.
824 argv
= new_argv(&argc
, "/bin/ls", "-a", "-z", "path", (char *)NULL
);
830 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
831 g_assert_cmpint(opt
, ==, 'a');
833 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
834 g_assert_cmpint(opt
, ==, '?');
835 g_assert_cmpint(ws_optopt
, ==, 'z');
837 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
838 g_assert_cmpint(opt
, ==, -1);
845 g_test_trap_subprocess(NULL
, 0, 0);
846 g_test_trap_assert_passed();
847 g_test_trap_assert_stderr(PROGNAME
": unrecognized option: z\n");
850 int main(int argc
, char **argv
)
854 /* Set the program name. */
855 g_set_prgname(PROGNAME
);
859 g_test_init(&argc
, &argv
, NULL
);
861 g_test_add_func("/inet_addr/inet_pton4", test_inet_pton4_test1
);
862 g_test_add_func("/inet_addr/inet_ntop4", test_inet_ntop4_test1
);
863 g_test_add_func("/inet_addr/inet_pton6", test_inet_pton6_test1
);
864 g_test_add_func("/inet_addr/inet_ntop6", test_inet_ntop6_test1
);
866 g_test_add_func("/str_util/format_size", test_format_size
);
867 g_test_add_func("/str_util/escape_string", test_escape_string
);
868 g_test_add_func("/str_util/strconcat", test_strconcat
);
869 g_test_add_func("/str_util/strsplit", test_strsplit
);
870 g_test_add_func("/str_util/str_ascii", test_str_ascii
);
871 g_test_add_func("/str_util/format_text", test_format_text
);
874 g_test_add_func("/str_util/format_text_perf", test_format_text_perf
);
877 g_test_add_func("/to_str/word_to_hex", test_word_to_hex
);
878 g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str
);
879 g_test_add_func("/to_str/bytes_to_str_punct", test_bytes_to_str_punct
);
880 g_test_add_func("/to_str/bytes_to_str_maxlen", test_bytes_to_str_maxlen
);
881 g_test_add_func("/to_str/bytes_to_str_punct_maxlen", test_bytes_to_str_punct_maxlen
);
882 g_test_add_func("/to_str/bytes_to_str_trunc1", test_bytes_to_string_trunc1
);
883 g_test_add_func("/to_str/bytes_to_str_punct_trunc1", test_bytes_to_string_punct_trunc1
);
884 g_test_add_func("/to_str/oct_to_str_back", test_oct_to_str_back
);
885 g_test_add_func("/to_str/oct64_to_str_back", test_oct64_to_str_back
);
886 g_test_add_func("/to_str/hex_to_str_back_len", test_hex_to_str_back_len
);
887 g_test_add_func("/to_str/hex64_to_str_back_len", test_hex64_to_str_back_len
);
888 g_test_add_func("/to_str/uint_to_str_back", test_uint_to_str_back
);
889 g_test_add_func("/to_str/uint64_to_str_back", test_uint64_to_str_back
);
890 g_test_add_func("/to_str/uint_to_str_back_len", test_uint_to_str_back_len
);
891 g_test_add_func("/to_str/uint64_to_str_back_len", test_uint64_to_str_back_len
);
892 g_test_add_func("/to_str/int_to_str_back", test_int_to_str_back
);
893 g_test_add_func("/to_str/int64_to_str_back", test_int64_to_str_back
);
894 g_test_add_func("/to_str/ip_addr_to_str_test1", test_ip_addr_to_str_test1
);
896 g_test_add_func("/nstime/from_iso8601", test_nstime_from_iso8601
);
898 g_test_add_func("/ws_getopt/basic1", test_getopt_long_basic1
);
899 g_test_add_func("/ws_getopt/basic2", test_getopt_long_basic2
);
900 g_test_add_func("/ws_getopt/optional1", test_getopt_optional_argument1
);
901 g_test_add_func("/ws_getopt/opterr1", test_getopt_opterr1
);
909 * Editor modelines - https://www.wireshark.org/tools/modelines.html
914 * indent-tabs-mode: nil
917 * vi: set shiftwidth=4 tabstop=8 expandtab:
918 * :indentSize=4:tabSize=8:noTabs=true: