1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
package http
import (
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/cgi"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
fixtures "github.com/go-git/go-git-fixtures/v4"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type ClientSuite struct {
Endpoint *transport.Endpoint
EmptyAuth transport.AuthMethod
}
var _ = Suite(&ClientSuite{})
func (s *ClientSuite) SetUpSuite(c *C) {
var err error
s.Endpoint, err = transport.NewEndpoint(
"https://github.com/git-fixtures/basic",
)
c.Assert(err, IsNil)
}
func (s *UploadPackSuite) TestNewClient(c *C) {
roundTripper := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
cl := &http.Client{Transport: roundTripper}
r, ok := NewClient(cl).(*client)
c.Assert(ok, Equals, true)
c.Assert(r.c, Equals, cl)
}
func (s *ClientSuite) TestNewBasicAuth(c *C) {
a := &BasicAuth{"foo", "qux"}
c.Assert(a.Name(), Equals, "http-basic-auth")
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
}
func (s *ClientSuite) TestNewTokenAuth(c *C) {
a := &TokenAuth{"OAUTH-TOKEN-TEXT"}
c.Assert(a.Name(), Equals, "http-token-auth")
c.Assert(a.String(), Equals, "http-token-auth - *******")
// Check header is set correctly
req, err := http.NewRequest("GET", "https://github.com/git-fixtures/basic", nil)
c.Assert(err, Equals, nil)
a.SetAuth(req)
c.Assert(req.Header.Get("Authorization"), Equals, "Bearer OAUTH-TOKEN-TEXT")
}
func (s *ClientSuite) TestNewErrOK(c *C) {
res := &http.Response{StatusCode: http.StatusOK}
err := NewErr(res)
c.Assert(err, IsNil)
}
func (s *ClientSuite) TestNewErrUnauthorized(c *C) {
s.testNewHTTPError(c, http.StatusUnauthorized, "authentication required")
}
func (s *ClientSuite) TestNewErrForbidden(c *C) {
s.testNewHTTPError(c, http.StatusForbidden, "authorization failed")
}
func (s *ClientSuite) TestNewErrNotFound(c *C) {
s.testNewHTTPError(c, http.StatusNotFound, "repository not found")
}
func (s *ClientSuite) TestNewHTTPError40x(c *C) {
s.testNewHTTPError(c, http.StatusPaymentRequired,
"unexpected client error.*")
}
func (s *ClientSuite) TestNewUnexpectedError(c *C) {
res := &http.Response{
StatusCode: 500,
Body: io.NopCloser(strings.NewReader("Unexpected error")),
}
err := NewErr(res)
c.Assert(err, NotNil)
c.Assert(err, FitsTypeOf, &plumbing.UnexpectedError{})
unexpectedError, _ := err.(*plumbing.UnexpectedError)
c.Assert(unexpectedError.Err, FitsTypeOf, &Err{})
httpError, _ := unexpectedError.Err.(*Err)
c.Assert(httpError.Reason, Equals, "Unexpected error")
}
func (s *ClientSuite) Test_newSession(c *C) {
cl := NewClientWithOptions(nil, &ClientOptions{
CacheMaxEntries: 2,
}).(*client)
insecureEP := s.Endpoint
insecureEP.InsecureSkipTLS = true
session, err := newSession(cl, insecureEP, nil)
c.Assert(err, IsNil)
sessionTransport := session.client.Transport.(*http.Transport)
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true)
t, ok := cl.fetchTransport(transportOptions{
insecureSkipTLS: true,
})
// transport should be cached.
c.Assert(ok, Equals, true)
// cached transport should be the one that's used.
c.Assert(sessionTransport, Equals, t)
caEndpoint := insecureEP
caEndpoint.CaBundle = []byte("this is the way")
session, err = newSession(cl, caEndpoint, nil)
c.Assert(err, IsNil)
sessionTransport = session.client.Transport.(*http.Transport)
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true)
c.Assert(sessionTransport.TLSClientConfig.RootCAs, NotNil)
t, ok = cl.fetchTransport(transportOptions{
insecureSkipTLS: true,
caBundle: "this is the way",
})
// transport should be cached.
c.Assert(ok, Equals, true)
// cached transport should be the one that's used.
c.Assert(sessionTransport, Equals, t)
session, err = newSession(cl, caEndpoint, nil)
c.Assert(err, IsNil)
sessionTransport = session.client.Transport.(*http.Transport)
// transport that's going to be used should be cached already.
c.Assert(sessionTransport, Equals, t)
// no new transport got cached.
c.Assert(cl.transports.Len(), Equals, 2)
// if the cache does not exist, the transport should still be correctly configured.
cl.transports = nil
session, err = newSession(cl, insecureEP, nil)
c.Assert(err, IsNil)
sessionTransport = session.client.Transport.(*http.Transport)
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true)
}
func (s *ClientSuite) testNewHTTPError(c *C, code int, msg string) {
req, _ := http.NewRequest("GET", "foo", nil)
res := &http.Response{
StatusCode: code,
Request: req,
}
err := NewErr(res)
c.Assert(err, NotNil)
c.Assert(err, ErrorMatches, msg)
}
func (s *ClientSuite) TestSetAuth(c *C) {
auth := &BasicAuth{}
r, err := DefaultClient.NewUploadPackSession(s.Endpoint, auth)
c.Assert(err, IsNil)
c.Assert(auth, Equals, r.(*upSession).auth)
}
type mockAuth struct{}
func (*mockAuth) Name() string { return "" }
func (*mockAuth) String() string { return "" }
func (s *ClientSuite) TestSetAuthWrongType(c *C) {
_, err := DefaultClient.NewUploadPackSession(s.Endpoint, &mockAuth{})
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
}
func (s *ClientSuite) TestModifyEndpointIfRedirect(c *C) {
sess := &session{endpoint: nil}
u, _ := url.Parse("https://example.com/info/refs")
res := &http.Response{Request: &http.Request{URL: u}}
c.Assert(func() {
sess.ModifyEndpointIfRedirect(res)
}, PanicMatches, ".*nil pointer dereference.*")
sess = &session{endpoint: nil}
// no-op - should return and not panic
sess.ModifyEndpointIfRedirect(&http.Response{})
data := []struct {
url string
endpoint *transport.Endpoint
expected *transport.Endpoint
}{
{"https://example.com/foo/bar", nil, nil},
{"https://example.com/foo.git/info/refs",
&transport.Endpoint{},
&transport.Endpoint{Protocol: "https", Host: "example.com", Path: "/foo.git"}},
{"https://example.com:8080/foo.git/info/refs",
&transport.Endpoint{},
&transport.Endpoint{Protocol: "https", Host: "example.com", Port: 8080, Path: "/foo.git"}},
}
for _, d := range data {
u, _ := url.Parse(d.url)
sess := &session{endpoint: d.endpoint}
sess.ModifyEndpointIfRedirect(&http.Response{
Request: &http.Request{URL: u},
})
c.Assert(d.endpoint, DeepEquals, d.expected)
}
}
type BaseSuite struct {
fixtures.Suite
base string
host string
port int
}
func (s *BaseSuite) SetUpTest(c *C) {
l, err := net.Listen("tcp", "localhost:0")
c.Assert(err, IsNil)
base, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-http-%d", s.port))
c.Assert(err, IsNil)
s.port = l.Addr().(*net.TCPAddr).Port
s.base = filepath.Join(base, s.host)
err = os.MkdirAll(s.base, 0755)
c.Assert(err, IsNil)
cmd := exec.Command("git", "--exec-path")
out, err := cmd.CombinedOutput()
c.Assert(err, IsNil)
server := &http.Server{
Handler: &cgi.Handler{
Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"),
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)},
},
}
go func() {
log.Fatal(server.Serve(l))
}()
}
func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) *transport.Endpoint {
fs := f.DotGit()
err := fixtures.EnsureIsBare(fs)
c.Assert(err, IsNil)
path := filepath.Join(s.base, name)
err = os.Rename(fs.Root(), path)
c.Assert(err, IsNil)
return s.newEndpoint(c, name)
}
func (s *BaseSuite) newEndpoint(c *C, name string) *transport.Endpoint {
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
c.Assert(err, IsNil)
return ep
}
func (s *BaseSuite) TearDownTest(c *C) {
err := os.RemoveAll(s.base)
c.Assert(err, IsNil)
}
|