Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 1eb3872

Browse files
committed
clients/ssh: test fix
1 parent 9013848 commit 1eb3872

File tree

8 files changed

+91
-162
lines changed

8 files changed

+91
-162
lines changed

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
language: go
2+
go:
3+
- 1.6
4+
- 1.7
5+
- tip
6+
matrix:
7+
allow_failures:
8+
- go: tip
9+
10+
111
language: go
212

313
go:
414
- 1.6
15+
- 1.7
516
- tip
617

718
matrix:
819
allow_failures:
920
- go: tip
1021

22+
23+
before_install:
24+
- openssl aes-256-cbc -K $encrypted_1477e58fe67a_key -iv $encrypted_1477e58fe67a_iv -in .travis/deploy.pem.enc -out .travis/deploy.pem -d
25+
- eval "$(ssh-agent -s)"
26+
- chmod 600 .travis/deploy.pem
27+
- ssh-add .travis/deploy.pem
28+
1129
install:
1230
- rm -rf $GOPATH/src/gopkg.in/src-d
1331
- mkdir -p $GOPATH/src/gopkg.in/src-d

.travis/deploy.pem.enc

1.64 KB
Binary file not shown.

clients/ssh/auth_method.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package ssh
22

33
import (
44
"fmt"
5+
"net"
6+
"os"
57

68
"golang.org/x/crypto/ssh"
9+
"golang.org/x/crypto/ssh/agent"
710
"gopkg.in/src-d/go-git.v4/clients/common"
811
)
912

@@ -134,3 +137,17 @@ func (a *PublicKeysCallback) clientConfig() *ssh.ClientConfig {
134137
Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)},
135138
}
136139
}
140+
141+
// Opens a pipe with the ssh agent and uses the pipe
142+
// as the implementer of the public key callback function.
143+
func NewSSHAgentAuth() (*PublicKeysCallback, error) {
144+
pipe, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
145+
if err != nil {
146+
return nil, err
147+
}
148+
149+
return &PublicKeysCallback{
150+
User: "git",
151+
Callback: agent.NewClient(pipe).Signers,
152+
}, nil
153+
}

clients/ssh/git_upload_pack.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ type GitUploadPackService struct {
4242
}
4343

4444
// NewGitUploadPackService initialises a GitUploadPackService.
45-
// TODO: remove this, as the struct is zero-value safe.
4645
func NewGitUploadPackService(endpoint common.Endpoint) common.GitUploadPackService {
4746
return &GitUploadPackService{endpoint: endpoint}
4847
}
4948

5049
// Connect cannot be used with SSH clients and always return
5150
// ErrAuthRequired. Use ConnectWithAuth instead.
52-
func (s *GitUploadPackService) Connect() (err error) {
53-
return ErrAuthRequired
51+
func (s *GitUploadPackService) Connect() error {
52+
auth, err := NewSSHAgentAuth()
53+
if err != nil {
54+
return err
55+
}
56+
57+
return s.ConnectWithAuth(auth)
5458
}
5559

5660
// ConnectWithAuth connects to ep using SSH. Authentication is handled

clients/ssh/git_upload_pack_test.go

Lines changed: 48 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,227 +1,128 @@
1-
// +build ssh
2-
31
package ssh
42

53
import (
6-
"fmt"
74
"io/ioutil"
8-
"net"
95
"os"
106

11-
"golang.org/x/crypto/ssh/agent"
12-
137
. "gopkg.in/check.v1"
148
"gopkg.in/src-d/go-git.v4/clients/common"
159
"gopkg.in/src-d/go-git.v4/core"
1610
)
1711

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
3914
}
4015

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{})
5617

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)
6122

62-
func (s *SuiteRemote) SetUpSuite(c *C) {
6323
if os.Getenv("SSH_AUTH_SOCK") == "" {
6424
c.Skip("SSH_AUTH_SOCK is not set")
6525
}
6626
}
6727

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-
9528
// A mock implementation of client.common.AuthMethod
9629
// to test non ssh auth method detection.
9730
type mockAuth struct{}
9831

9932
func (*mockAuth) Name() string { return "" }
10033
func (*mockAuth) String() string { return "" }
10134

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)
10638
}
10739

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+
}()
11246

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)
11848
}
11949

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)
12753
c.Assert(r.Disconnect(), IsNil)
128-
c.Assert(r.connected, Equals, false)
12954
}
13055

131-
func (s *SuiteRemote) TestDisconnectedWhenNonConnected(c *C) {
132-
r := NewGitUploadPackService()
56+
func (s *RemoteSuite) TestDisconnectedWhenNonConnected(c *C) {
57+
r := NewGitUploadPackService(s.Endpoint)
13358
c.Assert(r.Disconnect(), Equals, ErrNotConnected)
13459
}
13560

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)
14364
c.Assert(r.Disconnect(), IsNil)
14465
c.Assert(r.Disconnect(), Equals, ErrNotConnected)
145-
c.Assert(r.connected, Equals, false)
14666
}
14767

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)
15571
c.Assert(r.Disconnect(), IsNil)
15672

157-
c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil)
158-
c.Assert(r.connected, Equals, true)
73+
c.Assert(r.Connect(), IsNil)
15974
c.Assert(r.Disconnect(), IsNil)
160-
c.Assert(r.connected, Equals, false)
16175

162-
c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil)
163-
c.Assert(r.connected, Equals, true)
76+
c.Assert(r.Connect(), IsNil)
16477
c.Assert(r.Disconnect(), IsNil)
165-
c.Assert(r.connected, Equals, false)
16678
}
16779

168-
func (s *SuiteRemote) TestInfoNotConnected(c *C) {
169-
r := NewGitUploadPackService()
80+
func (s *RemoteSuite) TestInfoNotConnected(c *C) {
81+
r := NewGitUploadPackService(s.Endpoint)
17082
_, err := r.Info()
17183
c.Assert(err, Equals, ErrNotConnected)
17284
}
17385

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)
18189
defer func() { c.Assert(r.Disconnect(), IsNil) }()
18290

18391
info, err := r.Info()
18492
c.Assert(err, IsNil)
18593
c.Assert(info.Capabilities.SymbolicReference("HEAD"), Equals, "refs/heads/master")
18694
}
18795

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)
19599
defer func() { c.Assert(r.Disconnect(), IsNil) }()
196100

197101
info, err := r.Info()
198102
c.Assert(err, IsNil)
199103
c.Assert(info.Capabilities.Get("agent").Values, HasLen, 1)
200104
}
201105

202-
func (s *SuiteRemote) TestFetchNotConnected(c *C) {
203-
r := NewGitUploadPackService()
106+
func (s *RemoteSuite) TestFetchNotConnected(c *C) {
107+
r := NewGitUploadPackService(s.Endpoint)
204108
pr := &common.GitUploadPackRequest{}
205109
pr.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
206110
_, err := r.Fetch(pr)
207111
c.Assert(err, Equals, ErrNotConnected)
208112
}
209113

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)
217117
defer func() { c.Assert(r.Disconnect(), IsNil) }()
218118

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)
222123
c.Assert(err, IsNil)
223124

224125
b, err := ioutil.ReadAll(reader)
225126
c.Assert(err, IsNil)
226-
c.Assert(b, HasLen, 85374)
127+
c.Assert(len(b), Equals, 85585)
227128
}

config/config.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package config
33
import (
44
"errors"
55
"fmt"
6-
7-
"gopkg.in/src-d/go-git.v3/clients/common"
86
)
97

108
const (
@@ -40,10 +38,6 @@ func (c *RemoteConfig) Validate() error {
4038
return ErrRemoteConfigEmptyURL
4139
}
4240

43-
if _, err := common.NewEndpoint(c.URL); err != nil {
44-
return err
45-
}
46-
4741
if len(c.Fetch) == 0 {
4842
c.Fetch = []RefSpec{RefSpec(fmt.Sprintf(DefaultRefSpec, c.Name))}
4943
}

config/config_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ func (s *ConfigSuite) TestRemoteConfigValidateMissingURL(c *C) {
1111
c.Assert(config.Validate(), Equals, ErrRemoteConfigEmptyURL)
1212
}
1313

14-
func (s *ConfigSuite) TestRemoteConfigValidateInvalidURL(c *C) {
15-
config := &RemoteConfig{Name: "foo", URL: "foo"}
16-
c.Assert(config.Validate(), NotNil)
17-
}
18-
1914
func (s *ConfigSuite) TestRemoteConfigValidateMissingName(c *C) {
2015
config := &RemoteConfig{}
2116
c.Assert(config.Validate(), Equals, ErrRemoteConfigEmptyName)

0 commit comments

Comments
 (0)