|
| 1 | +package mirror |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPURLSourceLatestMirrorsNewestVersion(t *testing.T) { |
| 13 | + versions := &fakeVersions{versions: []string{"1.0.0", "1.10.0", "1.2.0"}} |
| 14 | + source := &PURLSource{PURLs: []string{"pkg:npm/left-pad", "pkg:npm/is-odd@3.0.0"}, Versions: versions, Latest: true} |
| 15 | + |
| 16 | + items := enumerate(t, source) |
| 17 | + |
| 18 | + want := []PackageVersion{ |
| 19 | + {Ecosystem: "npm", Name: "left-pad", Version: "1.10.0"}, |
| 20 | + {Ecosystem: "npm", Name: "is-odd", Version: "3.0.0"}, |
| 21 | + } |
| 22 | + if !reflect.DeepEqual(items, want) { |
| 23 | + t.Errorf("items = %v, want %v", items, want) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestPURLSourceLatestWithoutVersions(t *testing.T) { |
| 28 | + source := &PURLSource{PURLs: []string{"pkg:npm/left-pad"}, Versions: &fakeVersions{}, Latest: true} |
| 29 | + if items := enumerate(t, source); len(items) != 0 { |
| 30 | + t.Errorf("items = %v, want none", items) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestSBOMSourceLatest(t *testing.T) { |
| 35 | + bom := map[string]any{ |
| 36 | + "bomFormat": "CycloneDX", |
| 37 | + "specVersion": "1.4", |
| 38 | + "components": []map[string]any{{"type": "library", "name": "left-pad", "purl": "pkg:npm/left-pad"}}, |
| 39 | + } |
| 40 | + source := &SBOMSource{Data: marshalJSON(t, bom), Versions: &fakeVersions{versions: []string{"1.0.0", "1.3.0"}}, Latest: true} |
| 41 | + |
| 42 | + items := enumerate(t, source) |
| 43 | + |
| 44 | + if len(items) != 1 || items[0].Version != "1.3.0" { |
| 45 | + t.Errorf("items = %v, want only left-pad@1.3.0", items) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// TestRegistryVersionsLatestSkipsYanked checks that without an advertised |
| 50 | +// latest version the newest version by publish time that isn't yanked wins. |
| 51 | +func TestRegistryVersionsLatestSkipsYanked(t *testing.T) { |
| 52 | + registry := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | + if r.URL.Path != "/api/v1/crates/demo" { |
| 54 | + http.NotFound(w, r) |
| 55 | + return |
| 56 | + } |
| 57 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 58 | + "crate": map[string]any{"name": "demo"}, |
| 59 | + "versions": []map[string]any{ |
| 60 | + {"num": "1.3.0", "yanked": true, "created_at": "2026-03-01T00:00:00Z"}, |
| 61 | + {"num": "1.2.0", "created_at": "2026-02-01T00:00:00Z"}, |
| 62 | + {"num": "1.10.0-alpha.1", "created_at": "2026-01-01T00:00:00Z"}, |
| 63 | + }, |
| 64 | + }) |
| 65 | + })) |
| 66 | + defer registry.Close() |
| 67 | + versions := RegistryVersions{URL: func(string, string) (string, error) { return registry.URL, nil }} |
| 68 | + |
| 69 | + got, err := versions.LatestVersion(context.Background(), "cargo", "demo") |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("LatestVersion() error = %v", err) |
| 72 | + } |
| 73 | + if got != "1.2.0" { |
| 74 | + t.Errorf("LatestVersion() = %q, want 1.2.0", got) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// TestRegistryVersionsLatestPrefersAdvertised checks that the version the |
| 79 | +// registry advertises as latest wins over a higher one, here a 4.0.0 |
| 80 | +// published by mistake and deprecated. |
| 81 | +func TestRegistryVersionsLatestPrefersAdvertised(t *testing.T) { |
| 82 | + registry := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 83 | + if r.URL.Path != "/left-pad" { |
| 84 | + http.NotFound(w, r) |
| 85 | + return |
| 86 | + } |
| 87 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 88 | + "name": "left-pad", |
| 89 | + "dist-tags": map[string]any{"latest": "3.4.0"}, |
| 90 | + "versions": map[string]any{ |
| 91 | + "3.4.0": map[string]any{"name": "left-pad", "version": "3.4.0"}, |
| 92 | + "4.0.0": map[string]any{"name": "left-pad", "version": "4.0.0", "deprecated": "published by mistake"}, |
| 93 | + }, |
| 94 | + "time": map[string]any{"3.4.0": "2026-01-01T00:00:00Z", "4.0.0": "2026-02-01T00:00:00Z"}, |
| 95 | + }) |
| 96 | + })) |
| 97 | + defer registry.Close() |
| 98 | + versions := RegistryVersions{URL: func(string, string) (string, error) { return registry.URL, nil }} |
| 99 | + |
| 100 | + got, err := versions.LatestVersion(context.Background(), "npm", "left-pad") |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("LatestVersion() error = %v", err) |
| 103 | + } |
| 104 | + if got != "3.4.0" { |
| 105 | + t.Errorf("LatestVersion() = %q, want the advertised 3.4.0", got) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestSourceFromRequestPassesLatest(t *testing.T) { |
| 110 | + js := NewJobStore(context.Background(), setupTestMirror(t, 1)) |
| 111 | + |
| 112 | + source, err := js.sourceFromRequest(JobRequest{PURLs: []string{"pkg:npm/left-pad"}, Latest: true}) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("sourceFromRequest() error = %v", err) |
| 115 | + } |
| 116 | + if !source.(*PURLSource).Latest { |
| 117 | + t.Error("PURL source mirrors every version, want only the newest") |
| 118 | + } |
| 119 | + |
| 120 | + source, err = js.sourceFromRequest(JobRequest{SBOM: json.RawMessage(`{"bomFormat":"CycloneDX","components":[]}`), Latest: true}) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("sourceFromRequest() error = %v", err) |
| 123 | + } |
| 124 | + if !source.(*SBOMSource).Latest { |
| 125 | + t.Error("SBOM source mirrors every version, want only the newest") |
| 126 | + } |
| 127 | +} |
0 commit comments