librpc/tests: make use of replace.h in test_ndr*.c
[amitay/samba.git] / librpc / tests / test_ndr_string.c
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 static void test_pull_string_zero_len_nul_term(void **state)
46 {
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;
52
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);
60
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);
67
68 }
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 static void test_pull_string_len_1_nul_term(void **state)
76 {
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};
82
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);
90
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);
96 }
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 static void test_pull_string_len_2_nul_term(void **state)
103 {
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};
109
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);
117
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);
124
125
126 }
127
128 static void test_ndr_string_n_length(void **state)
129 {
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;
142
143         len = ndr_string_n_length(test_str1, sizeof(test_str1), 1);
144         assert_int_equal(len, 5);
145
146         len = ndr_string_n_length(test_str1, sizeof(test_str1) - 1, 1);
147         assert_int_equal(len, 4);
148
149         len = ndr_string_n_length(test_str2, sizeof(test_str2), 1);
150         assert_int_equal(len, 1);
151
152         len = ndr_string_n_length(test_str3, sizeof(test_str3), 1);
153         assert_int_equal(len, 19);
154
155         len = ndr_string_n_length(test_str3, 0, 1);
156         assert_int_equal(len, 0);
157
158         len = ndr_string_n_length(test_str_u16, 32, 2);
159         assert_int_equal(len, 26);
160 }
161
162 int main(int argc, const char **argv)
163 {
164         const struct CMUnitTest tests[] = {
165                 cmocka_unit_test(test_pull_string_zero_len_nul_term),
166                 cmocka_unit_test(test_pull_string_len_1_nul_term),
167                 cmocka_unit_test(test_pull_string_len_2_nul_term),
168                 cmocka_unit_test(test_ndr_string_n_length)
169         };
170
171         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
172         return cmocka_run_group_tests(tests, NULL, NULL);
173 }