Debian Codegraph @

tests/api/encode.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2015-2021 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Encode API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = permanent_pool = make_sub_pool(NULL);
34 }
35
36 #ifdef PR_USE_NLS
37 encode_init();
38 #endif /* PR_USE_NLS */
39
40 if (getenv("TEST_VERBOSE") != NULL) {
41 pr_trace_set_levels("encode", 1, 20);
42 }
43 }
44
45 static void tear_down(void) {
46 if (getenv("TEST_VERBOSE") != NULL) {
47 pr_trace_set_levels("encode", 0, 0);
48 }
49
50 #ifdef PR_USE_NLS
51 encode_free();
52 #endif /* PR_USE_NLS */
53
54 if (p) {
55 destroy_pool(p);
56 p = permanent_pool = NULL;
57 }
58 }
59
60 #ifdef PR_USE_NLS
61 START_TEST (encode_encode_str_test) {
62 register unsigned int i;
63 char junk[1024], *res;
64 const char *in_str;
65 size_t in_len, out_len = 0;
66
67 mark_point();
68 res = pr_encode_str(NULL, NULL, 0, NULL);
69 ck_assert_msg(res == NULL, "Failed to handle null arguments");
70 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
71 strerror(errno), errno);
72
73 mark_point();
74 res = pr_encode_str(p, NULL, 0, NULL);
75 ck_assert_msg(res == NULL, "Failed to handle null input string");
76 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
77 strerror(errno), errno);
78
79 mark_point();
80 in_str = "OK";
81 in_len = 2;
82 res = pr_encode_str(p, in_str, in_len, NULL);
83 ck_assert_msg(res == NULL, "Failed to handle null output string len");
84 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
85 strerror(errno), errno);
86
87 mark_point();
88 res = pr_encode_str(p, in_str, in_len, &out_len);
89 ck_assert_msg(res != NULL, "Failed to encode '%s': %s", in_str,
90 strerror(errno));
91 ck_assert_msg(strcmp(res, in_str) == 0, "Expected '%s', got '%s'", in_str,
92 res);
93
94 mark_point();
95 for (i = 0; i < sizeof(junk); i++) {
96 junk[i] = i;
97 }
98 in_str = junk;
99 in_len = sizeof(junk);
100 res = pr_encode_str(p, in_str, in_len, &out_len);
101 ck_assert_msg(res == NULL, "Failed to handle bad input");
102 ck_assert_msg(errno == EILSEQ, "Expected EILSEQ (%d), got %s (%d)", EILSEQ,
103 strerror(errno), errno);
104 }
105 END_TEST
106
107 START_TEST (encode_decode_str_test) {
108 register unsigned int i;
109 char junk[1024], *res;
110 const char *in_str;
111 size_t in_len, out_len = 0;
112
113 mark_point();
114 res = pr_decode_str(NULL, NULL, 0, NULL);
115 ck_assert_msg(res == NULL, "Failed to handle null arguments");
116 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
117 strerror(errno), errno);
118
119 mark_point();
120 res = pr_decode_str(p, NULL, 0, NULL);
121 ck_assert_msg(res == NULL, "Failed to handle null input string");
122 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
123 strerror(errno), errno);
124
125 mark_point();
126 in_str = "OK";
127 in_len = 2;
128 res = pr_decode_str(p, in_str, in_len, NULL);
129 ck_assert_msg(res == NULL, "Failed to handle null output string len");
130 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
131 strerror(errno), errno);
132
133 mark_point();
134 res = pr_decode_str(p, in_str, in_len, &out_len);
135 ck_assert_msg(res != NULL, "Failed to decode '%s': %s", in_str,
136 strerror(errno));
137 ck_assert_msg(strcmp(res, in_str) == 0, "Expected '%s', got '%s'", in_str,
138 res);
139
140 mark_point();
141 for (i = 0; i < sizeof(junk); i++) {
142 junk[i] = i;
143 }
144 in_str = junk;
145 in_len = sizeof(junk);
146 res = pr_decode_str(p, in_str, in_len, &out_len);
147 ck_assert_msg(res == NULL, "Failed to handle bad input");
148 ck_assert_msg(errno == EILSEQ, "Expected EILSEQ (%d), got %s (%d)", EILSEQ,
149 strerror(errno), errno);
150 }
151 END_TEST
152
153 START_TEST (encode_charset_test) {
154 int res;
155 const char *charset, *encoding;
156
157 charset = pr_encode_get_charset();
158 ck_assert_msg(charset != NULL, "Failed to get current charset: %s",
159 strerror(errno));
160
161 res = pr_encode_is_utf8(NULL);
162 ck_assert_msg(res == -1, "Failed to handle null arguments");
163 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
164 strerror(errno), errno);
165
166 charset = "utf8";
167 res = pr_encode_is_utf8(charset);
168 ck_assert_msg(res == TRUE, "Expected TRUE for '%s', got %d", charset, res);
169
170 charset = "utf-8";
171 res = pr_encode_is_utf8(charset);
172 ck_assert_msg(res == TRUE, "Expected TRUE for '%s', got %d", charset, res);
173
174 charset = "ascii";
175 res = pr_encode_is_utf8(charset);
176 ck_assert_msg(res == FALSE, "Expected FALSE for '%s', got %d", charset, res);
177
178 res = pr_encode_set_charset_encoding(NULL, NULL);
179 ck_assert_msg(res < 0, "Failed to handle null arguments");
180 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
181 strerror(errno), errno);
182
183 charset = "us-ascii";
184 res = pr_encode_set_charset_encoding(charset, NULL);
185 ck_assert_msg(res < 0, "Failed to handle null encoding");
186 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
187 strerror(errno), errno);
188
189 encoding = "utf-8";
190 res = pr_encode_set_charset_encoding(charset, encoding);
191 ck_assert_msg(res == 0, "Failed to set charset '%s', encoding '%s': %s",
192 charset, encoding, strerror(errno));
193
194 charset = "foo";
195 res = pr_encode_set_charset_encoding(charset, encoding);
196 ck_assert_msg(res < 0, "Failed to handle bad charset '%s'", charset);
197 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
198 strerror(errno), errno);
199
200 charset = "us-ascii";
201 encoding = "foo";
202 res = pr_encode_set_charset_encoding(charset, encoding);
203 ck_assert_msg(res < 0, "Failed to handle bad encoding '%s'", encoding);
204 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
205 strerror(errno), errno);
206 }
207 END_TEST
208
209 START_TEST (encode_encoding_test) {
210 int res;
211 const char *encoding;
212
213 res = pr_encode_enable_encoding(NULL);
214 ck_assert_msg(res < 0, "Failed to handle null arguments");
215 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
216 strerror(errno), errno);
217
218 encoding = "utf-8";
219 res = pr_encode_enable_encoding(encoding);
220 ck_assert_msg(res == 0, "Failed to enable encoding '%s': %s", encoding,
221 strerror(errno));
222
223 encoding = "iso-8859-1";
224 res = pr_encode_enable_encoding(encoding);
225 ck_assert_msg(res == 0, "Failed to enable encoding '%s': %s", encoding,
226 strerror(errno));
227
228 encoding = "foo";
229 res = pr_encode_enable_encoding(encoding);
230 ck_assert_msg(res < 0, "Failed to handle bad encoding '%s'", encoding);
231 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
232 strerror(errno), errno);
233
234 pr_encode_disable_encoding();
235
236 encoding = "utf-8";
237 res = pr_encode_enable_encoding(encoding);
238 ck_assert_msg(res == 0, "Failed to enable encoding '%s': %s", encoding,
239 strerror(errno));
240
241 encoding = pr_encode_get_encoding();
242 ck_assert_msg(encoding != NULL, "Failed to get encoding: %s", strerror(errno));
243 ck_assert_msg(strcasecmp(encoding, "utf-8") == 0,
244 "Expected 'utf-8', got '%s'", encoding);
245 }
246 END_TEST
247
248 START_TEST (encode_policy_test) {
249 unsigned long res;
250
251 res = pr_encode_get_policy();
252 ck_assert_msg(res == 0, "Expected policy 0, got %lu", res);
253
254 res = pr_encode_set_policy(7);
255 ck_assert_msg(res == 0, "Expected policy 0, got %lu", res);
256
257 res = pr_encode_get_policy();
258 ck_assert_msg(res == 7, "Expected policy 7, got %lu", res);
259
260 (void) pr_encode_set_policy(0);
261 }
262 END_TEST
263
264 START_TEST (encode_supports_telnet_iac_test) {
265 register unsigned int i;
266 int res;
267 const char *charset, *encoding;
268 const char *non_iac_encodings[] = {
269 "cp1251",
270 "cp866",
271 "iso-8859-1",
272 "koi8-r",
273 "windows-1251",
274 NULL
275 };
276
277 res = pr_encode_supports_telnet_iac();
278 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
279
280 charset = "us-ascii";
281
282 for (i = 0; non_iac_encodings[i]; i++) {
283 encoding = non_iac_encodings[i];
284
285 res = pr_encode_set_charset_encoding(charset, encoding);
286 ck_assert_msg(res == 0, "Failed to set charset '%s', encoding '%s': %s",
287 charset, encoding, strerror(errno));
288
289 res = pr_encode_supports_telnet_iac();
290 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
291 }
292
293 encoding = "utf-8";
294 res = pr_encode_set_charset_encoding(charset, encoding);
295 ck_assert_msg(res == 0, "Failed to set charset '%s', encoding '%s': %s",
296 charset, encoding, strerror(errno));
297 }
298 END_TEST
299 #endif /* PR_USE_NLS */
300
301 Suite *tests_get_encode_suite(void) {
302 Suite *suite;
303 TCase *testcase;
304
305 suite = suite_create("encode");
306
307 testcase = tcase_create("base");
308 tcase_add_checked_fixture(testcase, set_up, tear_down);
309
310 #ifdef PR_USE_NLS
311 tcase_add_test(testcase, encode_encode_str_test);
312 tcase_add_test(testcase, encode_decode_str_test);
313 tcase_add_test(testcase, encode_charset_test);
314 tcase_add_test(testcase, encode_encoding_test);
315 tcase_add_test(testcase, encode_policy_test);
316 tcase_add_test(testcase, encode_supports_telnet_iac_test);
317 #endif /* PR_USE_NLS */
318
319 suite_add_tcase(suite, testcase);
320 return suite;
321 }