github.com/quay/claircore@v1.5.28/pkg/ovalutil/links_test.go (about) 1 package ovalutil 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/quay/goval-parser/oval" 9 ) 10 11 type linksTypeTestCase struct { 12 name string 13 want string 14 def oval.Definition 15 } 16 17 func TestLinksDeduplication(t *testing.T) { 18 testCases := []linksTypeTestCase{ 19 { 20 def: oval.Definition{ 21 References: []oval.Reference{ 22 { 23 RefURL: "", 24 }, 25 }, 26 Advisory: oval.Advisory{ 27 Refs: []oval.Ref{ 28 { 29 URL: "", 30 }, 31 }, 32 Bugs: []oval.Bug{ 33 { 34 URL: "", 35 }, 36 }, 37 Cves: []oval.Cve{ 38 { 39 Href: "", 40 }, 41 }, 42 }, 43 }, 44 want: "", 45 name: "No fields populated", 46 }, 47 { 48 def: oval.Definition{ 49 References: []oval.Reference{ 50 { 51 RefURL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 52 }, 53 }, 54 Advisory: oval.Advisory{ 55 Refs: []oval.Ref{ 56 { 57 URL: "https://access.redhat.com/errata/RHSA-2022:8832", 58 }, 59 }, 60 Bugs: []oval.Bug{ 61 { 62 URL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 63 }, 64 }, 65 Cves: []oval.Cve{ 66 { 67 Href: "https://access.redhat.com/security/cve/cve-2023-4380", 68 }, 69 }, 70 }, 71 }, 72 want: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708 https://access.redhat.com/errata/RHSA-2022:8832 https://access.redhat.com/security/cve/cve-2023-4380", 73 name: "All fields populated, one duplicate link", 74 }, 75 { 76 def: oval.Definition{ 77 References: []oval.Reference{ 78 { 79 RefURL: "https://access.redhat.com/errata/RHSA-2022:8832", 80 }, 81 }, 82 }, 83 want: "https://access.redhat.com/errata/RHSA-2022:8832", 84 name: "Just References", 85 }, 86 { 87 def: oval.Definition{ 88 References: []oval.Reference{ 89 { 90 RefURL: "", 91 }, 92 }, 93 Advisory: oval.Advisory{ 94 Cves: []oval.Cve{ 95 { 96 Href: "https://access.redhat.com/security/cve/cve-2023-4380", 97 }, 98 { 99 Href: "https://access.redhat.com/security/cve/cve-2023-4380", 100 }, 101 { 102 Href: "https://access.redhat.com/security/cve/cve-2023-4381", 103 }, 104 }, 105 }, 106 }, 107 want: "https://access.redhat.com/security/cve/cve-2023-4380 https://access.redhat.com/security/cve/cve-2023-4381", 108 name: "Blank References RefURL, multiple Cves", 109 }, 110 { 111 def: oval.Definition{ 112 References: []oval.Reference{ 113 { 114 RefURL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 115 }, 116 { 117 RefURL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37709", 118 }, 119 }, 120 Advisory: oval.Advisory{ 121 Refs: []oval.Ref{ 122 { 123 URL: "https://access.redhat.com/errata/RHSA-2022:8832", 124 }, 125 { 126 URL: "", 127 }, 128 { 129 URL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 130 }, 131 }, 132 Bugs: []oval.Bug{ 133 { 134 URL: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 135 }, 136 { 137 URL: "https://access.redhat.com/errata/RHSA-2022:8832", 138 }, 139 { 140 URL: "https://access.redhat.com/errata/RHSA-2022:8833", 141 }, 142 }, 143 Cves: []oval.Cve{ 144 { 145 Href: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708", 146 }, 147 { 148 Href: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37709", 149 }, 150 { 151 Href: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-1245", 152 }, 153 { 154 Href: "", 155 }, 156 }, 157 }, 158 }, 159 want: "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37708 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-37709 https://access.redhat.com/errata/RHSA-2022:8832 https://access.redhat.com/errata/RHSA-2022:8833 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-1245", 160 name: "All fields populated, with duplicates across multiple potential areas including blanks", 161 }, 162 } 163 164 for _, tc := range testCases { 165 got := strings.Split(Links(tc.def), " ") 166 want := strings.Split(tc.want, " ") 167 168 if !reflect.DeepEqual(want, got) { 169 t.Errorf("%q failed: want %q, got %q", tc.name, want, got) 170 } 171 } 172 }