s3:rpc_server: Do not generate and build s3 RPC server code
[Samba.git] / librpc / tests / test_ndr.c
blob316c54368a09fe204c646bf4140160d1bdf6a0c2
1 /*
2 * Tests for librpc ndr functions
4 * Copyright (C) Catalyst.NET Ltd 2020
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 <stdarg.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <setjmp.h>
39 #include <cmocka.h>
41 #include "librpc/ndr/libndr.h"
44 * Test NDR_PULL_NEED_BYTES integer overflow handling.
46 static enum ndr_err_code wrap_NDR_PULL_NEED_BYTES(
47 struct ndr_pull *ndr,
48 uint32_t bytes) {
50 NDR_PULL_NEED_BYTES(ndr, bytes);
51 return NDR_ERR_SUCCESS;
54 static void test_NDR_PULL_NEED_BYTES(void **state)
56 struct ndr_pull ndr = {0};
57 enum ndr_err_code err;
59 ndr.data_size = UINT32_MAX;
60 ndr.offset = UINT32_MAX -1;
63 * This will not cause an overflow
65 err = wrap_NDR_PULL_NEED_BYTES(&ndr, 1);
66 assert_int_equal(NDR_ERR_SUCCESS, err);
69 * This will cause an overflow
70 * and (offset + n) will be less than data_size
72 err = wrap_NDR_PULL_NEED_BYTES(&ndr, 2);
73 assert_int_equal(NDR_ERR_BUFSIZE, err);
77 * Test NDR_PULL_ALIGN integer overflow handling.
79 static enum ndr_err_code wrap_NDR_PULL_ALIGN(
80 struct ndr_pull *ndr,
81 uint32_t bytes) {
83 NDR_PULL_ALIGN(ndr, bytes);
84 return NDR_ERR_SUCCESS;
87 static void test_NDR_PULL_ALIGN(void **state)
89 struct ndr_pull ndr = {0};
90 enum ndr_err_code err;
92 ndr.data_size = UINT32_MAX;
93 ndr.offset = UINT32_MAX -1;
96 * This will not cause an overflow
98 err = wrap_NDR_PULL_ALIGN(&ndr, 2);
99 assert_int_equal(NDR_ERR_SUCCESS, err);
102 * This will cause an overflow
103 * and (offset + n) will be less than data_size
105 err = wrap_NDR_PULL_ALIGN(&ndr, 4);
106 assert_int_equal(NDR_ERR_BUFSIZE, err);
110 * Test ndr_pull_advance integer overflow handling.
112 static void test_ndr_pull_advance(void **state)
114 struct ndr_pull ndr = {0};
115 enum ndr_err_code err;
117 ndr.data_size = UINT32_MAX;
118 ndr.offset = UINT32_MAX -1;
121 * This will not cause an overflow
123 err = ndr_pull_advance(&ndr, 1);
124 assert_int_equal(NDR_ERR_SUCCESS, err);
127 * This will cause an overflow
128 * and (offset + n) will be less than data_size
130 err = ndr_pull_advance(&ndr, 2);
131 assert_int_equal(NDR_ERR_BUFSIZE, err);
134 int main(int argc, const char **argv)
136 const struct CMUnitTest tests[] = {
137 cmocka_unit_test(test_NDR_PULL_NEED_BYTES),
138 cmocka_unit_test(test_NDR_PULL_ALIGN),
139 cmocka_unit_test(test_ndr_pull_advance),
142 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
143 return cmocka_run_group_tests(tests, NULL, NULL);