| 1 |
// Package test implements common test suite for different transport |
| 2 |
// implementations. |
| 3 |
package test |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"context" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
|
| 12 |
. "github.com/go-git/go-git/v5/internal/test" |
| 13 |
"github.com/go-git/go-git/v5/plumbing" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 15 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 16 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 17 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 18 |
"github.com/go-git/go-git/v5/storage/memory" |
| 19 |
|
| 20 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 21 |
. "gopkg.in/check.v1" |
| 22 |
) |
| 23 |
|
| 24 |
type ReceivePackSuite struct { |
| 25 |
Endpoint *transport.Endpoint |
| 26 |
EmptyEndpoint *transport.Endpoint |
| 27 |
NonExistentEndpoint *transport.Endpoint |
| 28 |
EmptyAuth transport.AuthMethod |
| 29 |
Client transport.Transport |
| 30 |
} |
| 31 |
|
| 32 |
func (s *ReceivePackSuite) TestAdvertisedReferencesEmpty(c *C) { |
| 33 |
r, err := s.Client.NewReceivePackSession(s.EmptyEndpoint, s.EmptyAuth) |
| 34 |
c.Assert(err, IsNil) |
| 35 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 36 |
|
| 37 |
ar, err := r.AdvertisedReferences() |
| 38 |
c.Assert(err, IsNil) |
| 39 |
c.Assert(ar.Head, IsNil) |
| 40 |
} |
| 41 |
|
| 42 |
func (s *ReceivePackSuite) TestAdvertisedReferencesNotExists(c *C) { |
| 43 |
r, err := s.Client.NewReceivePackSession(s.NonExistentEndpoint, s.EmptyAuth) |
| 44 |
c.Assert(err, IsNil) |
| 45 |
ar, err := r.AdvertisedReferences() |
| 46 |
c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) |
| 47 |
c.Assert(ar, IsNil) |
| 48 |
c.Assert(r.Close(), IsNil) |
| 49 |
|
| 50 |
r, err = s.Client.NewReceivePackSession(s.NonExistentEndpoint, s.EmptyAuth) |
| 51 |
c.Assert(err, IsNil) |
| 52 |
req := packp.NewReferenceUpdateRequest() |
| 53 |
req.Commands = []*packp.Command{ |
| 54 |
{Name: "master", Old: plumbing.ZeroHash, New: plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")}, |
| 55 |
} |
| 56 |
|
| 57 |
writer, err := r.ReceivePack(context.Background(), req) |
| 58 |
c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) |
| 59 |
c.Assert(writer, IsNil) |
| 60 |
c.Assert(r.Close(), IsNil) |
| 61 |
} |
| 62 |
|
| 63 |
func (s *ReceivePackSuite) TestCallAdvertisedReferenceTwice(c *C) { |
| 64 |
r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) |
| 65 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 66 |
c.Assert(err, IsNil) |
| 67 |
ar1, err := r.AdvertisedReferences() |
| 68 |
c.Assert(err, IsNil) |
| 69 |
c.Assert(ar1, NotNil) |
| 70 |
ar2, err := r.AdvertisedReferences() |
| 71 |
c.Assert(err, IsNil) |
| 72 |
c.Assert(ar2, DeepEquals, ar1) |
| 73 |
} |
| 74 |
|
| 75 |
func (s *ReceivePackSuite) TestDefaultBranch(c *C) { |
| 76 |
r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) |
| 77 |
c.Assert(err, IsNil) |
| 78 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 79 |
|
| 80 |
info, err := r.AdvertisedReferences() |
| 81 |
c.Assert(err, IsNil) |
| 82 |
ref, ok := info.References["refs/heads/master"] |
| 83 |
c.Assert(ok, Equals, true) |
| 84 |
c.Assert(ref.String(), Equals, fixtures.Basic().One().Head) |
| 85 |
} |
| 86 |
|
| 87 |
func (s *ReceivePackSuite) TestCapabilities(c *C) { |
| 88 |
r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) |
| 89 |
c.Assert(err, IsNil) |
| 90 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 91 |
|
| 92 |
info, err := r.AdvertisedReferences() |
| 93 |
c.Assert(err, IsNil) |
| 94 |
c.Assert(info.Capabilities.Get("agent"), HasLen, 1) |
| 95 |
} |
| 96 |
|
| 97 |
func (s *ReceivePackSuite) TestFullSendPackOnEmpty(c *C) { |
| 98 |
endpoint := s.EmptyEndpoint |
| 99 |
full := true |
| 100 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 101 |
req := packp.NewReferenceUpdateRequest() |
| 102 |
req.Commands = []*packp.Command{ |
| 103 |
{Name: "refs/heads/master", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 104 |
} |
| 105 |
s.receivePack(c, endpoint, req, fixture, full) |
| 106 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 107 |
} |
| 108 |
|
| 109 |
func (s *ReceivePackSuite) TestSendPackWithContext(c *C) { |
| 110 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 111 |
req := packp.NewReferenceUpdateRequest() |
| 112 |
req.Packfile = fixture.Packfile() |
| 113 |
req.Commands = []*packp.Command{ |
| 114 |
{Name: "refs/heads/master", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 115 |
} |
| 116 |
|
| 117 |
r, err := s.Client.NewReceivePackSession(s.EmptyEndpoint, s.EmptyAuth) |
| 118 |
c.Assert(err, IsNil) |
| 119 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 120 |
|
| 121 |
info, err := r.AdvertisedReferences() |
| 122 |
c.Assert(err, IsNil) |
| 123 |
c.Assert(info, NotNil) |
| 124 |
|
| 125 |
ctx, close := context.WithCancel(context.TODO()) |
| 126 |
close() |
| 127 |
|
| 128 |
report, err := r.ReceivePack(ctx, req) |
| 129 |
c.Assert(err, NotNil) |
| 130 |
c.Assert(report, IsNil) |
| 131 |
} |
| 132 |
|
| 133 |
func (s *ReceivePackSuite) TestSendPackOnEmpty(c *C) { |
| 134 |
endpoint := s.EmptyEndpoint |
| 135 |
full := false |
| 136 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 137 |
req := packp.NewReferenceUpdateRequest() |
| 138 |
req.Commands = []*packp.Command{ |
| 139 |
{Name: "refs/heads/master", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 140 |
} |
| 141 |
s.receivePack(c, endpoint, req, fixture, full) |
| 142 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 143 |
} |
| 144 |
|
| 145 |
func (s *ReceivePackSuite) TestSendPackOnEmptyWithReportStatus(c *C) { |
| 146 |
endpoint := s.EmptyEndpoint |
| 147 |
full := false |
| 148 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 149 |
req := packp.NewReferenceUpdateRequest() |
| 150 |
req.Commands = []*packp.Command{ |
| 151 |
{Name: "refs/heads/master", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 152 |
} |
| 153 |
req.Capabilities.Set(capability.ReportStatus) |
| 154 |
s.receivePack(c, endpoint, req, fixture, full) |
| 155 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 156 |
} |
| 157 |
|
| 158 |
func (s *ReceivePackSuite) TestFullSendPackOnNonEmpty(c *C) { |
| 159 |
endpoint := s.Endpoint |
| 160 |
full := true |
| 161 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 162 |
req := packp.NewReferenceUpdateRequest() |
| 163 |
req.Commands = []*packp.Command{ |
| 164 |
{Name: "refs/heads/master", Old: plumbing.NewHash(fixture.Head), New: plumbing.NewHash(fixture.Head)}, |
| 165 |
} |
| 166 |
s.receivePack(c, endpoint, req, fixture, full) |
| 167 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 168 |
} |
| 169 |
|
| 170 |
func (s *ReceivePackSuite) TestSendPackOnNonEmpty(c *C) { |
| 171 |
endpoint := s.Endpoint |
| 172 |
full := false |
| 173 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 174 |
req := packp.NewReferenceUpdateRequest() |
| 175 |
req.Commands = []*packp.Command{ |
| 176 |
{Name: "refs/heads/master", Old: plumbing.NewHash(fixture.Head), New: plumbing.NewHash(fixture.Head)}, |
| 177 |
} |
| 178 |
s.receivePack(c, endpoint, req, fixture, full) |
| 179 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 180 |
} |
| 181 |
|
| 182 |
func (s *ReceivePackSuite) TestSendPackOnNonEmptyWithReportStatus(c *C) { |
| 183 |
endpoint := s.Endpoint |
| 184 |
full := false |
| 185 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 186 |
req := packp.NewReferenceUpdateRequest() |
| 187 |
req.Commands = []*packp.Command{ |
| 188 |
{Name: "refs/heads/master", Old: plumbing.NewHash(fixture.Head), New: plumbing.NewHash(fixture.Head)}, |
| 189 |
} |
| 190 |
req.Capabilities.Set(capability.ReportStatus) |
| 191 |
|
| 192 |
s.receivePack(c, endpoint, req, fixture, full) |
| 193 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 194 |
} |
| 195 |
|
| 196 |
func (s *ReceivePackSuite) TestSendPackOnNonEmptyWithReportStatusWithError(c *C) { |
| 197 |
endpoint := s.Endpoint |
| 198 |
full := false |
| 199 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 200 |
req := packp.NewReferenceUpdateRequest() |
| 201 |
req.Commands = []*packp.Command{ |
| 202 |
{Name: "refs/heads/master", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 203 |
} |
| 204 |
req.Capabilities.Set(capability.ReportStatus) |
| 205 |
|
| 206 |
report, err := s.receivePackNoCheck(c, endpoint, req, fixture, full) |
| 207 |
//XXX: Recent git versions return "failed to update ref", while older |
| 208 |
// (>=1.9) return "failed to lock". |
| 209 |
// More recent versions: command error on <ref>: reference already exists |
| 210 |
c.Assert(err, ErrorMatches, ".*(failed to update ref|failed to lock|reference already exists).*") |
| 211 |
c.Assert(report.UnpackStatus, Equals, "ok") |
| 212 |
c.Assert(len(report.CommandStatuses), Equals, 1) |
| 213 |
c.Assert(report.CommandStatuses[0].ReferenceName, Equals, plumbing.ReferenceName("refs/heads/master")) |
| 214 |
c.Assert(report.CommandStatuses[0].Status, Matches, "(failed to update ref|failed to lock|reference already exists)") |
| 215 |
s.checkRemoteHead(c, endpoint, plumbing.NewHash(fixture.Head)) |
| 216 |
} |
| 217 |
|
| 218 |
func (s *ReceivePackSuite) receivePackNoCheck(c *C, ep *transport.Endpoint, |
| 219 |
req *packp.ReferenceUpdateRequest, fixture *fixtures.Fixture, |
| 220 |
callAdvertisedReferences bool) (*packp.ReportStatus, error) { |
| 221 |
url := "" |
| 222 |
if fixture != nil { |
| 223 |
url = fixture.URL |
| 224 |
} |
| 225 |
comment := Commentf( |
| 226 |
"failed with ep=%s fixture=%s callAdvertisedReferences=%s", |
| 227 |
ep.String(), url, callAdvertisedReferences, |
| 228 |
) |
| 229 |
|
| 230 |
// Set write permissions to endpoint directory files. By default |
| 231 |
// fixtures are generated with read only permissions, this casuses |
| 232 |
// errors deleting or modifying files. |
| 233 |
rootPath := ep.Path |
| 234 |
stat, err := os.Stat(ep.Path) |
| 235 |
|
| 236 |
if rootPath != "" && err == nil && stat.IsDir() { |
| 237 |
objectPath := filepath.Join(rootPath, "objects/pack") |
| 238 |
files, err := os.ReadDir(objectPath) |
| 239 |
c.Assert(err, IsNil) |
| 240 |
|
| 241 |
for _, file := range files { |
| 242 |
path := filepath.Join(objectPath, file.Name()) |
| 243 |
err = os.Chmod(path, 0644) |
| 244 |
c.Assert(err, IsNil) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
r, err := s.Client.NewReceivePackSession(ep, s.EmptyAuth) |
| 249 |
c.Assert(err, IsNil, comment) |
| 250 |
defer func() { c.Assert(r.Close(), IsNil, comment) }() |
| 251 |
|
| 252 |
if callAdvertisedReferences { |
| 253 |
info, err := r.AdvertisedReferences() |
| 254 |
c.Assert(err, IsNil, comment) |
| 255 |
c.Assert(info, NotNil, comment) |
| 256 |
} |
| 257 |
|
| 258 |
if fixture != nil { |
| 259 |
c.Assert(fixture.Packfile(), NotNil) |
| 260 |
req.Packfile = fixture.Packfile() |
| 261 |
} else { |
| 262 |
req.Packfile = s.emptyPackfile() |
| 263 |
} |
| 264 |
|
| 265 |
return r.ReceivePack(context.Background(), req) |
| 266 |
} |
| 267 |
|
| 268 |
func (s *ReceivePackSuite) receivePack(c *C, ep *transport.Endpoint, |
| 269 |
req *packp.ReferenceUpdateRequest, fixture *fixtures.Fixture, |
| 270 |
callAdvertisedReferences bool) { |
| 271 |
url := "" |
| 272 |
if fixture != nil { |
| 273 |
url = fixture.URL |
| 274 |
} |
| 275 |
|
| 276 |
comment := Commentf( |
| 277 |
"failed with ep=%s fixture=%s callAdvertisedReferences=%s", |
| 278 |
ep.String(), url, callAdvertisedReferences, |
| 279 |
) |
| 280 |
report, err := s.receivePackNoCheck(c, ep, req, fixture, callAdvertisedReferences) |
| 281 |
c.Assert(err, IsNil, comment) |
| 282 |
if req.Capabilities.Supports(capability.ReportStatus) { |
| 283 |
c.Assert(report, NotNil, comment) |
| 284 |
c.Assert(report.Error(), IsNil, comment) |
| 285 |
} else { |
| 286 |
c.Assert(report, IsNil, comment) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func (s *ReceivePackSuite) checkRemoteHead(c *C, ep *transport.Endpoint, head plumbing.Hash) { |
| 291 |
s.checkRemoteReference(c, ep, "refs/heads/master", head) |
| 292 |
} |
| 293 |
|
| 294 |
func (s *ReceivePackSuite) checkRemoteReference(c *C, ep *transport.Endpoint, |
| 295 |
refName string, head plumbing.Hash) { |
| 296 |
|
| 297 |
r, err := s.Client.NewUploadPackSession(ep, s.EmptyAuth) |
| 298 |
c.Assert(err, IsNil) |
| 299 |
defer func() { c.Assert(r.Close(), IsNil) }() |
| 300 |
ar, err := r.AdvertisedReferences() |
| 301 |
c.Assert(err, IsNil, Commentf("endpoint: %s", ep.String())) |
| 302 |
ref, ok := ar.References[refName] |
| 303 |
if head == plumbing.ZeroHash { |
| 304 |
c.Assert(ok, Equals, false) |
| 305 |
} else { |
| 306 |
c.Assert(ok, Equals, true) |
| 307 |
c.Assert(ref, DeepEquals, head) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func (s *ReceivePackSuite) TestSendPackAddDeleteReference(c *C) { |
| 312 |
s.testSendPackAddReference(c) |
| 313 |
s.testSendPackDeleteReference(c) |
| 314 |
} |
| 315 |
|
| 316 |
func (s *ReceivePackSuite) testSendPackAddReference(c *C) { |
| 317 |
r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) |
| 318 |
c.Assert(err, IsNil) |
| 319 |
|
| 320 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 321 |
|
| 322 |
ar, err := r.AdvertisedReferences() |
| 323 |
c.Assert(err, IsNil) |
| 324 |
|
| 325 |
req := packp.NewReferenceUpdateRequest() |
| 326 |
req.Commands = []*packp.Command{ |
| 327 |
{Name: "refs/heads/newbranch", Old: plumbing.ZeroHash, New: plumbing.NewHash(fixture.Head)}, |
| 328 |
} |
| 329 |
if ar.Capabilities.Supports(capability.ReportStatus) { |
| 330 |
req.Capabilities.Set(capability.ReportStatus) |
| 331 |
} |
| 332 |
|
| 333 |
c.Assert(r.Close(), IsNil) |
| 334 |
|
| 335 |
s.receivePack(c, s.Endpoint, req, nil, false) |
| 336 |
s.checkRemoteReference(c, s.Endpoint, "refs/heads/newbranch", plumbing.NewHash(fixture.Head)) |
| 337 |
} |
| 338 |
|
| 339 |
func (s *ReceivePackSuite) testSendPackDeleteReference(c *C) { |
| 340 |
r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) |
| 341 |
c.Assert(err, IsNil) |
| 342 |
|
| 343 |
fixture := fixtures.Basic().ByTag("packfile").One() |
| 344 |
|
| 345 |
ar, err := r.AdvertisedReferences() |
| 346 |
c.Assert(err, IsNil) |
| 347 |
|
| 348 |
req := packp.NewReferenceUpdateRequest() |
| 349 |
req.Commands = []*packp.Command{ |
| 350 |
{Name: "refs/heads/newbranch", Old: plumbing.NewHash(fixture.Head), New: plumbing.ZeroHash}, |
| 351 |
} |
| 352 |
if ar.Capabilities.Supports(capability.ReportStatus) { |
| 353 |
req.Capabilities.Set(capability.ReportStatus) |
| 354 |
} |
| 355 |
|
| 356 |
if !ar.Capabilities.Supports(capability.DeleteRefs) { |
| 357 |
c.Fatal("capability delete-refs not supported") |
| 358 |
} |
| 359 |
|
| 360 |
c.Assert(r.Close(), IsNil) |
| 361 |
|
| 362 |
s.receivePack(c, s.Endpoint, req, nil, false) |
| 363 |
s.checkRemoteReference(c, s.Endpoint, "refs/heads/newbranch", plumbing.ZeroHash) |
| 364 |
} |
| 365 |
|
| 366 |
func (s *ReceivePackSuite) emptyPackfile() io.ReadCloser { |
| 367 |
var buf bytes.Buffer |
| 368 |
e := packfile.NewEncoder(&buf, memory.NewStorage(), false) |
| 369 |
_, err := e.Encode(nil, 10) |
| 370 |
if err != nil { |
| 371 |
panic(err) |
| 372 |
} |
| 373 |
|
| 374 |
return io.NopCloser(&buf) |
| 375 |
} |