github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/arch/package_test.go (about)

     1  package arch
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/sergi/go-diff/diffmatchpatch"
     7  
     8  	"github.com/anchore/packageurl-go"
     9  	"github.com/anchore/syft/syft/linux"
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  func Test_PackageURL(t *testing.T) {
    14  	tests := []struct {
    15  		name     string
    16  		metadata *parsedData
    17  		distro   linux.Release
    18  		expected string
    19  	}{
    20  		{
    21  			name: "bad distro id",
    22  			metadata: &parsedData{
    23  				Licenses: "",
    24  				AlpmDBEntry: pkg.AlpmDBEntry{
    25  					Package:      "p",
    26  					Version:      "v",
    27  					Architecture: "a",
    28  				},
    29  			},
    30  			distro: linux.Release{
    31  				ID:      "something-else",
    32  				BuildID: "rolling",
    33  			},
    34  			expected: "",
    35  		},
    36  		{
    37  			name: "gocase",
    38  			metadata: &parsedData{
    39  				Licenses: "",
    40  				AlpmDBEntry: pkg.AlpmDBEntry{
    41  					Package:      "p",
    42  					Version:      "v",
    43  					Architecture: "a",
    44  				},
    45  			},
    46  			distro: linux.Release{
    47  				ID:      "arch",
    48  				BuildID: "rolling",
    49  			},
    50  			expected: "pkg:alpm/arch/p@v?arch=a&distro=arch-rolling",
    51  		},
    52  		{
    53  			name: "missing architecture",
    54  			metadata: &parsedData{
    55  				Licenses: "",
    56  				AlpmDBEntry: pkg.AlpmDBEntry{
    57  					Package: "p",
    58  					Version: "v",
    59  				},
    60  			},
    61  			distro: linux.Release{
    62  				ID: "arch",
    63  			},
    64  			expected: "pkg:alpm/arch/p@v?distro=arch",
    65  		},
    66  		{
    67  			metadata: &parsedData{
    68  				Licenses: "",
    69  				AlpmDBEntry: pkg.AlpmDBEntry{
    70  					Package:      "python",
    71  					Version:      "3.10.0",
    72  					Architecture: "any",
    73  				},
    74  			},
    75  			distro: linux.Release{
    76  				ID:      "arch",
    77  				BuildID: "rolling",
    78  			},
    79  			expected: "pkg:alpm/arch/python@3.10.0?arch=any&distro=arch-rolling",
    80  		},
    81  		{
    82  			metadata: &parsedData{
    83  				Licenses: "",
    84  				AlpmDBEntry: pkg.AlpmDBEntry{
    85  					Package:      "g plus plus",
    86  					Version:      "v84",
    87  					Architecture: "x86_64",
    88  				},
    89  			},
    90  			distro: linux.Release{
    91  				ID:      "arch",
    92  				BuildID: "rolling",
    93  			},
    94  			expected: "pkg:alpm/arch/g%20plus%20plus@v84?arch=x86_64&distro=arch-rolling",
    95  		},
    96  		{
    97  			name: "add source information as qualifier",
    98  			metadata: &parsedData{
    99  				Licenses: "",
   100  				AlpmDBEntry: pkg.AlpmDBEntry{
   101  					Package:      "p",
   102  					Version:      "v",
   103  					Architecture: "a",
   104  					BasePackage:  "origin",
   105  				},
   106  			},
   107  			distro: linux.Release{
   108  				ID:      "arch",
   109  				BuildID: "rolling",
   110  			},
   111  			expected: "pkg:alpm/arch/p@v?arch=a&upstream=origin&distro=arch-rolling",
   112  		},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		t.Run(test.name, func(t *testing.T) {
   117  			actual := packageURL(test.metadata, &test.distro)
   118  			if actual != test.expected {
   119  				dmp := diffmatchpatch.New()
   120  				diffs := dmp.DiffMain(test.expected, actual, true)
   121  				t.Errorf("diff: %s", dmp.DiffPrettyText(diffs))
   122  			}
   123  
   124  			if test.expected == "" {
   125  				return
   126  			}
   127  
   128  			// verify packageurl can parse
   129  			purl, err := packageurl.FromString(actual)
   130  			if err != nil {
   131  				t.Errorf("cannot re-parse purl: %s", actual)
   132  			}
   133  			if purl.Name != test.metadata.Package {
   134  				dmp := diffmatchpatch.New()
   135  				diffs := dmp.DiffMain(test.metadata.Package, purl.Name, true)
   136  				t.Errorf("invalid purl name: %s", dmp.DiffPrettyText(diffs))
   137  			}
   138  			if purl.Version != test.metadata.Version {
   139  				dmp := diffmatchpatch.New()
   140  				diffs := dmp.DiffMain(test.metadata.Version, purl.Version, true)
   141  				t.Errorf("invalid purl version: %s", dmp.DiffPrettyText(diffs))
   142  			}
   143  			if purl.Qualifiers.Map()["arch"] != test.metadata.Architecture {
   144  				dmp := diffmatchpatch.New()
   145  				diffs := dmp.DiffMain(test.metadata.Architecture, purl.Qualifiers.Map()["arch"], true)
   146  				t.Errorf("invalid purl architecture: %s", dmp.DiffPrettyText(diffs))
   147  			}
   148  		})
   149  	}
   150  }