1 #include <TTestCaseContext.h>
4 * Implementation of s_TestCaseContext connected functions.
8 * Create a new s_TestCaseContext and returns a pointer to self.
10 TestCaseContext_t TestCaseContext_new(void)
12 TestCaseContext_t context = calloc(1,sizeof(s_TestCaseContext_t));
16 setErrno(E_TEST_CASE_CONTEXT_ALLOCATION_FAILED);
20 context->inputBuffer = Buffer_new();
22 if(NULL == context->inputBuffer)
24 TestCaseContext_free(context);
28 context->outputBuffer = Buffer_new();
30 if(NULL == context->outputBuffer)
32 TestCaseContext_free(context);
36 context->expectedOutputBuffer = Buffer_new();
38 if(NULL == context->expectedOutputBuffer)
40 TestCaseContext_free(context);
44 context->commandLineBuffer = Buffer_new();
46 if(NULL == context->commandLineBuffer)
48 TestCaseContext_free(context);
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;
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;
71 context->createConsole = false;
72 context->exitCodeCheckingEnabled = false;
74 context->started = false;
76 memset(&(context->pi),0,sizeof(PROCESS_INFORMATION));
82 * Destroy the s_TestCaseContext referenced by context.
84 void TestCaseContext_free(TestCaseContext_t context)
89 if(NULL !=context->inputBuffer)
90 Buffer_free(context->inputBuffer);
92 if(NULL !=context->outputBuffer)
93 Buffer_free(context->outputBuffer);
95 if(NULL !=context->expectedOutputBuffer)
96 Buffer_free(context->expectedOutputBuffer);
98 if(NULL !=context->commandLineBuffer)
99 Buffer_free(context->commandLineBuffer);
101 if(NULL == context->name)
105 /* Close all pipe handles. */
106 if(context->hChildStdoutReadTmp)
107 CloseHandle(context->hChildStdoutReadTmp);
109 if(context->hChildStdInRead)
110 CloseHandle(context->hChildStdInRead);
112 if(context->hChildStdinWriteTmp)
113 CloseHandle(context->hChildStdinWriteTmp);
115 if(context->hChildStdOutWrite)
116 CloseHandle(context->hChildStdOutWrite);
118 if(context->hOutputRead)
119 CloseHandle(context->hOutputRead);
121 if(context->pi.hThread)
122 CloseHandle(context->pi.hThread);
124 /* Use some violence, no choice. */
125 if(context->pi.hProcess)
127 /* Kill the child process. */
128 TerminateProcess(context->pi.hProcess,0);
133 /* Terminate the thread */
134 TerminateThread(context->hThread,0);
137 if(context->hInputWrite)
138 CloseHandle(context->hInputWrite);
140 if(context->hChildStderr)
141 CloseHandle(context->hChildStderr);
148 * Set the timeout of the test case context.
150 void TestCaseContext_setTimeout(TestCaseContext_t context,int timeout)
152 context->timeoutValue = timeout;
156 * Enable the output checking of the test case context.
158 void TestCaseContext_enableOutputChecking(TestCaseContext_t context)
160 context->isOutputCheckingEnabled = true;
164 * Enable the output checking of the test case context.
166 void TestCaseContext_disableOutputChecking(TestCaseContext_t context)
168 /* If the post output checking mode is enable, disable it*/
169 context->isPostOutputCheckingEnabled = false;
170 context->isOutputCheckingEnabled = false;
174 * Enable the post output checking of the test case context.
176 void TestCaseContext_enable_post_output_checking(TestCaseContext_t context)
178 /* enable the post output checking mode if the output checking mode is enabled*/
179 if(context->isOutputCheckingEnabled)
180 context->isPostOutputCheckingEnabled = true;
184 * Disable the post output checking of the test case context.
186 void TestCaseContext_disablePostOutputChecking(TestCaseContext_t context)
188 context->isPostOutputCheckingEnabled = false;
191 void TestCaseContext_createConsole(TestCaseContext_t context)
193 context->createConsole = true;
196 void TestCaseContext_createNoConsole(TestCaseContext_t context)
198 context->createConsole = false;
202 * Set the expected exit code of the test case context.
204 void TestCaseContext_setExpectedExitCode(TestCaseContext_t context,int expected_code)
206 context->expectedExitCode = expected_code;
210 * Return true if the output checking mode is enabled for this
211 * test case context. Otherwise the functions returns false.
213 bool TestCaseContext_isOutputCheckingEnabled(TestCaseContext_t context)
215 return context->isOutputCheckingEnabled;
218 void TestCaseContext_enableExitCodeChecking(TestCaseContext_t context)
220 context->exitCodeCheckingEnabled = true;
223 void TestCaseContext_disableExitCodeChecking(TestCaseContext_t context)
225 context->exitCodeCheckingEnabled = false;
228 void TestCaseContext_setCommandLine(TestCaseContext_t context,char* cmdLine)
230 Buffer_append(context->commandLineBuffer,cmdLine);
235 * Append a child output to check in the
238 void TestCaseContext_appendExpectedOutput(TestCaseContext_t context,char* expected_output)
240 Buffer_append(context->expectedOutputBuffer,expected_output);
244 * Append a child output to check in the
247 void TestCaseContext_appendChildInput(TestCaseContext_t context,char* input)
249 Buffer_append(context->inputBuffer,input);
253 * Set the name of the test case name.
255 void TestCaseContext_setName(TestCaseContext_t context,char* name)
259 if(NULL != context->name)
264 context->name = strdup(name);
267 while ((context->name[size-1] == '\n') || (context->name[size-1] == '\r'))
269 context->name[size-1] = '\0';
275 /*context->name[strlen(context->name) - 1] ='\0';*/
279 * Clear the s_TestCaseContext referenced by context.
281 void TestCaseContext_clear(TestCaseContext_t context)
283 if(!Buffer_empty(context->inputBuffer))
284 Buffer_clear(context->inputBuffer);
286 if(!Buffer_empty(context->outputBuffer))
287 Buffer_clear(context->outputBuffer);
289 if(!Buffer_empty(context->expectedOutputBuffer))
290 Buffer_clear(context->expectedOutputBuffer);
292 if(!Buffer_empty(context->commandLineBuffer))
293 Buffer_clear(context->commandLineBuffer);
295 if(NULL == context->name)
298 context->name = NULL;
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;