Harden v1 upload-pack path coverage · Entire

Harden v1 upload-pack path coverage

e2a5ee2→main·

Soph·3mo ago·1 file·+96 added/-0 removed

Sessions

273279ad48cfView transcript

Changes

1

1 unmodified line

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
233 unmodified lines

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

1 unmodified line

import (
    "bytes"
    "context"
    "errors"
    "io"
    "net/http"
    "testing"
    "time"

git "github.com/go-git/go-git/v6"
    "github.com/go-git/go-git/v6/plumbing"
    "github.com/go-git/go-git/v6/plumbing/protocol/packp"
    "github.com/go-git/go-git/v6/plumbing/protocol/packp/capability"
    "github.com/go-git/go-git/v6/plumbing/protocol/packp/sideband"
    "github.com/go-git/go-git/v6/plumbing/transport"
)

func TestCapabilities(t *testing.T) {
233 unmodified lines
        t.Fatal("expected error when filter not supported")
}

func TestFetchPackV1ContextCanceled(t *testing.T) {
    started := make(chan struct{}, 1)
    ep, err := transport.NewEndpoint("https://example.com/repo.git")
    if err != nil {
        t.Fatalf("parse endpoint: %v", err)
    }
    conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
        started <- struct{}{}
        <-req.Context().Done()
        return nil, req.Context().Err()
    }))

adv := packp.NewAdvRefs()
    _ = adv.Capabilities.Set(capability.Sideband64k)
    desired := map[plumbing.ReferenceName]DesiredRef{
        plumbing.NewBranchReferenceName("main"): {
            SourceRef:  plumbing.NewBranchReferenceName("main"),
            TargetRef:  plumbing.NewBranchReferenceName("main"),
            SourceHash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
        },
    }

ctx, cancel := context.WithCancel(context.Background())
    done := make(chan error, 1)
    go func() {
        _, err := fetchPackV1(ctx, conn, adv, desired, nil)
        done <- err
    }()

select {
    case <-started:
    case <-time.After(2 * time.Second):
        t.Fatal("request did not reach server before timeout")
    }
    cancel()

select {
    case err = <-done:
    case <-time.After(2 * time.Second):
        t.Fatal("fetchPackV1 did not return after cancellation")
    }
    if err == nil {
        t.Fatal("expected cancellation error")
    }
    if !errors.Is(err, context.Canceled) {
        t.Fatalf("expected context.Canceled, got %v", err)
    }
}

func TestFetchPackV1ClosesBodyOnDecodeError(t *testing.T) {
    ep, err := transport.NewEndpoint("https://example.com/repo.git")
    if err != nil {
        t.Fatalf("parse endpoint: %v", err)
    }
    body := &trackingReadCloser{ReadCloser: io.NopCloser(bytes.NewBufferString("not-a-valid-server-response"))}
    conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
        return &http.Response{
            StatusCode: http.StatusOK,
            Request:    req,
            Body:       body,
        }, nil
    }))

adv := packp.NewAdvRefs()
    desired := map[plumbing.ReferenceName]DesiredRef{
        plumbing.NewBranchReferenceName("main"): {
            SourceRef:  plumbing.NewBranchReferenceName("main"),
            TargetRef:  plumbing.NewBranchReferenceName("main"),
            SourceHash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
        },
    }

_, err = fetchPackV1(context.Background(), conn, adv, desired, nil)
    if err == nil {
        t.Fatal("expected decode error")
    }
    if !body.closed {
        t.Fatal("expected response body to be closed on decode error")
    }
}

func TestBuildV1UploadPackBodyEmptyWantSet(t *testing.T) {
    adv := packp.NewAdvRefs()
    _, _, err := buildV1UploadPackBody(adv, nil, nil, false)
    if !errors.Is(err, git.NoErrAlreadyUpToDate) {
        t.Fatalf("expected NoErrAlreadyUpToDate, got %v", err)
    }
}