github.com/opcr-io/oras-go/v2@v2.0.0-20231122155130-eb4260d8a0ae/registry/remote/utils_test.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package remote 17 18 import ( 19 "errors" 20 "net/http" 21 "net/url" 22 "testing" 23 24 "github.com/opcr-io/oras-go/v2/errdef" 25 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 26 ) 27 28 func Test_parseLink(t *testing.T) { 29 tests := []struct { 30 name string 31 url string 32 header string 33 want string 34 wantErr bool 35 }{ 36 { 37 name: "catalog", 38 url: "https://localhost:5000/v2/_catalog", 39 header: `</v2/_catalog?last=alpine&n=1>; rel="next"`, 40 want: "https://localhost:5000/v2/_catalog?last=alpine&n=1", 41 }, 42 { 43 name: "list tag", 44 url: "https://localhost:5000/v2/hello-world/tags/list", 45 header: `</v2/hello-world/tags/list?last=latest&n=1>; rel="next"`, 46 want: "https://localhost:5000/v2/hello-world/tags/list?last=latest&n=1", 47 }, 48 { 49 name: "other domain", 50 url: "https://localhost:5000/v2/_catalog", 51 header: `<https://localhost:5001/v2/_catalog?last=alpine&n=1>; rel="next"`, 52 want: "https://localhost:5001/v2/_catalog?last=alpine&n=1", 53 }, 54 { 55 name: "invalid header", 56 url: "https://localhost:5000/v2/_catalog", 57 header: `</v2/_catalog`, 58 wantErr: true, 59 }, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 url, err := url.Parse(tt.url) 64 if err != nil { 65 t.Errorf("fail to parse url in the test case: %v", err) 66 } 67 resp := &http.Response{ 68 Request: &http.Request{ 69 URL: url, 70 }, 71 Header: http.Header{ 72 "Link": []string{tt.header}, 73 }, 74 } 75 got, err := parseLink(resp) 76 if (err != nil) != tt.wantErr { 77 t.Errorf("parseLink() error = %v, wantErr %v", err, tt.wantErr) 78 return 79 } 80 if got != tt.want { 81 t.Errorf("parseLink() = %v, want %v", got, tt.want) 82 } 83 }) 84 } 85 } 86 87 func Test_limitSize(t *testing.T) { 88 tests := []struct { 89 name string 90 desc ocispec.Descriptor 91 n int64 92 wantErr error 93 }{ 94 { 95 name: "size within specified limit", 96 desc: ocispec.Descriptor{ 97 Size: 1, 98 }, 99 n: 2, 100 wantErr: nil, 101 }, 102 { 103 name: "size equals specified limit", 104 desc: ocispec.Descriptor{ 105 Size: 1, 106 }, 107 n: 1, 108 wantErr: nil, 109 }, 110 { 111 name: "size exceeds specified limit", 112 desc: ocispec.Descriptor{ 113 Size: 2, 114 }, 115 n: 1, 116 wantErr: errdef.ErrSizeExceedsLimit, 117 }, 118 { 119 name: "size within default limit", 120 desc: ocispec.Descriptor{ 121 Size: 4*1024*1024 - 1, // 4 MiB - 1 122 }, 123 n: 0, 124 wantErr: nil, 125 }, 126 { 127 name: "size equals default limit", 128 desc: ocispec.Descriptor{ 129 Size: 4 * 1024 * 1024, // 4 MiB 130 }, 131 n: 0, 132 wantErr: nil, 133 }, 134 { 135 name: "size exceeds default limit", 136 desc: ocispec.Descriptor{ 137 Size: 4*1024*1024 + 1, // 4 MiB + 1 138 }, 139 n: 0, 140 wantErr: errdef.ErrSizeExceedsLimit, 141 }, 142 } 143 for _, tt := range tests { 144 t.Run(tt.name, func(t *testing.T) { 145 if err := limitSize(tt.desc, tt.n); !errors.Is(err, tt.wantErr) { 146 t.Errorf("limitSize() error = %v, wantErr %v", err, tt.wantErr) 147 } 148 }) 149 } 150 }