Debian Codegraph @

tests/CppUTest/UtestPlatformTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/CommandLineTestRunner.h"
29 #include "CppUTest/TestHarness.h"
30 #include "CppUTest/TestTestingFixture.h"
31 #include "CppUTest/PlatformSpecificFunctions.h"
32 #include "CppUTest/StandardCLibrary.h"
33 #include "CppUTest/TestMemoryAllocator.h"
34
35 #if CPPUTEST_USE_STD_C_LIB
36
37 // This will cause a crash in VS2010 due to PlatformSpecificFree being uninitialized
38 static const SimpleString str1("abc");
39 static const SimpleString str2("def");
40 static const SimpleString str3(str1 + str2);
41
42 TEST_GROUP(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess)
43 {
44 TestTestingFixture fixture;
45 };
46
47 #ifndef CPPUTEST_HAVE_FORK
48
49 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, DummyFailsWithMessage)
50 {
51 fixture.setRunTestsInSeperateProcess();
52 fixture.runAllTests();
53 fixture.assertPrintContains("-p doesn't work on this platform, as it is lacking fork.\b");
54 }
55
56 #else
57
58 static void _failFunction()
59 {
60 FAIL("This test fails");
61 }
62
63 static void _exitNonZeroFunction() _no_return_;
64 static void _exitNonZeroFunction()
65 {
66 /* destructor of static objects will be called. If StringCache was there then the allocator will report invalid deallocations of static SimpleString */
67 SimpleString::setStringAllocator(SimpleString::getStringAllocator()->actualAllocator());
68 exit(1);
69 }
70
71 #include <errno.h>
72
73 static int waitpid_while_debugging_stub_number_called = 0;
74 static int waitpid_while_debugging_stub_forced_failures = 0;
75
76 extern "C" {
77
78 static int (*original_waitpid)(int, int*, int) = NULLPTR;
79
80 static int fork_failed_stub(void) { return -1; }
81
82 static int waitpid_while_debugging_stub(int pid, int* status, int options)
83 {
84 static int saved_status;
85
86 if (waitpid_while_debugging_stub_number_called++ < waitpid_while_debugging_stub_forced_failures) {
87 saved_status = *status;
88 errno=EINTR;
89 return -1;
90 }
91 else {
92 *status = saved_status;
93 return original_waitpid(pid, status, options);
94 }
95 }
96
97 static int waitpid_failed_stub(int, int*, int) { return -1; }
98 }
99
100 #include <unistd.h>
101 #include <signal.h>
102
103 static void _stoppedTestFunction()
104 {
105 kill(getpid(), SIGSTOP);
106 }
107
108 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, TestInSeparateProcessWorks)
109 {
110 fixture.setRunTestsInSeperateProcess();
111 fixture.runAllTests();
112 fixture.assertPrintContains("OK (1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out");
113 }
114
115 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, FailureInSeparateProcessWorks)
116 {
117 fixture.setRunTestsInSeperateProcess();
118 fixture.setTestFunction(_failFunction);
119 fixture.runAllTests();
120 fixture.assertPrintContains("Failed in separate process");
121 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out");
122 }
123
124 #if (! CPPUTEST_SANITIZE_ADDRESS)
125
126 static int _accessViolationTestFunction()
127 {
128 return *(volatile int*) NULLPTR;
129 }
130
131 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, AccessViolationInSeparateProcessWorks)
132 {
133 fixture.setRunTestsInSeperateProcess();
134 fixture.setTestFunction((void(*)())_accessViolationTestFunction);
135 fixture.runAllTests();
136 fixture.assertPrintContains("Failed in separate process - killed by signal 11");
137 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran");
138 }
139
140 #endif
141
142 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, StoppedInSeparateProcessWorks)
143 {
144 fixture.setRunTestsInSeperateProcess();
145 fixture.setTestFunction(_stoppedTestFunction);
146 fixture.runAllTests();
147 fixture.assertPrintContains("Stopped in separate process - continuing");
148 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran");
149 }
150
151 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, CallToForkFailedInSeparateProcessWorks)
152 {
153 UT_PTR_SET(PlatformSpecificFork, fork_failed_stub);
154 fixture.setRunTestsInSeperateProcess();
155 fixture.runAllTests();
156 fixture.assertPrintContains("Call to fork() failed");
157 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran");
158 }
159
160 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, CallToWaitPidWhileDebuggingInSeparateProcessWorks)
161 {
162 UT_PTR_SET(original_waitpid, PlatformSpecificWaitPid);
163 UT_PTR_SET(PlatformSpecificWaitPid, waitpid_while_debugging_stub);
164 waitpid_while_debugging_stub_number_called = 0;
165 waitpid_while_debugging_stub_forced_failures = 10;
166 fixture.setRunTestsInSeperateProcess();
167 fixture.runAllTests();
168 fixture.assertPrintContains("OK (1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out");
169 // extra check to confirm that waitpid() was polled until it passed (and passed call adds one)
170 CHECK(waitpid_while_debugging_stub_number_called > waitpid_while_debugging_stub_forced_failures);
171 }
172
173 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, CallToWaitPidStopsAndReportsAnErrorAfter20TimesRetry)
174 {
175 UT_PTR_SET(original_waitpid, PlatformSpecificWaitPid);
176 UT_PTR_SET(PlatformSpecificWaitPid, waitpid_while_debugging_stub);
177 waitpid_while_debugging_stub_number_called = 0;
178 waitpid_while_debugging_stub_forced_failures = 40;
179 fixture.setRunTestsInSeperateProcess();
180 fixture.runAllTests();
181 fixture.assertPrintContains("Call to waitpid() failed with EINTR. Tried 30 times and giving up! Sometimes happens in debugger");
182 // extra check to confirm that waitpid() was polled until it passed (and passed call adds one)
183 CHECK(waitpid_while_debugging_stub_number_called > 30);
184 }
185
186
187 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, CallToWaitPidFailedInSeparateProcessWorks)
188 {
189 UT_PTR_SET(PlatformSpecificWaitPid, waitpid_failed_stub);
190 fixture.setRunTestsInSeperateProcess();
191 fixture.runAllTests();
192 fixture.assertPrintContains("Call to waitpid() failed");
193 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran");
194 }
195
196 TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, MultipleTestsInSeparateProcessAreCountedProperly)
197 {
198 fixture.setRunTestsInSeperateProcess();
199 fixture.runTestWithMethod(NULLPTR);
200 fixture.runTestWithMethod(_stoppedTestFunction);
201 fixture.runTestWithMethod(NULLPTR);
202 fixture.runTestWithMethod(_exitNonZeroFunction);
203 fixture.runTestWithMethod(NULLPTR);
204 fixture.assertPrintContains("Failed in separate process");
205 fixture.assertPrintContains("Stopped in separate process");
206 fixture.assertPrintContains("Errors (2 failures, 5 tests, 5 ran, 0 checks, 0 ignored, 0 filtered out");
207 }
208
209 #endif
210 #endif
211