smbd: Remove struct open_symlink_err
[Samba.git] / librpc / tests / test_ndr_string.c
blob3250f394270af01ef568b5332bcc7219eaaccd4f
1 /*
2 * Tests for librpc ndr_string.c
4 * Copyright (C) Catalyst.NET Ltd 2019
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * from cmocka.c:
23 * These headers or their equivalents should be included prior to
24 * including
25 * this header file.
27 * #include <stdarg.h>
28 * #include <stddef.h>
29 * #include <setjmp.h>
31 * This allows test applications to use custom definitions of C standard
32 * library functions and types.
35 #include "replace.h"
36 #include <setjmp.h>
37 #include <cmocka.h>
39 #include "librpc/ndr/ndr_string.c"
42 * Try and pull a null terminated string from a zero length buffer
43 * Should fail for both 1 byte, and 2 byte character strings.
45 static void test_pull_string_zero_len_nul_term(void **state)
47 struct ndr_pull ndr = {0};
48 enum ndr_err_code err;
49 ndr_flags_type flags = NDR_SCALARS;
50 uint8_t data[] = {0x0, 0x0};
51 const char *s = NULL;
53 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
54 ndr.data = data;
55 ndr.data_size = 0;
56 err = ndr_pull_string(&ndr, flags, &s);
57 assert_int_equal(err, NDR_ERR_BUFSIZE);
58 assert_null(s);
59 assert_int_equal(0, ndr.offset);
61 ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
62 ndr.offset = 0;
63 err = ndr_pull_string(&ndr, flags, &s);
64 assert_int_equal(err, NDR_ERR_BUFSIZE);
65 assert_null(s);
66 assert_int_equal(0, ndr.offset);
71 * Try and pull a null terminated string from a 1 byte buffer
72 * Should succeed for 1 byte character and
73 * fail for 2 byte character strings.
75 static void test_pull_string_len_1_nul_term(void **state)
77 struct ndr_pull ndr = {0};
78 enum ndr_err_code err;
79 ndr_flags_type flags = NDR_SCALARS;
80 const char *s = NULL;
81 uint8_t data[] = {0x0, 0x0};
83 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
84 ndr.data = data;
85 ndr.data_size = 1;
86 err = ndr_pull_string(&ndr, flags, &s);
87 assert_int_equal(err, NDR_ERR_SUCCESS);
88 assert_non_null(s);
89 assert_int_equal(1, ndr.offset);
91 ndr.offset = 0;
92 ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
93 err = ndr_pull_string(&ndr, flags, &s);
94 assert_int_equal(err, NDR_ERR_BUFSIZE);
95 assert_int_equal(0, ndr.offset);
99 * Try and pull a null terminated string from a 2 byte buffer
100 * Should succeed for both 1 byte, and 2 byte character strings.
102 static void test_pull_string_len_2_nul_term(void **state)
104 struct ndr_pull ndr = {0};
105 enum ndr_err_code err;
106 ndr_flags_type flags = NDR_SCALARS;
107 const char *s;
108 uint8_t data[] = {0x0, 0x0};
110 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
111 ndr.data = data;
112 ndr.data_size = 2;
113 err = ndr_pull_string(&ndr, flags, &s);
114 assert_int_equal(err, NDR_ERR_SUCCESS);
115 assert_non_null(s);
116 assert_int_equal(1, ndr.offset);
118 ndr.offset = 0;
119 ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
120 err = ndr_pull_string(&ndr, flags, &s);
121 assert_int_equal(err, NDR_ERR_SUCCESS);
122 assert_non_null(s);
123 assert_int_equal(2, ndr.offset);
128 static void test_ndr_string_n_length(void **state)
130 char test_str1[5] = "Test";
131 char test_str2[5] = {0};
132 char test_str3[32] = "This is a test too";
133 uint8_t test_str_u16[64] = {
134 0x5C, 0x00, 0x5C, 0x00, 0x4C, 0x00, 0x6F, 0x00,
135 0x67, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x2D, 0x00,
136 0x6D, 0x00, 0x75, 0x00, 0x63, 0x00, 0x5C, 0x00,
137 0x6B, 0x00, 0x79, 0x00, 0x6F, 0x00, 0x63, 0x00,
138 0x65, 0x00, 0x72, 0x00, 0x61, 0x00, 0x2D, 0x00,
139 0x6D, 0x00, 0x75, 0x00, 0x63, 0x00, 0x2D, 0x00,
140 0x6E, 0x00, 0x00, 0x00 };
141 size_t len;
143 len = ndr_string_n_length(test_str1, sizeof(test_str1), 1);
144 assert_int_equal(len, 5);
146 len = ndr_string_n_length(test_str1, sizeof(test_str1) - 1, 1);
147 assert_int_equal(len, 4);
149 len = ndr_string_n_length(test_str2, sizeof(test_str2), 1);
150 assert_int_equal(len, 1);
152 len = ndr_string_n_length(test_str3, sizeof(test_str3), 1);
153 assert_int_equal(len, 19);
155 len = ndr_string_n_length(test_str3, 0, 1);
156 assert_int_equal(len, 0);
158 len = ndr_string_n_length(test_str_u16, 32, 2);
159 assert_int_equal(len, 26);
162 static void test_pull_string_array(void **state)
164 /* We try pulling long string arrays without long strings */
165 const char **r = NULL;
166 struct ndr_pull ndr = {0};
167 enum ndr_err_code err;
168 TALLOC_CTX *mem_ctx = talloc_new(NULL);
169 size_t len = 1 * 1024 * 1024;
170 uint8_t *data = talloc_array(mem_ctx, uint8_t, len);
171 size_t i;
173 for (i = 0; i < len; i++) {
174 data[i] = (i & 1) ? '\0' : 'X';
177 ndr.current_mem_ctx = mem_ctx;
179 ndr.flags = (LIBNDR_FLAG_REF_ALLOC |
180 LIBNDR_FLAG_REMAINING |
181 LIBNDR_FLAG_STR_NULLTERM |
182 LIBNDR_FLAG_STR_RAW8);
183 ndr.data = data;
184 ndr.data_size = len;
186 err = ndr_pull_string_array(&ndr, NDR_SCALARS, &r);
187 assert_int_equal(err, NDR_ERR_SUCCESS);
188 assert_string_equal(r[0], "X");
189 assert_string_equal(r[len / 3], "X");
190 assert_string_equal(r[len / 2 - 1], "X");
191 assert_ptr_equal(r[len / 2], NULL);
192 TALLOC_FREE(mem_ctx);
195 static void test_pull_string_zero_len_utf8_NOTERM_STR_NO_EMBEDDED_NUL(void **state)
197 struct ndr_pull ndr = {0};
198 enum ndr_err_code err;
199 ndr_flags_type flags = NDR_SCALARS;
200 const char *s = NULL;
201 uint8_t data[] = { 0x0, 0x0 };
203 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
205 ndr.data = data;
206 ndr.data_size = sizeof(data);
207 err = ndr_pull_string(&ndr, flags, &s);
208 assert_int_equal(err, NDR_ERR_SUCCESS);
209 assert_non_null(s);
210 assert_string_equal(s, "");
211 assert_int_equal(sizeof(data), ndr.offset);
215 static void test_pull_string_utf8_nul_term_STR_NO_EMBEDDED_NUL(void **state)
218 struct ndr_pull ndr = {0};
219 enum ndr_err_code err;
220 ndr_flags_type flags = NDR_SCALARS;
221 const char *s = NULL;
222 uint8_t data[] = { 0x2, 0x0, 'a', 0x0 };
224 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
226 ndr.data = data;
227 ndr.data_size = sizeof(data);
228 err = ndr_pull_string(&ndr, flags, &s);
229 assert_int_equal(err, NDR_ERR_SUCCESS);
230 assert_non_null(s);
231 assert_string_equal(s, "a");
232 assert_int_equal(sizeof(data), ndr.offset);
236 static void test_pull_string_utf8_nul_term_NOTERM_STR_NO_EMBEDDED_NUL(void **state)
239 struct ndr_pull ndr = {0};
240 enum ndr_err_code err;
241 ndr_flags_type flags = NDR_SCALARS;
242 const char *s = NULL;
243 uint8_t data[] = { 0x2, 0x0, 'a', 0x0 };
245 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
247 ndr.data = data;
248 ndr.data_size = sizeof(data);
249 err = ndr_pull_string(&ndr, flags, &s);
250 assert_int_equal(err, NDR_ERR_CHARCNV);
251 assert_int_equal(2, ndr.offset);
255 static void test_pull_string_utf8_nullterm_STR_NO_EMBEDDED_NUL(void **state)
258 struct ndr_pull ndr = {0};
259 enum ndr_err_code err;
260 ndr_flags_type flags = NDR_SCALARS;
261 const char *s = NULL;
262 uint8_t data[] = { 0x4, 0x0, 'a', 'b', 'c', 0x0};
264 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
266 ndr.data = data;
267 ndr.data_size = sizeof(data);
268 err = ndr_pull_string(&ndr, flags, &s);
269 assert_int_equal(err, NDR_ERR_SUCCESS);
270 assert_non_null(s);
271 assert_string_equal(s, "abc");
272 assert_int_equal(sizeof(data), ndr.offset);
276 static void test_pull_string_utf8_STR_NO_EMBEDDED_NUL(void **state)
279 struct ndr_pull ndr = {0};
280 enum ndr_err_code err;
281 ndr_flags_type flags = NDR_SCALARS;
282 const char *s = NULL;
283 uint8_t data[] = { 0x3, 0x0, 'a', 'b', 'c'};
285 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
287 ndr.data = data;
288 ndr.data_size = sizeof(data);
289 err = ndr_pull_string(&ndr, flags, &s);
290 assert_int_equal(err, NDR_ERR_CHARCNV);
291 assert_int_equal(2, ndr.offset);
295 static void test_pull_string_utf8_NOTERM_STR_NO_EMBEDDED_NUL(void **state)
298 struct ndr_pull ndr = {0};
299 enum ndr_err_code err;
300 ndr_flags_type flags = NDR_SCALARS;
301 const char *s = NULL;
302 uint8_t data[] = { 0x3, 0x0, 'a', 'b', 'c'};
304 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
306 ndr.data = data;
307 ndr.data_size = sizeof(data);
308 err = ndr_pull_string(&ndr, flags, &s);
309 assert_int_equal(err, NDR_ERR_SUCCESS);
310 assert_non_null(s);
311 assert_string_equal(s, "abc");
312 assert_int_equal(sizeof(data), ndr.offset);
316 static void test_pull_string_utf8_nullterm_NOTERM_STR_NO_EMBEDDED_NUL(void **state)
319 struct ndr_pull ndr = {0};
320 enum ndr_err_code err;
321 ndr_flags_type flags = NDR_SCALARS;
322 const char *s = NULL;
323 uint8_t data[] = { 0x4, 0x0, 'a', 'b', 'c', 0x0};
325 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
327 ndr.data = data;
328 ndr.data_size = sizeof(data);
329 err = ndr_pull_string(&ndr, flags, &s);
330 assert_int_equal(err, NDR_ERR_CHARCNV);
331 assert_int_equal(2, ndr.offset);
335 static void test_pull_string_utf8_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL_fail(void **state)
338 struct ndr_pull ndr = {0};
339 enum ndr_err_code err;
340 ndr_flags_type flags = NDR_SCALARS;
341 const char *s = NULL;
342 uint8_t data[] = { 0x3, 0x0, 'a', 0x0, 'a'};
344 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
346 ndr.data = data;
347 ndr.data_size = sizeof(data);
348 err = ndr_pull_string(&ndr, flags, &s);
349 assert_int_equal(err, NDR_ERR_CHARCNV);
350 assert_int_equal(2, ndr.offset);
354 static void test_pull_string_utf16_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL(void **state)
357 struct ndr_pull ndr = {0};
358 enum ndr_err_code err;
359 ndr_flags_type flags = NDR_SCALARS;
360 const char *s = NULL;
361 uint8_t data[] = { 0x3, 0x0, 'a', 0x0, 'b', 0x0, 'c', 0x0};
363 ndr.flags = LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
365 ndr.data = data;
366 ndr.data_size = sizeof(data);
367 err = ndr_pull_string(&ndr, flags, &s);
368 assert_int_equal(err, NDR_ERR_SUCCESS);
369 assert_non_null(s);
370 assert_string_equal(s, "abc");
371 assert_int_equal(sizeof(data), ndr.offset);
375 static void test_pull_string_utf16_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL_fail(void **state)
378 struct ndr_pull ndr = {0};
379 enum ndr_err_code err;
380 ndr_flags_type flags = NDR_SCALARS;
381 const char *s = NULL;
382 uint8_t data[] = { 0x3, 0x0, 'a', 0x0, 0x0, 0x0, 'c', 0x0};
384 ndr.flags = LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
386 ndr.data = data;
387 ndr.data_size = sizeof(data);
388 err = ndr_pull_string(&ndr, flags, &s);
389 assert_int_equal(err, NDR_ERR_CHARCNV);
390 assert_int_equal(2, ndr.offset);
394 static void test_pull_string_zero_len_utf8_STR_NO_EMBEDDED_NUL(void **state)
397 struct ndr_pull ndr = {0};
398 enum ndr_err_code err;
399 ndr_flags_type flags = NDR_SCALARS;
400 const char *s = NULL;
401 uint8_t data[] = { 0x0, 0x0 };
403 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
405 ndr.data = data;
406 ndr.data_size = sizeof(data);
407 err = ndr_pull_string(&ndr, flags, &s);
408 assert_int_equal(err, NDR_ERR_SUCCESS);
409 assert_non_null(s);
410 assert_string_equal(s, "");
411 assert_int_equal(sizeof(data), ndr.offset);
415 static void test_pull_string_nul_only_utf8_STR_NO_EMBEDDED_NUL(void **state)
418 struct ndr_pull ndr = {0};
419 enum ndr_err_code err;
420 ndr_flags_type flags = NDR_SCALARS;
421 const char *s = NULL;
422 uint8_t data[] = { 0x2, 0x0, 0x0, 0x0 };
424 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_SIZE2 | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
426 ndr.data = data;
427 ndr.data_size = sizeof(data);
428 err = ndr_pull_string(&ndr, flags, &s);
429 assert_int_equal(err, NDR_ERR_CHARCNV);
430 assert_int_equal(2, ndr.offset);
434 static void test_pull_string_nul_term_utf8_NOTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL(void **state)
437 struct ndr_pull ndr = {0};
438 enum ndr_err_code err;
439 ndr_flags_type flags = NDR_SCALARS;
440 const char *s = NULL;
441 uint8_t data[] = {'a', 'b', 'c', 0x0 };
443 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_REMAINING | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
445 ndr.data = data;
446 ndr.data_size = sizeof(data);
447 err = ndr_pull_string(&ndr, flags, &s);
448 assert_int_equal(err, NDR_ERR_CHARCNV);
449 assert_int_equal(0, ndr.offset);
453 static void test_pull_string_utf8_NOTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL(void **state)
456 struct ndr_pull ndr = {0};
457 enum ndr_err_code err;
458 ndr_flags_type flags = NDR_SCALARS;
459 const char *s = NULL;
460 uint8_t data[] = {'a', 'b', 'c' };
462 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NOTERM | LIBNDR_FLAG_REMAINING | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
464 ndr.data = data;
465 ndr.data_size = sizeof(data);
466 err = ndr_pull_string(&ndr, flags, &s);
467 assert_int_equal(err, NDR_ERR_SUCCESS);
468 assert_non_null(s);
469 assert_string_equal(s, "abc");
470 assert_int_equal(sizeof(data), ndr.offset);
474 static void test_pull_string_nul_term_utf8_STR_NULLTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL(void **state)
477 struct ndr_pull ndr = {0};
478 enum ndr_err_code err;
479 ndr_flags_type flags = NDR_SCALARS;
480 const char *s = NULL;
481 uint8_t data[] = {'a', 'b', 'c', 0x0 };
483 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM | LIBNDR_FLAG_REMAINING | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
485 ndr.data = data;
486 ndr.data_size = sizeof(data);
487 err = ndr_pull_string(&ndr, flags, &s);
488 assert_int_equal(err, NDR_ERR_SUCCESS);
489 assert_non_null(s);
490 assert_string_equal(s, "abc");
491 assert_int_equal(sizeof(data), ndr.offset);
495 static void test_pull_string_utf8_NDR_REMAINING_STR_NULLTERM_STR_NO_EMBEDDED_NUL(void **state)
498 struct ndr_pull ndr = {0};
499 enum ndr_err_code err;
500 ndr_flags_type flags = NDR_SCALARS;
501 const char *s = NULL;
502 uint8_t data[] = {'a', 'b', 'c' };
504 ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM | LIBNDR_FLAG_REMAINING | LIBNDR_FLAG_STR_NO_EMBEDDED_NUL;
506 ndr.data = data;
507 ndr.data_size = sizeof(data);
508 err = ndr_pull_string(&ndr, flags, &s);
509 assert_int_equal(err, NDR_ERR_CHARCNV);
510 assert_int_equal(0, ndr.offset);
514 int main(int argc, const char **argv)
516 const struct CMUnitTest tests[] = {
517 cmocka_unit_test(test_pull_string_zero_len_nul_term),
518 cmocka_unit_test(test_pull_string_len_1_nul_term),
519 cmocka_unit_test(test_pull_string_len_2_nul_term),
520 cmocka_unit_test(test_ndr_string_n_length),
521 cmocka_unit_test(test_pull_string_array),
522 cmocka_unit_test(test_pull_string_zero_len_utf8_NOTERM_STR_NO_EMBEDDED_NUL),
523 cmocka_unit_test(test_pull_string_utf8_nul_term_STR_NO_EMBEDDED_NUL),
524 cmocka_unit_test(test_pull_string_utf8_nul_term_NOTERM_STR_NO_EMBEDDED_NUL),
525 cmocka_unit_test(test_pull_string_utf8_nullterm_STR_NO_EMBEDDED_NUL),
526 cmocka_unit_test(test_pull_string_utf8_STR_NO_EMBEDDED_NUL),
527 cmocka_unit_test(test_pull_string_utf8_NOTERM_STR_NO_EMBEDDED_NUL),
528 cmocka_unit_test(test_pull_string_utf8_nullterm_NOTERM_STR_NO_EMBEDDED_NUL),
529 cmocka_unit_test(test_pull_string_utf8_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL_fail),
530 cmocka_unit_test(test_pull_string_utf16_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL),
531 cmocka_unit_test(test_pull_string_utf16_LIBNDR_FLAG_STR_NOTERM_STR_NO_EMBEDDED_NUL_fail),
532 cmocka_unit_test(test_pull_string_zero_len_utf8_STR_NO_EMBEDDED_NUL),
533 cmocka_unit_test(test_pull_string_nul_only_utf8_STR_NO_EMBEDDED_NUL),
534 cmocka_unit_test(test_pull_string_nul_term_utf8_NOTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL),
535 cmocka_unit_test(test_pull_string_utf8_NOTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL),
536 cmocka_unit_test(test_pull_string_nul_term_utf8_STR_NULLTERM_NDR_REMAINING_STR_NO_EMBEDDED_NUL),
537 cmocka_unit_test(test_pull_string_utf8_NDR_REMAINING_STR_NULLTERM_STR_NO_EMBEDDED_NUL)
540 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
541 return cmocka_run_group_tests(tests, NULL, NULL);