• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		free(__str2); \
43 		free(__str); \
44 	} while (0)
45 
46 
47 /* Override CU_*_FATAL() in order to help static analyzers by really asserting that an assertion holds */
48 #ifdef __CHECKER__
49 
50 #undef CU_ASSERT_FATAL
51 #define CU_ASSERT_FATAL(value) do { \
52 		int _value = (value); \
53 		CU_ASSERT(_value); \
54 		assert(_value); \
55 	} while (0)
56 
57 #undef CU_FAIL_FATAL
58 #define CU_FAIL_FATAL(msg) do { \
59 		CU_FAIL(msg); \
60 		assert(0); \
61 	} while (0)
62 
63 #undef CU_ASSERT_PTR_NOT_NULL_FATAL
64 #define CU_ASSERT_PTR_NOT_NULL_FATAL(value) do { \
65 		const void *_value = (value); \
66 		CU_ASSERT_PTR_NOT_NULL(_value); \
67 		assert(_value != NULL); \
68 	} while (0)
69 
70 #endif /* __CHECKER__ */
71 
72 #define I_NULL  -1
73 #define I_FIRST  0
74 #define I_SECOND 1
75 #define I_THIRD  2
76 
77 typedef enum { SH_NULL, SH_HANDLE, SH_CONNECT, SH_TRANS } level_t;
78 
79 void test_msg_handler(void *varg, semanage_handle_t *handle, const char *fmt,
80 		      ...);
81 
82 void setup_handle(level_t level);
83 void cleanup_handle(level_t level);
84 void setup_handle_invalid_store(level_t level);
85 
86 void helper_handle_create(void);
87 void helper_handle_destroy(void);
88 void helper_connect(void);
89 void helper_disconnect(void);
90 void helper_begin_transaction(void);
91 void helper_commit(void);
92 
93 int create_test_store(void);
94 int write_test_policy_from_file(const char *filename);
95 int write_test_policy_src(unsigned char *data, unsigned int data_len);
96 int destroy_test_store(void);
97 void enable_test_store(void);
98 void disable_test_store(void);
99 
100 #endif
101