|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/src-d/go-git-fixtures" |
| 15 | + "gopkg.in/src-d/go-git.v4/plumbing/transport" |
| 16 | + "gopkg.in/src-d/go-git.v4/plumbing/transport/test" |
| 17 | + |
| 18 | + . "gopkg.in/check.v1" |
| 19 | +) |
| 20 | + |
| 21 | +type ReceivePackSuite struct { |
| 22 | + test.ReceivePackSuite |
| 23 | + fixtures.Suite |
| 24 | + |
| 25 | + base string |
| 26 | + daemon *exec.Cmd |
| 27 | +} |
| 28 | + |
| 29 | +var _ = Suite(&ReceivePackSuite{}) |
| 30 | + |
| 31 | +func (s *ReceivePackSuite) SetUpTest(c *C) { |
| 32 | + s.ReceivePackSuite.Client = DefaultClient |
| 33 | + |
| 34 | + port, err := freePort() |
| 35 | + c.Assert(err, IsNil) |
| 36 | + |
| 37 | + base, err := ioutil.TempDir(os.TempDir(), "go-git-daemon-test") |
| 38 | + c.Assert(err, IsNil) |
| 39 | + s.base = base |
| 40 | + |
| 41 | + host := fmt.Sprintf("localhost_%d", port) |
| 42 | + interpolatedBase := filepath.Join(base, host) |
| 43 | + err = os.MkdirAll(interpolatedBase, 0755) |
| 44 | + c.Assert(err, IsNil) |
| 45 | + |
| 46 | + dotgit := fixtures.Basic().One().DotGit().Base() |
| 47 | + prepareRepo(c, dotgit) |
| 48 | + err = os.Rename(dotgit, filepath.Join(interpolatedBase, "basic.git")) |
| 49 | + c.Assert(err, IsNil) |
| 50 | + |
| 51 | + ep, err := transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/basic.git", port)) |
| 52 | + c.Assert(err, IsNil) |
| 53 | + s.ReceivePackSuite.Endpoint = ep |
| 54 | + |
| 55 | + dotgit = fixtures.ByTag("empty").One().DotGit().Base() |
| 56 | + prepareRepo(c, dotgit) |
| 57 | + err = os.Rename(dotgit, filepath.Join(interpolatedBase, "empty.git")) |
| 58 | + c.Assert(err, IsNil) |
| 59 | + |
| 60 | + ep, err = transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/empty.git", port)) |
| 61 | + c.Assert(err, IsNil) |
| 62 | + s.ReceivePackSuite.EmptyEndpoint = ep |
| 63 | + |
| 64 | + ep, err = transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/non-existent.git", port)) |
| 65 | + c.Assert(err, IsNil) |
| 66 | + s.ReceivePackSuite.NonExistentEndpoint = ep |
| 67 | + |
| 68 | + s.daemon = exec.Command( |
| 69 | + "git", |
| 70 | + "daemon", |
| 71 | + fmt.Sprintf("--base-path=%s", base), |
| 72 | + "--export-all", |
| 73 | + "--enable=receive-pack", |
| 74 | + "--reuseaddr", |
| 75 | + fmt.Sprintf("--port=%d", port), |
| 76 | + // Use interpolated paths to validate that clients are specifying |
| 77 | + // host and port properly. |
| 78 | + // Note that some git versions (e.g. v2.11.0) had a bug that prevented |
| 79 | + // the use of repository paths containing colons (:), so we use |
| 80 | + // underscore (_) instead of colon in the interpolation. |
| 81 | + // See https://github.com/git/git/commit/fe050334074c5132d01e1df2c1b9a82c9b8d394c |
| 82 | + fmt.Sprintf("--interpolated-path=%s/%%H_%%P%%D", base), |
| 83 | + // Unless max-connections is limited to 1, a git-receive-pack |
| 84 | + // might not be seen by a subsequent operation. |
| 85 | + "--max-connections=1", |
| 86 | + // Whitelist required for interpolated paths. |
| 87 | + fmt.Sprintf("%s/%s", interpolatedBase, "basic.git"), |
| 88 | + fmt.Sprintf("%s/%s", interpolatedBase, "empty.git"), |
| 89 | + ) |
| 90 | + |
| 91 | + // Environment must be inherited in order to acknowledge GIT_EXEC_PATH if set. |
| 92 | + s.daemon.Env = os.Environ() |
| 93 | + |
| 94 | + err = s.daemon.Start() |
| 95 | + c.Assert(err, IsNil) |
| 96 | + |
| 97 | + // Connections might be refused if we start sending request too early. |
| 98 | + time.Sleep(time.Millisecond * 500) |
| 99 | +} |
| 100 | + |
| 101 | +func (s *ReceivePackSuite) TearDownTest(c *C) { |
| 102 | + err := s.daemon.Process.Signal(os.Interrupt) |
| 103 | + c.Assert(err, IsNil) |
| 104 | + _ = s.daemon.Wait() |
| 105 | + err = os.RemoveAll(s.base) |
| 106 | + c.Assert(err, IsNil) |
| 107 | +} |
| 108 | + |
| 109 | +func freePort() (int, error) { |
| 110 | + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") |
| 111 | + if err != nil { |
| 112 | + return 0, err |
| 113 | + } |
| 114 | + |
| 115 | + l, err := net.ListenTCP("tcp", addr) |
| 116 | + if err != nil { |
| 117 | + return 0, err |
| 118 | + } |
| 119 | + |
| 120 | + return l.Addr().(*net.TCPAddr).Port, l.Close() |
| 121 | +} |
| 122 | + |
| 123 | +const bareConfig = `[core] |
| 124 | +repositoryformatversion = 0 |
| 125 | +filemode = true |
| 126 | +bare = true` |
| 127 | + |
| 128 | +func prepareRepo(c *C, path string) { |
| 129 | + // git-receive-pack refuses to update refs/heads/master on non-bare repo |
| 130 | + // so we ensure bare repo config. |
| 131 | + config := filepath.Join(path, "config") |
| 132 | + if _, err := os.Stat(config); err == nil { |
| 133 | + f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0) |
| 134 | + c.Assert(err, IsNil) |
| 135 | + content := strings.NewReader(bareConfig) |
| 136 | + _, err = io.Copy(f, content) |
| 137 | + c.Assert(err, IsNil) |
| 138 | + c.Assert(f.Close(), IsNil) |
| 139 | + } |
| 140 | +} |
0 commit comments