Repository navigation
Expand file tree
/
Copy pathregistries_test.go
More file actions
351 lines (316 loc) · 9.75 KB
/
Copy pathregistries_test.go
File metadata and controls
351 lines (316 loc) · 9.75 KB
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
package registries_test
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
"time"
"github.com/git-pkgs/registries"
_ "github.com/git-pkgs/registries/all"
)
func TestSupportedEcosystems(t *testing.T) {
ecosystems := registries.SupportedEcosystems()
expected := []string{"apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deb", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "rpm", "swift", "terraform", "vscode-extension"}
sort.Strings(ecosystems)
if len(ecosystems) != len(expected) {
t.Fatalf("expected %d ecosystems, got %d: %v", len(expected), len(ecosystems), ecosystems)
}
for i, eco := range expected {
if ecosystems[i] != eco {
t.Errorf("expected ecosystem %q at position %d, got %q", eco, i, ecosystems[i])
}
}
}
func TestNew(t *testing.T) {
tests := []struct {
ecosystem string
wantErr bool
}{
{"brew", false},
{"cargo", false},
{"npm", false},
{"gem", false},
{"pypi", false},
{"golang", false},
{"hex", false},
{"pub", false},
{"composer", false},
{"nuget", false},
{"maven", false},
{"cocoapods", false},
{"clojars", false},
{"cpan", false},
{"hackage", false},
{"cran", false},
{"conda", false},
{"julia", false},
{"elm", false},
{"dub", false},
{"luarocks", false},
{"nimble", false},
{"haxelib", false},
{"deno", false},
{"terraform", false},
{"githubactions", false},
{"vscode-extension", false},
{"helm", true},
{"swift", true},
{"apm", true},
{"apk", true},
{"deb", true},
{"rpm", true},
{"unknown", true},
}
for _, tt := range tests {
t.Run(tt.ecosystem, func(t *testing.T) {
_, err := registries.New(tt.ecosystem, "", nil)
if (err != nil) != tt.wantErr {
t.Errorf("New(%q) error = %v, wantErr %v", tt.ecosystem, err, tt.wantErr)
}
})
}
}
func TestDefaultURL(t *testing.T) {
tests := []struct {
ecosystem string
want string
}{
{"brew", "https://formulae.brew.sh"},
{"cargo", "https://crates.io"},
{"npm", "https://registry.npmjs.org"},
{"gem", "https://rubygems.org"},
{"pypi", "https://pypi.org"},
{"golang", "https://proxy.golang.org"},
{"hex", "https://hex.pm"},
{"pub", "https://pub.dev"},
{"composer", "https://packagist.org"},
{"nuget", "https://api.nuget.org/v3"},
{"maven", "https://repo1.maven.org/maven2"},
{"cocoapods", "https://trunk.cocoapods.org"},
{"clojars", "https://clojars.org"},
{"cpan", "https://fastapi.metacpan.org"},
{"hackage", "https://hackage.haskell.org"},
{"cran", "https://cran.r-project.org"},
{"conda", "https://api.anaconda.org"},
{"julia", "https://raw.githubusercontent.com/JuliaRegistries/General/master"},
{"elm", "https://package.elm-lang.org"},
{"dub", "https://code.dlang.org"},
{"luarocks", "https://luarocks.org"},
{"nimble", "https://nimble.directory"},
{"haxelib", "https://lib.haxe.org"},
{"deno", "https://apiland.deno.dev"},
{"terraform", "https://registry.terraform.io"},
{"githubactions", "https://api.github.com"},
{"vscode-extension", "https://open-vsx.org"},
{"helm", ""},
{"swift", ""},
{"apm", ""},
{"apk", ""},
{"deb", ""},
{"rpm", ""},
}
for _, tt := range tests {
t.Run(tt.ecosystem, func(t *testing.T) {
got := registries.DefaultURL(tt.ecosystem)
if got != tt.want {
t.Errorf("DefaultURL(%q) = %q, want %q", tt.ecosystem, got, tt.want)
}
})
}
}
func TestNewWithRequiredCustomURL(t *testing.T) {
reg, err := registries.New("helm", "https://charts.example.com", nil)
if err != nil {
t.Fatalf("New failed: %v", err)
}
if reg.Ecosystem() != "helm" {
t.Errorf("Ecosystem = %q, want helm", reg.Ecosystem())
}
_, err = registries.New("helm", "", nil)
if err == nil || err.Error() != "no registry URL configured for ecosystem: helm" {
t.Errorf("New without URL error = %v, want clear missing URL error", err)
}
}
func TestIntegration(t *testing.T) {
// Test with a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v1/crates/serde" {
resp := map[string]interface{}{
"crate": map[string]interface{}{
"id": "serde",
"name": "serde",
"description": "A serialization framework",
"repository": "https://github.com/serde-rs/serde",
},
"versions": []map[string]interface{}{
{
"id": 1,
"num": "1.0.0",
"license": "MIT",
"checksum": "abc123",
"yanked": false,
"created_at": "2023-01-01T00:00:00Z",
},
},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
return
}
w.WriteHeader(404)
}))
defer server.Close()
reg, err := registries.New("cargo", server.URL, registries.DefaultClient())
if err != nil {
t.Fatalf("New failed: %v", err)
}
// Test Ecosystem
if reg.Ecosystem() != "cargo" {
t.Errorf("expected ecosystem 'cargo', got %q", reg.Ecosystem())
}
// Test FetchPackage
pkg, err := reg.FetchPackage(context.Background(), "serde")
if err != nil {
t.Fatalf("FetchPackage failed: %v", err)
}
if pkg.Name != "serde" {
t.Errorf("expected name 'serde', got %q", pkg.Name)
}
if pkg.Repository != "https://github.com/serde-rs/serde" {
t.Errorf("unexpected repository: %q", pkg.Repository)
}
// Test URLs
urls := reg.URLs()
if urls.PURL("serde", "1.0.0") != "pkg:cargo/serde@1.0.0" {
t.Errorf("unexpected PURL: %q", urls.PURL("serde", "1.0.0"))
}
}
func TestBuildURLs(t *testing.T) {
reg, err := registries.New("cargo", "", nil)
if err != nil {
t.Fatalf("New failed: %v", err)
}
got := registries.BuildURLs(reg.URLs(), "serde", "1.0.0")
if got["registry"] == "" {
t.Error("expected non-empty registry URL")
}
if got["docs"] == "" {
t.Error("expected non-empty docs URL")
}
if got["purl"] != "pkg:cargo/serde@1.0.0" {
t.Errorf("purl = %q, want %q", got["purl"], "pkg:cargo/serde@1.0.0")
}
}
func TestBuildURLsCleansPURLConstraints(t *testing.T) {
reg, err := registries.New("npm", "", nil)
if err != nil {
t.Fatalf("New failed: %v", err)
}
tests := []struct {
name string
version string
want string
}{
{name: "exclusive lower bound", version: ">1.0.0", want: "pkg:npm/lodash@%3E1.0.0"},
{name: "union uses ecosystem ordering", version: "2.0.0 || 1.0.0", want: "pkg:npm/lodash@1.0.0"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := registries.BuildURLs(reg.URLs(), "lodash", test.version)
if got["purl"] != test.want {
t.Errorf("purl = %q, want %q", got["purl"], test.want)
}
})
}
}
func TestConstants(t *testing.T) {
// Verify constants are exported correctly
if registries.Runtime != "runtime" {
t.Errorf("Runtime constant mismatch")
}
if registries.Development != "development" {
t.Errorf("Development constant mismatch")
}
if registries.StatusYanked != "yanked" {
t.Errorf("StatusYanked constant mismatch")
}
}
func TestRootClientOptions(t *testing.T) {
custom := &http.Client{Timeout: 7 * time.Second}
c := registries.NewClient(registries.WithHTTPClient(custom))
if c.HTTPClient != custom {
t.Errorf("WithHTTPClient set HTTPClient to %p; want %p", c.HTTPClient, custom)
}
transport := http.DefaultTransport
c = registries.NewClient(registries.WithTransport(transport))
if c.HTTPClient.Transport != transport {
t.Errorf("WithTransport set Transport to %v; want %v", c.HTTPClient.Transport, transport)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
c = registries.NewClient(registries.WithSafeHTTP())
resp, err := c.HTTPClient.Get(server.URL)
if err == nil {
_ = resp.Body.Close()
t.Fatal("WithSafeHTTP should refuse a loopback address")
}
if !strings.Contains(err.Error(), "loopback") {
t.Errorf("WithSafeHTTP error %v should mention loopback", err)
}
}
func TestAPKFromPURL(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/alpine/v3.23/main/x86_64/APKINDEX.tar.gz" {
http.NotFound(w, r)
return
}
_, _ = w.Write(apkIndex(t, "P:curl\nV:8.22.0-r0\nA:x86_64\nt:1788539493\nD:libcurl=8.22.0-r0\n"))
}))
defer srv.Close()
purl := "pkg:apk/alpine/curl@8.22.0-r0?arch=x86_64&repository_url=" + srv.URL + "/alpine/v3.23/main/x86_64"
v, err := registries.FetchVersionFromPURL(context.Background(), purl, nil)
if err != nil {
t.Fatalf("FetchVersionFromPURL failed: %v", err)
}
if v.Number != "8.22.0-r0" || v.Artifacts[0].URL != srv.URL+"/alpine/v3.23/main/x86_64/curl-8.22.0-r0.apk" {
t.Errorf("version = %+v", v)
}
pkg, err := registries.FetchPackageFromPURL(context.Background(), purl, nil)
if err != nil || pkg.Namespace != "alpine" || pkg.Name != "curl" {
t.Errorf("package = %+v, %v", pkg, err)
}
deps, err := registries.FetchDependenciesFromPURL(context.Background(), purl, nil)
if err != nil || len(deps) != 1 || deps[0].Name != "libcurl" {
t.Errorf("deps = %+v, %v", deps, err)
}
if _, err := registries.FetchVersionFromPURL(context.Background(), "pkg:apk/alpine/curl@8.22.0-r0?arch=x86_64", nil); err == nil || !strings.Contains(err.Error(), "no registry URL configured") {
t.Errorf("missing URL err = %v", err)
}
}
func apkIndex(t *testing.T, index string) []byte {
t.Helper()
var out bytes.Buffer
gz := gzip.NewWriter(&out)
tw := tar.NewWriter(gz)
if err := tw.WriteHeader(&tar.Header{Name: "APKINDEX", Mode: 0o644, Size: int64(len(index))}); err != nil {
t.Fatal(err)
}
if _, err := tw.Write([]byte(index)); err != nil {
t.Fatal(err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
if err := gz.Close(); err != nil {
t.Fatal(err)
}
return out.Bytes()
}