-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrash_tests.cpp
More file actions
173 lines (131 loc) · 5.34 KB
/
Copy pathcrash_tests.cpp
File metadata and controls
173 lines (131 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <gtest/gtest.h>
#include <vector>
#include <string>
#include <iostream>
#include <memory>
class CrashTests : public ::testing::Test
{
};
//==============================================================================
// Segmentation Fault Test
//==============================================================================
// This test demonstrates a segmentation fault (accessing invalid memory)
TEST(CrashTests, SegmentationFault)
{
std::cout << "This test will cause a segmentation fault by dereferencing a null pointer." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
// GTEST_SKIP() << "Skipping intentional crash test";
// This will cause a segmentation fault
int *nullPtr = nullptr;
*nullPtr = 42;
// We should never reach this point
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Divide By Zero Test
//==============================================================================
// This test demonstrates a floating point exception (division by zero)
TEST(CrashTests, DivideByZero)
{
std::cout << "This test will cause a floating point exception by dividing by zero." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
// GTEST_SKIP() << "Skipping intentional crash test";
// This will cause a floating point exception
volatile int zero = 0;
volatile int result = 10 / zero;
// We should never reach this point
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Abort Test
//==============================================================================
// This test demonstrates handling of abort() calls
TEST(CrashTests, Abort)
{
std::cout << "This test will cause a program abort." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
// GTEST_SKIP() << "Skipping intentional crash test";
// This will abort the program
std::abort();
// We should never reach this point
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Stack Overflow Test
//==============================================================================
// Recursive function with no termination condition
void infiniteRecursion(int depth)
{
// Create a local variable to consume stack space
char buffer[1024] = {0};
// Print depth occasionally
if (depth % 100 == 0)
{
std::cout << "Recursion depth: " << depth << std::endl;
}
// Recurse deeper with no end condition
infiniteRecursion(depth + 1);
}
// This test demonstrates a stack overflow
TEST(CrashTests, StackOverflow)
{
std::cout << "This test will cause a stack overflow through infinite recursion." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
GTEST_SKIP() << "Skipping intentional crash test";
// This will cause a stack overflow
infiniteRecursion(1);
// We should never reach this point
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Out of Bounds Access Test
//==============================================================================
// This test demonstrates an out-of-bounds memory access
TEST(CrashTests, OutOfBounds)
{
std::cout << "This test will cause an out-of-bounds access." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
GTEST_SKIP() << "Skipping intentional crash test";
std::vector<int> v(5);
std::cout << "Vector size: " << v.size() << std::endl;
// This will cause an out-of-bounds access
v.at(10) = 42;
// We should never reach this point
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Stream Redirection Test
//==============================================================================
// This test demonstrates how TestCOE handles crashes with redirected streams
TEST(CrashTests, StreamRedirection)
{
std::cout << "This test will demonstrate stream redirection during a crash." << std::endl;
// Skip by default - uncomment the next line and comment the GTEST_SKIP to run
GTEST_SKIP() << "Skipping intentional crash test";
// Redirect stdout to a stringstream
std::stringstream buffer;
std::streambuf *oldBuf = std::cout.rdbuf(buffer.rdbuf());
std::cout << "This should be captured in the buffer" << std::endl;
// Cause a crash
int *nullPtr = nullptr;
*nullPtr = 42;
// This should never be reached
std::cout.rdbuf(oldBuf);
std::cout << "Buffer contained: " << buffer.str() << std::endl;
FAIL() << "Test did not crash as expected";
}
//==============================================================================
// Non-crashing Tests
//==============================================================================
// These tests ensure the example runs successfully when no crash tests are enabled
class BasicTests : public ::testing::Test
{
};
TEST(BasicTests, Addition)
{
EXPECT_EQ(2 + 2, 4);
}
TEST(BasicTests, Subtraction)
{
EXPECT_EQ(5 - 3, 2);
}