github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/spdxjson/decoder_test.go (about)

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