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 static void test_inet_pton4_test1(void)
23 ws_in4_addr result
, expect
;
25 str
= "198.51.100.200";
26 expect
= g_htonl(3325256904);
27 ok
= ws_inet_pton4(str
, &result
);
29 g_assert_cmpint(result
, ==, expect
);
32 static void test_inet_ntop4_test1(void)
34 char result
[WS_INET_ADDRSTRLEN
];
35 const char *expect
, *ptr
;
38 addr
= g_htonl(3325256904);
39 expect
= "198.51.100.200";
40 ptr
= ws_inet_ntop4(&addr
, result
, sizeof(result
));
41 g_assert_true(ptr
== result
);
42 g_assert_cmpstr(result
, ==, expect
);
46 char str
[WS_INET6_ADDRSTRLEN
];
50 static const struct in6_test in6_test1
= {
51 .str
= "2001:db8:ffaa:ddbb:1199:2288:3377:1",
52 .addr
= { { 0x20, 0x01, 0x0d, 0xb8, 0xff, 0xaa, 0xdd, 0xbb,
53 0x11, 0x99, 0x22, 0x88, 0x33, 0x77, 0x00, 0x01 } }
56 static void test_inet_pton6_test1(void)
61 ok
= ws_inet_pton6(in6_test1
.str
, &result
);
63 g_assert_cmpmem(&result
, sizeof(result
), &in6_test1
.addr
, sizeof(in6_test1
.addr
));
66 static void test_inet_ntop6_test1(void)
68 char result
[WS_INET6_ADDRSTRLEN
];
71 ptr
= ws_inet_ntop6(&in6_test1
.addr
, result
, sizeof(result
));
72 g_assert_true(ptr
== result
);
73 g_assert_cmpstr(result
, ==, in6_test1
.str
);
76 static void test_ip_addr_to_str_test1(void)
78 char result
[WS_INET_ADDRSTRLEN
];
82 addr
= g_htonl(3325256904);
83 expect
= "198.51.100.200";
84 ip_addr_to_str_buf(&addr
, result
, sizeof(result
));
86 g_assert_cmpstr(result
, ==, expect
);
91 static void test_format_size(void)
95 str
= format_size(10000, FORMAT_SIZE_UNIT_BYTES
, FORMAT_SIZE_PREFIX_SI
);
96 g_assert_cmpstr(str
, ==, "10 kB");
99 str
= format_size(100000, FORMAT_SIZE_UNIT_BYTES
, FORMAT_SIZE_PREFIX_IEC
);
100 g_assert_cmpstr(str
, ==, "97 KiB");
103 str
= format_size(20971520, FORMAT_SIZE_UNIT_BITS
, FORMAT_SIZE_PREFIX_IEC
);
104 g_assert_cmpstr(str
, ==, "20 Mib");
108 static void test_escape_string(void)
112 buf
= ws_escape_string(NULL
, "quoted \"\\\" backslash", true);
113 g_assert_cmpstr(buf
, ==, "\"quoted \\\"\\\\\\\" backslash\"");
114 wmem_free(NULL
, buf
);
116 buf
= ws_escape_string(NULL
, "whitespace \t \n \r \f \v", true);
117 g_assert_cmpstr(buf
, ==, "\"whitespace \\t \\n \\r \\f \\v""\"");
118 wmem_free(NULL
, buf
);
120 const char s1
[] = { 'a', 'b', 'c', '\0', 'e', 'f', 'g'};
121 buf
= ws_escape_null(NULL
, s1
, sizeof(s1
), true);
122 g_assert_cmpstr(buf
, ==, "\"abc\\0efg\"");
123 wmem_free(NULL
, buf
);
125 const char s2
[] = { 'a', 'b', 'c', '\0', '"', 'e', 'f', 'g'};
126 buf
= ws_escape_null(NULL
, s2
, sizeof(s2
), true);
127 g_assert_cmpstr(buf
, ==, "\"abc\\0\\\"efg\"");
128 wmem_free(NULL
, buf
);
130 buf
= ws_escape_csv(NULL
, "CSV-style \" escape", true, '"', true, false);
131 g_assert_cmpstr(buf
, ==, "\"CSV-style \"\" escape\"");
132 wmem_free(NULL
, buf
);
135 static void test_strconcat(void)
137 wmem_allocator_t
*allocator
;
140 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
142 new_str
= wmem_strconcat(allocator
, "ABC", NULL
);
143 g_assert_cmpstr(new_str
, ==, "ABC");
145 new_str
= wmem_strconcat(allocator
, "ABC", "DEF", NULL
);
146 g_assert_cmpstr(new_str
, ==, "ABCDEF");
148 new_str
= wmem_strconcat(allocator
, "", "", "ABCDEF", "", "GH", NULL
);
149 g_assert_cmpstr(new_str
, ==, "ABCDEFGH");
151 wmem_destroy_allocator(allocator
);
154 static void test_strsplit(void)
156 wmem_allocator_t
*allocator
;
159 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
161 split_str
= wmem_strsplit(allocator
, "A-C", "-", 2);
162 g_assert_cmpstr(split_str
[0], ==, "A");
163 g_assert_cmpstr(split_str
[1], ==, "C");
164 g_assert_null(split_str
[2]);
166 split_str
= wmem_strsplit(allocator
, "A-C", "-", 0);
167 g_assert_cmpstr(split_str
[0], ==, "A");
168 g_assert_cmpstr(split_str
[1], ==, "C");
169 g_assert_null(split_str
[2]);
171 split_str
= wmem_strsplit(allocator
, "--aslkf-asio--asfj-as--", "-", 10);
172 g_assert_cmpstr(split_str
[0], ==, "");
173 g_assert_cmpstr(split_str
[1], ==, "");
174 g_assert_cmpstr(split_str
[2], ==, "aslkf");
175 g_assert_cmpstr(split_str
[3], ==, "asio");
176 g_assert_cmpstr(split_str
[4], ==, "");
177 g_assert_cmpstr(split_str
[5], ==, "asfj");
178 g_assert_cmpstr(split_str
[6], ==, "as");
179 g_assert_cmpstr(split_str
[7], ==, "");
180 g_assert_cmpstr(split_str
[8], ==, "");
181 g_assert_null(split_str
[9]);
183 split_str
= wmem_strsplit(allocator
, "--aslkf-asio--asfj-as--", "-", 5);
184 g_assert_cmpstr(split_str
[0], ==, "");
185 g_assert_cmpstr(split_str
[1], ==, "");
186 g_assert_cmpstr(split_str
[2], ==, "aslkf");
187 g_assert_cmpstr(split_str
[3], ==, "asio");
188 g_assert_cmpstr(split_str
[4], ==, "-asfj-as--");
189 g_assert_null(split_str
[5]);
191 split_str
= wmem_strsplit(allocator
, "", "-", -1);
192 g_assert_null(split_str
[0]);
194 wmem_destroy_allocator(allocator
);
197 static void test_str_ascii(void)
199 wmem_allocator_t
*allocator
;
200 const char *orig_str
;
203 allocator
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
205 orig_str
= "TeStAsCiIsTrDoWn";
206 new_str
= wmem_ascii_strdown(allocator
, orig_str
, -1);
207 g_assert_cmpstr(new_str
, ==, "testasciistrdown");
209 wmem_destroy_allocator(allocator
);
212 static void test_format_text(void)
214 const char *have
, *want
;
220 res
= format_text_string(NULL
, have
);
221 g_assert_cmpstr(res
, ==, want
);
224 /* ASCII with special escape characters. */
225 have
= "abc\td\fe\nf";
226 want
= "abc\\td\\fe\\nf";
227 res
= format_text_string(NULL
, have
);
228 g_assert_cmpstr(res
, ==, want
);
231 /* ASCII with non-printable characters. */
232 have
= "abc \004 def";
233 want
= "abc \\004 def";
234 res
= format_text_string(NULL
, have
);
235 g_assert_cmpstr(res
, ==, want
);
239 have
= u8
"Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο";
240 want
= u8
"Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο";
241 res
= format_text_string(NULL
, have
);
242 g_assert_cmpstr(res
, ==, want
);
245 /* UTF-8 with non-ASCII non-printable characters. */
246 have
= u8
"String with BOM \ufeff";
247 want
= u8
"String with BOM \\uFEFF";
248 res
= format_text_string(NULL
, have
);
249 g_assert_cmpstr(res
, ==, want
);
254 #define RESOURCE_USAGE_START get_resource_usage(&start_utime, &start_stime)
256 #define RESOURCE_USAGE_END \
257 get_resource_usage(&end_utime, &end_stime); \
258 utime_ms = (end_utime - start_utime) * 1000.0; \
259 stime_ms = (end_stime - start_stime) * 1000.0
261 static void test_format_text_perf(void)
263 #define LOOP_COUNT (1 * 1000 * 1000)
266 double start_utime
, start_stime
, end_utime
, end_stime
, utime_ms
, stime_ms
;
268 const char *text
= "The quick brown fox\tjumps over the lazy \001dog"UTF8_HORIZONTAL_ELLIPSIS
"\n";
270 RESOURCE_USAGE_START
;
271 for (i
= 0; i
< LOOP_COUNT
; i
++) {
272 str
= format_text_string(NULL
, text
);
276 g_test_minimized_result(utime_ms
+ stime_ms
,
277 "format_text_string(): u %.3f ms s %.3f ms", utime_ms
, stime_ms
);
282 static void test_word_to_hex(void)
285 char *str
; /* String is not NULL terminated. */
287 str
= uint8_to_hex(buf
, 0x34);
288 g_assert_true(str
== buf
+ 2);
289 g_assert_cmpint(str
[-1], ==, '4');
290 g_assert_cmpint(str
[-2], ==, '3');
292 str
= word_to_hex(buf
, 0x1234);
293 g_assert_true(str
== buf
+ 4);
294 g_assert_cmpint(str
[-1], ==, '4');
295 g_assert_cmpint(str
[-2], ==, '3');
296 g_assert_cmpint(str
[-3], ==, '2');
297 g_assert_cmpint(str
[-4], ==, '1');
299 str
= dword_to_hex(buf
, 0x1234);
300 g_assert_true(str
== buf
+ 8);
301 g_assert_cmpint(str
[-1], ==, '4');
302 g_assert_cmpint(str
[-2], ==, '3');
303 g_assert_cmpint(str
[-3], ==, '2');
304 g_assert_cmpint(str
[-4], ==, '1');
305 g_assert_cmpint(str
[-5], ==, '0');
306 g_assert_cmpint(str
[-6], ==, '0');
307 g_assert_cmpint(str
[-7], ==, '0');
308 g_assert_cmpint(str
[-8], ==, '0');
310 str
= qword_to_hex(buf
, UINT64_C(0xFEDCBA987654321));
311 g_assert_true(str
== buf
+ 16);
312 g_assert_cmpint(str
[-1], ==, '1');
313 g_assert_cmpint(str
[-2], ==, '2');
314 g_assert_cmpint(str
[-3], ==, '3');
315 g_assert_cmpint(str
[-4], ==, '4');
316 g_assert_cmpint(str
[-5], ==, '5');
317 g_assert_cmpint(str
[-6], ==, '6');
318 g_assert_cmpint(str
[-7], ==, '7');
319 g_assert_cmpint(str
[-8], ==, '8');
320 g_assert_cmpint(str
[-9], ==, '9');
321 g_assert_cmpint(str
[-10], ==, 'a');
322 g_assert_cmpint(str
[-11], ==, 'b');
323 g_assert_cmpint(str
[-12], ==, 'c');
324 g_assert_cmpint(str
[-13], ==, 'd');
325 g_assert_cmpint(str
[-14], ==, 'e');
326 g_assert_cmpint(str
[-15], ==, 'f');
327 g_assert_cmpint(str
[-16], ==, '0');
330 static void test_bytes_to_str(void)
334 const uint8_t buf
[] = { 1, 2, 3};
336 str
= bytes_to_str(NULL
, buf
, sizeof(buf
));
337 g_assert_cmpstr(str
, ==, "010203");
341 static void test_bytes_to_str_punct(void)
345 const uint8_t buf
[] = { 1, 2, 3};
347 str
= bytes_to_str_punct(NULL
, buf
, sizeof(buf
), ':');
348 g_assert_cmpstr(str
, ==, "01:02:03");
352 static void test_bytes_to_str_punct_maxlen(void)
356 const uint8_t buf
[] = { 1, 2, 3};
358 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 4);
359 g_assert_cmpstr(str
, ==, "01:02:03");
362 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 3);
363 g_assert_cmpstr(str
, ==, "01:02:03");
366 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 2);
367 g_assert_cmpstr(str
, ==, "01:02:" UTF8_HORIZONTAL_ELLIPSIS
);
370 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 1);
371 g_assert_cmpstr(str
, ==, "01:" UTF8_HORIZONTAL_ELLIPSIS
);
374 str
= bytes_to_str_punct_maxlen(NULL
, buf
, sizeof(buf
), ':', 0);
375 g_assert_cmpstr(str
, ==, "01:02:03");
379 static void test_bytes_to_str_maxlen(void)
383 const uint8_t buf
[] = { 1, 2, 3};
385 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 4);
386 g_assert_cmpstr(str
, ==, "010203");
389 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 3);
390 g_assert_cmpstr(str
, ==, "010203");
393 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 2);
394 g_assert_cmpstr(str
, ==, "0102" UTF8_HORIZONTAL_ELLIPSIS
);
397 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 1);
398 g_assert_cmpstr(str
, ==, "01" UTF8_HORIZONTAL_ELLIPSIS
);
401 str
= bytes_to_str_maxlen(NULL
, buf
, sizeof(buf
), 0);
402 g_assert_cmpstr(str
, ==, "010203");
406 static void test_bytes_to_string_trunc1(void)
410 const uint8_t buf
[] = {
411 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
412 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
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
420 "112233445566778899aa"
421 "112233445566778899aa"
422 "112233445566778899aa"
423 "112233445566" UTF8_HORIZONTAL_ELLIPSIS
;
425 str
= bytes_to_str(NULL
, buf
, sizeof(buf
));
426 g_assert_cmpstr(str
, ==, expect
);
430 static void test_bytes_to_string_punct_trunc1(void)
434 const uint8_t buf
[] = {
435 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
436 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
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
444 "11:22:33:44:55:66:77:88:99:aa:"
445 "11:22:33:44:55:66:77:88:99:aa:"
446 "11:22:33:44:" UTF8_HORIZONTAL_ELLIPSIS
;
448 str
= bytes_to_str_punct(NULL
, buf
, sizeof(buf
), ':');
449 g_assert_cmpstr(str
, ==, expect
);
453 static char to_str_back_buf
[32];
454 #define BACK_PTR (&to_str_back_buf[31]) /* pointer to NUL string terminator */
456 static void test_oct_to_str_back(void)
460 str
= oct_to_str_back(BACK_PTR
, 958769886);
461 g_assert_cmpstr(str
, ==, "07111325336");
463 str
= oct_to_str_back(BACK_PTR
, 781499127);
464 g_assert_cmpstr(str
, ==, "05645135367");
466 str
= oct_to_str_back(BACK_PTR
, 1177329882);
467 g_assert_cmpstr(str
, ==, "010613120332");
470 static void test_oct64_to_str_back(void)
474 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(13873797580070999420));
475 g_assert_cmpstr(str
, ==, "01402115026217563452574");
477 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(7072159458371400691));
478 g_assert_cmpstr(str
, ==, "0610452670726711271763");
480 str
= oct64_to_str_back(BACK_PTR
, UINT64_C(12453513102400590374));
481 g_assert_cmpstr(str
, ==, "01263236102754220511046");
484 static void test_hex_to_str_back_len(void)
488 str
= hex_to_str_back_len(BACK_PTR
, 2481, 8);
489 g_assert_cmpstr(str
, ==, "0x000009b1");
491 str
= hex_to_str_back_len(BACK_PTR
, 2457, 8);
492 g_assert_cmpstr(str
, ==, "0x00000999");
494 str
= hex_to_str_back_len(BACK_PTR
, 16230, 8);
495 g_assert_cmpstr(str
, ==, "0x00003f66");
498 static void test_hex64_to_str_back_len(void)
502 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(1), 16);
503 g_assert_cmpstr(str
, ==, "0x0000000000000001");
505 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(4294967295), 16);
506 g_assert_cmpstr(str
, ==, "0x00000000ffffffff");
508 str
= hex64_to_str_back_len(BACK_PTR
, UINT64_C(18446744073709551615), 16);
509 g_assert_cmpstr(str
, ==, "0xffffffffffffffff");
512 static void test_uint_to_str_back(void)
516 str
= uint_to_str_back(BACK_PTR
, 873735883);
517 g_assert_cmpstr(str
, ==, "873735883");
519 str
= uint_to_str_back(BACK_PTR
, 1801148094);
520 g_assert_cmpstr(str
, ==, "1801148094");
522 str
= uint_to_str_back(BACK_PTR
, 181787997);
523 g_assert_cmpstr(str
, ==, "181787997");
526 static void test_uint64_to_str_back(void)
530 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(585143757104211265));
531 g_assert_cmpstr(str
, ==, "585143757104211265");
533 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(7191580247919484847));
534 g_assert_cmpstr(str
, ==, "7191580247919484847");
536 str
= uint64_to_str_back(BACK_PTR
, UINT64_C(95778573911934485));
537 g_assert_cmpstr(str
, ==, "95778573911934485");
540 static void test_uint_to_str_back_len(void)
544 str
= uint_to_str_back_len(BACK_PTR
, 26630, 8);
545 g_assert_cmpstr(str
, ==, "00026630");
547 str
= uint_to_str_back_len(BACK_PTR
, 25313, 8);
548 g_assert_cmpstr(str
, ==, "00025313");
550 str
= uint_to_str_back_len(BACK_PTR
, 18750000, 8);
551 g_assert_cmpstr(str
, ==, "18750000");
554 static void test_uint64_to_str_back_len(void)
558 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(1), 16);
559 g_assert_cmpstr(str
, ==, "0000000000000001");
561 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(4294967295), 16);
562 g_assert_cmpstr(str
, ==, "0000004294967295");
564 str
= uint64_to_str_back_len(BACK_PTR
, UINT64_C(18446744073709551615), 16);
565 g_assert_cmpstr(str
, ==, "18446744073709551615");
568 static void test_int_to_str_back(void)
572 str
= int_to_str_back(BACK_PTR
, -763689611);
573 g_assert_cmpstr(str
, ==, "-763689611");
575 str
= int_to_str_back(BACK_PTR
, -296015954);
576 g_assert_cmpstr(str
, ==, "-296015954");
578 str
= int_to_str_back(BACK_PTR
, 898901469);
579 g_assert_cmpstr(str
, ==, "898901469");
582 static void test_int64_to_str_back(void)
586 str
= int64_to_str_back(BACK_PTR
, INT64_C(-9223372036854775807));
587 g_assert_cmpstr(str
, ==, "-9223372036854775807");
589 str
= int64_to_str_back(BACK_PTR
, INT64_C(1));
590 g_assert_cmpstr(str
, ==, "1");
592 str
= int64_to_str_back(BACK_PTR
, INT64_C(9223372036854775807));
593 g_assert_cmpstr(str
, ==, "9223372036854775807");
597 #include "time_util.h"
599 void test_nstime_from_iso8601(void)
603 nstime_t result
, expect
;
606 memset(&tm1
, 0, sizeof(tm1
));
611 tm1
.tm_mon
= 4; /* starts at zero */
612 tm1
.tm_year
= 2013 - 1900;
615 /* Date and time with local time. */
616 str
= "2013-05-30T23:45:25.349124";
617 expect
.secs
= mktime(&tm1
);
618 expect
.nsecs
= 349124 * 1000;
619 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
620 g_assert_nonnull(endp
);
621 g_assert(*endp
== '\0');
622 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
623 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
625 /* Date and time with UTC timezone. */
626 str
= "2013-05-30T23:45:25.349124Z";
627 expect
.secs
= mktime_utc(&tm1
);
628 expect
.nsecs
= 349124 * 1000;
629 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
630 g_assert_nonnull(endp
);
631 g_assert(*endp
== '\0');
632 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
633 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
635 /* Date and time with timezone offset with separator. */
636 str
= "2013-05-30T23:45:25.349124+01:00";
637 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
638 expect
.nsecs
= 349124 * 1000;
639 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
640 g_assert_nonnull(endp
);
641 g_assert(*endp
== '\0');
642 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
643 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
645 /* Date and time with timezone offset without separator. */
646 str
= "2013-05-30T23:45:25.349124+0100";
647 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
648 expect
.nsecs
= 349124 * 1000;
649 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
650 g_assert_nonnull(endp
);
651 g_assert(*endp
== '\0');
652 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
653 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
655 /* Date and time with timezone offset with hours only. */
656 str
= "2013-05-30T23:45:25.349124+01";
657 expect
.secs
= mktime_utc(&tm1
) - 1 * 60 * 60;
658 expect
.nsecs
= 349124 * 1000;
659 endp
= iso8601_to_nstime(&result
, str
, ISO8601_DATETIME_AUTO
);
660 g_assert_nonnull(endp
);
661 g_assert(*endp
== '\0');
662 g_assert_cmpint(result
.secs
, ==, expect
.secs
);
663 g_assert_cmpint(result
.nsecs
, ==, expect
.nsecs
);
666 #include "ws_getopt.h"
670 static char **new_argv(int *argc_ptr
, const char *args
, ...)
676 argv
= g_malloc((ARGV_MAX
+ 1) * sizeof(char *));
679 while (args
!= NULL
) {
680 /* Increase ARGV_MAX or use a dynamic size if this assertion fails. */
681 g_assert_true(argc
< ARGV_MAX
);
682 argv
[argc
++] = g_strdup(args
);
683 args
= va_arg(ap
, const char *);
692 static void free_argv(char **argv
)
694 for (char **p
= argv
; *p
!= NULL
; p
++) {
700 static void test_getopt_long_basic1(void)
705 const char *optstring
= "ab:c";
706 argv
= new_argv(&argc
, "/bin/ls", "-a", "-b", "arg1", "-c", "path", (char *)NULL
);
711 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
712 g_assert_cmpint(opt
, ==, 'a');
713 g_assert_null(ws_optarg
);
715 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
716 g_assert_cmpint(opt
, ==, 'b');
717 g_assert_cmpstr(ws_optarg
, ==, "arg1");
719 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
720 g_assert_cmpint(opt
, ==, 'c');
721 g_assert_null(ws_optarg
);
723 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
724 g_assert_cmpint(opt
, ==, -1);
729 static void test_getopt_long_basic2(void)
734 struct ws_option longopts
[] = {
735 { "opt1", ws_no_argument
, NULL
, '1' },
736 { "opt2", ws_required_argument
, NULL
, '2' },
737 { "opt3", ws_required_argument
, NULL
, '3' },
740 argv
= new_argv(&argc
, "/bin/ls", "--opt1", "--opt2", "arg1", "--opt3=arg2", "path", (char *)NULL
);
745 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
746 g_assert_cmpint(opt
, ==, '1');
747 g_assert_null(ws_optarg
);
749 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
750 g_assert_cmpint(opt
, ==, '2');
751 g_assert_cmpstr(ws_optarg
, ==, "arg1");
753 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
754 g_assert_cmpint(opt
, ==, '3');
755 g_assert_cmpstr(ws_optarg
, ==, "arg2");
757 opt
= ws_getopt_long(argc
, argv
, "", longopts
, NULL
);
758 g_assert_cmpint(opt
, ==, -1);
763 static void test_getopt_optional_argument1(void)
769 struct ws_option longopts_optional
[] = {
770 { "optional", ws_optional_argument
, NULL
, '1' },
774 argv
= new_argv(&argc
, "/bin/ls", "--optional=arg1", (char *)NULL
);
777 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
778 g_assert_cmpint(opt
, ==, '1');
779 g_assert_cmpstr(ws_optarg
, ==, "arg1");
782 argv
= new_argv(&argc
, "/bin/ls", "--optional", "arg1", (char *)NULL
);
785 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
786 g_assert_cmpint(opt
, ==, '1');
787 /* Optional argument does not recognize the form "--arg param" (it's ambiguous). */
788 g_assert_null(ws_optarg
);
791 argv
= new_argv(&argc
, "/bin/ls", "--optional", (char *)NULL
);
794 opt
= ws_getopt_long(argc
, argv
, "", longopts_optional
, NULL
);
795 g_assert_cmpint(opt
, ==, '1');
796 g_assert_null(ws_optarg
);
801 static void test_getopt_opterr1(void)
807 g_test_skip("Not supported on Windows");
811 if (g_test_subprocess()) {
812 const char *optstring
= "ab";
813 argv
= new_argv(&argc
, "/bin/ls", "-a", "-z", "path", (char *)NULL
);
819 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
820 g_assert_cmpint(opt
, ==, 'a');
822 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
823 g_assert_cmpint(opt
, ==, '?');
824 g_assert_cmpint(ws_optopt
, ==, 'z');
826 opt
= ws_getopt_long(argc
, argv
, optstring
, NULL
, NULL
);
827 g_assert_cmpint(opt
, ==, -1);
834 g_test_trap_subprocess(NULL
, 0, 0);
835 g_test_trap_assert_passed();
836 g_test_trap_assert_stderr("/bin/ls: unrecognized option: z\n");
839 int main(int argc
, char **argv
)
843 ws_log_init("test_wsutil", NULL
);
845 g_test_init(&argc
, &argv
, NULL
);
847 g_test_add_func("/inet_addr/inet_pton4", test_inet_pton4_test1
);
848 g_test_add_func("/inet_addr/inet_ntop4", test_inet_ntop4_test1
);
849 g_test_add_func("/inet_addr/inet_pton6", test_inet_pton6_test1
);
850 g_test_add_func("/inet_addr/inet_ntop6", test_inet_ntop6_test1
);
852 g_test_add_func("/str_util/format_size", test_format_size
);
853 g_test_add_func("/str_util/escape_string", test_escape_string
);
854 g_test_add_func("/str_util/strconcat", test_strconcat
);
855 g_test_add_func("/str_util/strsplit", test_strsplit
);
856 g_test_add_func("/str_util/str_ascii", test_str_ascii
);
857 g_test_add_func("/str_util/format_text", test_format_text
);
860 g_test_add_func("/str_util/format_text_perf", test_format_text_perf
);
863 g_test_add_func("/to_str/word_to_hex", test_word_to_hex
);
864 g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str
);
865 g_test_add_func("/to_str/bytes_to_str_punct", test_bytes_to_str_punct
);
866 g_test_add_func("/to_str/bytes_to_str_maxlen", test_bytes_to_str_maxlen
);
867 g_test_add_func("/to_str/bytes_to_str_punct_maxlen", test_bytes_to_str_punct_maxlen
);
868 g_test_add_func("/to_str/bytes_to_str_trunc1", test_bytes_to_string_trunc1
);
869 g_test_add_func("/to_str/bytes_to_str_punct_trunc1", test_bytes_to_string_punct_trunc1
);
870 g_test_add_func("/to_str/oct_to_str_back", test_oct_to_str_back
);
871 g_test_add_func("/to_str/oct64_to_str_back", test_oct64_to_str_back
);
872 g_test_add_func("/to_str/hex_to_str_back_len", test_hex_to_str_back_len
);
873 g_test_add_func("/to_str/hex64_to_str_back_len", test_hex64_to_str_back_len
);
874 g_test_add_func("/to_str/uint_to_str_back", test_uint_to_str_back
);
875 g_test_add_func("/to_str/uint64_to_str_back", test_uint64_to_str_back
);
876 g_test_add_func("/to_str/uint_to_str_back_len", test_uint_to_str_back_len
);
877 g_test_add_func("/to_str/uint64_to_str_back_len", test_uint64_to_str_back_len
);
878 g_test_add_func("/to_str/int_to_str_back", test_int_to_str_back
);
879 g_test_add_func("/to_str/int64_to_str_back", test_int64_to_str_back
);
880 g_test_add_func("/to_str/ip_addr_to_str_test1", test_ip_addr_to_str_test1
);
882 g_test_add_func("/nstime/from_iso8601", test_nstime_from_iso8601
);
884 g_test_add_func("/ws_getopt/basic1", test_getopt_long_basic1
);
885 g_test_add_func("/ws_getopt/basic2", test_getopt_long_basic2
);
886 g_test_add_func("/ws_getopt/optional1", test_getopt_optional_argument1
);
887 g_test_add_func("/ws_getopt/opterr1", test_getopt_opterr1
);
895 * Editor modelines - https://www.wireshark.org/tools/modelines.html
900 * indent-tabs-mode: nil
903 * vi: set shiftwidth=4 tabstop=8 expandtab:
904 * :indentSize=4:tabSize=8:noTabs=true: