Debian Codegraph @

registry/remote/utils_test.go

raw
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 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25 "oras.land/oras-go/v2/errdef"
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: "same origin, absolute URL",
50 url: "https://localhost:5000/v2/_catalog",
51 header: `<https://localhost:5000/v2/_catalog?last=alpine&n=1>; rel="next"`,
52 want: "https://localhost:5000/v2/_catalog?last=alpine&n=1",
53 },
54 {
55 name: "cross-origin, different host",
56 url: "https://localhost:5000/v2/_catalog",
57 header: `<https://localhost:5001/v2/_catalog?last=alpine&n=1>; rel="next"`,
58 wantErr: true,
59 },
60 {
61 name: "cross-origin, metadata endpoint (SSRF)",
62 url: "https://registry.example.com/v2/_catalog",
63 header: `<http://169.254.169.254/latest/meta-data/>; rel="next"`,
64 wantErr: true,
65 },
66 {
67 name: "cross-origin, scheme downgrade",
68 url: "https://localhost:5000/v2/_catalog",
69 header: `<http://localhost:5000/v2/_catalog?last=alpine&n=1>; rel="next"`,
70 wantErr: true,
71 },
72 {
73 name: "cross-origin, different port",
74 url: "https://localhost:5000/v2/_catalog",
75 header: `<https://localhost:6000/v2/_catalog?last=alpine&n=1>; rel="next"`,
76 wantErr: true,
77 },
78 {
79 name: "invalid header, missing <",
80 url: "https://localhost:5000/v2/_catalog",
81 header: `/v2/_catalog>`,
82 wantErr: true,
83 },
84 {
85 name: "invalid header, missing >",
86 url: "https://localhost:5000/v2/_catalog",
87 header: `</v2/_catalog`,
88 wantErr: true,
89 },
90 }
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 url, err := url.Parse(tt.url)
94 if err != nil {
95 t.Errorf("fail to parse url in the test case: %v", err)
96 }
97 resp := &http.Response{
98 Request: &http.Request{
99 URL: url,
100 },
101 Header: http.Header{
102 "Link": []string{tt.header},
103 },
104 }
105 got, err := parseLink(resp)
106 if (err != nil) != tt.wantErr {
107 t.Errorf("parseLink() error = %v, wantErr %v", err, tt.wantErr)
108 return
109 }
110 if got != tt.want {
111 t.Errorf("parseLink() = %v, want %v", got, tt.want)
112 }
113 })
114 }
115 }
116
117 func Test_limitSize(t *testing.T) {
118 tests := []struct {
119 name string
120 desc ocispec.Descriptor
121 n int64
122 wantErr error
123 }{
124 {
125 name: "size within specified limit",
126 desc: ocispec.Descriptor{
127 Size: 1,
128 },
129 n: 2,
130 wantErr: nil,
131 },
132 {
133 name: "size equals specified limit",
134 desc: ocispec.Descriptor{
135 Size: 1,
136 },
137 n: 1,
138 wantErr: nil,
139 },
140 {
141 name: "size exceeds specified limit",
142 desc: ocispec.Descriptor{
143 Size: 2,
144 },
145 n: 1,
146 wantErr: errdef.ErrSizeExceedsLimit,
147 },
148 {
149 name: "size within default limit",
150 desc: ocispec.Descriptor{
151 Size: 4*1024*1024 - 1, // 4 MiB - 1
152 },
153 n: 0,
154 wantErr: nil,
155 },
156 {
157 name: "size equals default limit",
158 desc: ocispec.Descriptor{
159 Size: 4 * 1024 * 1024, // 4 MiB
160 },
161 n: 0,
162 wantErr: nil,
163 },
164 {
165 name: "size exceeds default limit",
166 desc: ocispec.Descriptor{
167 Size: 4*1024*1024 + 1, // 4 MiB + 1
168 },
169 n: 0,
170 wantErr: errdef.ErrSizeExceedsLimit,
171 },
172 }
173 for _, tt := range tests {
174 t.Run(tt.name, func(t *testing.T) {
175 if err := limitSize(tt.desc, tt.n); !errors.Is(err, tt.wantErr) {
176 t.Errorf("limitSize() error = %v, wantErr %v", err, tt.wantErr)
177 }
178 })
179 }
180 }