docs-xml: remove support for "SMB2_24"
[Samba.git] / librpc / tests / test_ndr.c
bloba26c75ab05354333d29cc5667ee391f1193a5b5a
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 "replace.h"
36 #include <setjmp.h>
37 #include <cmocka.h>
39 #include "librpc/ndr/libndr.h"
42 * Test NDR_PULL_NEED_BYTES integer overflow handling.
44 static enum ndr_err_code wrap_NDR_PULL_NEED_BYTES(
45 struct ndr_pull *ndr,
46 uint32_t bytes) {
48 NDR_PULL_NEED_BYTES(ndr, bytes);
49 return NDR_ERR_SUCCESS;
52 static void test_NDR_PULL_NEED_BYTES(void **state)
54 struct ndr_pull ndr = {0};
55 enum ndr_err_code err;
57 ndr.data_size = UINT32_MAX;
58 ndr.offset = UINT32_MAX -1;
61 * This will not cause an overflow
63 err = wrap_NDR_PULL_NEED_BYTES(&ndr, 1);
64 assert_int_equal(NDR_ERR_SUCCESS, err);
67 * This will cause an overflow
68 * and (offset + n) will be less than data_size
70 err = wrap_NDR_PULL_NEED_BYTES(&ndr, 2);
71 assert_int_equal(NDR_ERR_BUFSIZE, err);
75 * Test NDR_PULL_ALIGN integer overflow handling.
77 static enum ndr_err_code wrap_NDR_PULL_ALIGN(
78 struct ndr_pull *ndr,
79 uint32_t bytes) {
81 NDR_PULL_ALIGN(ndr, bytes);
82 return NDR_ERR_SUCCESS;
85 static void test_NDR_PULL_ALIGN(void **state)
87 struct ndr_pull ndr = {0};
88 enum ndr_err_code err;
90 ndr.data_size = UINT32_MAX;
91 ndr.offset = UINT32_MAX -1;
94 * This will not cause an overflow
96 err = wrap_NDR_PULL_ALIGN(&ndr, 2);
97 assert_int_equal(NDR_ERR_SUCCESS, err);
100 * This will cause an overflow
101 * and (offset + n) will be less than data_size
103 err = wrap_NDR_PULL_ALIGN(&ndr, 4);
104 assert_int_equal(NDR_ERR_BUFSIZE, err);
108 * Test ndr_pull_advance integer overflow handling.
110 static void test_ndr_pull_advance(void **state)
112 struct ndr_pull ndr = {0};
113 enum ndr_err_code err;
115 ndr.data_size = UINT32_MAX;
116 ndr.offset = UINT32_MAX -1;
119 * This will not cause an overflow
121 err = ndr_pull_advance(&ndr, 1);
122 assert_int_equal(NDR_ERR_SUCCESS, err);
125 * This will cause an overflow
126 * and (offset + n) will be less than data_size
128 err = ndr_pull_advance(&ndr, 2);
129 assert_int_equal(NDR_ERR_BUFSIZE, err);
132 int main(int argc, const char **argv)
134 const struct CMUnitTest tests[] = {
135 cmocka_unit_test(test_NDR_PULL_NEED_BYTES),
136 cmocka_unit_test(test_NDR_PULL_ALIGN),
137 cmocka_unit_test(test_ndr_pull_advance),
140 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
141 return cmocka_run_group_tests(tests, NULL, NULL);