Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
test case context concept implementation
[simgrid.git] / win32_test_app / src / TTestCaseContext.c
1 #include <TTestCaseContext.h>
2
3 /*
4  * Implementation of s_TestCaseContext connected functions.
5  */
6
7 /* 
8  * Create a new s_TestCaseContext and returns a pointer to self.
9  */
10 TestCaseContext_t TestCaseContext_new(void)
11 {
12         TestCaseContext_t context = calloc(1,sizeof(s_TestCaseContext_t));
13         
14         if(NULL == context)
15         {
16                 setErrno(E_TEST_CASE_CONTEXT_ALLOCATION_FAILED);
17                 return NULL;
18         }
19         
20         context->inputBuffer = Buffer_new();
21         
22         if(NULL == context->inputBuffer)
23         {
24                 TestCaseContext_free(context);
25                 return NULL;
26         }
27                 
28         context->outputBuffer = Buffer_new();
29         
30         if(NULL == context->outputBuffer)
31         {
32                 TestCaseContext_free(context);
33                 return NULL;    
34         }
35         
36         context->expectedOutputBuffer = Buffer_new();
37         
38         if(NULL == context->expectedOutputBuffer)
39         {
40                 TestCaseContext_free(context);
41                 return NULL;
42         }
43
44     context->commandLineBuffer = Buffer_new();
45
46     if(NULL == context->commandLineBuffer)
47         {
48                 TestCaseContext_free(context);
49                 return NULL;
50         }
51
52         
53         context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
54     context->isPostOutputCheckingEnabled        = DEFAULT_POST_OUTPUT_CHECKING_MODE;
55         context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
56         context->expectedExitCode = INVALID_EXIT_CODE;
57     context->exitCode = INVALID_EXIT_CODE;
58     context->name = NULL;
59     
60     context->runThread = true;
61         context->hThread = NULL;
62         context->hOutputRead = NULL;                                            
63         context->hInputWrite = NULL;                                            
64         context->hChildStdInRead = NULL;                                        
65         context->hChildStdOutWrite = NULL;                              
66         context->hChildStderr = NULL;
67         context->hChildStdoutReadTmp = NULL;
68         context->hChildStdinWriteTmp = NULL;
69     context->hConsole = NULL;
70
71     context->createConsole = false;
72     context->exitCodeCheckingEnabled = false;
73
74     context->started = false;
75
76         memset(&(context->pi),0,sizeof(PROCESS_INFORMATION));
77         
78         return context; 
79 }
80
81 /* 
82  * Destroy the s_TestCaseContext referenced by context. 
83  */
84 void TestCaseContext_free(TestCaseContext_t context)
85 {
86         if(NULL == context)
87                 return;
88                 
89         if(NULL !=context->inputBuffer)
90                 Buffer_free(context->inputBuffer);
91         
92         if(NULL !=context->outputBuffer)
93                 Buffer_free(context->outputBuffer);
94         
95         if(NULL !=context->expectedOutputBuffer)
96                 Buffer_free(context->expectedOutputBuffer);
97
98     if(NULL !=context->commandLineBuffer)
99                 Buffer_free(context->commandLineBuffer);
100                 
101         if(NULL == context->name)
102                 free(context->name);
103                 
104                 
105         /* Close all pipe handles. */   
106         if(context->hChildStdoutReadTmp)
107                 CloseHandle(context->hChildStdoutReadTmp);
108                 
109         if(context->hChildStdInRead)
110                 CloseHandle(context->hChildStdInRead);
111         
112         if(context->hChildStdinWriteTmp)
113                 CloseHandle(context->hChildStdinWriteTmp);
114         
115         if(context->hChildStdOutWrite)
116                 CloseHandle(context->hChildStdOutWrite);
117                 
118         if(context->hOutputRead)
119                 CloseHandle(context->hOutputRead);
120                 
121         if(context->pi.hThread)
122                 CloseHandle(context->pi.hThread);
123         
124         /* Use some violence, no choice. */     
125         if(context->pi.hProcess)
126         {
127                 /* Kill the child process. */
128                 TerminateProcess(context->pi.hProcess,0);
129         }
130         
131         if(context->hThread)
132         {
133                 /* Terminate the thread */
134                 TerminateThread(context->hThread,0);
135     }
136
137      if(context->hInputWrite)
138         CloseHandle(context->hInputWrite);
139                 
140         if(context->hChildStderr)
141                 CloseHandle(context->hChildStderr);
142         
143         free(context);
144         context = NULL;
145 }
146
147 /* 
148  * Set the timeout of the test case context.
149  */
150 void TestCaseContext_setTimeout(TestCaseContext_t context,int timeout)
151 {
152         context->timeoutValue = timeout;
153 }
154
155 /*
156  * Enable the output checking of the test case context.
157  */
158 void TestCaseContext_enableOutputChecking(TestCaseContext_t context)
159 {
160         context->isOutputCheckingEnabled = true;
161 }
162
163 /*
164  * Enable the output checking of the test case context.
165  */
166 void TestCaseContext_disableOutputChecking(TestCaseContext_t context)
167 {
168         /* If the post output checking mode is enable, disable it*/
169         context->isPostOutputCheckingEnabled = false;
170         context->isOutputCheckingEnabled = false;
171 }
172
173 /*
174  * Enable the post output checking of the test case context.
175  */
176 void TestCaseContext_enable_post_output_checking(TestCaseContext_t context)
177 {
178         /* enable the post output checking mode if the output checking mode is enabled*/
179         if(context->isOutputCheckingEnabled)
180                 context->isPostOutputCheckingEnabled = true;
181 }
182
183 /*
184  * Disable the post output checking of the test case context.
185  */
186 void TestCaseContext_disablePostOutputChecking(TestCaseContext_t context)
187 {
188         context->isPostOutputCheckingEnabled = false;
189 }
190
191 void TestCaseContext_createConsole(TestCaseContext_t context)
192 {
193     context->createConsole = true;
194 }
195
196 void TestCaseContext_createNoConsole(TestCaseContext_t context)
197 {
198     context->createConsole = false;
199 }
200
201 /*
202  * Set the expected exit code of the test case context.
203  */
204 void TestCaseContext_setExpectedExitCode(TestCaseContext_t context,int expected_code)
205 {
206         context->expectedExitCode = expected_code;
207 }
208
209 /*
210  * Return true if the output checking mode is enabled for this
211  * test case context. Otherwise the functions returns false.
212  */
213 bool TestCaseContext_isOutputCheckingEnabled(TestCaseContext_t context)
214 {
215         return context->isOutputCheckingEnabled;
216 }
217
218 void TestCaseContext_enableExitCodeChecking(TestCaseContext_t context)
219 {
220     context->exitCodeCheckingEnabled = true;
221 }
222
223 void TestCaseContext_disableExitCodeChecking(TestCaseContext_t context)
224 {
225     context->exitCodeCheckingEnabled = false;
226 }
227
228 void TestCaseContext_setCommandLine(TestCaseContext_t context,char* cmdLine)
229 {
230     Buffer_append(context->commandLineBuffer,cmdLine);
231 }
232
233
234 /*
235  * Append a child output to check in the 
236  * test case context.
237  */
238 void TestCaseContext_appendExpectedOutput(TestCaseContext_t context,char* expected_output)
239 {
240         Buffer_append(context->expectedOutputBuffer,expected_output);
241 }
242
243 /*
244  * Append a child output to check in the 
245  * test case context.
246  */
247 void TestCaseContext_appendChildInput(TestCaseContext_t context,char* input)
248 {
249         Buffer_append(context->inputBuffer,input);
250 }
251
252 /*
253  * Set the name of the test case name.
254  */
255 void TestCaseContext_setName(TestCaseContext_t context,char* name)
256 {
257     size_t size;
258
259         if(NULL != context->name)
260     {
261         free(context->name);
262     }
263                 
264         context->name = strdup(name);
265     size = strlen(name);
266
267     while ((context->name[size-1] == '\n') || (context->name[size-1] == '\r'))
268         {
269                 context->name[size-1] = '\0';
270                 
271                 if(size)
272                         size--;
273         }
274
275         /*context->name[strlen(context->name) - 1] ='\0';*/     
276 }
277
278 /* 
279  * Clear the s_TestCaseContext referenced by context.
280  */
281 void TestCaseContext_clear(TestCaseContext_t context)
282 {
283         if(!Buffer_empty(context->inputBuffer))
284                 Buffer_clear(context->inputBuffer);                     
285         
286         if(!Buffer_empty(context->outputBuffer))
287                 Buffer_clear(context->outputBuffer);
288                 
289         if(!Buffer_empty(context->expectedOutputBuffer))
290                 Buffer_clear(context->expectedOutputBuffer);
291
292     if(!Buffer_empty(context->commandLineBuffer))
293         Buffer_clear(context->commandLineBuffer);
294                 
295         if(NULL == context->name)
296         {
297                 free(context->name);
298                 context->name = NULL;
299         }
300                                                 
301         context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
302         context->isPostOutputCheckingEnabled    = DEFAULT_POST_OUTPUT_CHECKING_MODE;
303         context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
304         context->expectedExitCode = INVALID_EXIT_CODE;
305         context->exitCode = INVALID_EXIT_CODE;
306 }