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