|
1 |
| -// +build ssh |
2 |
| - |
3 | 1 | package ssh
|
4 | 2 |
|
5 | 3 | import (
|
6 |
| - "fmt" |
7 | 4 | "io/ioutil"
|
8 |
| - "net" |
9 | 5 | "os"
|
10 | 6 |
|
11 |
| - "golang.org/x/crypto/ssh/agent" |
12 |
| - |
13 | 7 | . "gopkg.in/check.v1"
|
14 | 8 | "gopkg.in/src-d/go-git.v4/clients/common"
|
15 | 9 | "gopkg.in/src-d/go-git.v4/core"
|
16 | 10 | )
|
17 | 11 |
|
18 |
| -type SuiteRemote struct{} |
19 |
| - |
20 |
| -var _ = Suite(&SuiteRemote{}) |
21 |
| - |
22 |
| -const ( |
23 |
| - fixRepo = "[email protected]:tyba/git-fixture.git" |
24 |
| - fixRepoBadVcs = "www.example.com" |
25 |
| - fixRepoNonGit = "https://code.google.com/p/go" |
26 |
| - fixGitRepoNonGithub = "https://bitbucket.org/user/repo.git" |
27 |
| -) |
28 |
| - |
29 |
| -func (s *SuiteRemote) TestConnect(c *C) { |
30 |
| - r := NewGitUploadPackService() |
31 |
| - c.Assert(r.Connect(fixRepo), Equals, ErrAuthRequired) |
32 |
| -} |
33 |
| - |
34 |
| -// We will use a running ssh agent for testing |
35 |
| -// ssh authentication. |
36 |
| -type sshAgentConn struct { |
37 |
| - pipe net.Conn |
38 |
| - auth *PublicKeysCallback |
| 12 | +type RemoteSuite struct { |
| 13 | + Endpoint common.Endpoint |
39 | 14 | }
|
40 | 15 |
|
41 |
| -// Opens a pipe with the ssh agent and uses the pipe |
42 |
| -// as the implementer of the public key callback function. |
43 |
| -func newSSHAgentConn() (*sshAgentConn, error) { |
44 |
| - pipe, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) |
45 |
| - if err != nil { |
46 |
| - return nil, err |
47 |
| - } |
48 |
| - return &sshAgentConn{ |
49 |
| - pipe: pipe, |
50 |
| - auth: &PublicKeysCallback{ |
51 |
| - User: "git", |
52 |
| - Callback: agent.NewClient(pipe).Signers, |
53 |
| - }, |
54 |
| - }, nil |
55 |
| -} |
| 16 | +var _ = Suite(&RemoteSuite{}) |
56 | 17 |
|
57 |
| -// Closes the pipe with the ssh agent |
58 |
| -func (c *sshAgentConn) close() error { |
59 |
| - return c.pipe.Close() |
60 |
| -} |
| 18 | +func (s *RemoteSuite) SetUpSuite(c *C) { |
| 19 | + var err error |
| 20 | + s.Endpoint, err = common.NewEndpoint("[email protected]:git-fixtures/basic.git") |
| 21 | + c.Assert(err, IsNil) |
61 | 22 |
|
62 |
| -func (s *SuiteRemote) SetUpSuite(c *C) { |
63 | 23 | if os.Getenv("SSH_AUTH_SOCK") == "" {
|
64 | 24 | c.Skip("SSH_AUTH_SOCK is not set")
|
65 | 25 | }
|
66 | 26 | }
|
67 | 27 |
|
68 |
| -func (s *SuiteRemote) TestConnectWithPublicKeysCallback(c *C) { |
69 |
| - agent, err := newSSHAgentConn() |
70 |
| - c.Assert(err, IsNil) |
71 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
72 |
| - |
73 |
| - r := NewGitUploadPackService() |
74 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
75 |
| - defer func() { c.Assert(r.Disconnect(), IsNil) }() |
76 |
| - c.Assert(r.connected, Equals, true) |
77 |
| - c.Assert(r.auth, Equals, agent.auth) |
78 |
| -} |
79 |
| - |
80 |
| -func (s *SuiteRemote) TestConnectBadVcs(c *C) { |
81 |
| - r := NewGitUploadPackService() |
82 |
| - c.Assert(r.ConnectWithAuth(fixRepoBadVcs, nil), ErrorMatches, fmt.Sprintf(".*%s.*", fixRepoBadVcs)) |
83 |
| -} |
84 |
| - |
85 |
| -func (s *SuiteRemote) TestConnectNonGit(c *C) { |
86 |
| - r := NewGitUploadPackService() |
87 |
| - c.Assert(r.ConnectWithAuth(fixRepoNonGit, nil), Equals, ErrUnsupportedVCS) |
88 |
| -} |
89 |
| - |
90 |
| -func (s *SuiteRemote) TestConnectNonGithub(c *C) { |
91 |
| - r := NewGitUploadPackService() |
92 |
| - c.Assert(r.ConnectWithAuth(fixGitRepoNonGithub, nil), Equals, ErrUnsupportedRepo) |
93 |
| -} |
94 |
| - |
95 | 28 | // A mock implementation of client.common.AuthMethod
|
96 | 29 | // to test non ssh auth method detection.
|
97 | 30 | type mockAuth struct{}
|
98 | 31 |
|
99 | 32 | func (*mockAuth) Name() string { return "" }
|
100 | 33 | func (*mockAuth) String() string { return "" }
|
101 | 34 |
|
102 |
| -func (s *SuiteRemote) TestConnectWithAuthWrongType(c *C) { |
103 |
| - r := NewGitUploadPackService() |
104 |
| - c.Assert(r.ConnectWithAuth(fixRepo, &mockAuth{}), Equals, ErrInvalidAuthMethod) |
105 |
| - c.Assert(r.connected, Equals, false) |
| 35 | +func (s *RemoteSuite) TestConnectWithAuthWrongType(c *C) { |
| 36 | + r := NewGitUploadPackService(s.Endpoint) |
| 37 | + c.Assert(r.ConnectWithAuth(&mockAuth{}), Equals, ErrInvalidAuthMethod) |
106 | 38 | }
|
107 | 39 |
|
108 |
| -func (s *SuiteRemote) TestAlreadyConnected(c *C) { |
109 |
| - agent, err := newSSHAgentConn() |
110 |
| - c.Assert(err, IsNil) |
111 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
| 40 | +func (s *RemoteSuite) TestAlreadyConnected(c *C) { |
| 41 | + r := NewGitUploadPackService(s.Endpoint) |
| 42 | + c.Assert(r.Connect(), IsNil) |
| 43 | + defer func() { |
| 44 | + c.Assert(r.Disconnect(), IsNil) |
| 45 | + }() |
112 | 46 |
|
113 |
| - r := NewGitUploadPackService() |
114 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
115 |
| - defer func() { c.Assert(r.Disconnect(), IsNil) }() |
116 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), Equals, ErrAlreadyConnected) |
117 |
| - c.Assert(r.connected, Equals, true) |
| 47 | + c.Assert(r.Connect(), Equals, ErrAlreadyConnected) |
118 | 48 | }
|
119 | 49 |
|
120 |
| -func (s *SuiteRemote) TestDisconnect(c *C) { |
121 |
| - agent, err := newSSHAgentConn() |
122 |
| - c.Assert(err, IsNil) |
123 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
124 |
| - |
125 |
| - r := NewGitUploadPackService() |
126 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 50 | +func (s *RemoteSuite) TestDisconnect(c *C) { |
| 51 | + r := NewGitUploadPackService(s.Endpoint) |
| 52 | + c.Assert(r.Connect(), IsNil) |
127 | 53 | c.Assert(r.Disconnect(), IsNil)
|
128 |
| - c.Assert(r.connected, Equals, false) |
129 | 54 | }
|
130 | 55 |
|
131 |
| -func (s *SuiteRemote) TestDisconnectedWhenNonConnected(c *C) { |
132 |
| - r := NewGitUploadPackService() |
| 56 | +func (s *RemoteSuite) TestDisconnectedWhenNonConnected(c *C) { |
| 57 | + r := NewGitUploadPackService(s.Endpoint) |
133 | 58 | c.Assert(r.Disconnect(), Equals, ErrNotConnected)
|
134 | 59 | }
|
135 | 60 |
|
136 |
| -func (s *SuiteRemote) TestAlreadyDisconnected(c *C) { |
137 |
| - agent, err := newSSHAgentConn() |
138 |
| - c.Assert(err, IsNil) |
139 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
140 |
| - |
141 |
| - r := NewGitUploadPackService() |
142 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 61 | +func (s *RemoteSuite) TestAlreadyDisconnected(c *C) { |
| 62 | + r := NewGitUploadPackService(s.Endpoint) |
| 63 | + c.Assert(r.Connect(), IsNil) |
143 | 64 | c.Assert(r.Disconnect(), IsNil)
|
144 | 65 | c.Assert(r.Disconnect(), Equals, ErrNotConnected)
|
145 |
| - c.Assert(r.connected, Equals, false) |
146 | 66 | }
|
147 | 67 |
|
148 |
| -func (s *SuiteRemote) TestServeralConnections(c *C) { |
149 |
| - agent, err := newSSHAgentConn() |
150 |
| - c.Assert(err, IsNil) |
151 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
152 |
| - |
153 |
| - r := NewGitUploadPackService() |
154 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 68 | +func (s *RemoteSuite) TestServeralConnections(c *C) { |
| 69 | + r := NewGitUploadPackService(s.Endpoint) |
| 70 | + c.Assert(r.Connect(), IsNil) |
155 | 71 | c.Assert(r.Disconnect(), IsNil)
|
156 | 72 |
|
157 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
158 |
| - c.Assert(r.connected, Equals, true) |
| 73 | + c.Assert(r.Connect(), IsNil) |
159 | 74 | c.Assert(r.Disconnect(), IsNil)
|
160 |
| - c.Assert(r.connected, Equals, false) |
161 | 75 |
|
162 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
163 |
| - c.Assert(r.connected, Equals, true) |
| 76 | + c.Assert(r.Connect(), IsNil) |
164 | 77 | c.Assert(r.Disconnect(), IsNil)
|
165 |
| - c.Assert(r.connected, Equals, false) |
166 | 78 | }
|
167 | 79 |
|
168 |
| -func (s *SuiteRemote) TestInfoNotConnected(c *C) { |
169 |
| - r := NewGitUploadPackService() |
| 80 | +func (s *RemoteSuite) TestInfoNotConnected(c *C) { |
| 81 | + r := NewGitUploadPackService(s.Endpoint) |
170 | 82 | _, err := r.Info()
|
171 | 83 | c.Assert(err, Equals, ErrNotConnected)
|
172 | 84 | }
|
173 | 85 |
|
174 |
| -func (s *SuiteRemote) TestDefaultBranch(c *C) { |
175 |
| - agent, err := newSSHAgentConn() |
176 |
| - c.Assert(err, IsNil) |
177 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
178 |
| - |
179 |
| - r := NewGitUploadPackService() |
180 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 86 | +func (s *RemoteSuite) TestDefaultBranch(c *C) { |
| 87 | + r := NewGitUploadPackService(s.Endpoint) |
| 88 | + c.Assert(r.Connect(), IsNil) |
181 | 89 | defer func() { c.Assert(r.Disconnect(), IsNil) }()
|
182 | 90 |
|
183 | 91 | info, err := r.Info()
|
184 | 92 | c.Assert(err, IsNil)
|
185 | 93 | c.Assert(info.Capabilities.SymbolicReference("HEAD"), Equals, "refs/heads/master")
|
186 | 94 | }
|
187 | 95 |
|
188 |
| -func (s *SuiteRemote) TestCapabilities(c *C) { |
189 |
| - agent, err := newSSHAgentConn() |
190 |
| - c.Assert(err, IsNil) |
191 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
192 |
| - |
193 |
| - r := NewGitUploadPackService() |
194 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 96 | +func (s *RemoteSuite) TestCapabilities(c *C) { |
| 97 | + r := NewGitUploadPackService(s.Endpoint) |
| 98 | + c.Assert(r.Connect(), IsNil) |
195 | 99 | defer func() { c.Assert(r.Disconnect(), IsNil) }()
|
196 | 100 |
|
197 | 101 | info, err := r.Info()
|
198 | 102 | c.Assert(err, IsNil)
|
199 | 103 | c.Assert(info.Capabilities.Get("agent").Values, HasLen, 1)
|
200 | 104 | }
|
201 | 105 |
|
202 |
| -func (s *SuiteRemote) TestFetchNotConnected(c *C) { |
203 |
| - r := NewGitUploadPackService() |
| 106 | +func (s *RemoteSuite) TestFetchNotConnected(c *C) { |
| 107 | + r := NewGitUploadPackService(s.Endpoint) |
204 | 108 | pr := &common.GitUploadPackRequest{}
|
205 | 109 | pr.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
|
206 | 110 | _, err := r.Fetch(pr)
|
207 | 111 | c.Assert(err, Equals, ErrNotConnected)
|
208 | 112 | }
|
209 | 113 |
|
210 |
| -func (s *SuiteRemote) TestFetch(c *C) { |
211 |
| - agent, err := newSSHAgentConn() |
212 |
| - c.Assert(err, IsNil) |
213 |
| - defer func() { c.Assert(agent.close(), IsNil) }() |
214 |
| - |
215 |
| - r := NewGitUploadPackService() |
216 |
| - c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil) |
| 114 | +func (s *RemoteSuite) TestFetch(c *C) { |
| 115 | + r := NewGitUploadPackService(s.Endpoint) |
| 116 | + c.Assert(r.Connect(), IsNil) |
217 | 117 | defer func() { c.Assert(r.Disconnect(), IsNil) }()
|
218 | 118 |
|
219 |
| - pr := &common.GitUploadPackRequest{} |
220 |
| - pr.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
221 |
| - reader, err := r.Fetch(pr) |
| 119 | + req := &common.GitUploadPackRequest{} |
| 120 | + req.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 121 | + req.Want(core.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 122 | + reader, err := r.Fetch(req) |
222 | 123 | c.Assert(err, IsNil)
|
223 | 124 |
|
224 | 125 | b, err := ioutil.ReadAll(reader)
|
225 | 126 | c.Assert(err, IsNil)
|
226 |
| - c.Assert(b, HasLen, 85374) |
| 127 | + c.Assert(len(b), Equals, 85585) |
227 | 128 | }
|
0 commit comments