github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/rust/cataloger_test.go (about)

     1  package rust
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/anchore/syft/syft/artifact"
     7  	"github.com/anchore/syft/syft/file"
     8  	"github.com/anchore/syft/syft/pkg"
     9  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    10  )
    11  
    12  func TestNewAuditBinaryCataloger(t *testing.T) {
    13  	locations := file.NewLocationSet(file.NewVirtualLocation("/usr/local/bin/hello_world", "/usr/local/bin/hello_world"))
    14  
    15  	argh := pkg.Package{
    16  		Name:      "argh",
    17  		Version:   "0.1.12",
    18  		PURL:      "pkg:cargo/argh@0.1.12",
    19  		FoundBy:   "cargo-auditable-binary-cataloger",
    20  		Locations: locations,
    21  		Language:  pkg.Rust,
    22  		Type:      pkg.RustPkg,
    23  		Metadata: pkg.RustBinaryAuditEntry{
    24  			Name:    "argh",
    25  			Version: "0.1.12",
    26  			Source:  "crates.io",
    27  		},
    28  	}
    29  
    30  	arghDerive := pkg.Package{
    31  		Name:      "argh_derive",
    32  		Version:   "0.1.12",
    33  		PURL:      "pkg:cargo/argh_derive@0.1.12",
    34  		FoundBy:   "cargo-auditable-binary-cataloger",
    35  		Locations: locations,
    36  		Language:  pkg.Rust,
    37  		Type:      pkg.RustPkg,
    38  		Metadata: pkg.RustBinaryAuditEntry{
    39  			Name:    "argh_derive",
    40  			Version: "0.1.12",
    41  			Source:  "crates.io",
    42  		},
    43  	}
    44  
    45  	arghShared := pkg.Package{
    46  		Name:      "argh_shared",
    47  		Version:   "0.1.12",
    48  		PURL:      "pkg:cargo/argh_shared@0.1.12",
    49  		FoundBy:   "cargo-auditable-binary-cataloger",
    50  		Locations: locations,
    51  		Language:  pkg.Rust,
    52  		Type:      pkg.RustPkg,
    53  		Metadata: pkg.RustBinaryAuditEntry{
    54  			Name:    "argh_shared",
    55  			Version: "0.1.12",
    56  			Source:  "crates.io",
    57  		},
    58  	}
    59  
    60  	helloWorld := pkg.Package{
    61  		Name:      "hello_world",
    62  		Version:   "0.1.0",
    63  		PURL:      "pkg:cargo/hello_world@0.1.0",
    64  		FoundBy:   "cargo-auditable-binary-cataloger",
    65  		Locations: locations,
    66  		Language:  pkg.Rust,
    67  		Type:      pkg.RustPkg,
    68  		Metadata: pkg.RustBinaryAuditEntry{
    69  			Name:    "hello_world",
    70  			Version: "0.1.0",
    71  			Source:  "local",
    72  		},
    73  	}
    74  
    75  	procMacro2 := pkg.Package{
    76  		Name:      "proc-macro2",
    77  		Version:   "1.0.92",
    78  		PURL:      "pkg:cargo/proc-macro2@1.0.92",
    79  		FoundBy:   "cargo-auditable-binary-cataloger",
    80  		Locations: locations,
    81  		Language:  pkg.Rust,
    82  		Type:      pkg.RustPkg,
    83  		Metadata: pkg.RustBinaryAuditEntry{
    84  			Name:    "proc-macro2",
    85  			Version: "1.0.92",
    86  			Source:  "crates.io",
    87  		},
    88  	}
    89  
    90  	quote := pkg.Package{
    91  		Name:      "quote",
    92  		Version:   "1.0.37",
    93  		PURL:      "pkg:cargo/quote@1.0.37",
    94  		FoundBy:   "cargo-auditable-binary-cataloger",
    95  		Locations: locations,
    96  		Language:  pkg.Rust,
    97  		Type:      pkg.RustPkg,
    98  		Metadata: pkg.RustBinaryAuditEntry{
    99  			Name:    "quote",
   100  			Version: "1.0.37",
   101  			Source:  "crates.io",
   102  		},
   103  	}
   104  
   105  	serde := pkg.Package{
   106  		Name:      "serde",
   107  		Version:   "1.0.215",
   108  		PURL:      "pkg:cargo/serde@1.0.215",
   109  		FoundBy:   "cargo-auditable-binary-cataloger",
   110  		Locations: locations,
   111  		Language:  pkg.Rust,
   112  		Type:      pkg.RustPkg,
   113  		Metadata: pkg.RustBinaryAuditEntry{
   114  			Name:    "serde",
   115  			Version: "1.0.215",
   116  			Source:  "crates.io",
   117  		},
   118  	}
   119  
   120  	serdeDerive := pkg.Package{
   121  		Name:      "serde_derive",
   122  		Version:   "1.0.215",
   123  		PURL:      "pkg:cargo/serde_derive@1.0.215",
   124  		FoundBy:   "cargo-auditable-binary-cataloger",
   125  		Locations: locations,
   126  		Language:  pkg.Rust,
   127  		Type:      pkg.RustPkg,
   128  		Metadata: pkg.RustBinaryAuditEntry{
   129  			Name:    "serde_derive",
   130  			Version: "1.0.215",
   131  			Source:  "crates.io",
   132  		},
   133  	}
   134  
   135  	syn := pkg.Package{
   136  		Name:      "syn",
   137  		Version:   "2.0.90",
   138  		PURL:      "pkg:cargo/syn@2.0.90",
   139  		FoundBy:   "cargo-auditable-binary-cataloger",
   140  		Locations: locations,
   141  		Language:  pkg.Rust,
   142  		Type:      pkg.RustPkg,
   143  		Metadata: pkg.RustBinaryAuditEntry{
   144  			Name:    "syn",
   145  			Version: "2.0.90",
   146  			Source:  "crates.io",
   147  		},
   148  	}
   149  
   150  	unicodeIdent := pkg.Package{
   151  		Name:      "unicode-ident",
   152  		Version:   "1.0.14",
   153  		PURL:      "pkg:cargo/unicode-ident@1.0.14",
   154  		FoundBy:   "cargo-auditable-binary-cataloger",
   155  		Locations: locations,
   156  		Language:  pkg.Rust,
   157  		Type:      pkg.RustPkg,
   158  		Metadata: pkg.RustBinaryAuditEntry{
   159  			Name:    "unicode-ident",
   160  			Version: "1.0.14",
   161  			Source:  "crates.io",
   162  		},
   163  	}
   164  
   165  	expectedPkgs := []pkg.Package{
   166  		argh,
   167  		arghDerive,
   168  		arghShared,
   169  		helloWorld,
   170  		procMacro2,
   171  		quote,
   172  		serde,
   173  		serdeDerive,
   174  		syn,
   175  		unicodeIdent,
   176  	}
   177  
   178  	expectedRelationships := []artifact.Relationship{
   179  		{
   180  			From: argh,
   181  			To:   helloWorld,
   182  			Type: artifact.DependencyOfRelationship,
   183  		},
   184  		{
   185  			From: arghDerive,
   186  			To:   argh,
   187  			Type: artifact.DependencyOfRelationship,
   188  		},
   189  		{
   190  			From: arghShared,
   191  			To:   argh,
   192  			Type: artifact.DependencyOfRelationship,
   193  		},
   194  		{
   195  			From: arghShared,
   196  			To:   arghDerive,
   197  			Type: artifact.DependencyOfRelationship,
   198  		},
   199  		{
   200  			From: procMacro2,
   201  			To:   arghDerive,
   202  			Type: artifact.DependencyOfRelationship,
   203  		},
   204  		{
   205  			From: procMacro2,
   206  			To:   quote,
   207  			Type: artifact.DependencyOfRelationship,
   208  		},
   209  		{
   210  			From: procMacro2,
   211  			To:   serdeDerive,
   212  			Type: artifact.DependencyOfRelationship,
   213  		},
   214  		{
   215  			From: procMacro2,
   216  			To:   syn,
   217  			Type: artifact.DependencyOfRelationship,
   218  		},
   219  		{
   220  			From: quote,
   221  			To:   arghDerive,
   222  			Type: artifact.DependencyOfRelationship,
   223  		},
   224  		{
   225  			From: quote,
   226  			To:   serdeDerive,
   227  			Type: artifact.DependencyOfRelationship,
   228  		},
   229  		{
   230  			From: quote,
   231  			To:   syn,
   232  			Type: artifact.DependencyOfRelationship,
   233  		},
   234  		{
   235  			From: serde,
   236  			To:   arghShared,
   237  			Type: artifact.DependencyOfRelationship,
   238  		},
   239  		{
   240  			From: serdeDerive,
   241  			To:   serde,
   242  			Type: artifact.DependencyOfRelationship,
   243  		},
   244  		{
   245  			From: syn,
   246  			To:   arghDerive,
   247  			Type: artifact.DependencyOfRelationship,
   248  		},
   249  		{
   250  			From: syn,
   251  			To:   serdeDerive,
   252  			Type: artifact.DependencyOfRelationship,
   253  		},
   254  		{
   255  			From: unicodeIdent,
   256  			To:   procMacro2,
   257  			Type: artifact.DependencyOfRelationship,
   258  		},
   259  		{
   260  			From: unicodeIdent,
   261  			To:   syn,
   262  			Type: artifact.DependencyOfRelationship,
   263  		},
   264  	}
   265  
   266  	pkgtest.NewCatalogTester().
   267  		WithImageResolver(t, "image-audit").
   268  		IgnoreLocationLayer(). // this fixture can be rebuilt, thus the layer ID will change
   269  		Expects(expectedPkgs, expectedRelationships).
   270  		TestCataloger(t, NewAuditBinaryCataloger())
   271  }
   272  
   273  func Test_CargoLockCataloger_Globs(t *testing.T) {
   274  	tests := []struct {
   275  		name     string
   276  		fixture  string
   277  		expected []string
   278  	}{
   279  		{
   280  			name:    "obtain Cargo.lock files",
   281  			fixture: "test-fixtures/glob-paths",
   282  			expected: []string{
   283  				"src/Cargo.lock",
   284  			},
   285  		},
   286  	}
   287  
   288  	for _, test := range tests {
   289  		t.Run(test.name, func(t *testing.T) {
   290  			pkgtest.NewCatalogTester().
   291  				FromDirectory(t, test.fixture).
   292  				ExpectsResolverContentQueries(test.expected).
   293  				TestCataloger(t, NewCargoLockCataloger())
   294  		})
   295  	}
   296  }
   297  
   298  func Test_AuditBinaryCataloger_Globs(t *testing.T) {
   299  	tests := []struct {
   300  		name     string
   301  		fixture  string
   302  		expected []string
   303  	}{
   304  		{
   305  			name:    "obtain audit binary files",
   306  			fixture: "test-fixtures/glob-paths",
   307  			expected: []string{
   308  				"partial-binary",
   309  			},
   310  		},
   311  	}
   312  
   313  	for _, test := range tests {
   314  		t.Run(test.name, func(t *testing.T) {
   315  			pkgtest.NewCatalogTester().
   316  				FromDirectory(t, test.fixture).
   317  				ExpectsResolverContentQueries(test.expected).
   318  				TestCataloger(t, NewAuditBinaryCataloger())
   319  		})
   320  	}
   321  }
   322  
   323  func Test_corruptAuditBinary(t *testing.T) {
   324  	pkgtest.NewCatalogTester().
   325  		FromFile(t, "test-fixtures/glob-paths/partial-binary").
   326  		WithError().
   327  		TestParser(t, parseAuditBinary)
   328  }