internal/mirror/mirror_test.go
197 lines · 6484 bytes
1package mirror
2
3import (
4 "context"
5 "net"
6 "net/http/cgi"
7 "net/http/httptest"
8 "net/url"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "slices"
13 "strings"
14 "testing"
15
16 "gitbay.org/gitbay/internal/config"
17 "gitbay.org/gitbay/internal/control"
18 "gitbay.org/gitbay/internal/store"
19)
20
21func git(t *testing.T, dir string, args ...string) string {
22 t.Helper()
23 cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
24 cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "HOME="+t.TempDir(),
25 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.test",
26 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.test")
27 out, err := cmd.CombinedOutput()
28 if err != nil {
29 t.Fatalf("git %v: %v\n%s", args, err, out)
30 }
31 return strings.TrimSpace(string(out))
32}
33
34// upstream serves a bare repository with one commit on main over smart
35// HTTP and returns its URL and that commit.
36func upstream(t *testing.T) (string, string) {
37 t.Helper()
38 parent := t.TempDir()
39 bare := filepath.Join(parent, "remote.git")
40 work := filepath.Join(parent, "work")
41 git(t, parent, "init", "-q", "--bare", "--initial-branch=main", bare)
42 git(t, parent, "init", "-q", "--initial-branch=main", work)
43 git(t, work, "commit", "-q", "--allow-empty", "-m", "one")
44 git(t, work, "push", "-q", bare, "main")
45 sha := git(t, work, "rev-parse", "HEAD")
46 execPath := git(t, parent, "--exec-path")
47 srv := httptest.NewServer(&cgi.Handler{
48 Path: filepath.Join(execPath, "git-http-backend"),
49 Env: []string{"GIT_PROJECT_ROOT=" + parent, "GIT_HTTP_EXPORT_ALL=1"},
50 })
51 t.Cleanup(srv.Close)
52 return srv.URL + "/remote.git", sha
53}
54
55// local returns a store with alice/app, its bare repository under root,
56// and the pull mirror row for url.
57func local(t *testing.T, root, mirrorURL string) (*store.Store, store.Mirror, string) {
58 t.Helper()
59 st, err := store.Open(filepath.Join(t.TempDir(), "gitbay.db"))
60 if err != nil {
61 t.Fatal(err)
62 }
63 t.Cleanup(func() { st.Close() })
64 if err := st.MigrateUp(); err != nil {
65 t.Fatal(err)
66 }
67 uid, err := st.CreateUser("alice", false)
68 if err != nil {
69 t.Fatal(err)
70 }
71 repoID, err := st.CreateRepo("user", uid, "app", "public")
72 if err != nil {
73 t.Fatal(err)
74 }
75 dir := control.RepoDir(root, "alice", "app")
76 os.MkdirAll(filepath.Dir(dir), 0o755)
77 git(t, root, "init", "-q", "--bare", dir)
78 if _, err := st.AddMirror(repoID, "pull", mirrorURL, "", ""); err != nil {
79 t.Fatal(err)
80 }
81 due, err := st.DueMirrors(900)
82 if err != nil || len(due) != 1 {
83 t.Fatalf("due mirrors: %v %v", due, err)
84 }
85 return st, due[0], dir
86}
87
88// mirror.test does not resolve; the fetch works only because git was
89// pinned to the address the worker looked up and checked.
90func TestSyncConnectsToTheCheckedAddress(t *testing.T) {
91 remote, sha := upstream(t)
92 u, _ := url.Parse(remote)
93 root := t.TempDir()
94 st, m, dir := local(t, root, "http://mirror.test:"+u.Port()+"/remote.git")
95 var cfg config.Config
96 cfg.Server.Root = root
97 cfg.Webhooks.AllowLocal = true
98 var asked []string
99 w := &Worker{St: st, Cfg: cfg, Lookup: func(ctx context.Context, host string) ([]net.IP, error) {
100 asked = append(asked, host)
101 return []net.IP{net.ParseIP("127.0.0.1")}, nil
102 }}
103 if err := w.sync(m); err != nil {
104 t.Fatal(err)
105 }
106 if got := git(t, dir, "rev-parse", "refs/heads/main"); got != sha {
107 t.Fatalf("main = %s, want %s", got, sha)
108 }
109 if !slices.Equal(asked, []string{"mirror.test"}) {
110 t.Fatalf("looked up %v", asked)
111 }
112}
113
114// The URL passed the check when it was saved; the answer at sync time
115// is what counts.
116func TestSyncRefusesAPrivateAddressAtSyncTime(t *testing.T) {
117 root := t.TempDir()
118 st, m, _ := local(t, root, "https://mirror.test/x.git")
119 var cfg config.Config
120 cfg.Server.Root = root
121 w := &Worker{St: st, Cfg: cfg, Lookup: func(context.Context, string) ([]net.IP, error) {
122 return []net.IP{net.ParseIP("10.0.0.7")}, nil
123 }}
124 err := w.sync(m)
125 if err == nil || !strings.Contains(err.Error(), "10.0.0.7") {
126 t.Fatalf("sync = %v, want a refusal naming 10.0.0.7", err)
127 }
128}
129
130// A refusal is a sync failure like any other: the sweep records it on
131// the mirror, where repo mirror list shows it.
132func TestSweepRecordsTheRefusal(t *testing.T) {
133 root := t.TempDir()
134 st, m, _ := local(t, root, "https://mirror.test/x.git")
135 var cfg config.Config
136 cfg.Server.Root = root
137 cfg.Mirrors.PullIntervalMinutes = 15
138 w := &Worker{St: st, Cfg: cfg, Lookup: func(context.Context, string) ([]net.IP, error) {
139 return []net.IP{net.ParseIP("100.64.0.9")}, nil
140 }}
141 w.sweep()
142 ms, err := st.ListMirrors(m.RepoID)
143 if err != nil || len(ms) != 1 {
144 t.Fatalf("mirrors: %v %v", ms, err)
145 }
146 if !strings.Contains(ms[0].LastError, "100.64.0.9") {
147 t.Fatalf("last error = %q", ms[0].LastError)
148 }
149}
150
151func TestSyncRefusesAnEmptyAnswer(t *testing.T) {
152 root := t.TempDir()
153 st, m, _ := local(t, root, "https://mirror.test/x.git")
154 var cfg config.Config
155 cfg.Server.Root = root
156 cfg.Webhooks.AllowLocal = true
157 w := &Worker{St: st, Cfg: cfg, Lookup: func(context.Context, string) ([]net.IP, error) {
158 return nil, nil
159 }}
160 if err := w.sync(m); err == nil || !strings.Contains(err.Error(), "no address") {
161 t.Fatalf("sync = %v, want a refusal", err)
162 }
163}
164
165func TestSyncRefusesANonHTTPScheme(t *testing.T) {
166 root := t.TempDir()
167 st, m, _ := local(t, root, "ssh://mirror.test/x.git")
168 var cfg config.Config
169 cfg.Server.Root = root
170 cfg.Webhooks.AllowLocal = true
171 w := &Worker{St: st, Cfg: cfg, Lookup: func(context.Context, string) ([]net.IP, error) {
172 t.Fatal("looked up a host for an ssh URL")
173 return nil, nil
174 }}
175 if err := w.sync(m); err == nil || !strings.Contains(err.Error(), "not http or https") {
176 t.Fatalf("sync = %v, want a refusal", err)
177 }
178}
179
180func TestPinArgs(t *testing.T) {
181 u, _ := url.Parse("https://git.example/x.git")
182 got := pinArgs(u, []net.IP{net.ParseIP("203.0.113.5"), net.ParseIP("2001:db8::1")})
183 want := []string{"-c", "http.followRedirects=false",
184 "-c", "http.curloptResolve=git.example:443:203.0.113.5,[2001:db8::1]"}
185 if !slices.Equal(got, want) {
186 t.Fatalf("https: %q", got)
187 }
188 u, _ = url.Parse("http://git.example:8080/x.git")
189 if got := pinArgs(u, []net.IP{net.ParseIP("203.0.113.5")}); got[3] != "http.curloptResolve=git.example:8080:203.0.113.5" {
190 t.Fatalf("http with port: %q", got)
191 }
192 // An address literal is its own resolution; there is nothing to pin.
193 u, _ = url.Parse("https://203.0.113.5/x.git")
194 if got := pinArgs(u, []net.IP{net.ParseIP("203.0.113.5")}); !slices.Equal(got, []string{"-c", "http.followRedirects=false"}) {
195 t.Fatalf("literal: %q", got)
196 }
197}