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 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
|
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package registry
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"github.com/notaryproject/notation-go/internal/envelope"
"github.com/notaryproject/notation-go/internal/mock/ocilayout"
"github.com/notaryproject/notation-go/internal/slices"
"github.com/notaryproject/notation-go/registry/internal/artifactspec"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/content/oci"
"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"
)
const (
zeroDigest = "sha256:0000000000000000000000000000000000000000000000000000000000000000"
emptyConfigDigest = "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"
validDigest = "6c3c624b58dbbcd3c0dd82b4c53f04194d1247c6eebdaab7c610cf7d66709b3b"
validDigest2 = "1834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f2"
invalidDigest = "invaliddigest"
algo = "sha256"
validDigestWithAlgo = algo + ":" + validDigest
validDigestWithAlgo2 = algo + ":" + validDigest2
validHost = "localhost"
validRegistry = validHost + ":5000"
invalidHost = "badhost"
invalidRegistry = invalidHost + ":5000"
validRepo = "test"
msg = "message"
errMsg = "error message"
validReference = validRegistry + "/" + validRepo + "@" + validDigestWithAlgo
referenceWithInvalidHost = invalidRegistry + "/" + validRepo + "@" + validDigestWithAlgo
invalidReference = "invalid reference"
joseTag = "application/jose+json"
coseTag = "application/cose"
validTimestamp = "2022-07-29T02:23:10Z"
validPage = `
{
"Manifests": [
{
"MediaType": "application/vnd.oci.image.manifest.v1+json",
"Digest": "sha256:cf2a0974295fc17b8351ef52abae2f40212e20e0359ea980ec5597bb0315347b",
"Size": 620,
"ArtifactType": "application/vnd.cncf.notary.signature"
}
]
}`
validPageDigest = "sha256:cf2a0974295fc17b8351ef52abae2f40212e20e0359ea980ec5597bb0315347b"
validPageImage = `
{
"Manifests": [
{
"MediaType": "application/vnd.oci.image.manifest.v1+json",
"Digest": "sha256:c8f1c1a1bdf099fbc1b70ec4b98da3d8704e27d863f1407db06aad1e022a32cf",
"Size": 733,
"ArtifactType": "application/vnd.cncf.notary.signature"
}
]
}`
validPageImageDigest = "sha256:c8f1c1a1bdf099fbc1b70ec4b98da3d8704e27d863f1407db06aad1e022a32cf"
validBlob = `{
"digest": "sha256:1834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f2",
"size": 90
}`
)
var (
validDigestWithAlgoSlice = []string{validDigestWithAlgo, validDigestWithAlgo2}
signaturePath = filepath.FromSlash("../internal/testdata/jws_signature.sig")
)
type args struct {
ctx context.Context
reference string
remoteClient remote.Client
plainHttp bool
annotations map[string]string
subjectManifest ocispec.Descriptor
signature []byte
signatureMediaType string
signatureManifestDesc ocispec.Descriptor
artifactManifestDesc ocispec.Descriptor
}
type mockRemoteClient struct {
}
func (c mockRemoteClient) Do(req *http.Request) (*http.Response, error) {
switch req.URL.Path {
case "/v2/test/manifests/" + validDigest:
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(msg))),
Header: map[string][]string{
"Content-Type": {joseTag},
"Docker-Content-Digest": {validDigestWithAlgo},
},
}, nil
case "/v2/test/blobs/" + validDigestWithAlgo2:
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(validBlob))),
ContentLength: maxBlobSizeLimit + 1,
Header: map[string][]string{
"Content-Type": {joseTag},
"Docker-Content-Digest": {validDigestWithAlgo2},
},
}, nil
case "/v2/test/blobs/" + emptyConfigDigest:
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(bytes.NewReader([]byte{})),
}, nil
case "/v2/test/manifests/" + invalidDigest:
return &http.Response{}, fmt.Errorf(errMsg)
case "v2/test/manifest/" + validDigest2:
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(validDigest2))),
Header: map[string][]string{
"Content-Type": {joseTag},
"Docker-Content-Digest": {validDigestWithAlgo2},
},
}, nil
case "/v2/test/blobs/uploads/":
switch req.Host {
case validRegistry:
return &http.Response{
StatusCode: http.StatusAccepted,
Body: io.NopCloser(bytes.NewReader([]byte(msg))),
Request: &http.Request{
Header: map[string][]string{},
},
Header: map[string][]string{
"Location": {"test"},
},
}, nil
default:
return &http.Response{}, fmt.Errorf(msg)
}
case "/v2/test/referrers/":
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{ocispec.MediaTypeImageIndex},
},
Body: io.NopCloser(bytes.NewReader([]byte(validPage))),
Request: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/v2/test/referrers/"},
},
}, nil
case "/v2/test/referrers/" + validDigestWithAlgo:
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{ocispec.MediaTypeImageIndex},
},
Body: io.NopCloser(bytes.NewReader([]byte(validPage))),
Request: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/v2/test/referrers/" + validDigestWithAlgo},
},
}, nil
case "/v2/test/referrers/" + zeroDigest:
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{ocispec.MediaTypeImageIndex},
},
Body: io.NopCloser(bytes.NewReader([]byte(validPageImage))),
Request: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/v2/test/referrers/" + zeroDigest},
},
}, nil
case validRepo:
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(bytes.NewReader([]byte(msg))),
}, nil
default:
_, digest, found := strings.Cut(req.URL.Path, "/v2/test/manifests/")
if found && !slices.Contains(validDigestWithAlgoSlice, digest) {
resp := &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(bytes.NewReader([]byte(msg))),
Header: http.Header{
"Content-Type": []string{joseTag},
"Oci-Subject": []string{validDigestWithAlgo},
},
}
return resp, nil
}
return &http.Response{}, fmt.Errorf(errMsg)
}
}
func TestResolve(t *testing.T) {
tests := []struct {
name string
args args
expect ocispec.Descriptor
expectErr bool
}{
{
name: "failed to resolve",
args: args{
ctx: context.Background(),
reference: invalidReference,
remoteClient: mockRemoteClient{},
plainHttp: false,
},
expect: ocispec.Descriptor{},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := tt.args
ref, _ := registry.ParseReference(args.reference)
client := newRepositoryClient(args.remoteClient, ref, args.plainHttp)
res, err := client.Resolve(args.ctx, args.reference)
if (err != nil) != tt.expectErr {
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr)
}
if !reflect.DeepEqual(res, tt.expect) {
t.Errorf("expect %+v, got %+v", tt.expect, res)
}
})
}
}
func TestFetchSignatureBlob(t *testing.T) {
tests := []struct {
name string
args args
expect []byte
expectErr bool
}{
{
name: "failed to resolve",
expect: nil,
expectErr: true,
args: args{
ctx: context.Background(),
reference: validReference,
remoteClient: mockRemoteClient{},
plainHttp: false,
signatureManifestDesc: ocispec.Descriptor{
MediaType: artifactspec.MediaTypeArtifactManifest,
Digest: digest.Digest(invalidDigest),
},
},
},
{
name: "exceed max blob size",
expect: nil,
expectErr: true,
args: args{
ctx: context.Background(),
reference: validReference,
remoteClient: mockRemoteClient{},
plainHttp: false,
signatureManifestDesc: ocispec.Descriptor{
MediaType: artifactspec.MediaTypeArtifactManifest,
Digest: digest.Digest(validDigestWithAlgo2),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := tt.args
ref, _ := registry.ParseReference(args.reference)
client := newRepositoryClient(args.remoteClient, ref, args.plainHttp)
res, _, err := client.FetchSignatureBlob(args.ctx, args.signatureManifestDesc)
if (err != nil) != tt.expectErr {
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr)
}
if !reflect.DeepEqual(res, tt.expect) {
t.Errorf("expect %+v, got %+v", tt.expect, res)
}
})
}
}
func TestListSignatures(t *testing.T) {
tests := []struct {
name string
args args
expect []interface{}
expectErr bool
}{
{
name: "successfully fetch content",
expectErr: false,
expect: nil,
args: args{
ctx: context.Background(),
reference: validReference,
remoteClient: mockRemoteClient{},
plainHttp: false,
artifactManifestDesc: ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: validDigestWithAlgo,
Size: 481,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := tt.args
ref, err := registry.ParseReference(args.reference)
if err != nil {
t.Fatal(err)
}
client := newRepositoryClient(args.remoteClient, ref, args.plainHttp)
err = client.ListSignatures(args.ctx, args.artifactManifestDesc, func(signatureManifests []ocispec.Descriptor) error {
if len(signatureManifests) != 1 {
return fmt.Errorf("length of signatureManifests expected 1, got %d", len(signatureManifests))
}
for _, sigManifest := range signatureManifests {
sigManifestDigest := sigManifest.Digest.String()
if sigManifestDigest != validPageDigest {
return fmt.Errorf("signature manifest digest expected: %s, got %s", validPageDigest, sigManifestDigest)
}
}
return nil
})
if (err != nil) != tt.expectErr {
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr)
}
})
}
}
func TestPushSignature(t *testing.T) {
signature, err := os.ReadFile(signaturePath)
if err != nil {
t.Fatalf("failed to read signature: %v", err)
}
tests := []struct {
name string
args args
expectDes ocispec.Descriptor
expectManifest ocispec.Descriptor
expectErr bool
}{
{
name: "failed to upload signature",
expectErr: true,
args: args{
reference: referenceWithInvalidHost,
signatureMediaType: joseTag,
signature: signature,
ctx: context.Background(),
remoteClient: mockRemoteClient{},
},
},
{
name: "successfully uploaded signature manifest",
expectErr: false,
args: args{
reference: validReference,
signatureMediaType: joseTag,
signature: signature,
ctx: context.Background(),
remoteClient: mockRemoteClient{},
subjectManifest: ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: validDigestWithAlgo,
Size: 481,
},
annotations: map[string]string{
envelope.AnnotationX509ChainThumbprint: "[\"9f5f5aecee24b5cfdc7a91f6d5ac5c3a5348feb17c934d403f59ac251549ea0d\"]",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := tt.args
ref, _ := registry.ParseReference(args.reference)
client := newRepositoryClient(args.remoteClient, ref, args.plainHttp)
_, _, err := client.PushSignature(args.ctx, args.signatureMediaType, args.signature, args.subjectManifest, args.annotations)
if (err != nil) != tt.expectErr {
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr)
}
})
}
}
func TestPushSignatureImageManifest(t *testing.T) {
ref, err := registry.ParseReference(validReference)
if err != nil {
t.Fatalf("failed to parse reference")
}
client := newRepositoryClientWithImageManifest(mockRemoteClient{}, ref, false)
signature, err := os.ReadFile(signaturePath)
if err != nil {
t.Fatalf("failed to read signature: %v", err)
}
_, manifestDesc, err := client.PushSignature(context.Background(), joseTag, signature, ocispec.Descriptor{}, annotations)
if err != nil {
t.Fatalf("failed to push signature")
}
if manifestDesc.MediaType != ocispec.MediaTypeImageManifest {
t.Errorf("expect manifestDesc.MediaType: %v, got %v", ocispec.MediaTypeImageManifest, manifestDesc.MediaType)
}
}
// newRepositoryClient creates a new repository client
func newRepositoryClient(client remote.Client, ref registry.Reference, plainHTTP bool) *repositoryClient {
repo := remote.Repository{
Client: client,
Reference: ref,
PlainHTTP: plainHTTP,
}
return &repositoryClient{
GraphTarget: &repo,
}
}
// newRepositoryClientWithImageManifest creates a new repository client for
// pushing OCI image manifest
func newRepositoryClientWithImageManifest(client remote.Client, ref registry.Reference, plainHTTP bool) *repositoryClient {
return &repositoryClient{
GraphTarget: &remote.Repository{
Client: client,
Reference: ref,
PlainHTTP: plainHTTP,
},
}
}
var (
reference = "sha256:19dbd2e48e921426ee8ace4dc892edfb2ecdc1d1a72d5416c83670c30acecef0"
expectedTargetDesc = ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:19dbd2e48e921426ee8ace4dc892edfb2ecdc1d1a72d5416c83670c30acecef0",
Size: 481,
}
annotations = map[string]string{
envelope.AnnotationX509ChainThumbprint: "[\"9f5f5aecee24b5cfdc7a91f6d5ac5c3a5348feb17c934d403f59ac251549ea0d\"]",
ocispec.AnnotationCreated: "2023-03-14T08:10:02Z",
}
expectedSignatureManifestDesc = ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:baeaea44f55c94499b7e082bd3c98ad5ec40fdf23ef89cdf4e5db6b83e4f18f5",
Size: 728,
}
expectedSignatureBlobDesc = ocispec.Descriptor{
MediaType: joseTag,
Digest: "sha256:586c5e0f341d7d07e835a06b7c9f21c21fff4f4a85933079e5859f99ba0ad02d",
Size: 2078,
}
)
func TestOciLayoutRepositoryPushAndFetch(t *testing.T) {
// create a temp OCI layout
ociLayoutTestdataPath, err := filepath.Abs(filepath.Join("..", "internal", "testdata", "oci-layout"))
if err != nil {
t.Fatalf("failed to get oci layout path: %v", err)
}
newOCILayoutPath := t.TempDir()
if err := ocilayout.Copy(ociLayoutTestdataPath, newOCILayoutPath, "v2"); err != nil {
t.Fatalf("failed to create temp oci layout: %v", err)
}
repo, err := NewOCIRepository(newOCILayoutPath, RepositoryOptions{})
if err != nil {
t.Fatalf("failed to create oci.Store as registry.Repository: %v", err)
}
// test resolve
targetDesc, err := repo.Resolve(context.Background(), reference)
if err != nil {
t.Fatalf("failed to resolve reference: %v", err)
}
if !content.Equal(targetDesc, expectedTargetDesc) {
t.Fatalf("failed to resolve reference. expected descriptor: %v, but got: %v", expectedTargetDesc, targetDesc)
}
t.Run("oci layout push", func(t *testing.T) {
signature, err := os.ReadFile(signaturePath)
if err != nil {
t.Fatalf("failed to read signature: %v", err)
}
_, signatureManifestDesc, err := repo.PushSignature(context.Background(), joseTag, signature, targetDesc, annotations)
if err != nil {
t.Fatalf("failed to push signature: %v", err)
}
if !content.Equal(expectedSignatureManifestDesc, signatureManifestDesc) {
t.Fatalf("expected desc: %v, got: %v", expectedSignatureManifestDesc, signatureManifestDesc)
}
expectedAnnotations := map[string]string{
envelope.AnnotationX509ChainThumbprint: "[\"9f5f5aecee24b5cfdc7a91f6d5ac5c3a5348feb17c934d403f59ac251549ea0d\"]",
ocispec.AnnotationCreated: "2023-03-14T08:10:02Z",
}
if !reflect.DeepEqual(expectedAnnotations, signatureManifestDesc.Annotations) {
t.Fatalf("expected annotations: %v, but got: %v", expectedAnnotations, signatureManifestDesc.Annotations)
}
})
t.Run("oci layout fetch", func(t *testing.T) {
err = repo.ListSignatures(context.Background(), targetDesc, func(signatureManifests []ocispec.Descriptor) error {
if len(signatureManifests) == 0 {
return fmt.Errorf("expected to find signature in the OCI layout folder, but got none")
}
var found bool
for _, sigManifestDesc := range signatureManifests {
if !content.Equal(sigManifestDesc, expectedSignatureManifestDesc) {
continue
}
_, sigDesc, err := repo.FetchSignatureBlob(context.Background(), sigManifestDesc)
if err != nil {
return fmt.Errorf("failed to fetch blob: %w", err)
}
if !content.Equal(expectedSignatureBlobDesc, sigDesc) {
return fmt.Errorf("expected to get signature blob desc: %v, got: %v", expectedSignatureBlobDesc, sigDesc)
}
found = true
}
if !found {
return fmt.Errorf("expected to find the signature with manifest desc: %v, but failed", expectedSignatureManifestDesc)
}
return nil
})
if err != nil {
t.Fatal(err)
}
})
}
func TestNewRepository(t *testing.T) {
target, err := oci.New(t.TempDir())
if err != nil {
t.Fatalf("failed to create oci.Store as registry.Repository: %v", err)
}
repo := NewRepository(target)
if repo == nil {
t.Fatalf("failed to create repository")
}
repoClient, ok := repo.(*repositoryClient)
if !ok {
t.Fatalf("failed to create repositoryClient")
}
if target != repoClient.GraphTarget {
t.Fatalf("expected target: %v, got: %v", target, repoClient.GraphTarget)
}
}
func TestNewOCIRepositoryFailed(t *testing.T) {
t.Run("os stat failed", func(t *testing.T) {
_, err := NewOCIRepository("invalid-path", RepositoryOptions{})
if err == nil {
t.Fatalf("expected to fail with invalid path")
}
})
t.Run("path is regular file", func(t *testing.T) {
// create a regular file in the temp dir
filePath := filepath.Join(t.TempDir(), "file")
file, err := os.Create(filePath)
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
file.Close()
_, err = NewOCIRepository(filePath, RepositoryOptions{})
if err == nil {
t.Fatalf("expected to fail with regular file")
}
})
t.Run("no permission to create new path", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows")
}
// create a directory in the temp dir
dirPath := filepath.Join(t.TempDir(), "dir")
err := os.Mkdir(dirPath, 0000)
if err != nil {
t.Fatalf("failed to create dir: %v", err)
}
_, err = NewOCIRepository(dirPath, RepositoryOptions{})
if err == nil {
t.Fatalf("expected to fail with no permission to create new path")
}
})
}
// testStorage implements content.ReadOnlyGraphStorage
type testStorage struct {
store *memory.Store
FetchError error
FetchContent []byte
PredecessorsError error
PredecessorsDesc []ocispec.Descriptor
}
func (s *testStorage) Push(ctx context.Context, expected ocispec.Descriptor, reader io.Reader) error {
return s.store.Push(ctx, expected, reader)
}
func (s *testStorage) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) {
if s.FetchError != nil {
return nil, s.FetchError
}
return io.NopCloser(bytes.NewReader(s.FetchContent)), nil
}
func (s *testStorage) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) {
return s.store.Exists(ctx, target)
}
func (s *testStorage) Predecessors(ctx context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if s.PredecessorsError != nil {
return nil, s.PredecessorsError
}
return s.PredecessorsDesc, nil
}
func TestSignatureReferrers(t *testing.T) {
t.Run("get predecessors failed", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsError: fmt.Errorf("failed to get predecessors"),
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{})
if err == nil {
t.Fatalf("expected to fail with getting predecessors")
}
})
t.Run("artifact manifest exceds max blob size", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: validDigestWithAlgo2,
MediaType: "application/vnd.oci.artifact.manifest.v1+json",
Size: 4*1024*1024 + 1,
},
},
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: validDigestWithAlgo2,
})
if err == nil {
t.Fatalf("expected to fail with artifact manifest exceds max blob size")
}
})
t.Run("image manifest exceds max blob size", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: validDigestWithAlgo2,
MediaType: "application/vnd.oci.image.manifest.v1+json",
Size: 4*1024*1024 + 1,
},
},
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: validDigestWithAlgo2,
})
if err == nil {
t.Fatalf("expected to fail with image manifest exceds max blob size")
}
})
t.Run("artifact manifest fetchAll failed", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: validDigestWithAlgo,
MediaType: "application/vnd.oci.artifact.manifest.v1+json",
Size: 481,
},
},
FetchError: fmt.Errorf("failed to fetch all"),
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: validDigestWithAlgo,
})
if err == nil {
t.Fatalf("expected to fail with fetchAll failed")
}
})
t.Run("image manifest fetchAll failed", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: validDigestWithAlgo,
MediaType: "application/vnd.oci.image.manifest.v1+json",
Size: 481,
},
},
FetchError: fmt.Errorf("failed to fetch all"),
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: validDigestWithAlgo,
})
if err == nil {
t.Fatalf("expected to fail with fetchAll failed")
}
})
t.Run("artifact manifest marshal failed", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: "sha256:24aafc739daae02bcd33471a1b28bcfaaef0bb5e530ef44cd4e5d2445e606690",
MediaType: "application/vnd.oci.artifact.manifest.v1+json",
Size: 15,
},
},
FetchContent: []byte("invalid content"),
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: "sha256:24aafc739daae02bcd33471a1b28bcfaaef0bb5e530ef44cd4e5d2445e606690",
})
if err == nil {
t.Fatalf("expected to fail with marshal failed")
}
})
t.Run("image manifest marshal failed", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: "sha256:24aafc739daae02bcd33471a1b28bcfaaef0bb5e530ef44cd4e5d2445e606690",
MediaType: "application/vnd.oci.image.manifest.v1+json",
Size: 15,
},
},
FetchContent: []byte("invalid content"),
}
_, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: "sha256:24aafc739daae02bcd33471a1b28bcfaaef0bb5e530ef44cd4e5d2445e606690",
})
if err == nil {
t.Fatalf("expected to fail with marshal failed")
}
})
t.Run("no valid artifact manifest", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
MediaType: "application/vnd.oci.artifact.manifest.v1+json",
Size: 2,
},
},
FetchContent: []byte("{}"),
}
descriptors, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
})
if err != nil {
t.Fatalf("failed to get referrers: %v", err)
}
if len(descriptors) != 0 {
t.Fatalf("expected to get no referrers, but got: %v", descriptors)
}
})
t.Run("no valid image manifest", func(t *testing.T) {
store := &testStorage{
store: &memory.Store{},
PredecessorsDesc: []ocispec.Descriptor{
{
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
MediaType: "application/vnd.oci.image.manifest.v1+json",
Size: 2,
},
},
FetchContent: []byte("{}"),
}
descriptors, err := signatureReferrers(context.Background(), store, ocispec.Descriptor{
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
})
if err != nil {
t.Fatalf("failed to get referrers: %v", err)
}
if len(descriptors) != 0 {
t.Fatalf("expected to get no referrers, but got: %v", descriptors)
}
})
}
|