-
Notifications
You must be signed in to change notification settings - Fork 1
/
omnitrail_test.go
168 lines (140 loc) · 3.94 KB
/
omnitrail_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
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
package omnitrail
import (
"encoding/json"
"fmt"
"os"
"os/user"
"reflect"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmpty(t *testing.T) {
// TODO, use a tempdir instead of making one in ./test
if _, err := os.Stat("./test/empty"); os.IsNotExist(err) {
err := os.Mkdir("./test/empty", 0755)
assert.NoError(t, err)
}
name := "empty"
if err := testAdd(t, name); err != nil {
t.Fatalf("TestEmpty failed: %v", err)
}
}
func TestOneFiles(t *testing.T) {
name := "one-file"
if err := testAdd(t, name); err != nil {
t.Fatalf("TestOneFiles failed: %v", err)
}
}
func TestTwoFiles(t *testing.T) {
name := "two-files"
if err := testAdd(t, name); err != nil {
t.Fatalf("TestTwoFiles failed: %v", err)
}
}
func TestDeepStructure(t *testing.T) {
name := "deep"
if err := testAdd(t, name); err != nil {
t.Fatalf("TestDeepStructure failed: %v", err)
}
}
func TestSymlinkGood(t *testing.T) {
name := "symlink-good"
if err := testAdd(t, name); err != nil {
t.Fatalf("TestSymlinkGood failed: %v", err)
}
}
func TestSymlinkBroken(t *testing.T) {
name := "symlink-broken"
if err := testAdd(t, name); err != nil {
t.Fatalf("should ignore a bad symlink: %v", err)
}
}
func TestSymlinkOutOfBounds(t *testing.T) {
name := "symlink-out-of-bounds"
err := os.WriteFile("/tmp/omnitrail-well-known-file", []byte("hello"), 0644)
if err != nil {
t.Fatalf("unable to write temporary file: %v", err)
}
defer os.Remove("/tmp/omnitrail-well-known-file")
err = testAdd(t, name)
if !strings.Contains(err.Error(), "not in the allow list") {
t.Fatalf("unexpected error: %v", err)
}
if err == nil {
t.Fatalf("TestSymlinkOutOfBounds failed: should report a symlik out of bounds")
}
}
func testAdd(t *testing.T, name string) error {
mapping := NewTrail()
err := mapping.Add("./test/" + name)
if err != nil {
return err
}
a, _ := json.MarshalIndent(mapping.Envelope(), "", " ")
fmt.Println(string(a))
// WARNING: these are only for generating new test cases easily
// file, err := json.MarshalIndent(mapping.Envelope(), "", " ")
// os.WriteFile("./test/"+name+".json", file, 0644)
// res := FormatADGString(mapping)
// os.WriteFile("./test/"+name+".adg", []byte(res), 0644)
// END WARNING
expectedBytes, err := os.ReadFile("./test/" + name + ".json")
if err != nil {
return err
}
var expectedEnvelope Envelope
err = json.Unmarshal(expectedBytes, &expectedEnvelope)
if err != nil {
return err
}
shortestExpectedKey := getShortestKey(&expectedEnvelope)
shortestActualKey := getShortestKey(mapping.Envelope())
fmt.Println(shortestExpectedKey)
fmt.Println(shortestActualKey)
for oldKey, val := range expectedEnvelope.Mapping {
fmt.Println("old key: ", oldKey)
newKey := strings.Replace(oldKey, shortestExpectedKey, shortestActualKey, 1)
fmt.Println("new key: ", oldKey)
delete(expectedEnvelope.Mapping, oldKey)
expectedEnvelope.Mapping[newKey] = val
}
// get current username
currentUser, err := user.Current()
if err != nil {
return err
}
uid := currentUser.Uid
gid := currentUser.Gid
for _, v := range expectedEnvelope.Mapping {
v.Posix.OwnerUID = uid
v.Posix.OwnerGID = gid
}
assert.Equal(t, &expectedEnvelope, mapping.Envelope())
if !reflect.DeepEqual(&expectedEnvelope, mapping.Envelope()) {
return fmt.Errorf("expected envelope does not match actual envelope")
}
res := FormatADGString(mapping)
expectedBytes, err = os.ReadFile("./test/" + name + ".adg")
if err != nil {
return err
}
if string(expectedBytes) != res {
return fmt.Errorf("expected ADG string does not match actual ADG string")
}
return nil
}
func getShortestKey(expectedEnvelope *Envelope) string {
// get map keys
keys := make([]string, 0, len(expectedEnvelope.Mapping))
for key := range expectedEnvelope.Mapping {
keys = append(keys, key)
}
// sort keys from shortest to longest
sort.Slice(keys, func(i, j int) bool {
return len(keys[i]) < len(keys[j])
})
shortestKey := keys[0]
return shortestKey
}