1
+ /* mock_helper.c -- mock helper for bash plugin UT. */
2
+ #include <stdarg.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #include <CUnit/CUnit.h>
7
+ #include <CUnit/Basic.h>
8
+ #include "mock_helper.h"
9
+
10
+ // define BASH_PLUGIN_UT_DEBUG to output UT debug message.
11
+ //#define BASH_PLUGIN_UT_DEBUG
12
+ #if defined (BASH_PLUGIN_UT_DEBUG )
13
+ # define debug_printf printf
14
+ #else
15
+ # define debug_printf
16
+ #endif
17
+
18
+ /* itrace buffer */
19
+ char mock_itrace_message_buffer [1024 ];
20
+
21
+ /* bash run command buffer */
22
+ char mock_onshell_execve_command_buffer [1024 ];
23
+
24
+ /* plugin handles. */
25
+ void * mock_plugin_handle = (void * )TEST_MOCK_PLUGIN_HANDLE ;
26
+ void * mock_plugin_default_function_handle = (void * )0x2234 ;
27
+ void * mock_plugin_on_shell_execve_handle = (void * )0x3234 ;
28
+ char * mock_dlerror_failed = "MOCK error" ;
29
+ char * mock_dlerror = NULL ;
30
+
31
+ /* define test scenarios for mock functions return different value by scenario. */
32
+ int test_scenario ;
33
+
34
+ /* define test scenarios for different return value. */
35
+ int plugin_init_status ;
36
+
37
+ /* define memory allocate counter. */
38
+ int memory_allocate_count ;
39
+
40
+ /* Set test scenario for test*/
41
+ void set_test_scenario (int scenario )
42
+ {
43
+ test_scenario = scenario ;
44
+ }
45
+
46
+ /* Get test scenario for test*/
47
+ int get_test_scenario ()
48
+ {
49
+ return test_scenario ;
50
+ }
51
+
52
+ /* Set plugin init status for test*/
53
+ void set_plugin_init_status (int status )
54
+ {
55
+ plugin_init_status = status ;
56
+ }
57
+
58
+ /* Get plugin init status for test*/
59
+ int get_plugin_init_status ()
60
+ {
61
+ return plugin_init_status ;
62
+ }
63
+
64
+ /* Set memory allocate count for test*/
65
+ void set_memory_allocate_count (int count )
66
+ {
67
+ memory_allocate_count = count ;
68
+ }
69
+
70
+ /* Get memory allocate count for test*/
71
+ int get_memory_allocate_count ()
72
+ {
73
+ return memory_allocate_count ;
74
+ }
75
+
76
+ /* MOCK plugin_init method*/
77
+ int mock_plugin_init ()
78
+ {
79
+ set_plugin_init_status (PLUGIN_INITIALIZED );
80
+ }
81
+
82
+ /* MOCK plugin_init method*/
83
+ int mock_plugin_uninit ()
84
+ {
85
+ set_plugin_init_status (PLUGIN_NOT_INITIALIZE );
86
+ }
87
+
88
+ /* MOCK on_shell_execve method*/
89
+ int mock_on_shell_execve (char * user , int shell_level , char * cmd , char * * argv )
90
+ {
91
+ // set mock command data to buffer for UT.
92
+ memset (mock_onshell_execve_command_buffer , 0 , sizeof (mock_onshell_execve_command_buffer ));
93
+
94
+ snprintf (mock_onshell_execve_command_buffer , sizeof (mock_onshell_execve_command_buffer ), "on_shell_execve: user: %s, level: %d, command: %s, argv: %p\n" , user , shell_level , cmd , argv );
95
+
96
+ debug_printf ("MOCK: mock_on_shell_execve: %s\n" , mock_onshell_execve_command_buffer );
97
+ }
98
+
99
+ /* MOCK dlopen*/
100
+ void * dlopen (const char * filename , int flags )
101
+ {
102
+ debug_printf ("MOCK: dlopen: %s\n" , filename );
103
+ if (TEST_SCEANRIO_PLUGIN_NOT_EXIT == test_scenario )
104
+ {
105
+ // return null when plugin not exist
106
+ mock_dlerror = mock_dlerror_failed ;
107
+ return NULL ;
108
+ }
109
+
110
+ // all other case return mock handle
111
+ mock_dlerror = NULL ;
112
+ return mock_plugin_handle ;
113
+ }
114
+
115
+ /* MOCK dlclose*/
116
+ int dlclose (void * handle )
117
+ {
118
+ debug_printf ("MOCK: dlclose: %p\n" , handle );
119
+ // check if the close handle match the opened handle
120
+ CU_ASSERT_EQUAL (handle , mock_plugin_handle );
121
+ }
122
+
123
+ /* MOCK dlsym*/
124
+ void * dlsym (void * restrict handle , const char * restrict symbol )
125
+ {
126
+ debug_printf ("MOCK: dlsym: %p, %s\n" , handle , symbol );
127
+ mock_dlerror = NULL ;
128
+ switch (test_scenario )
129
+ {
130
+ case TEST_SCEANRIO_PLUGIN_EXECVE_NOT_EXIT :
131
+ if (strcmp (symbol , "on_shell_execve" ) == 0 )
132
+ {
133
+ mock_dlerror = mock_dlerror_failed ;
134
+ return NULL ;
135
+ }
136
+
137
+ case TEST_SCEANRIO_PLUGIN_UNINIT_NOT_EXIT :
138
+ if (strcmp (symbol , "plugin_uninit" ) == 0 )
139
+ {
140
+ mock_dlerror = mock_dlerror_failed ;
141
+ return NULL ;
142
+ }
143
+
144
+ case TEST_SCEANRIO_PLUGIN_INIT_NOT_EXIT :
145
+ if (strcmp (symbol , "plugin_init" ) == 0 )
146
+ {
147
+ mock_dlerror = mock_dlerror_failed ;
148
+ return NULL ;
149
+ }
150
+
151
+ case TEST_SCEANRIO_PLUGIN_INIT_SUCCESS :
152
+ if (strcmp (symbol , "plugin_init" ) == 0 )
153
+ {
154
+ // return mock method handle so plugin framework will call it to initialize
155
+ return mock_plugin_init ;
156
+ }
157
+ else if (strcmp (symbol , "plugin_uninit" ) == 0 )
158
+ {
159
+ // return mock method handle so plugin framework will call it to initialize
160
+ return mock_plugin_uninit ;
161
+ }
162
+ else if (strcmp (symbol , "on_shell_execve" ) == 0 )
163
+ {
164
+ // return mock method handle so plugin framework will call it to initialize
165
+ return mock_on_shell_execve ;
166
+ }
167
+ }
168
+
169
+ return mock_plugin_default_function_handle ;
170
+ }
171
+
172
+ /* MOCK dlerror*/
173
+ char * dlerror (void )
174
+ {
175
+ return mock_dlerror ;
176
+ }
177
+
178
+ /* MOCK get_string_value*/
179
+ char * get_string_value (const char * str )
180
+ {
181
+ return "1" ;
182
+ }
183
+
184
+ /* MOCK absolute_program*/
185
+ int absolute_program (const char * str )
186
+ {
187
+ return 0 ;
188
+ }
189
+
190
+ /* MOCK itrace*/
191
+ void itrace (const char * format , ...)
192
+ {
193
+ // set mock message data to buffer for UT.
194
+ memset (mock_itrace_message_buffer , 0 , sizeof (mock_itrace_message_buffer ));
195
+
196
+ va_list args ;
197
+ va_start (args , format );
198
+ // save message to buffer to UT check later
199
+ vsnprintf (mock_itrace_message_buffer , sizeof (mock_itrace_message_buffer ), format , args );
200
+ va_end (args );
201
+ debug_printf ("MOCK: itrace: %s\n" , mock_itrace_message_buffer );
202
+ }
203
+
204
+ /* MOCK malloc method*/
205
+ void * mock_malloc (size_t size )
206
+ {
207
+ memory_allocate_count ++ ;
208
+ debug_printf ("MOCK: malloc memory count: %d\n" , memory_allocate_count );
209
+ return malloc (size );
210
+ }
211
+
212
+ /* MOCK free method*/
213
+ void mock_free (void * ptr )
214
+ {
215
+ memory_allocate_count -- ;
216
+ debug_printf ("MOCK: free memory count: %d\n" , memory_allocate_count );
217
+ free (ptr );
218
+ }
0 commit comments