1 /* Authors: Christopher Ashworth <cashworth@tresys.com> 2 * 3 * Copyright (C) 2006 Tresys Technology, LLC 4 * Copyright (C) 2019 Red Hat, Inc. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library 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 GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef __UTILITIES_H__ 22 #define __UTILITIES_H__ 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <stdarg.h> 27 #include <fts.h> 28 #include <assert.h> 29 #include <sys/stat.h> 30 #include <sys/types.h> 31 #include <CUnit/Basic.h> 32 33 #include "semanage/semanage.h" 34 35 #define CU_ASSERT_CONTEXT_EQUAL(CON1,CON2) \ 36 do { \ 37 char *__str; \ 38 char *__str2; \ 39 CU_ASSERT(semanage_context_to_string(sh, CON1, &__str) >= 0); \ 40 CU_ASSERT(semanage_context_to_string(sh, CON2, &__str2) >= 0); \ 41 CU_ASSERT_STRING_EQUAL(__str, __str2); \ 42 } while (0) 43 44 45 /* Override CU_*_FATAL() in order to help static analyzers by really asserting that an assertion holds */ 46 #ifdef __CHECKER__ 47 48 #undef CU_ASSERT_FATAL 49 #define CU_ASSERT_FATAL(value) do { \ 50 int _value = (value); \ 51 CU_ASSERT(_value); \ 52 assert(_value); \ 53 } while (0) 54 55 #undef CU_FAIL_FATAL 56 #define CU_FAIL_FATAL(msg) do { \ 57 CU_FAIL(msg); \ 58 assert(0); \ 59 } while (0) 60 61 #undef CU_ASSERT_PTR_NOT_NULL_FATAL 62 #define CU_ASSERT_PTR_NOT_NULL_FATAL(value) do { \ 63 const void *_value = (value); \ 64 CU_ASSERT_PTR_NOT_NULL(_value); \ 65 assert(_value != NULL); \ 66 } while (0) 67 68 #endif /* __CHECKER__ */ 69 70 #define I_NULL -1 71 #define I_FIRST 0 72 #define I_SECOND 1 73 #define I_THIRD 2 74 75 typedef enum { SH_NULL, SH_HANDLE, SH_CONNECT, SH_TRANS } level_t; 76 77 void test_msg_handler(void *varg, semanage_handle_t *handle, const char *fmt, 78 ...); 79 80 void setup_handle(level_t level); 81 void cleanup_handle(level_t level); 82 void setup_handle_invalid_store(level_t level); 83 84 void helper_handle_create(void); 85 void helper_handle_destroy(void); 86 void helper_connect(void); 87 void helper_disconnect(void); 88 void helper_begin_transaction(void); 89 void helper_commit(void); 90 91 int create_test_store(void); 92 int write_test_policy_from_file(const char *filename); 93 int write_test_policy_src(unsigned char *data, unsigned int data_len); 94 int destroy_test_store(void); 95 void enable_test_store(void); 96 void disable_test_store(void); 97 98 #endif 99