github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/spdxjson/decoder_test.go (about) 1 package spdxjson 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/nextlinux/gosbom/gosbom/pkg" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestSPDXJSONDecoder(t *testing.T) { 14 tests := []struct { 15 path string 16 fail bool 17 packages []string 18 relationships []string 19 }{ 20 { 21 path: "alpine-3.10.gosbom.spdx.json", 22 packages: []string{"busybox", "libssl1.1", "ssl_client"}, 23 relationships: []string{"busybox", "busybox", "libssl1.1", "libssl1.1"}, 24 }, 25 { 26 path: "alpine-3.10.vendor.spdx.json", 27 packages: []string{"alpine", "busybox", "ssl_client"}, 28 relationships: []string{}, 29 }, 30 { 31 path: "example7-bin.spdx.json", 32 }, 33 { 34 path: "example7-go-module.spdx.json", 35 }, 36 { 37 path: "example7-golang.spdx.json", 38 }, 39 { 40 path: "example7-third-party-modules.spdx.json", 41 }, 42 { 43 path: "bad/example7-bin.spdx.json", 44 fail: true, 45 }, 46 { 47 path: "bad/example7-go-module.spdx.json", 48 fail: true, 49 }, 50 { 51 path: "bad/example7-golang.spdx.json", 52 fail: true, 53 }, 54 { 55 path: "bad/example7-third-party-modules.spdx.json", 56 fail: true, 57 }, 58 } 59 60 for _, test := range tests { 61 t.Run(test.path, func(t *testing.T) { 62 f, err := os.Open("test-fixtures/spdx/" + test.path) 63 require.NoError(t, err) 64 65 sbom, err := decoder(f) 66 67 if test.fail { 68 require.Error(t, err) 69 return 70 } else { 71 require.NoError(t, err) 72 } 73 74 if test.packages != nil { 75 assert.Equal(t, sbom.Artifacts.Packages.PackageCount(), len(test.packages)) 76 77 packages: 78 for _, pkgName := range test.packages { 79 for _, p := range sbom.Artifacts.Packages.Sorted() { 80 if p.Name == pkgName { 81 continue packages 82 } 83 } 84 assert.NoError(t, fmt.Errorf("Unable to find package: %s", pkgName)) 85 } 86 } 87 88 if test.relationships != nil { 89 assert.Len(t, sbom.Relationships, len(test.relationships)) 90 91 relationships: 92 for _, pkgName := range test.relationships { 93 for _, rel := range sbom.Relationships { 94 p, ok := rel.From.(*pkg.Package) 95 if ok && p.Name == pkgName { 96 continue relationships 97 } 98 } 99 assert.NoError(t, fmt.Errorf("Unable to find relationship: %s", pkgName)) 100 } 101 } 102 }) 103 } 104 }