VERSION: Disable GIT_SNAPSHOT for the 4.20.0rc4 release.
[Samba.git] / librpc / tests / test_ndr_macros.c
blob337bc95b57ad1d2cfb6d9dcfd15350853c17ceb2
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_RECURSION_CHECK.
44 static enum ndr_err_code wrap_NDR_RECURSION_CHECK(
45 struct ndr_pull *ndr,
46 uint32_t bytes) {
48 NDR_RECURSION_CHECK(ndr, bytes);
49 return NDR_ERR_SUCCESS;
52 static void test_NDR_RECURSION_CHECK(void **state)
54 struct ndr_pull ndr = {0};
55 enum ndr_err_code err;
58 ndr.global_max_recursion = 0;
59 ndr.recursion_depth = 42;
60 err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
61 assert_int_equal(NDR_ERR_SUCCESS, err);
62 assert_int_equal(43, ndr.recursion_depth);
64 ndr.global_max_recursion = 0;
65 ndr.recursion_depth = 43;
66 err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
67 assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
68 assert_int_equal(44, ndr.recursion_depth);
70 ndr.global_max_recursion = 0;
71 ndr.recursion_depth = 44;
72 err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
73 assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
74 assert_int_equal(45, ndr.recursion_depth);
76 ndr.global_max_recursion = 5;
77 ndr.recursion_depth = 5;
78 err = wrap_NDR_RECURSION_CHECK(&ndr, 20);
79 assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
80 assert_int_equal(6, ndr.recursion_depth);
82 ndr.global_max_recursion = 5;
83 ndr.recursion_depth = 4;
84 err = wrap_NDR_RECURSION_CHECK(&ndr, 20);
85 assert_int_equal(NDR_ERR_SUCCESS, err);
86 assert_int_equal(5, ndr.recursion_depth);
88 ndr.global_max_recursion = 20;
89 ndr.recursion_depth = 5;
90 err = wrap_NDR_RECURSION_CHECK(&ndr, 5);
91 assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
92 assert_int_equal(6, ndr.recursion_depth);
94 ndr.global_max_recursion = 20;
95 ndr.recursion_depth = 4;
96 err = wrap_NDR_RECURSION_CHECK(&ndr, 5);
97 assert_int_equal(NDR_ERR_SUCCESS, err);
98 assert_int_equal(5, ndr.recursion_depth);
102 * Test NDR_RECURSION_RETURN.
104 static enum ndr_err_code wrap_NDR_RECURSION_UNWIND(
105 struct ndr_pull *ndr) {
107 NDR_RECURSION_UNWIND(ndr);
108 return NDR_ERR_SUCCESS;
111 static void test_NDR_RECURSION_UNWIND(void **state)
113 struct ndr_pull ndr = {0};
114 enum ndr_err_code err;
116 ndr.recursion_depth = 5;
117 err = wrap_NDR_RECURSION_UNWIND(&ndr);
118 assert_int_equal(NDR_ERR_SUCCESS, err);
119 assert_int_equal(4, ndr.recursion_depth);
121 ndr.recursion_depth = 0;
122 err = wrap_NDR_RECURSION_UNWIND(&ndr);
123 assert_int_equal(NDR_ERR_UNDERFLOW, err);
124 assert_int_equal(0, ndr.recursion_depth);
127 int main(int argc, const char **argv)
129 const struct CMUnitTest tests[] = {
130 cmocka_unit_test(test_NDR_RECURSION_CHECK),
131 cmocka_unit_test(test_NDR_RECURSION_UNWIND),
134 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
135 return cmocka_run_group_tests(tests, NULL, NULL);