-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_test.go
78 lines (60 loc) · 1.53 KB
/
function_test.go
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
package main
import (
"io"
"os"
"testing"
fuzz "github.com/google/gofuzz"
)
func TestRunSimple(t *testing.T) {
cases := []struct {
command string
args []string
payload []byte
stdout string
stderr string
}{
{"echo", []string{"test"}, []byte("{}"), "test\n", ""},
{"sh", []string{"-c", "echo test >&2"}, []byte("{}"), "", "test\n"},
}
for i, c := range cases {
fn := NewFunction(c.command, c.args, c.payload)
rstdout, wstdout, _ := os.Pipe()
rstderr, wstderr, _ := os.Pipe()
fn.SetStdout(wstdout)
fn.SetStderr(wstderr)
stdout, stderr, err := fn.Run()
wstdout.Close()
wstderr.Close()
if err != nil {
t.Errorf("simple case(%d) failed): %s", i, err)
}
if stdout != c.stdout {
t.Errorf("simple case(%d) stdout want: %s, got: %s", i, c.stdout, stdout)
}
rso, _ := io.ReadAll(rstdout)
if string(rso) != c.stdout {
t.Errorf("simple case(%d) rstdout want: %s, got: %s", i, c.stdout, rso)
}
if stderr != c.stderr {
t.Errorf("simple case(%d) stderr want: %s, got: %s", i, c.stderr, stderr)
}
rse, _ := io.ReadAll(rstderr)
if string(rse) != c.stderr {
t.Errorf("simple case(%d) rstderr want: %s, got: %s", i, c.stderr, rse)
}
}
}
func TestRunFuzzing(t *testing.T) {
for i := 1; i <= 10; i++ {
f := fuzz.New()
var str string
f.Fuzz(&str)
fn := NewFunction("echo", []string{"-n", str}, []byte("{}"))
fn.SetStdout(io.Discard)
fn.SetStderr(io.Discard)
stdout, _, _ := fn.Run()
if stdout != str {
t.Errorf("fuzzing stdout want: %s, got: %s", str, stdout)
}
}
}