BlueGit

timfa bluegit

Files

bluegit / internal / server / archive_test.go

archive_test.go

fix: haal het AUR-archief van BlueGit
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed c66968f4 History
archive_test.go 5.9 KiB Download Raw
1package server
2
3import (
4 "archive/tar"
5 "archive/zip"
6 "bytes"
7 "compress/gzip"
8 "crypto/sha256"
9 "encoding/hex"
10 "io"
11 "net/http"
12 "net/http/cookiejar"
13 "net/http/httptest"
14 "net/url"
15 "os/exec"
16 "path/filepath"
17 "strings"
18 "testing"
19
20 "gitlab.com/Timfa/bluegit/internal/auth"
21 "gitlab.com/Timfa/bluegit/internal/config"
22 "gitlab.com/Timfa/bluegit/internal/store"
23)
24
25func TestCommitArchivePublicAndStable(t *testing.T) {
26 if _, err := exec.LookPath("git"); err != nil {
27 t.Skip("git ontbreekt")
28 }
29 storage := t.TempDir()
30 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
31 if err != nil {
32 t.Fatal(err)
33 }
34 t.Cleanup(func() { _ = st.Close() })
35 cfg := config.Config{
36 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
37 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
38 Git: config.Git{Binary: "git", DefaultBranch: "master"},
39 }
40 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
41 ts := httptest.NewServer(srv.Handler())
42 t.Cleanup(ts.Close)
43 jar, _ := cookiejar.New(nil)
44 tim := &http.Client{Jar: jar}
45 anon := &http.Client{}
46
47 page := read(t, mustGet(t, tim, ts.URL+"/login"))
48 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
49 "csrf": {csrfOf(t, page)}, "name": {"archief"}, "visibility": {"public"},
50 }))
51 page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{
52 "csrf": {csrfOf(t, page)}, "name": {"laptop"},
53 }))
54 token := between(page, `<p class="code">`, "</p>")
55 work := t.TempDir()
56 git(t, work, "init", "-b", "master")
57 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
58 git(t, work, "config", "user.name", "tim")
59 writeFile(t, filepath.Join(work, "hello.txt"), "alpha\n")
60 git(t, work, "add", "hello.txt")
61 git(t, work, "commit", "-m", "start")
62 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/archief.git"
63 git(t, work, "remote", "add", "origin", remote)
64 git(t, work, "push", "-u", "origin", "master")
65 sha := strings.TrimSpace(gitOutOK(t, work, "rev-parse", "HEAD"))
66
67 tarURL := ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".tar.gz"
68 first := getBody(t, anon, tarURL)
69 second := getBody(t, anon, tarURL)
70 if first.code != http.StatusOK || !bytes.Equal(first.body, second.body) {
71 t.Fatalf("tar.gz %d, gelijk %v, lengte %d", first.code, bytes.Equal(first.body, second.body), len(first.body))
72 }
73 if len(first.body) < 2 || first.body[0] != 0x1f || first.body[1] != 0x8b {
74 t.Fatal("geen gzip")
75 }
76 if !strings.Contains(first.header.Get("Content-Disposition"), "archief-"+sha+".tar.gz") {
77 t.Fatalf("bestandsnaam: %s", first.header.Get("Content-Disposition"))
78 }
79 sum := sha256.Sum256(first.body)
80 if hex.EncodeToString(sum[:]) != hex.EncodeToString(sha256Sum(second.body)) {
81 t.Fatal("sha256 verschilt tussen twee downloads")
82 }
83 names := tarNames(t, first.body)
84 want := "archief-" + sha + "/hello.txt"
85 if !containsString(names, want) {
86 t.Fatalf("tar mist %s in %v", want, names)
87 }
88
89 zipURL := ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".zip"
90 zipRes := getBody(t, anon, zipURL)
91 if zipRes.code != http.StatusOK || len(zipRes.body) < 2 || zipRes.body[0] != 'P' || zipRes.body[1] != 'K' {
92 t.Fatalf("zip %d", zipRes.code)
93 }
94 zr, err := zip.NewReader(bytes.NewReader(zipRes.body), int64(len(zipRes.body)))
95 if err != nil {
96 t.Fatal(err)
97 }
98 found := false
99 for _, f := range zr.File {
100 if f.Name == want {
101 found = true
102 }
103 }
104 if !found {
105 t.Fatalf("zip mist %s", want)
106 }
107 again := getBody(t, anon, zipURL)
108 if !bytes.Equal(zipRes.body, again.body) {
109 t.Fatal("zip verschilt tussen twee downloads")
110 }
111
112 for _, raw := range []string{
113 ts.URL + "/tim/archief/-/archive/" + sha[:12] + "/archief.tar.gz",
114 ts.URL + "/tim/archief/-/archive/master/archief-master.tar.gz",
115 ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".tar.bz2",
116 ts.URL + "/tim/archief/-/archive/" + strings.Repeat("ab", 20) + "/archief.tar.gz",
117 } {
118 res := getBody(t, anon, raw)
119 if res.code != http.StatusNotFound {
120 t.Fatalf("%s: %d", raw, res.code)
121 }
122 }
123
124 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
125 "csrf": {csrfOf(t, page)}, "name": {"geheim"}, "visibility": {"private"},
126 }))
127 secret := t.TempDir()
128 git(t, secret, "init", "-b", "master")
129 git(t, secret, "config", "user.email", "tim@users.git.timfalken.com")
130 git(t, secret, "config", "user.name", "tim")
131 writeFile(t, filepath.Join(secret, "hello.txt"), "geheim\n")
132 git(t, secret, "add", "hello.txt")
133 git(t, secret, "commit", "-m", "geheim")
134 privRemote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/geheim.git"
135 git(t, secret, "remote", "add", "origin", privRemote)
136 git(t, secret, "push", "-u", "origin", "master")
137 privSHA := strings.TrimSpace(gitOutOK(t, secret, "rev-parse", "HEAD"))
138 hidden := getBody(t, anon, ts.URL+"/tim/geheim/-/archive/"+privSHA+"/geheim-"+privSHA+".tar.gz")
139 if hidden.code != http.StatusNotFound {
140 t.Fatalf("privé-archief: %d", hidden.code)
141 }
142}
143
144type bodyResult struct {
145 code int
146 header http.Header
147 body []byte
148}
149
150func getBody(t *testing.T, c *http.Client, raw string) bodyResult {
151 t.Helper()
152 res := mustGet(t, c, raw)
153 defer res.Body.Close()
154 body, err := io.ReadAll(res.Body)
155 if err != nil {
156 t.Fatal(err)
157 }
158 return bodyResult{code: res.StatusCode, header: res.Header, body: body}
159}
160
161func tarNames(t *testing.T, gz []byte) []string {
162 t.Helper()
163 zr, err := gzip.NewReader(bytes.NewReader(gz))
164 if err != nil {
165 t.Fatal(err)
166 }
167 defer zr.Close()
168 tr := tar.NewReader(zr)
169 var names []string
170 for {
171 hdr, err := tr.Next()
172 if err == io.EOF {
173 break
174 }
175 if err != nil {
176 t.Fatal(err)
177 }
178 names = append(names, hdr.Name)
179 }
180 return names
181}
182
183func sha256Sum(b []byte) []byte {
184 sum := sha256.Sum256(b)
185 return sum[:]
186}
187
188func containsString(list []string, want string) bool {
189 for _, item := range list {
190 if item == want {
191 return true
192 }
193 }
194 return false
195}