■ ■ ■ ■ ■ ■
grype/pkg/qualifier/platformcpe/qualifier_test.go
| 1 | + | package platformcpe |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "testing" |
| 5 | + | |
| 6 | + | "github.com/stretchr/testify/assert" |
| 7 | + | |
| 8 | + | "github.com/anchore/grype/grype/distro" |
| 9 | + | "github.com/anchore/grype/grype/pkg" |
| 10 | + | "github.com/anchore/grype/grype/pkg/qualifier" |
| 11 | + | ) |
| 12 | + | |
| 13 | + | func TestPlatformCPE_Satisfied(t *testing.T) { |
| 14 | + | tests := []struct { |
| 15 | + | name string |
| 16 | + | platformCPE qualifier.Qualifier |
| 17 | + | pkg pkg.Package |
| 18 | + | distro *distro.Distro |
| 19 | + | satisfied bool |
| 20 | + | hasError bool |
| 21 | + | }{ |
| 22 | + | { |
| 23 | + | name: "no filter on nil distro", |
| 24 | + | platformCPE: New("cpe:2.3:o:microsoft:windows:-:*:*:*:*:*:*:*"), |
| 25 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 26 | + | distro: nil, |
| 27 | + | satisfied: true, |
| 28 | + | hasError: false, |
| 29 | + | }, |
| 30 | + | { |
| 31 | + | name: "no filter when platform CPE is empty", |
| 32 | + | platformCPE: New(""), |
| 33 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 34 | + | distro: &distro.Distro{Type: distro.Windows}, |
| 35 | + | satisfied: true, |
| 36 | + | hasError: false, |
| 37 | + | }, |
| 38 | + | { |
| 39 | + | name: "no filter when platform CPE is invalid", |
| 40 | + | platformCPE: New(";;;"), |
| 41 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 42 | + | distro: &distro.Distro{Type: distro.Windows}, |
| 43 | + | satisfied: true, |
| 44 | + | hasError: true, |
| 45 | + | }, |
| 46 | + | // Windows |
| 47 | + | { |
| 48 | + | name: "filter windows platform vuln when distro is not windows", |
| 49 | + | platformCPE: New("cpe:2.3:o:microsoft:windows:-:*:*:*:*:*:*:*"), |
| 50 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 51 | + | distro: &distro.Distro{Type: distro.Debian}, |
| 52 | + | satisfied: false, |
| 53 | + | hasError: false, |
| 54 | + | }, |
| 55 | + | { |
| 56 | + | name: "filter windows server platform vuln when distro is not windows", |
| 57 | + | platformCPE: New("cpe:2.3:o:microsoft:windows_server_2022:-:*:*:*:*:*:*:*"), |
| 58 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 59 | + | distro: &distro.Distro{Type: distro.Debian}, |
| 60 | + | satisfied: false, |
| 61 | + | hasError: false, |
| 62 | + | }, |
| 63 | + | { |
| 64 | + | name: "no filter windows platform vuln when distro is windows", |
| 65 | + | platformCPE: New("cpe:2.3:o:microsoft:windows:-:*:*:*:*:*:*:*"), |
| 66 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 67 | + | distro: &distro.Distro{Type: distro.Windows}, |
| 68 | + | satisfied: true, |
| 69 | + | hasError: false, |
| 70 | + | }, |
| 71 | + | { |
| 72 | + | name: "no filter windows server platform vuln when distro is windows", |
| 73 | + | platformCPE: New("cpe:2.3:o:microsoft:windows_server_2022:-:*:*:*:*:*:*:*"), |
| 74 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 75 | + | distro: &distro.Distro{Type: distro.Windows}, |
| 76 | + | satisfied: true, |
| 77 | + | hasError: false, |
| 78 | + | }, |
| 79 | + | // Debian |
| 80 | + | { |
| 81 | + | name: "filter debian platform vuln when distro is not debian", |
| 82 | + | platformCPE: New("cpe:2.3:o:debian:debian_linux:-:*:*:*:*:*:*:*"), |
| 83 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 84 | + | distro: &distro.Distro{Type: distro.Ubuntu}, |
| 85 | + | satisfied: false, |
| 86 | + | hasError: false, |
| 87 | + | }, |
| 88 | + | { |
| 89 | + | name: "filter debian platform vuln when distro is not debian (alternate encountered cpe)", |
| 90 | + | platformCPE: New("cpe:2.3:o:debian:linux:-:*:*:*:*:*:*:*"), |
| 91 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 92 | + | distro: &distro.Distro{Type: distro.SLES}, |
| 93 | + | satisfied: false, |
| 94 | + | hasError: false, |
| 95 | + | }, |
| 96 | + | { |
| 97 | + | name: "no filter debian platform vuln when distro is debian", |
| 98 | + | platformCPE: New("cpe:2.3:o:debian:debian_linux:-:*:*:*:*:*:*:*"), |
| 99 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 100 | + | distro: &distro.Distro{Type: distro.Debian}, |
| 101 | + | satisfied: true, |
| 102 | + | hasError: false, |
| 103 | + | }, |
| 104 | + | { |
| 105 | + | name: "no filter debian platform vuln when distro is debian (alternate encountered cpe)", |
| 106 | + | platformCPE: New("cpe:2.3:o:debian:linux:-:*:*:*:*:*:*:*"), |
| 107 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 108 | + | distro: &distro.Distro{Type: distro.Debian}, |
| 109 | + | satisfied: true, |
| 110 | + | hasError: false, |
| 111 | + | }, |
| 112 | + | // Ubuntu |
| 113 | + | { |
| 114 | + | name: "filter ubuntu platform vuln when distro is not ubuntu", |
| 115 | + | platformCPE: New("cpe:2.3:o:canonical:ubuntu_linux:-:*:*:*:*:*:*:*"), |
| 116 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 117 | + | distro: &distro.Distro{Type: distro.SLES}, |
| 118 | + | satisfied: false, |
| 119 | + | hasError: false, |
| 120 | + | }, |
| 121 | + | { |
| 122 | + | name: "filter ubuntu platform vuln when distro is not ubuntu (alternate encountered cpe)", |
| 123 | + | platformCPE: New("cpe:2.3:o:ubuntu:vivid:-:*:*:*:*:*:*:*"), |
| 124 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 125 | + | distro: &distro.Distro{Type: distro.Alpine}, |
| 126 | + | satisfied: false, |
| 127 | + | hasError: false, |
| 128 | + | }, |
| 129 | + | { |
| 130 | + | name: "no filter ubuntu platform vuln when distro is ubuntu", |
| 131 | + | platformCPE: New("cpe:2.3:o:canonical:ubuntu_linux:-:*:*:*:*:*:*:*"), |
| 132 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 133 | + | distro: &distro.Distro{Type: distro.Ubuntu}, |
| 134 | + | satisfied: true, |
| 135 | + | hasError: false, |
| 136 | + | }, |
| 137 | + | { |
| 138 | + | name: "no filter ubuntu platform vuln when distro is ubuntu (alternate encountered cpe)", |
| 139 | + | platformCPE: New("cpe:2.3:o:ubuntu:vivid:-:*:*:*:*:*:*:*"), |
| 140 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 141 | + | distro: &distro.Distro{Type: distro.Ubuntu}, |
| 142 | + | satisfied: true, |
| 143 | + | hasError: false, |
| 144 | + | }, |
| 145 | + | // Wordpress |
| 146 | + | { |
| 147 | + | name: "always filter wordpress platform vulns (no known distro)", |
| 148 | + | platformCPE: New("cpe:2.3:o:wordpress:wordpress:-:*:*:*:*:*:*:*"), |
| 149 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 150 | + | distro: nil, |
| 151 | + | satisfied: false, |
| 152 | + | hasError: false, |
| 153 | + | }, |
| 154 | + | { |
| 155 | + | name: "always filter wordpress platform vulns (known distro)", |
| 156 | + | platformCPE: New("cpe:2.3:o:ubuntu:vivid:-:*:*:*:*:*:*:*"), |
| 157 | + | pkg: pkg.Package{MetadataType: pkg.UnknownMetadataType}, |
| 158 | + | distro: &distro.Distro{Type: distro.Alpine}, |
| 159 | + | satisfied: false, |
| 160 | + | hasError: false, |
| 161 | + | }, |
| 162 | + | } |
| 163 | + | |
| 164 | + | for _, test := range tests { |
| 165 | + | t.Run(test.name, func(t *testing.T) { |
| 166 | + | s, err := test.platformCPE.Satisfied(test.distro, test.pkg) |
| 167 | + | |
| 168 | + | if test.hasError { |
| 169 | + | assert.Error(t, err) |
| 170 | + | } else { |
| 171 | + | assert.NoError(t, err) |
| 172 | + | } |
| 173 | + | |
| 174 | + | assert.Equal(t, test.satisfied, s) |
| 175 | + | }) |
| 176 | + | } |
| 177 | + | } |
| 178 | + | |