Line data Source code
1 : /* 2 : * Tests for librpc ndr functions 3 : * 4 : * Copyright (C) Catalyst.NET Ltd 2020 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/libndr.h" 40 : 41 : /* 42 : * Test NDR_RECURSION_CHECK. 43 : */ 44 7 : static enum ndr_err_code wrap_NDR_RECURSION_CHECK( 45 : struct ndr_pull *ndr, 46 : uint32_t bytes) { 47 : 48 11 : NDR_RECURSION_CHECK(ndr, bytes); 49 : return NDR_ERR_SUCCESS; 50 : } 51 : 52 1 : static void test_NDR_RECURSION_CHECK(void **state) 53 : { 54 1 : struct ndr_pull ndr = {0}; 55 1 : enum ndr_err_code err; 56 : 57 : 58 1 : ndr.global_max_recursion = 0; 59 1 : ndr.recursion_depth = 42; 60 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 43); 61 1 : assert_int_equal(NDR_ERR_SUCCESS, err); 62 1 : assert_int_equal(43, ndr.recursion_depth); 63 : 64 1 : ndr.global_max_recursion = 0; 65 1 : ndr.recursion_depth = 43; 66 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 43); 67 1 : assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err); 68 1 : assert_int_equal(44, ndr.recursion_depth); 69 : 70 1 : ndr.global_max_recursion = 0; 71 1 : ndr.recursion_depth = 44; 72 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 43); 73 1 : assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err); 74 1 : assert_int_equal(45, ndr.recursion_depth); 75 : 76 1 : ndr.global_max_recursion = 5; 77 1 : ndr.recursion_depth = 5; 78 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 20); 79 1 : assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err); 80 1 : assert_int_equal(6, ndr.recursion_depth); 81 : 82 1 : ndr.global_max_recursion = 5; 83 1 : ndr.recursion_depth = 4; 84 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 20); 85 1 : assert_int_equal(NDR_ERR_SUCCESS, err); 86 1 : assert_int_equal(5, ndr.recursion_depth); 87 : 88 1 : ndr.global_max_recursion = 20; 89 1 : ndr.recursion_depth = 5; 90 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 5); 91 1 : assert_int_equal(NDR_ERR_MAX_RECURSION_EXCEEDED, err); 92 1 : assert_int_equal(6, ndr.recursion_depth); 93 : 94 1 : ndr.global_max_recursion = 20; 95 1 : ndr.recursion_depth = 4; 96 1 : err = wrap_NDR_RECURSION_CHECK(&ndr, 5); 97 1 : assert_int_equal(NDR_ERR_SUCCESS, err); 98 1 : assert_int_equal(5, ndr.recursion_depth); 99 1 : } 100 : 101 : /* 102 : * Test NDR_RECURSION_RETURN. 103 : */ 104 2 : static enum ndr_err_code wrap_NDR_RECURSION_UNWIND( 105 : struct ndr_pull *ndr) { 106 : 107 2 : NDR_RECURSION_UNWIND(ndr); 108 1 : return NDR_ERR_SUCCESS; 109 : } 110 : 111 1 : static void test_NDR_RECURSION_UNWIND(void **state) 112 : { 113 1 : struct ndr_pull ndr = {0}; 114 1 : enum ndr_err_code err; 115 : 116 1 : ndr.recursion_depth = 5; 117 1 : err = wrap_NDR_RECURSION_UNWIND(&ndr); 118 1 : assert_int_equal(NDR_ERR_SUCCESS, err); 119 1 : assert_int_equal(4, ndr.recursion_depth); 120 : 121 1 : ndr.recursion_depth = 0; 122 1 : err = wrap_NDR_RECURSION_UNWIND(&ndr); 123 1 : assert_int_equal(NDR_ERR_UNDERFLOW, err); 124 1 : assert_int_equal(0, ndr.recursion_depth); 125 : 126 1 : } 127 1 : int main(int argc, const char **argv) 128 : { 129 1 : const struct CMUnitTest tests[] = { 130 : cmocka_unit_test(test_NDR_RECURSION_CHECK), 131 : cmocka_unit_test(test_NDR_RECURSION_UNWIND), 132 : }; 133 : 134 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT); 135 1 : return cmocka_run_group_tests(tests, NULL, NULL); 136 : }