CVE-2020-25719 heimdal:kdc: Move fetching krbtgt entry to before enctype selection
[Samba.git] / librpc / tests / test_ndr_string.c
blob24ec93b17d565aa78829b804a7c1e0ff9848fd8d
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 int 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 int 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 int 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 int main(int argc, const char **argv)
197 const struct CMUnitTest tests[] = {
198 cmocka_unit_test(test_pull_string_zero_len_nul_term),
199 cmocka_unit_test(test_pull_string_len_1_nul_term),
200 cmocka_unit_test(test_pull_string_len_2_nul_term),
201 cmocka_unit_test(test_ndr_string_n_length),
202 cmocka_unit_test(test_pull_string_array)
205 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
206 return cmocka_run_group_tests(tests, NULL, NULL);