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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
package zip
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
var chdirSet = false
func TestOpen(t *testing.T) {
zip, cleanup := openZipArchive(t)
defer cleanup()
tests := map[string]struct {
file string
expectedContent string
expectedErr error
}{
"file_exists": {
file: "index.html",
expectedContent: "zip.gitlab.io/project/index.html\n",
expectedErr: nil,
},
"file_exists_in_subdir": {
file: "subdir/hello.html",
expectedContent: "zip.gitlab.io/project/subdir/hello.html\n",
expectedErr: nil,
},
"file_exists_symlink": {
file: "symlink.html",
expectedContent: "subdir/linked.html",
expectedErr: nil,
},
"is_dir": {
file: "subdir",
expectedContent: "zip.gitlab.io/project/index.html\n",
expectedErr: nil,
},
"file_does_not_exist": {
file: "unknown.html",
expectedErr: os.ErrNotExist,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
f, err := zip.Open(context.Background(), tt.file)
if tt.expectedErr != nil {
require.EqualError(t, err, tt.expectedErr.Error())
return
}
require.NoError(t, err)
if name == "is_dir" {
// cannot ioutil.ReadAll dirs but zip.Open should not fail
return
}
data, err := ioutil.ReadAll(f)
require.NoError(t, err)
require.Equal(t, tt.expectedContent, string(data))
require.NoError(t, f.Close())
})
}
}
func TestLstat(t *testing.T) {
zip, cleanup := openZipArchive(t)
defer cleanup()
tests := map[string]struct {
file string
isDir bool
isSymlink bool
expectedErr error
}{
"file_exists": {
file: "index.html",
},
"file_exists_in_subdir": {
file: "subdir/hello.html",
},
"file_exists_symlink": {
file: "symlink.html",
isSymlink: true,
},
"is_dir": {
file: "subdir",
isDir: true,
},
"file_does_not_exist": {
file: "unknown.html",
expectedErr: os.ErrNotExist,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fi, err := zip.Lstat(context.Background(), tt.file)
if tt.expectedErr != nil {
require.EqualError(t, err, tt.expectedErr.Error())
return
}
require.NoError(t, err)
require.Contains(t, tt.file, fi.Name())
require.Equal(t, tt.isDir, fi.IsDir())
require.NotEmpty(t, fi.ModTime())
if tt.isDir {
require.Zero(t, fi.Size())
require.True(t, fi.IsDir())
return
}
require.NotZero(t, fi.Size())
if tt.isSymlink {
require.NotZero(t, fi.Mode()&os.ModeSymlink)
} else {
require.True(t, fi.Mode().IsRegular())
}
})
}
}
func TestReadLink(t *testing.T) {
zip, cleanup := openZipArchive(t)
defer cleanup()
tests := map[string]struct {
file string
expectedErr error
}{
"symlink_success": {
file: "symlink.html",
},
"file": {
file: "index.html",
expectedErr: errNotSymlink,
},
"dir": {
file: "subdir",
expectedErr: errNotSymlink,
},
"symlink_too_big": {
file: "bad_symlink.html",
expectedErr: errSymlinkSize,
},
"file_does_not_exist": {
file: "unknown.html",
expectedErr: os.ErrNotExist,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
link, err := zip.Readlink(context.Background(), tt.file)
if tt.expectedErr != nil {
require.EqualError(t, err, tt.expectedErr.Error())
return
}
require.NoError(t, err)
require.NotEmpty(t, link)
})
}
}
func openZipArchive(t *testing.T) (*zipArchive, func()) {
t.Helper()
testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip")
zip := newArchive(testServerURL + "/public.zip")
err := zip.openArchive(context.Background())
require.NoError(t, err)
// public/ public/index.html public/404.html public/symlink.html
// public/subdir/ public/subdir/hello.html public/subdir/linked.html
// public/bad_symlink.html public/subdir/2bp3Qzs...
require.NotZero(t, zip.files)
return zip, func() {
zip.close()
cleanup()
}
}
func newZipFileServerURL(t *testing.T, zipFilePath string) (string, func()) {
t.Helper()
chdir := testhelpers.ChdirInPath(t, "../../../shared/pages", &chdirSet)
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, zipFilePath)
}))
return testServer.URL, func() {
chdir()
testServer.Close()
}
}
|