|
| 1 | +/* |
| 2 | +Copyright 2023 The OpenVEX Authors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package vex |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestProductMatches(t *testing.T) { |
| 16 | + for testCase, tc := range map[string]struct { |
| 17 | + sut *Product |
| 18 | + product string |
| 19 | + subcomponent string |
| 20 | + mustMach bool |
| 21 | + }{ |
| 22 | + "identifier only": { |
| 23 | + sut: &Product{ |
| 24 | + Component: Component{ ID: "pkg:apk/alpine/[email protected]"}, |
| 25 | + }, |
| 26 | + product: "pkg:apk/alpine/[email protected]", |
| 27 | + subcomponent: "", |
| 28 | + mustMach: true, |
| 29 | + }, |
| 30 | + "purl only": { |
| 31 | + sut: &Product{ |
| 32 | + Component: Component{Identifiers: map[IdentifierType]string{ |
| 33 | + PURL: "pkg:apk/alpine/[email protected]", |
| 34 | + }}, |
| 35 | + }, |
| 36 | + product: "pkg:apk/alpine/[email protected]", |
| 37 | + subcomponent: "", |
| 38 | + mustMach: true, |
| 39 | + }, |
| 40 | + "generic purl only": { |
| 41 | + sut: &Product{ |
| 42 | + Component: Component{Identifiers: map[IdentifierType]string{ |
| 43 | + PURL: "pkg:apk/alpine/libcrypto3", |
| 44 | + }}, |
| 45 | + }, |
| 46 | + product: "pkg:apk/alpine/[email protected]", |
| 47 | + subcomponent: "", |
| 48 | + mustMach: true, |
| 49 | + }, |
| 50 | + "identifier and components in doc and statement": { |
| 51 | + sut: &Product{ |
| 52 | + Component: Component{ID: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126"}, |
| 53 | + Subcomponents: []Subcomponent{ |
| 54 | + { |
| 55 | + Component{ ID: "pkg:apk/alpine/[email protected]"}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + product: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126", |
| 60 | + subcomponent: "pkg:apk/alpine/[email protected]", |
| 61 | + mustMach: true, |
| 62 | + }, |
| 63 | + "identifier and no components in query": { |
| 64 | + sut: &Product{ |
| 65 | + Component: Component{ID: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126"}, |
| 66 | + Subcomponents: []Subcomponent{ |
| 67 | + { |
| 68 | + Component{ ID: "pkg:apk/alpine/[email protected]"}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + product: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126", |
| 73 | + subcomponent: "", |
| 74 | + mustMach: true, |
| 75 | + }, |
| 76 | + "identifier and no components in document": { |
| 77 | + sut: &Product{ |
| 78 | + Component: Component{ID: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126"}, |
| 79 | + Subcomponents: []Subcomponent{}, |
| 80 | + }, |
| 81 | + product: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126", |
| 82 | + subcomponent: "pkg:apk/alpine/[email protected]", |
| 83 | + mustMach: true, |
| 84 | + }, |
| 85 | + "identifier + multicomponent doc": { |
| 86 | + sut: &Product{ |
| 87 | + Component: Component{ID: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126"}, |
| 88 | + Subcomponents: []Subcomponent{ |
| 89 | + { Component{ ID: "pkg:apk/alpine/[email protected]"}}, |
| 90 | + { Component{ ID: "pkg:apk/alpine/[email protected]"}}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + product: "pkg:oci/alpine@sha256%3A124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126", |
| 94 | + subcomponent: "pkg:apk/alpine/[email protected]", |
| 95 | + mustMach: true, |
| 96 | + }, |
| 97 | + } { |
| 98 | + require.Equal(t, tc.mustMach, tc.sut.Matches(tc.product, tc.subcomponent), fmt.Sprintf("failed: %s", testCase)) |
| 99 | + } |
| 100 | +} |
0 commit comments