Line data Source code
1 : /*
2 : * Tests for librpc ndr_string.c
3 : *
4 : * Copyright (C) Catalyst.NET Ltd 2019
5 : *
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.
10 : *
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.
15 : *
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/>.
18 : *
19 : */
20 :
21 : /*
22 : * from cmocka.c:
23 : * These headers or their equivalents should be included prior to
24 : * including
25 : * this header file.
26 : *
27 : * #include <stdarg.h>
28 : * #include <stddef.h>
29 : * #include <setjmp.h>
30 : *
31 : * This allows test applications to use custom definitions of C standard
32 : * library functions and types.
33 : *
34 : */
35 : #include "replace.h"
36 : #include <setjmp.h>
37 : #include <cmocka.h>
38 :
39 : #include "librpc/ndr/ndr_string.c"
40 :
41 : /*
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.
44 : */
45 1 : static void test_pull_string_zero_len_nul_term(void **state)
46 : {
47 1 : struct ndr_pull ndr = {0};
48 1 : enum ndr_err_code err;
49 1 : ndr_flags_type flags = NDR_SCALARS;
50 1 : uint8_t data[] = {0x0, 0x0};
51 1 : const char *s = NULL;
52 :
53 1 : ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
54 1 : ndr.data = data;
55 1 : ndr.data_size = 0;
56 1 : err = ndr_pull_string(&ndr, flags, &s);
57 1 : assert_int_equal(err, NDR_ERR_BUFSIZE);
58 1 : assert_null(s);
59 1 : assert_int_equal(0, ndr.offset);
60 :
61 1 : ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
62 1 : ndr.offset = 0;
63 1 : err = ndr_pull_string(&ndr, flags, &s);
64 1 : assert_int_equal(err, NDR_ERR_BUFSIZE);
65 1 : assert_null(s);
66 1 : assert_int_equal(0, ndr.offset);
67 :
68 1 : }
69 :
70 : /*
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.
74 : */
75 1 : static void test_pull_string_len_1_nul_term(void **state)
76 : {
77 1 : struct ndr_pull ndr = {0};
78 1 : enum ndr_err_code err;
79 1 : ndr_flags_type flags = NDR_SCALARS;
80 1 : const char *s = NULL;
81 1 : uint8_t data[] = {0x0, 0x0};
82 :
83 1 : ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
84 1 : ndr.data = data;
85 1 : ndr.data_size = 1;
86 1 : err = ndr_pull_string(&ndr, flags, &s);
87 1 : assert_int_equal(err, NDR_ERR_SUCCESS);
88 1 : assert_non_null(s);
89 1 : assert_int_equal(1, ndr.offset);
90 :
91 1 : ndr.offset = 0;
92 1 : ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
93 1 : err = ndr_pull_string(&ndr, flags, &s);
94 1 : assert_int_equal(err, NDR_ERR_BUFSIZE);
95 1 : assert_int_equal(0, ndr.offset);
96 1 : }
97 :
98 : /*
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.
101 : */
102 1 : static void test_pull_string_len_2_nul_term(void **state)
103 : {
104 1 : struct ndr_pull ndr = {0};
105 1 : enum ndr_err_code err;
106 1 : ndr_flags_type flags = NDR_SCALARS;
107 1 : const char *s;
108 1 : uint8_t data[] = {0x0, 0x0};
109 :
110 1 : ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
111 1 : ndr.data = data;
112 1 : ndr.data_size = 2;
113 1 : err = ndr_pull_string(&ndr, flags, &s);
114 1 : assert_int_equal(err, NDR_ERR_SUCCESS);
115 1 : assert_non_null(s);
116 1 : assert_int_equal(1, ndr.offset);
117 :
118 1 : ndr.offset = 0;
119 1 : ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
120 1 : err = ndr_pull_string(&ndr, flags, &s);
121 1 : assert_int_equal(err, NDR_ERR_SUCCESS);
122 1 : assert_non_null(s);
123 1 : assert_int_equal(2, ndr.offset);
124 :
125 :
126 1 : }
127 :
128 1 : static void test_ndr_string_n_length(void **state)
129 : {
130 1 : char test_str1[5] = "Test";
131 1 : char test_str2[5] = {0};
132 1 : char test_str3[32] = "This is a test too";
133 1 : 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 1 : size_t len;
142 :
143 1 : len = ndr_string_n_length(test_str1, sizeof(test_str1), 1);
144 1 : assert_int_equal(len, 5);
145 :
146 1 : len = ndr_string_n_length(test_str1, sizeof(test_str1) - 1, 1);
147 1 : assert_int_equal(len, 4);
148 :
149 1 : len = ndr_string_n_length(test_str2, sizeof(test_str2), 1);
150 1 : assert_int_equal(len, 1);
151 :
152 1 : len = ndr_string_n_length(test_str3, sizeof(test_str3), 1);
153 1 : assert_int_equal(len, 19);
154 :
155 1 : len = ndr_string_n_length(test_str3, 0, 1);
156 1 : assert_int_equal(len, 0);
157 :
158 1 : len = ndr_string_n_length(test_str_u16, 32, 2);
159 1 : assert_int_equal(len, 26);
160 1 : }
161 :
162 1 : static void test_pull_string_array(void **state)
163 : {
164 : /* We try pulling long string arrays without long strings */
165 1 : const char **r = NULL;
166 1 : struct ndr_pull ndr = {0};
167 1 : enum ndr_err_code err;
168 1 : TALLOC_CTX *mem_ctx = talloc_new(NULL);
169 1 : size_t len = 1 * 1024 * 1024;
170 1 : uint8_t *data = talloc_array(mem_ctx, uint8_t, len);
171 1 : size_t i;
172 :
173 1048578 : for (i = 0; i < len; i++) {
174 1572864 : data[i] = (i & 1) ? '\0' : 'X';
175 : }
176 :
177 1 : ndr.current_mem_ctx = mem_ctx;
178 :
179 1 : ndr.flags = (LIBNDR_FLAG_REF_ALLOC |
180 : LIBNDR_FLAG_REMAINING |
181 : LIBNDR_FLAG_STR_NULLTERM |
182 : LIBNDR_FLAG_STR_RAW8);
183 1 : ndr.data = data;
184 1 : ndr.data_size = len;
185 :
186 1 : err = ndr_pull_string_array(&ndr, NDR_SCALARS, &r);
187 1 : assert_int_equal(err, NDR_ERR_SUCCESS);
188 1 : assert_string_equal(r[0], "X");
189 1 : assert_string_equal(r[len / 3], "X");
190 1 : assert_string_equal(r[len / 2 - 1], "X");
191 1 : assert_ptr_equal(r[len / 2], NULL);
192 1 : TALLOC_FREE(mem_ctx);
193 1 : }
194 :
195 1 : int main(int argc, const char **argv)
196 : {
197 1 : 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)
203 : };
204 :
205 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
206 1 : return cmocka_run_group_tests(tests, NULL, NULL);
207 : }
|