-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvanity_integration_test.go
154 lines (145 loc) · 3.27 KB
/
vanity_integration_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
package vanity_test
import (
"io/fs"
"net/http"
"net/http/httptest"
"os"
"testing"
"kkn.fi/vanity"
)
func tmpdir(t *testing.T) string {
t.Helper()
dir, err := os.CreateTemp("/tmp", "permission-")
if err != nil {
t.Fatalf("error while creating test temp dir: %v", err)
}
return dir.Name()
}
func integrationTest(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
}
func TestStaticDirPermissionsIntegration(t *testing.T) {
integrationTest(t)
tests := []struct {
name string
permissions fs.FileMode
wantErr bool
}{
{
name: "directory doesn't have any permissions",
permissions: 0000,
wantErr: true,
},
{
name: "directory has all exec permissions",
permissions: 0111,
wantErr: true,
},
{
name: "directory has all write permissions",
permissions: 0222,
wantErr: true,
},
{
name: "directory has all read permissions",
permissions: 0444,
wantErr: true,
},
{
name: "directory has other read permissions",
permissions: 0004,
wantErr: true,
},
{
name: "directory has group read permissions",
permissions: 0040,
wantErr: true,
},
{
name: "directory has user read permissions",
permissions: 0400,
wantErr: true,
},
}
for _, test := range tests {
testCase := test
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "https://kkn.fi", nil)
path := tmpdir(t)
if err := os.Chmod(path, testCase.permissions); err != nil {
t.Fatalf("error while setting tmp dir permissions: %v", err)
}
srv, err := vanity.NewHandlerWithOptions(
vanity.StaticDir(path, "/files/"),
)
if (err != nil) != testCase.wantErr {
t.Errorf("expecting error: %v, got '%v'", testCase.wantErr, err)
return
}
if srv == nil {
return
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusNotFound {
t.Errorf("%v: expected response status 404, but got %v\n", testCase.name, res.StatusCode)
}
})
}
}
func TestStaticDirIntegration(t *testing.T) {
integrationTest(t)
tests := []struct {
name string
path string
wantErr bool
}{
{
name: "local directory not found",
path: "/not/found",
wantErr: true,
},
{
name: "local directory found",
path: "/tmp",
wantErr: false,
},
{
name: "directory is a file",
path: "/etc/passwd",
wantErr: true,
},
{
name: "directory doesn't have read permissions",
path: "",
wantErr: true,
},
}
for _, test := range tests {
testCase := test
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "https://kkn.fi", nil)
srv, err := vanity.NewHandlerWithOptions(
vanity.StaticDir(testCase.path, "/files/"),
)
if (err != nil) != testCase.wantErr {
t.Errorf("expecting error: %v, got '%v'", testCase.wantErr, err)
return
}
if srv == nil {
return
}
srv.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusNotFound {
t.Errorf("%v: expected response status 404, but got %v\n", testCase.name, res.StatusCode)
}
})
}
}