| 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 remotescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/ |
| 17 | |
| 18 | import ( |
| 19 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 20 | "net/httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/" |
| 21 | "net/urlscip-go gomod github.com/golang/go/src go1.23.0 `net/url`/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 23 | |
| 24 | ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/ "github.com/opencontainers/image-spec/specs-go/v1scip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/" |
| 25 | "oras.land/oras-go/v2/errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/" |
| 26 | ) |
| 27 | |
| 28 | func Test_parseLinkscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/Test_parseLink().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 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, missing <", |
| 56 | url: "https://localhost:5000/v2/_catalog", |
| 57 | header: `/v2/_catalog>`, |
| 58 | wantErr: true, |
| 59 | }, |
| 60 | { |
| 61 | name: "invalid header, missing >", |
| 62 | url: "https://localhost:5000/v2/_catalog", |
| 63 | header: `</v2/_catalog`, |
| 64 | wantErr: true, |
| 65 | }, |
| 66 | } |
| 67 | for _, tt := range tests { |
| 68 | t.Runscip-go gomod github.com/golang/go/src go1.23.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 69 | url, err := urlscip-go gomod github.com/golang/go/src go1.23.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.23.0 `net/url`/Parse().(tt.url) |
| 70 | if err != nil { |
| 71 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("fail to parse url in the test case: %v", err) |
| 72 | } |
| 73 | resp := &httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#{ |
| 74 | Requestscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Request.: &httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Request#{ |
| 75 | URLscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Request#URL.: url, |
| 76 | }, |
| 77 | Headerscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Header.: httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Header#{ |
| 78 | "Link": []string{tt.header}, |
| 79 | }, |
| 80 | } |
| 81 | got, err := parseLinkscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/parseLink().(resp) |
| 82 | if (err != nil) != tt.wantErr { |
| 83 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("parseLink() error = %v, wantErr %v", err, tt.wantErr) |
| 84 | return |
| 85 | } |
| 86 | if got != tt.want { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("parseLink() = %v, want %v", got, tt.want) |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func Test_limitSizescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/Test_limitSize().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 94 | tests := []struct { |
| 95 | name string |
| 96 | desc ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor# |
| 97 | n int64 |
| 98 | wantErr error |
| 99 | }{ |
| 100 | { |
| 101 | name: "size within specified limit", |
| 102 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 103 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 1, |
| 104 | }, |
| 105 | n: 2, |
| 106 | wantErr: nil, |
| 107 | }, |
| 108 | { |
| 109 | name: "size equals specified limit", |
| 110 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 111 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 1, |
| 112 | }, |
| 113 | n: 1, |
| 114 | wantErr: nil, |
| 115 | }, |
| 116 | { |
| 117 | name: "size exceeds specified limit", |
| 118 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 119 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 2, |
| 120 | }, |
| 121 | n: 1, |
| 122 | wantErr: errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrSizeExceedsLimitscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrSizeExceedsLimit., |
| 123 | }, |
| 124 | { |
| 125 | name: "size within default limit", |
| 126 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 127 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 4*1024*1024 - 1, // 4 MiB - 1 |
| 128 | }, |
| 129 | n: 0, |
| 130 | wantErr: nil, |
| 131 | }, |
| 132 | { |
| 133 | name: "size equals default limit", |
| 134 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 135 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 4 * 1024 * 1024, // 4 MiB |
| 136 | }, |
| 137 | n: 0, |
| 138 | wantErr: nil, |
| 139 | }, |
| 140 | { |
| 141 | name: "size exceeds default limit", |
| 142 | desc: ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 143 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: 4*1024*1024 + 1, // 4 MiB + 1 |
| 144 | }, |
| 145 | n: 0, |
| 146 | wantErr: errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrSizeExceedsLimitscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrSizeExceedsLimit., |
| 147 | }, |
| 148 | } |
| 149 | for _, tt := range tests { |
| 150 | t.Runscip-go gomod github.com/golang/go/src go1.23.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 151 | if err := limitSizescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/limitSize().(tt.desc, tt.n); !errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Isscip-go gomod github.com/golang/go/src go1.23.0 errors/Is().(err, tt.wantErr) { |
| 152 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("limitSize() error = %v, wantErr %v", err, tt.wantErr) |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | } |