summaryrefslogtreecommitdiffstats
path: root/lib/util/charset/tests/util_unistr.c
blob: 1a9fcaafdf5a1296ee246899959465dd1ebfde0e (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
/*
   Unix SMB/CIFS implementation.
   test suite for the util_unistr utility functions

   Copyright (C) Catalyst.Net Ltd. 2023

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "torture/torture.h"

#undef strcasecmp
#undef strncasecmp

struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx);

static bool test_utf16_len(struct torture_context *tctx)
{
	static const uint16_t empty_string[] = {'\0'};
	static const uint16_t foo_bar[] = {
		'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
	static const uint16_t foo_bar_alternative[] = {0xd83c,
						       0xdd75,
						       0xd83c,
						       0xdd7e,
						       0xd83c,
						       0xdd7e,
						       ' ',
						       0xd83c,
						       0xdd31,
						       0xd83c,
						       0xdd30,
						       0xd83c,
						       0xdd41,
						       '\0'};

	torture_assert_size_equal(tctx,
				  utf16_len(empty_string),
				  0,
				  "length of empty string");
	torture_assert_size_equal(tctx,
				  utf16_null_terminated_len(empty_string),
				  2,
				  "null‐terminated length of empty string");
	torture_assert_size_equal(tctx,
				  utf16_len(foo_bar),
				  14,
				  "length of “foo bar”");
	torture_assert_size_equal(tctx,
				  utf16_null_terminated_len(foo_bar),
				  16,
				  "null‐terminated length of “foo bar”");
	torture_assert_size_equal(tctx,
				  utf16_len(foo_bar_alternative),
				  26,
				  "length of “🅵🅾🅾 🄱🄰🅁”");
	torture_assert_size_equal(tctx,
				  utf16_null_terminated_len(
					  foo_bar_alternative),
				  28,
				  "null‐terminated length of “🅵🅾🅾 🄱🄰🅁”");

	return true;
}

static bool test_utf16_len_n(struct torture_context *tctx)
{
	static const uint16_t empty_string[] = {'\0'};
	static const uint16_t foo_bar[] = {'f', 'o', 'o', ' ', 'b', 'a', 'r'};
	static const uint16_t null_terminated_foo_bar[] = {
		'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
	static const uint16_t twice_null_terminated_abc[] = {
		'a', 'b', 'c', '\0', '\0'};

	torture_assert_size_equal(tctx,
				  utf16_len_n(empty_string, 0),
				  0,
				  "length of empty string");
	torture_assert_size_equal(tctx,
				  utf16_null_terminated_len_n(empty_string, 0),
				  0,
				  "null‐terminated length of empty string");

	torture_assert_size_equal(tctx,
				  utf16_len_n(empty_string,
					      sizeof empty_string),
				  0,
				  "length of null‐terminated empty string");
	torture_assert_size_equal(
		tctx,
		utf16_null_terminated_len_n(empty_string, sizeof empty_string),
		2,
		"null‐terminated length of null‐terminated empty string");

	torture_assert_size_equal(tctx,
				  utf16_len_n(foo_bar, sizeof foo_bar),
				  14,
				  "length of “foo bar”");
	torture_assert_size_equal(tctx,
				  utf16_null_terminated_len_n(foo_bar,
							      sizeof foo_bar),
				  14,
				  "null‐terminated length of “foo bar”");

	torture_assert_size_equal(tctx,
				  utf16_len_n(null_terminated_foo_bar,
					      sizeof null_terminated_foo_bar),
				  14,
				  "length of null‐terminated “foo bar”");
	torture_assert_size_equal(
		tctx,
		utf16_null_terminated_len_n(null_terminated_foo_bar,
					    sizeof null_terminated_foo_bar),
		16,
		"null‐terminated length of null‐terminated “foo bar”");

	torture_assert_size_equal(tctx,
				  utf16_len_n(null_terminated_foo_bar,
					      sizeof null_terminated_foo_bar -
						      1),
				  14,
				  "length of “foo bar” minus one byte");
	torture_assert_size_equal(
		tctx,
		utf16_null_terminated_len_n(null_terminated_foo_bar,
					    sizeof null_terminated_foo_bar - 1),
		14,
		"null‐terminated length of “foo bar” minus one byte");

	torture_assert_size_equal(tctx,
				  utf16_len_n(twice_null_terminated_abc,
					      sizeof twice_null_terminated_abc),
				  6,
				  "length of twice–null‐terminated “abc”");
	torture_assert_size_equal(
		tctx,
		utf16_null_terminated_len_n(twice_null_terminated_abc,
					    sizeof twice_null_terminated_abc),
		8,
		"null‐terminated length of twice–null‐terminated “abc”");

	return true;
}

struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx,
							   "util_unistr");

	torture_suite_add_simple_test(suite, "utf16_len", test_utf16_len);
	torture_suite_add_simple_test(suite, "utf16_len_n", test_utf16_len_n);

	return suite;
}