-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
161 lines (136 loc) · 4.27 KB
/
request_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
package main
import (
"testing"
"strings"
"os"
"path/filepath"
"time"
)
func TestCheckRequestFile(t *testing.T) {
staging, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(staging, "request-foo"), []byte("bar"), 0644)
if err != nil {
t.Fatal(err)
}
t.Run("success", func(t *testing.T) {
out, err := checkRequestFile("request-foo", staging, time.Minute)
if err != nil {
t.Fatal(err)
}
if out != filepath.Join(staging, "request-foo") {
t.Fatalf("unexpected path %q", out)
}
})
t.Run("name failure", func(t *testing.T) {
_, err := checkRequestFile("foo", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "request-") {
t.Fatal("should have failed")
}
})
t.Run("locality failure", func(t *testing.T) {
_, err := checkRequestFile("request-blah/../../foo", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "name of a file") {
t.Fatal("should have failed")
}
})
t.Run("not present", func(t *testing.T) {
_, err := checkRequestFile("request-blah", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "failed to access") {
t.Fatal("should have failed")
}
})
err = os.Mkdir(filepath.Join(staging, "request-blah"), 0700)
if err != nil {
t.Fatal(err)
}
t.Run("directory", func(t *testing.T) {
_, err := checkRequestFile("request-blah", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "directory") {
t.Fatal("should have failed")
}
})
err = os.Symlink(filepath.Join(staging, "request-foo"), filepath.Join(staging, "request-symlink"))
if err != nil {
t.Fatal(err)
}
t.Run("symlink", func(t *testing.T) {
_, err := checkRequestFile("request-symlink", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "symbolic link") {
t.Fatal("should have failed")
}
})
err = os.Link(filepath.Join(staging, "request-foo"), filepath.Join(staging, "request-hardlink"))
if err != nil {
t.Fatal(err)
}
t.Run("hard link", func(t *testing.T) {
_, err := checkRequestFile("request-hardlink", staging, time.Minute)
if err == nil || !strings.Contains(err.Error(), "hard link") {
t.Fatal("should have failed")
}
})
err = os.Remove(filepath.Join(staging, "request-hardlink")) // removing the hardlink to test the rest.
if err != nil {
t.Fatal(err)
}
t.Run("expired", func(t *testing.T) {
time.Sleep(time.Millisecond)
_, err := checkRequestFile("request-foo", staging, 0)
if err == nil || !strings.Contains(err.Error(), "expired") {
t.Fatal("should have failed")
}
})
}
func TestActiveRequestRegistry(t *testing.T) {
a := newActiveRequestRegistry(3)
path := "adasdasdasd"
ok := a.Add(path)
if !ok {
t.Fatal("expected a successful addition")
}
ok = a.Add(path)
if ok {
t.Fatal("expected a failed addition")
}
a.Remove(path)
ok = a.Add(path)
if !ok {
t.Fatal("expected a successful addition again")
}
ok = a.Add("xyxyxyxyxyx")
if !ok {
t.Fatal("expected a successful addition again")
}
}
func TestPrefillActiveRequestRegistry(t *testing.T) {
staging, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
names := []string{ "foo", "bar", "whee" }
for _, f := range names {
err = os.WriteFile(filepath.Join(staging, f), []byte{}, 0644)
if err != nil {
t.Fatal(err)
}
}
a := newActiveRequestRegistry(3)
err = prefillActiveRequestRegistry(a, staging, time.Millisecond * 100)
if err != nil {
t.Fatal(err)
}
for _, f := range names {
if a.Add(f) {
t.Fatalf("%s should already be present in the registry", f)
}
}
time.Sleep(time.Millisecond * 200)
for _, f := range names {
if !a.Add(f) {
t.Fatalf("%s should have been removed from the registry", f)
}
}
}