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
12 #include <wsutil/utf8_entities.h>
15 * FIXME: LABEL_LENGTH includes the nul byte terminator.
16 * This is confusing but matches ITEM_LABEL_LENGTH.
18 #define LABEL_LENGTH 8
20 void test_label_strcat(void)
22 char label
[LABEL_LENGTH
];
28 pos
= ws_label_strcpy(label
, sizeof(label
), pos
, src
, 0);
29 g_assert_cmpstr(label
, ==, "ABCD");
30 g_assert_cmpuint(pos
, ==, 4);
33 pos
= ws_label_strcpy(label
, sizeof(label
), pos
, src
, 0);
34 g_assert_cmpstr(label
, ==, "ABCDEFG");
35 g_assert_cmpuint(pos
, ==, 8);
39 pos
= ws_label_strcpy(label
, sizeof(label
), pos
, src
, 0);
40 g_assert_cmpstr(label
, ==, "ABCDEFG");
41 g_assert_cmpuint(pos
, ==, 11);
43 /* UTF-8 multibyte does not fit, do not truncate. */
44 src
= "ABCDEF"UTF8_MIDDLE_DOT
;
46 pos
= ws_label_strcpy(label
, sizeof(label
), pos
, src
, 0);
47 g_assert_cmpstr(label
, ==, "ABCDEF");
48 g_assert_cmpuint(pos
, ==, 8); /* Tried to write 8 bytes. */
51 void test_label_strcat_escape_whitespace(void)
54 const char *src
, *dst
;
57 src
= "ABCD\n\t\f\r\aE"UTF8_MIDDLE_DOT
"Z";
58 dst
= "ABCD\\n\\t\\f\\r\\aE"UTF8_MIDDLE_DOT
"Z";
59 pos
= ws_label_strcpy(label
, sizeof(label
), 0, src
, 0);
60 g_assert_cmpstr(label
, ==, dst
);
61 g_assert_cmpuint(pos
, ==, strlen(dst
));
64 void test_label_escape_control(void)
67 const char *src
, *dst
;
70 src
= "ABCD \x04\x17\xC2\x80 EFG \xC2\x90 HIJ \xC2\x9F Z";
71 dst
= "ABCD \\x04\\x17\\u0080 EFG \\u0090 HIJ \\u009F Z";
72 pos
= ws_label_strcpy(label
, sizeof(label
), 0, src
, 0);
73 g_assert_cmpstr(label
, ==, dst
);
74 g_assert_cmpuint(pos
, ==, strlen(dst
));
77 int main(int argc
, char **argv
)
81 ws_log_init("test_proto", NULL
);
83 g_test_init(&argc
, &argv
, NULL
);
85 g_test_add_func("/label/strcat", test_label_strcat
);
86 g_test_add_func("/label/escape_whitespace", test_label_strcat_escape_whitespace
);
87 g_test_add_func("/label/escape_control", test_label_escape_control
);
95 * Editor modelines - https://www.wireshark.org/tools/modelines.html
100 * indent-tabs-mode: nil
103 * vi: set shiftwidth=4 tabstop=8 expandtab:
104 * :indentSize=4:tabSize=8:noTabs=true: