-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
79 lines (66 loc) · 1.82 KB
/
Copy pathfuzz_test.go
File metadata and controls
79 lines (66 loc) · 1.82 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
package trexec_test
import (
"context"
"testing"
"time"
"github.com/Chokqu/trexec"
)
// FuzzContextCancellation tests that cancelling the context at arbitrary random millisecond
// intervals never deadlocks, crashes, or leaks the process tree.
func FuzzContextCancellation(f *testing.F) {
// Seed corpus with various cancellation delays (in milliseconds)
f.Add(0)
f.Add(1)
f.Add(10)
f.Add(50)
f.Add(100)
f.Add(250)
f.Fuzz(func(t *testing.T, cancelDelayMs int) {
if cancelDelayMs < 0 || cancelDelayMs > 500 {
return
}
ctx, cancel := context.WithCancel(context.Background())
cmd := trexec.CommandContext(ctx, helperBin, []string{"-sleep=10s"},
trexec.WithGracePeriod(50*time.Millisecond),
)
if err := cmd.Start(); err != nil {
cancel()
return
}
if cancelDelayMs > 0 {
time.Sleep(time.Duration(cancelDelayMs) * time.Millisecond)
}
cancel()
res := cmd.Wait()
if res == nil {
t.Fatal("expected non-nil Result")
}
})
}
// FuzzCommandExecution verifies that various combinations of stdout output and exit codes
// are handled cleanly without panic or buffer corruption.
func FuzzCommandExecution(f *testing.F) {
f.Add(0, "short")
f.Add(1, "error message")
f.Add(42, "custom exit")
f.Add(0, "multiline\noutput\ntest")
f.Fuzz(func(t *testing.T, exitCode int, outputText string) {
if exitCode < 0 || exitCode > 125 || len(outputText) > 1024 {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
args := []string{
"-exit=" + string(rune('0'+(exitCode%10))),
"-stdout=" + outputText,
}
out, res, err := trexec.Output(ctx, helperBin, args)
if res == nil {
t.Fatal("expected non-nil Result from Output")
}
if res.Success() && err != nil {
t.Errorf("success result should have nil error, got: %v", err)
}
_ = out
})
}