summaryrefslogtreecommitdiffstats
path: root/librpc/tests/test_ndr_macros.c
blob: 337bc95b57ad1d2cfb6d9dcfd15350853c17ceb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Tests for librpc ndr functions
 *
 * Copyright (C) Catalyst.NET Ltd 2020
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * from cmocka.c:
 * These headers or their equivalents should be included prior to
 * including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 *
 */
#include "replace.h"
#include <setjmp.h>
#include <cmocka.h>

#include "librpc/ndr/libndr.h"

/*
 * Test NDR_RECURSION_CHECK.
 */
static enum ndr_err_code wrap_NDR_RECURSION_CHECK(
	struct ndr_pull *ndr,
	uint32_t bytes) {

	NDR_RECURSION_CHECK(ndr, bytes);
	return NDR_ERR_SUCCESS;
}

static void test_NDR_RECURSION_CHECK(void **state)
{
	struct ndr_pull ndr = {0};
	enum ndr_err_code err;


	ndr.global_max_recursion = 0;
	ndr.recursion_depth = 42;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
	assert_int_equal(NDR_ERR_SUCCESS, err);
	assert_int_equal(43, ndr.recursion_depth);

	ndr.global_max_recursion = 0;
	ndr.recursion_depth = 43;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
	assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
	assert_int_equal(44, ndr.recursion_depth);

	ndr.global_max_recursion = 0;
	ndr.recursion_depth = 44;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 43);
	assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
	assert_int_equal(45, ndr.recursion_depth);

	ndr.global_max_recursion = 5;
	ndr.recursion_depth = 5;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 20);
	assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
	assert_int_equal(6, ndr.recursion_depth);

	ndr.global_max_recursion = 5;
	ndr.recursion_depth = 4;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 20);
	assert_int_equal(NDR_ERR_SUCCESS, err);
	assert_int_equal(5, ndr.recursion_depth);

	ndr.global_max_recursion = 20;
	ndr.recursion_depth = 5;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 5);
	assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err);
	assert_int_equal(6, ndr.recursion_depth);

	ndr.global_max_recursion = 20;
	ndr.recursion_depth = 4;
	err = wrap_NDR_RECURSION_CHECK(&ndr, 5);
	assert_int_equal(NDR_ERR_SUCCESS, err);
	assert_int_equal(5, ndr.recursion_depth);
}

/*
 * Test NDR_RECURSION_RETURN.
 */
static enum ndr_err_code wrap_NDR_RECURSION_UNWIND(
	struct ndr_pull *ndr) {

	NDR_RECURSION_UNWIND(ndr);
	return NDR_ERR_SUCCESS;
}

static void test_NDR_RECURSION_UNWIND(void **state)
{
	struct ndr_pull ndr = {0};
	enum ndr_err_code err;

	ndr.recursion_depth = 5;
	err = wrap_NDR_RECURSION_UNWIND(&ndr);
	assert_int_equal(NDR_ERR_SUCCESS, err);
	assert_int_equal(4, ndr.recursion_depth);

	ndr.recursion_depth = 0;
	err = wrap_NDR_RECURSION_UNWIND(&ndr);
	assert_int_equal(NDR_ERR_UNDERFLOW, err);
	assert_int_equal(0, ndr.recursion_depth);

}
int main(int argc, const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_NDR_RECURSION_CHECK),
		cmocka_unit_test(test_NDR_RECURSION_UNWIND),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}