github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/common/spdxhelpers/source_info_test.go (about) 1 package spdxhelpers 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/anchore/syft/syft/file" 9 "github.com/anchore/syft/syft/pkg" 10 ) 11 12 func Test_SourceInfo(t *testing.T) { 13 tests := []struct { 14 name string 15 input pkg.Package 16 expected []string 17 }{ 18 { 19 name: "locations are captured", 20 input: pkg.Package{ 21 // note: no type given 22 Locations: file.NewLocationSet( 23 file.NewVirtualLocation("/a-place", "/b-place"), 24 file.NewVirtualLocation("/c-place", "/d-place"), 25 ), 26 }, 27 expected: []string{ 28 "from the following paths", 29 "/a-place", 30 "/c-place", 31 }, 32 }, 33 { 34 // note: no specific support for this 35 input: pkg.Package{ 36 Type: pkg.KbPkg, 37 }, 38 expected: []string{ 39 "from the following paths", 40 }, 41 }, 42 { 43 input: pkg.Package{ 44 Type: pkg.RpmPkg, 45 }, 46 expected: []string{ 47 "from RPM DB", 48 }, 49 }, 50 { 51 input: pkg.Package{ 52 Type: pkg.ApkPkg, 53 }, 54 expected: []string{ 55 "from APK DB", 56 }, 57 }, 58 { 59 input: pkg.Package{ 60 Type: pkg.DebPkg, 61 }, 62 expected: []string{ 63 "from DPKG DB", 64 }, 65 }, 66 { 67 input: pkg.Package{ 68 Type: pkg.NpmPkg, 69 }, 70 expected: []string{ 71 "from installed node module manifest file", 72 }, 73 }, 74 { 75 input: pkg.Package{ 76 Type: pkg.PythonPkg, 77 }, 78 expected: []string{ 79 "from installed python package manifest file", 80 }, 81 }, 82 { 83 input: pkg.Package{ 84 Type: pkg.JavaPkg, 85 }, 86 expected: []string{ 87 "from installed java archive", 88 }, 89 }, 90 { 91 input: pkg.Package{ 92 Type: pkg.JenkinsPluginPkg, 93 }, 94 expected: []string{ 95 "from installed java archive", 96 }, 97 }, 98 { 99 input: pkg.Package{ 100 Type: pkg.GemPkg, 101 }, 102 expected: []string{ 103 "from installed gem metadata file", 104 }, 105 }, 106 { 107 input: pkg.Package{ 108 Type: pkg.GoModulePkg, 109 }, 110 expected: []string{ 111 "from go module information", 112 }, 113 }, 114 { 115 input: pkg.Package{ 116 Type: pkg.RustPkg, 117 }, 118 expected: []string{ 119 "from rust cargo manifest", 120 }, 121 }, 122 { 123 input: pkg.Package{ 124 Type: pkg.PhpComposerPkg, 125 }, 126 expected: []string{ 127 "from PHP composer manifest", 128 }, 129 }, 130 { 131 input: pkg.Package{ 132 Type: pkg.DartPubPkg, 133 }, 134 expected: []string{ 135 "from pubspec manifest", 136 }, 137 }, 138 { 139 input: pkg.Package{ 140 Type: pkg.DotnetPkg, 141 }, 142 expected: []string{ 143 "from dotnet project assets file", 144 }, 145 }, 146 { 147 input: pkg.Package{ 148 Type: pkg.AlpmPkg, 149 }, 150 expected: []string{ 151 "from ALPM DB", 152 }, 153 }, 154 { 155 input: pkg.Package{ 156 Type: pkg.CocoapodsPkg, 157 }, 158 expected: []string{ 159 "installed cocoapods manifest file", 160 }, 161 }, 162 { 163 input: pkg.Package{ 164 Type: pkg.ConanPkg, 165 }, 166 expected: []string{ 167 "from conan manifest", 168 }, 169 }, 170 { 171 input: pkg.Package{ 172 Type: pkg.PortagePkg, 173 }, 174 expected: []string{ 175 "from portage DB", 176 }, 177 }, 178 { 179 input: pkg.Package{ 180 Type: pkg.HackagePkg, 181 }, 182 expected: []string{ 183 "from cabal or stack manifest files", 184 }, 185 }, 186 { 187 input: pkg.Package{ 188 Type: pkg.BinaryPkg, 189 }, 190 expected: []string{ 191 "acquired package info from the following paths", 192 }, 193 }, 194 { 195 input: pkg.Package{ 196 Type: pkg.HexPkg, 197 }, 198 expected: []string{ 199 "from rebar3 or mix manifest file", 200 }, 201 }, 202 { 203 input: pkg.Package{ 204 Type: pkg.LinuxKernelPkg, 205 }, 206 expected: []string{ 207 "from linux kernel archive", 208 }, 209 }, 210 { 211 input: pkg.Package{ 212 Type: pkg.LinuxKernelModulePkg, 213 }, 214 expected: []string{ 215 "from linux kernel module files", 216 }, 217 }, 218 { 219 input: pkg.Package{ 220 Type: pkg.NixPkg, 221 }, 222 expected: []string{ 223 "from nix store path", 224 }, 225 }, 226 { 227 input: pkg.Package{ 228 Type: pkg.Rpkg, 229 }, 230 expected: []string{ 231 "acquired package info from R-package DESCRIPTION file", 232 }, 233 }, 234 { 235 input: pkg.Package{ 236 Type: pkg.SwiftPkg, 237 }, 238 expected: []string{ 239 "from resolved Swift package manifest", 240 }, 241 }, 242 { 243 input: pkg.Package{ 244 Type: pkg.GithubActionPkg, 245 }, 246 expected: []string{ 247 "from GitHub Actions workflow file or composite action file", 248 }, 249 }, 250 { 251 input: pkg.Package{ 252 Type: pkg.GithubActionWorkflowPkg, 253 }, 254 expected: []string{ 255 "from GitHub Actions workflow file or composite action file", 256 }, 257 }, 258 } 259 var pkgTypes []pkg.Type 260 for _, test := range tests { 261 t.Run(test.name+" "+string(test.input.Type), func(t *testing.T) { 262 if test.input.Type != "" { 263 pkgTypes = append(pkgTypes, test.input.Type) 264 } 265 actual := SourceInfo(test.input) 266 for _, expected := range test.expected { 267 assert.Contains(t, actual, expected) 268 } 269 }) 270 } 271 assert.ElementsMatch(t, pkg.AllPkgs, pkgTypes, "missing one or more package types to test against (maybe a package type was added?)") 272 }