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));
14 if (NULL == context) {
15 setErrno(E_TEST_CASE_CONTEXT_ALLOCATION_FAILED);
19 context->inputBuffer = Buffer_new();
21 if (NULL == context->inputBuffer) {
22 TestCaseContext_free(context);
26 context->outputBuffer = Buffer_new();
28 if (NULL == context->outputBuffer) {
29 TestCaseContext_free(context);
33 context->expectedOutputBuffer = Buffer_new();
35 if (NULL == context->expectedOutputBuffer) {
36 TestCaseContext_free(context);
40 context->commandLineBuffer = Buffer_new();
42 if (NULL == context->commandLineBuffer) {
43 TestCaseContext_free(context);
48 context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
49 context->isPostOutputCheckingEnabled = DEFAULT_POST_OUTPUT_CHECKING_MODE;
50 context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
51 context->expectedExitCode = INVALID_EXIT_CODE;
52 context->exitCode = INVALID_EXIT_CODE;
55 context->runThread = true;
56 context->hThread = NULL;
57 context->hOutputRead = NULL;
58 context->hInputWrite = NULL;
59 context->hChildStdInRead = NULL;
60 context->hChildStdOutWrite = NULL;
61 context->hChildStderr = NULL;
62 context->hChildStdoutReadTmp = NULL;
63 context->hChildStdinWriteTmp = NULL;
64 context->hConsole = NULL;
66 context->createConsole = false;
67 context->exitCodeCheckingEnabled = false;
69 context->started = false;
71 memset(&(context->pi), 0, sizeof(PROCESS_INFORMATION));
77 * Destroy the s_TestCaseContext referenced by context.
79 void TestCaseContext_free(TestCaseContext_t context)
84 if (NULL != context->inputBuffer)
85 Buffer_free(context->inputBuffer);
87 if (NULL != context->outputBuffer)
88 Buffer_free(context->outputBuffer);
90 if (NULL != context->expectedOutputBuffer)
91 Buffer_free(context->expectedOutputBuffer);
93 if (NULL != context->commandLineBuffer)
94 Buffer_free(context->commandLineBuffer);
96 if (NULL == context->name)
100 /* Close all pipe handles. */
101 if (context->hChildStdoutReadTmp)
102 CloseHandle(context->hChildStdoutReadTmp);
104 if (context->hChildStdInRead)
105 CloseHandle(context->hChildStdInRead);
107 if (context->hChildStdinWriteTmp)
108 CloseHandle(context->hChildStdinWriteTmp);
110 if (context->hChildStdOutWrite)
111 CloseHandle(context->hChildStdOutWrite);
113 if (context->hOutputRead)
114 CloseHandle(context->hOutputRead);
116 if (context->pi.hThread)
117 CloseHandle(context->pi.hThread);
119 /* Use some violence, no choice. */
120 if (context->pi.hProcess) {
121 /* Kill the child process. */
122 TerminateProcess(context->pi.hProcess, 0);
125 if (context->hThread) {
126 /* Terminate the thread */
127 TerminateThread(context->hThread, 0);
130 if (context->hInputWrite)
131 CloseHandle(context->hInputWrite);
133 if (context->hChildStderr)
134 CloseHandle(context->hChildStderr);
141 * Set the timeout of the test case context.
143 void TestCaseContext_setTimeout(TestCaseContext_t context, int timeout)
145 context->timeoutValue = timeout;
149 * Enable the output checking of the test case context.
151 void TestCaseContext_enableOutputChecking(TestCaseContext_t context)
153 context->isOutputCheckingEnabled = true;
157 * Enable the output checking of the test case context.
159 void TestCaseContext_disableOutputChecking(TestCaseContext_t context)
161 /* If the post output checking mode is enable, disable it */
162 context->isPostOutputCheckingEnabled = false;
163 context->isOutputCheckingEnabled = false;
167 * Enable the post output checking of the test case context.
169 void TestCaseContext_enable_post_output_checking(TestCaseContext_t context)
171 /* enable the post output checking mode if the output checking mode is enabled */
172 if (context->isOutputCheckingEnabled)
173 context->isPostOutputCheckingEnabled = true;
177 * Disable the post output checking of the test case context.
179 void TestCaseContext_disablePostOutputChecking(TestCaseContext_t context)
181 context->isPostOutputCheckingEnabled = false;
184 void TestCaseContext_createConsole(TestCaseContext_t context)
186 context->createConsole = true;
189 void TestCaseContext_createNoConsole(TestCaseContext_t context)
191 context->createConsole = false;
195 * Set the expected exit code of the test case context.
197 void TestCaseContext_setExpectedExitCode(TestCaseContext_t context,
200 context->expectedExitCode = expected_code;
204 * Return true if the output checking mode is enabled for this
205 * test case context. Otherwise the functions returns false.
207 bool TestCaseContext_isOutputCheckingEnabled(TestCaseContext_t context)
209 return context->isOutputCheckingEnabled;
212 void TestCaseContext_enableExitCodeChecking(TestCaseContext_t context)
214 context->exitCodeCheckingEnabled = true;
217 void TestCaseContext_disableExitCodeChecking(TestCaseContext_t context)
219 context->exitCodeCheckingEnabled = false;
222 void TestCaseContext_setCommandLine(TestCaseContext_t context,
225 Buffer_append(context->commandLineBuffer, cmdLine);
230 * Append a child output to check in the
233 void TestCaseContext_appendExpectedOutput(TestCaseContext_t context,
234 char *expected_output)
236 Buffer_append(context->expectedOutputBuffer, expected_output);
240 * Append a child output to check in the
243 void TestCaseContext_appendChildInput(TestCaseContext_t context,
246 Buffer_append(context->inputBuffer, input);
250 * Set the name of the test case name.
252 void TestCaseContext_setName(TestCaseContext_t context, char *name)
256 if (NULL != context->name) {
260 context->name = strdup(name);
263 while ((context->name[size - 1] == '\n')
264 || (context->name[size - 1] == '\r')) {
265 context->name[size - 1] = '\0';
271 /*context->name[strlen(context->name) - 1] ='\0'; */
275 * Clear the s_TestCaseContext referenced by context.
277 void TestCaseContext_clear(TestCaseContext_t context)
279 if (!Buffer_empty(context->inputBuffer))
280 Buffer_clear(context->inputBuffer);
282 if (!Buffer_empty(context->outputBuffer))
283 Buffer_clear(context->outputBuffer);
285 if (!Buffer_empty(context->expectedOutputBuffer))
286 Buffer_clear(context->expectedOutputBuffer);
288 if (!Buffer_empty(context->commandLineBuffer))
289 Buffer_clear(context->commandLineBuffer);
291 if (NULL == context->name) {
293 context->name = NULL;
296 context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
297 context->isPostOutputCheckingEnabled = DEFAULT_POST_OUTPUT_CHECKING_MODE;
298 context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
299 context->expectedExitCode = INVALID_EXIT_CODE;
300 context->exitCode = INVALID_EXIT_CODE;