-
Notifications
You must be signed in to change notification settings - Fork 8
/
gobox_test.go
82 lines (71 loc) · 1.93 KB
/
gobox_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
79
80
81
82
package main
import (
"bytes"
"os/exec"
"strings"
"testing"
)
func TestExec(t *testing.T) {
s := []string{"pass.txt"}
patterns := []string{""}
reqs, err := Exec("cat", s, patterns, patterns)
if err != nil {
t.Errorf("Something went wrong: %v", err)
}
if len(reqs) != 2 {
t.Errorf("reqs count was incorrect, got: %d, want: %d.", len(reqs), 2)
}
}
func TestInput(t *testing.T) {
cmd := exec.Command("./gobox", "cat", "./pass.txt")
var out bytes.Buffer
var in bytes.Buffer
cmd.Stdout = &out
cmd.Stdout = &in
in.Write([]byte("n\n\r"))
err := cmd.Run()
if err != nil {
t.Errorf("Something went wrong: %v", err)
}
if strings.Contains(out.String(), "Blocked READ on ...") {
t.Errorf("Expected %s output got %s", "Blocked READ on ...", out.String())
}
}
// TODO we probably should instead just pass a mock reader for stdin into the Exec function and then call the fn
// directly rather that full bin tests
func TestAllowList(t *testing.T) {
cmd := exec.Command("./gobox", "--y", "*.so", "--y", "*.txt", "cat", "./pass.txt")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
t.Errorf("Something went wrong: %v", err)
}
if out.String() != "123\n" {
t.Errorf("Expected %s output got %s", "123", out.String())
}
}
func TestBlockList(t *testing.T) {
cmd := exec.Command("./gobox", "--y", "*.so", "--n", "*.txt", "cat", "./password.txt")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
t.Errorf("Something went wrong: %v", err)
}
if !strings.Contains(out.String(), "Blocked READ on ") {
t.Errorf("Expected %s output got %s", "Blocked READ on", out.String())
}
}
func TestHelp(t *testing.T) {
cmd := exec.Command("./gobox", "-h")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
t.Errorf("Something went wrong: %v", err)
}
if strings.Contains(out.String(), "Usage of ./gobox:") {
t.Errorf("Expected %s output got %s", "123", out.String())
}
}