github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/php/parse_pecl_pear_test.go (about)

     1  package php
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/artifact"
     8  	"github.com/anchore/syft/syft/file"
     9  	"github.com/anchore/syft/syft/pkg"
    10  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    11  )
    12  
    13  func TestParsePear(t *testing.T) {
    14  	ctx := context.TODO()
    15  	tests := []struct {
    16  		name                  string
    17  		fixture               string
    18  		expectedPkgs          []pkg.Package
    19  		expectedRelationships []artifact.Relationship
    20  	}{
    21  		{
    22  			name:    "v6 format",
    23  			fixture: "test-fixtures/memcached-v6-format.reg",
    24  			expectedPkgs: []pkg.Package{
    25  				{
    26  					Name:      "memcached",
    27  					Version:   "3.2.0",
    28  					PURL:      "pkg:pear/pecl.php.net/memcached@3.2.0",
    29  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/memcached-v6-format.reg")),
    30  					Licenses: pkg.NewLicenseSet(
    31  						pkg.NewLicenseFromLocationsWithContext(ctx, "PHP License", file.NewLocation("test-fixtures/memcached-v6-format.reg")),
    32  					),
    33  					Language: pkg.PHP,
    34  					Type:     pkg.PhpPearPkg,
    35  					Metadata: pkg.PhpPearEntry{
    36  						Name:    "memcached",
    37  						Channel: "pecl.php.net",
    38  						Version: "3.2.0",
    39  						License: []string{"PHP License"},
    40  					},
    41  				},
    42  			},
    43  		},
    44  		{
    45  			name:    "v5 format",
    46  			fixture: "test-fixtures/memcached-v5-format.reg",
    47  			expectedPkgs: []pkg.Package{
    48  				{
    49  					Name:      "memcached",
    50  					Version:   "3.2.0",
    51  					PURL:      "pkg:pear/pecl.php.net/memcached@3.2.0",
    52  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/memcached-v5-format.reg")),
    53  					Licenses: pkg.NewLicenseSet(
    54  						pkg.NewLicenseFromLocationsWithContext(ctx, "PHP License", file.NewLocation("test-fixtures/memcached-v5-format.reg")),
    55  					),
    56  					Language: pkg.PHP,
    57  					Type:     pkg.PhpPearPkg,
    58  					Metadata: pkg.PhpPearEntry{ // important: missing channel
    59  						Name:    "memcached",
    60  						Version: "3.2.0",
    61  						License: []string{"PHP License"},
    62  					},
    63  				},
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			pkgtest.TestFileParser(t, tt.fixture, parsePear, tt.expectedPkgs, tt.expectedRelationships)
    71  		})
    72  	}
    73  }
    74  
    75  func TestParsePecl(t *testing.T) {
    76  	ctx := context.TODO()
    77  	tests := []struct {
    78  		name                  string
    79  		fixture               string
    80  		expectedPkgs          []pkg.Package
    81  		expectedRelationships []artifact.Relationship
    82  	}{
    83  		{
    84  			name:    "v6 format",
    85  			fixture: "test-fixtures/memcached-v6-format.reg",
    86  			expectedPkgs: []pkg.Package{
    87  				{
    88  					Name:      "memcached",
    89  					Version:   "3.2.0",
    90  					PURL:      "pkg:pear/pecl.php.net/memcached@3.2.0",
    91  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/memcached-v6-format.reg")),
    92  					Licenses: pkg.NewLicenseSet(
    93  						pkg.NewLicenseFromLocationsWithContext(ctx, "PHP License", file.NewLocation("test-fixtures/memcached-v6-format.reg")),
    94  					),
    95  					Language: pkg.PHP,
    96  					Type:     pkg.PhpPeclPkg, // important!
    97  					Metadata: pkg.PhpPeclEntry{ // important!
    98  						Name:    "memcached",
    99  						Channel: "pecl.php.net",
   100  						Version: "3.2.0",
   101  						License: []string{"PHP License"},
   102  					},
   103  				},
   104  			},
   105  		},
   106  		{
   107  			name:    "v5 format",
   108  			fixture: "test-fixtures/memcached-v5-format.reg",
   109  			expectedPkgs: []pkg.Package{
   110  				{
   111  					Name:      "memcached",
   112  					Version:   "3.2.0",
   113  					PURL:      "pkg:pear/pecl.php.net/memcached@3.2.0",
   114  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/memcached-v5-format.reg")),
   115  					Licenses: pkg.NewLicenseSet(
   116  						pkg.NewLicenseFromLocationsWithContext(ctx, "PHP License", file.NewLocation("test-fixtures/memcached-v5-format.reg")),
   117  					),
   118  					Language: pkg.PHP,
   119  					Type:     pkg.PhpPeclPkg, // important!
   120  					Metadata: pkg.PhpPeclEntry{ // important!
   121  						Name:    "memcached",
   122  						Version: "3.2.0",
   123  						License: []string{"PHP License"},
   124  					},
   125  				},
   126  			},
   127  		},
   128  	}
   129  
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			pkgtest.TestFileParser(t, tt.fixture, parsePecl, tt.expectedPkgs, tt.expectedRelationships)
   133  		})
   134  	}
   135  }
   136  
   137  func Test_corruptPecl(t *testing.T) {
   138  	pkgtest.NewCatalogTester().
   139  		FromFile(t, "test-fixtures/glob-paths/php/.registry/.channel.pecl.php.net/memcached.reg").
   140  		WithError().
   141  		TestParser(t, parseComposerLock)
   142  }