forked from esell/deb-simple
/
main_test.go
307 lines (289 loc) · 10.2 KB
/
main_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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var goodOutput = `Package: vim-tiny
Source: vim
Version: 2:7.4.052-1ubuntu3
Architecture: amd64
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Installed-Size: 931
Depends: vim-common (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libselinux1 (>= 1.32), libtinfo5
Suggests: indent
Provides: editor
Section: editors
Priority: important
Homepage: http://www.vim.org/
Description: Vi IMproved - enhanced vi editor - compact version
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a minimal version of vim compiled with no
GUI and a small subset of features in order to keep small the
package size. This package does not depend on the vim-runtime
package, but installing it you will get its additional benefits
(online documentation, plugins, ...).
Original-Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
`
var goodPkgGzOutput = `Package: vim-tiny
Source: vim
Version: 2:7.4.052-1ubuntu3
Architecture: amd64
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Installed-Size: 931
Depends: vim-common (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libselinux1 (>= 1.32), libtinfo5
Suggests: indent
Provides: editor
Section: editors
Priority: important
Homepage: http://www.vim.org/
Description: Vi IMproved - enhanced vi editor - compact version
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a minimal version of vim compiled with no
GUI and a small subset of features in order to keep small the
package size. This package does not depend on the vim-runtime
package, but installing it you will get its additional benefits
(online documentation, plugins, ...).
Original-Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
Filename: dists/stable/main/binary-cats/test.deb
Size: 391240
MD5sum: 0ec79417129746ff789fcff0976730c5
SHA1: b2ac976af80f0f50a8336402d5a29c67a2880b9b
SHA256: 9938ec82a8c882ebc2d59b64b0bf2ac01e9cbc5a235be4aa268d4f8484e75eab
`
func TestCreateDirs(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %s", err)
}
config := Conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
t.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
t.Log("creating temp dirs in ", config.RootRepoPath)
dirSuccess := createDirs(config)
if err := dirSuccess; err != nil {
t.Errorf("createDirs() failed ")
}
for _, archDir := range config.SupportArch {
if _, err := os.Stat(config.RootRepoPath + "/dists/stable/main/binary-" + archDir); err != nil {
if os.IsNotExist(err) {
t.Errorf("Directory for %s does not exist", archDir)
}
}
}
// cleanup
if err := os.RemoveAll(config.RootRepoPath); err != nil {
t.Errorf("error cleaning up after createDirs(): %s", err)
}
}
func TestInspectPackage(t *testing.T) {
parsedControl, err := inspectPackage("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Error(err)
}
if parsedControl != goodOutput {
t.Errorf("control file does not match")
}
}
func TestInspectPackageControl(t *testing.T) {
sampleDeb, err := ioutil.ReadFile("samples/control.tar.gz")
if err != nil {
t.Errorf("error opening sample deb file: %s", err)
}
var controlBuf bytes.Buffer
cfReader := bytes.NewReader(sampleDeb)
io.Copy(&controlBuf, cfReader)
parsedControl, err := inspectPackageControl(controlBuf)
if err != nil {
t.Error(err)
}
if parsedControl != goodOutput {
t.Errorf("control file does not match")
}
}
func TestCreatePackagesGz(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %s", err)
}
config := Conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
t.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
// copy sample deb to repo location (assuming it exists)
origDeb, err := os.Open("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("error opening up sample deb: %s", err)
}
defer origDeb.Close()
for _, archDir := range config.SupportArch {
// do not use the built-in createDirs() in case it is broken
dirErr := os.MkdirAll(config.RootRepoPath+"/dists/stable/main/binary-"+archDir, 0755)
if dirErr != nil {
t.Errorf("error creating directory for %s: %s\n", archDir, dirErr)
}
copyDeb, err := os.Create(config.RootRepoPath + "/dists/stable/main/binary-" + archDir + "/test.deb")
if err != nil {
t.Errorf("error creating copy of deb: %s", err)
}
_, err = io.Copy(copyDeb, origDeb)
err = copyDeb.Close()
if err != nil {
t.Errorf("error saving copy of deb: %s", err)
}
}
if err := createPackagesGz(config, "cats"); err != nil {
t.Errorf("error creating packages gzip for cats")
}
pkgGzip, err := ioutil.ReadFile(config.RootRepoPath + "/dists/stable/main/binary-cats/Packages.gz")
if err != nil {
t.Errorf("error reading Packages.gz: %s", err)
}
pkgReader, err := gzip.NewReader(bytes.NewReader(pkgGzip))
if err != nil {
t.Errorf("error reading existing Packages.gz: %s", err)
}
buf := bytes.NewBuffer(nil)
io.Copy(buf, pkgReader)
if goodPkgGzOutput != string(buf.Bytes()) {
t.Errorf("Packages.gz does not match, returned value is: %s", string(buf.Bytes()))
}
// cleanup
if err := os.RemoveAll(config.RootRepoPath); err != nil {
t.Errorf("error cleaning up after createPackagesGz(): %s", err)
}
}
func TestUploadHandler(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %s", err)
}
config := Conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
t.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
uploadHandle := uploadHandler(config)
// GET
req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
uploadHandle.ServeHTTP(w, req)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("uploadHandler GET returned %v, should be %v", w.Code, http.StatusMethodNotAllowed)
}
// POST
// create "all" arch as it's the default
dirErr := os.MkdirAll(config.RootRepoPath+"/dists/stable/main/binary-all", 0755)
if dirErr != nil {
t.Error("error creating directory for POST testing")
}
sampleDeb, err := os.Open("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("error opening sample deb file: %s", err)
}
defer sampleDeb.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("error FormFile: %s", err)
}
_, err = io.Copy(part, sampleDeb)
if err != nil {
t.Errorf("error copying sampleDeb to FormFile: %s", err)
}
err = writer.Close()
if err != nil {
t.Errorf("error closing form writer: %s", err)
}
req, _ = http.NewRequest("POST", "", body)
req.Header.Add("Content-Type", writer.FormDataContentType())
w = httptest.NewRecorder()
uploadHandle.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("uploadHandler POST returned %v, should be %v", w.Code, http.StatusOK)
}
// verify uploaded file matches sample
uploadFile, _ := ioutil.ReadFile(config.RootRepoPath + "/dists/stable/main/binary-all/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
uploadmd5hash := md5.New()
uploadmd5hash.Write(uploadFile)
uploadFilemd5 := hex.EncodeToString(uploadmd5hash.Sum(nil))
sampleFile, _ := ioutil.ReadFile("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
samplemd5hash := md5.New()
samplemd5hash.Write(sampleFile)
sampleFilemd5 := hex.EncodeToString(samplemd5hash.Sum(nil))
if uploadFilemd5 != sampleFilemd5 {
t.Errorf("uploaded file MD5 is %s, should be %s", uploadFilemd5, sampleFilemd5)
}
// cleanup
if err := os.RemoveAll(config.RootRepoPath); err != nil {
t.Errorf("error cleaning up after uploadHandler(): %s", err)
}
}
func BenchmarkUploadHandler(b *testing.B) {
pwd, err := os.Getwd()
if err != nil {
b.Errorf("Unable to get current working directory: %s", err)
}
config := &Conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
b.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
uploadHandle := uploadHandler(*config)
dirErr := os.MkdirAll(config.RootRepoPath+"/dists/stable/main/binary-all", 0755)
if dirErr != nil {
b.Errorf("error creating directory for POST testing")
}
sampleDeb, err := os.Open("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
b.Errorf("error opening sample deb file: %s", err)
}
defer sampleDeb.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// temporary (i hope) hack to solve "http: MultipartReader called twice" error
b.StopTimer()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
b.Errorf("error FormFile: %s", err)
}
if _, err := io.Copy(part, sampleDeb); err != nil {
b.Errorf("error copying sampleDeb to FormFile: %s", err)
}
if err := writer.Close(); err != nil {
b.Errorf("error closing form writer: %s", err)
}
req, _ := http.NewRequest("POST", "/upload", body)
req.Header.Add("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
b.StartTimer()
uploadHandle.ServeHTTP(w, req)
}
b.StopTimer()
// cleanup
_ = os.RemoveAll(config.RootRepoPath)
}