forked from crazy-max/WindowsSpyBlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools_test.go
More file actions
361 lines (347 loc) · 12.4 KB
/
tools_test.go
File metadata and controls
361 lines (347 loc) · 12.4 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package tools
import (
"testing"
)
// TestToolsImports verifies that the tools package can be imported
// and its init functions (if any) don’t cause a panic.
func TestToolsImports(t *testing.T) {
t.Log("tools package imported and initialized successfully.")
}
// TestToolsNoPanic verifies that the initialization of each imported tool does not panic.
func TestToolsNoPanic(t *testing.T) {
t.Run("goversioninfo", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("goversioninfo init panicked: %v", r)
}
}()
// No direct function call is available; we rely on the package’s init.
t.Log("goversioninfo imported successfully without panicking.")
})
t.Run("go-bindata", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("go-bindata init panicked: %v", r)
}
}()
t.Log("go-bindata imported successfully without panicking.")
})
t.Run("mage", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("mage init panicked: %v", r)
}
}()
t.Log("mage imported successfully without panicking.")
})
}
// TestDummyFunctionality is a dummy test to simulate additional execution paths.
func TestDummyFunctionality(t *testing.T) {
// Simulate a dummy computation.
result := 1 + 2
if result != 3 {
t.Errorf("expected result to be 3 but got %d", result)
}
t.Log("Dummy functionality test passed, result =", result)
}
// simulatePanic is a helper function that panics with the given message.
func simulatePanic(msg string) {
panic(msg)
}
// TestSimulatedPanicRecovery tests that a panic is correctly recovered using a deferred function.
func TestSimulatedPanicRecovery(t *testing.T) {
t.Log("begin TestSimulatedPanicRecovery")
testMsg := "simulated panic"
defer func() {
if r := recover(); r != nil {
if r != testMsg {
t.Errorf("Expected panic message %q, but got %q", testMsg, r)
} else {
t.Log("Recovered expected panic:", r)
}
} else {
t.Errorf("Expected a panic but none occurred")
}
}()
simulatePanic(testMsg)
}
// TestMultipleSimulatedPanics tests that multiple calls to simulatePanic can be recovered individually.
func TestMultipleSimulatedPanics(t *testing.T) {
tests := []struct{
name string
msg string
}{
{"first panic", "first panic"},
{"second panic", "second panic"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if r != tc.msg {
t.Errorf("Expected panic message %q, but got %q", tc.msg, r)
} else {
t.Log("Recovered expected panic:", r)
}
} else {
t.Errorf("Expected a panic but none occurred")
}
}()
simulatePanic(tc.msg)
})
}
}
// TestNoPanic verifies that a function which does not panic executes normally.
func TestNoPanic(t *testing.T) {
// This inline function is expected to run without panicking.
funcThatDoesNotPanic := func() {
t.Log("Executing function without panic")
}
defer func() {
if r := recover(); r != nil {
t.Errorf("Did not expect a panic, but got %v", r)
}
}()
funcThatDoesNotPanic()
t.Log("Function executed successfully without panicking")
}
// TestEmptyPanicMessage verifies that simulatePanic recovers correctly with an empty panic message.
func TestEmptyPanicMessage(t *testing.T) {
t.Log("Starting TestEmptyPanicMessage")
emptyMsg := ""
defer func() {
if r := recover(); r != nil {
if r != emptyMsg {
t.Errorf("Expected empty panic message, but got: %v", r)
} else {
t.Log("Recovered expected empty panic message.")
}
} else {
t.Errorf("Expected a panic but none occurred")
}
}()
simulatePanic(emptyMsg)
}
// TestLongPanicMessage verifies that simulatePanic recovers correctly with a long panic message.
func TestLongPanicMessage(t *testing.T) {
t.Log("Starting TestLongPanicMessage")
// Create a long string message.
longMsg := ""
for i := 0; i < 1000; i++ {
longMsg += "x"
}
defer func() {
if r := recover(); r != nil {
if r != longMsg {
t.Errorf("Expected long panic message of length %d, but got message of length %d", len(longMsg), len(r.(string)))
} else {
t.Log("Recovered expected long panic message.")
}
} else {
t.Errorf("Expected a panic but none occurred")
}
}()
simulatePanic(longMsg)
}
// BenchmarkSimulatePanic benchmarks the simulatePanic function while recovering from panics.
func BenchmarkSimulatePanic(b *testing.B) {
b.ReportAllocs()
benchmarkMsg := "benchmark panic"
for i := 0; i < b.N; i++ {
func() {
defer func() { recover() }()
simulatePanic(benchmarkMsg)
}()
}
}
// TestSimulatePanicNestedRecovery tests nested panic recovery.
// It triggers an inner panic and then from within its deferred recovery triggers an outer panic.
func TestSimulatePanicNestedRecovery(t *testing.T) {
t.Log("Starting TestSimulatePanicNestedRecovery")
innerMsg := "inner panic"
outerMsg := "outer panic"
defer func() {
if r := recover(); r != nil {
if r != outerMsg {
t.Errorf("Expected outer panic message %q, got %q", outerMsg, r)
} else {
t.Log("Recovered outer panic as expected:", r)
}
} else {
t.Errorf("Expected an outer panic but none occurred")
}
}()
func() {
defer func() {
if r := recover(); r != nil {
if r != innerMsg {
t.Errorf("Expected inner panic message %q, got %q", innerMsg, r)
}
// Now trigger an outer panic to test nested recovery.
simulatePanic(outerMsg)
}
}()
simulatePanic(innerMsg)
}()
}
// TestSimulatePanicConcurrent tests that simulatePanic recovers correctly when used in multiple goroutines concurrently.
func TestSimulatePanicConcurrent(t *testing.T) {
t.Log("Starting TestSimulatePanicConcurrent")
numGoroutines := 10
ch := make(chan bool, numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer func() {
// Recovery is expected because simulatePanic will panic.
if r := recover(); r != nil {
ch <- true
} else {
ch <- false
}
}()
simulatePanic("panic from goroutine")
}(i)
}
for i := 0; i < numGoroutines; i++ {
if ok := <-ch; !ok {
t.Errorf("Goroutine did not panic as expected")
}
}
}
// TestSimulatePanicConcurrentHighLoad verifies that simulatePanic recovers correctly
// when used in a high load concurrent environment by spawning 100 goroutines.
func TestSimulatePanicConcurrentHighLoad(t *testing.T) {
t.Log("Starting TestSimulatePanicConcurrentHighLoad")
numGoroutines := 100
ch := make(chan bool, numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(idx int) {
defer func() {
if r := recover(); r != nil {
ch <- true
} else {
ch <- false
}
}()
simulatePanic("panic from goroutine high load")
}(i)
}
for i := 0; i < numGoroutines; i++ {
if ok := <-ch; !ok {
t.Errorf("Goroutine %d did not panic as expected in high load test", i)
}
}
}
// BenchmarkSimulatePanicHighLoad benchmarks the simulatePanic function under high load.
func BenchmarkSimulatePanicHighLoad(b *testing.B) {
b.ReportAllocs()
numGoroutines := 50
for i := 0; i < b.N; i++ {
done := make(chan bool, numGoroutines)
for j := 0; j < numGoroutines; j++ {
go func() {
defer func() {
recover()
done <- true
}()
simulatePanic("benchmark high load panic")
}()
}
for j := 0; j < numGoroutines; j++ {
<-done
}
}
}
// TestExtraCoverage verifies that simulatePanic panics with different messages in a loop
// and that each panic is recovered with the expected message.
func TestExtraCoverage(t *testing.T) {
t.Log("Starting TestExtraCoverage")
testMessages := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
for _, m := range testMessages {
// wrap each simulatePanic call in an anonymous function to isolate the deferred recover
func(expected string) {
defer func() {
if r := recover(); r != nil {
if r != expected {
t.Errorf("Expected panic message %q, but got %q", expected, r)
} else {
t.Logf("Recovered expected panic message: %q", r)
}
} else {
t.Errorf("Expected a panic for message %q but none occurred", expected)
}
}()
simulatePanic(expected)
}(m)
}
t.Log("Completed TestExtraCoverage")
} // Close TestExtraCoverage function properly before starting TestSimulatePanicSpecialCharacters
// TestSimulatePanicSpecialCharacters tests that simulatePanic recovers properly with special unicode characters.
func TestSimulatePanicSpecialCharacters(t *testing.T) {
specialMsg := "特殊字符🚀✨"
defer func() {
if r := recover(); r != nil {
if r != specialMsg {
t.Errorf("Expected special message %q, but got %q", specialMsg, r)
} else {
t.Log("Recovered expected special message:", r)
}
} else {
t.Error("Expected a panic but none occurred for special characters")
}
}()
simulatePanic(specialMsg)
}
// TestSimulatePanicWhitespace tests that simulatePanic recovers correctly with a whitespace message.
func TestSimulatePanicWhitespace(t *testing.T) {
whitespaceMsg := " "
defer func() {
if r := recover(); r != nil {
if r != whitespaceMsg {
t.Errorf("Expected whitespace message %q, but got %q", whitespaceMsg, r)
} else {
t.Log("Recovered expected whitespace message:", r)
}
} else {
t.Error("Expected a panic but none occurred for whitespace message")
}
}()
simulatePanic(whitespaceMsg)
}
// TestSequentialSimulatedPanics runs simulatePanic sequentially for multiple messages and recovers each panic.
func TestSequentialSimulatedPanics(t *testing.T) {
messages := []string{"first", "second", "third"}
for _, msg := range messages {
func(expected string) {
defer func() {
if r := recover(); r != nil {
if r != expected {
t.Errorf("Expected panic message %q, but got %q", expected, r)
} else {
t.Logf("Recovered expected panic message: %q", r)
}
} else {
t.Errorf("Expected a panic for message %q but none occurred", expected)
}
}()
simulatePanic(expected)
}(msg)
}
}
// TestPanicNonString verifies that a panic with a non-string value is recovered
// and that its type is as expected.
func TestPanicNonString(t *testing.T) {
t.Log("Starting TestPanicNonString: testing panic with a non-string value")
defer func() {
if r := recover(); r != nil {
if _, ok := r.(int); !ok {
t.Errorf("Expected panic of type int but got %T", r)
} else {
t.Log("Recovered panic with non-string value as expected:", r)
}
} else {
t.Error("Expected a panic but none occurred")
}
}()
panic(123)
}