Debian Codegraph @

lib/util/charset/tests/util_unistr.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for the util_unistr utility functions
4
5 Copyright (C) Catalyst.Net Ltd. 2023
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23
24 #undef strcasecmp
25 #undef strncasecmp
26
27 struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx);
28
29 static bool test_utf16_len(struct torture_context *tctx)
30 {
31 static const uint16_t empty_string[] = {'\0'};
32 static const uint16_t foo_bar[] = {
33 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
34 static const uint16_t foo_bar_alternative[] = {0xd83c,
35 0xdd75,
36 0xd83c,
37 0xdd7e,
38 0xd83c,
39 0xdd7e,
40 ' ',
41 0xd83c,
42 0xdd31,
43 0xd83c,
44 0xdd30,
45 0xd83c,
46 0xdd41,
47 '\0'};
48
49 torture_assert_size_equal(tctx,
50 utf16_len(empty_string),
51 0,
52 "length of empty string");
53 torture_assert_size_equal(tctx,
54 utf16_null_terminated_len(empty_string),
55 2,
56 "null‐terminated length of empty string");
57 torture_assert_size_equal(tctx,
58 utf16_len(foo_bar),
59 14,
60 "length of “foo bar”");
61 torture_assert_size_equal(tctx,
62 utf16_null_terminated_len(foo_bar),
63 16,
64 "null‐terminated length of “foo bar”");
65 torture_assert_size_equal(tctx,
66 utf16_len(foo_bar_alternative),
67 26,
68 "length of “🅵🅾🅾 🄱🄰🅁”");
69 torture_assert_size_equal(tctx,
70 utf16_null_terminated_len(
71 foo_bar_alternative),
72 28,
73 "null‐terminated length of “🅵🅾🅾 🄱🄰🅁”");
74
75 return true;
76 }
77
78 static bool test_utf16_len_n(struct torture_context *tctx)
79 {
80 static const uint16_t empty_string[] = {'\0'};
81 static const uint16_t foo_bar[] = {'f', 'o', 'o', ' ', 'b', 'a', 'r'};
82 static const uint16_t null_terminated_foo_bar[] = {
83 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
84 static const uint16_t twice_null_terminated_abc[] = {
85 'a', 'b', 'c', '\0', '\0'};
86
87 torture_assert_size_equal(tctx,
88 utf16_len_n(empty_string, 0),
89 0,
90 "length of empty string");
91 torture_assert_size_equal(tctx,
92 utf16_null_terminated_len_n(empty_string, 0),
93 0,
94 "null‐terminated length of empty string");
95
96 torture_assert_size_equal(tctx,
97 utf16_len_n(empty_string,
98 sizeof empty_string),
99 0,
100 "length of null‐terminated empty string");
101 torture_assert_size_equal(
102 tctx,
103 utf16_null_terminated_len_n(empty_string, sizeof empty_string),
104 2,
105 "null‐terminated length of null‐terminated empty string");
106
107 torture_assert_size_equal(tctx,
108 utf16_len_n(foo_bar, sizeof foo_bar),
109 14,
110 "length of “foo bar”");
111 torture_assert_size_equal(tctx,
112 utf16_null_terminated_len_n(foo_bar,
113 sizeof foo_bar),
114 14,
115 "null‐terminated length of “foo bar”");
116
117 torture_assert_size_equal(tctx,
118 utf16_len_n(null_terminated_foo_bar,
119 sizeof null_terminated_foo_bar),
120 14,
121 "length of null‐terminated “foo bar”");
122 torture_assert_size_equal(
123 tctx,
124 utf16_null_terminated_len_n(null_terminated_foo_bar,
125 sizeof null_terminated_foo_bar),
126 16,
127 "null‐terminated length of null‐terminated “foo bar”");
128
129 torture_assert_size_equal(tctx,
130 utf16_len_n(null_terminated_foo_bar,
131 sizeof null_terminated_foo_bar -
132 1),
133 14,
134 "length of “foo bar” minus one byte");
135 torture_assert_size_equal(
136 tctx,
137 utf16_null_terminated_len_n(null_terminated_foo_bar,
138 sizeof null_terminated_foo_bar - 1),
139 14,
140 "null‐terminated length of “foo bar” minus one byte");
141
142 torture_assert_size_equal(tctx,
143 utf16_len_n(twice_null_terminated_abc,
144 sizeof twice_null_terminated_abc),
145 6,
146 "length of twice–null‐terminated “abc”");
147 torture_assert_size_equal(
148 tctx,
149 utf16_null_terminated_len_n(twice_null_terminated_abc,
150 sizeof twice_null_terminated_abc),
151 8,
152 "null‐terminated length of twice–null‐terminated “abc”");
153
154 return true;
155 }
156
157 struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx)
158 {
159 struct torture_suite *suite = torture_suite_create(mem_ctx,
160 "util_unistr");
161
162 torture_suite_add_simple_test(suite, "utf16_len", test_utf16_len);
163 torture_suite_add_simple_test(suite, "utf16_len_n", test_utf16_len_n);
164
165 return suite;
166 }