git_test.go (1645B)
1 package ssh 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 ) 8 9 func TestPublicRepoDir(t *testing.T) { 10 dir := t.TempDir() 11 t.Setenv("GIT_REPOS_DIR", dir) 12 for _, repo := range []string{"janitor", "mos"} { 13 if err := os.MkdirAll(filepath.Join(dir, repo+".git"), 0o755); err != nil { 14 t.Fatal(err) 15 } 16 } 17 if err := os.WriteFile(filepath.Join(dir, "janitor.git", "git-daemon-export-ok"), nil, 0o644); err != nil { 18 t.Fatal(err) 19 } 20 21 want := filepath.Join(dir, "janitor.git") 22 for _, in := range []string{"janitor.git", "janitor", "/janitor.git", "~/janitor.git"} { 23 if got, ok := publicRepoDir(in); !ok || got != want { 24 t.Errorf("publicRepoDir(%q) = %q, %v; want %q", in, got, ok, want) 25 } 26 } 27 for _, in := range []string{"mos.git", "missing.git", "../janitor.git", "a/janitor.git", "", ".git"} { 28 if got, ok := publicRepoDir(in); ok { 29 t.Errorf("publicRepoDir(%q) = %q, want rejected", in, got) 30 } 31 } 32 } 33 34 func TestOwnerRepoDirIncludesPrivate(t *testing.T) { 35 dir := t.TempDir() 36 t.Setenv("GIT_REPOS_DIR", dir) 37 if err := os.MkdirAll(filepath.Join(dir, "mos.git"), 0o755); err != nil { 38 t.Fatal(err) 39 } 40 if err := os.WriteFile(filepath.Join(dir, "mos.git", "HEAD"), []byte("ref: refs/heads/master\n"), 0o644); err != nil { 41 t.Fatal(err) 42 } 43 if got, ok := ownerRepoDir("mos.git"); !ok || got != filepath.Join(dir, "mos.git") { 44 t.Errorf("ownerRepoDir(mos.git) = %q, %v", got, ok) 45 } 46 if _, ok := publicRepoDir("mos.git"); ok { 47 t.Error("private repo served to guests") 48 } 49 for _, in := range []string{"missing.git", "../mos.git", "a/mos.git"} { 50 if _, ok := ownerRepoDir(in); ok { 51 t.Errorf("ownerRepoDir(%q) accepted", in) 52 } 53 } 54 }