github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/alpine/dependency_test.go (about)

     1  package alpine
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/pkg"
     9  	"github.com/anchore/syft/syft/pkg/cataloger/internal/dependency"
    10  )
    11  
    12  func Test_dbEntryDependencySpecifier(t *testing.T) {
    13  	tests := []struct {
    14  		name string
    15  		p    pkg.Package
    16  		want dependency.Specification
    17  	}{
    18  		{
    19  			name: "keeps given values + package name",
    20  			p: pkg.Package{
    21  				Name: "package-c",
    22  				Metadata: pkg.ApkDBEntry{
    23  					Provides:     []string{"a-thing"},
    24  					Dependencies: []string{"b-thing"},
    25  				},
    26  			},
    27  			want: dependency.Specification{
    28  				ProvidesRequires: dependency.ProvidesRequires{
    29  					Provides: []string{"package-c", "a-thing"},
    30  					Requires: []string{"b-thing"},
    31  				},
    32  			},
    33  		},
    34  		{
    35  			name: "strip version specifiers",
    36  			p: pkg.Package{
    37  				Name: "package-a",
    38  				Metadata: pkg.ApkDBEntry{
    39  					Provides:     []string{"so:libc.musl-x86_64.so.1=1"},
    40  					Dependencies: []string{"so:libc.musl-x86_64.so.2=2"},
    41  				},
    42  			},
    43  			want: dependency.Specification{
    44  				ProvidesRequires: dependency.ProvidesRequires{
    45  					Provides: []string{"package-a", "so:libc.musl-x86_64.so.1"},
    46  					Requires: []string{"so:libc.musl-x86_64.so.2"},
    47  				},
    48  			},
    49  		},
    50  		{
    51  			name: "empty dependency data entries",
    52  			p: pkg.Package{
    53  				Name: "package-a",
    54  				Metadata: pkg.ApkDBEntry{
    55  					Provides:     []string{""},
    56  					Dependencies: []string{""},
    57  				},
    58  			},
    59  			want: dependency.Specification{
    60  				ProvidesRequires: dependency.ProvidesRequires{
    61  					Provides: []string{"package-a"},
    62  					Requires: nil,
    63  				},
    64  			},
    65  		},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			assert.Equal(t, tt.want, dbEntryDependencySpecifier(tt.p))
    70  		})
    71  	}
    72  }
    73  
    74  func Test_stripVersionSpecifier(t *testing.T) {
    75  	tests := []struct {
    76  		name    string
    77  		version string
    78  		want    string
    79  	}{
    80  		{
    81  			name:    "empty expression",
    82  			version: "",
    83  			want:    "",
    84  		},
    85  		{
    86  			name:    "no expression",
    87  			version: "cmd:foo",
    88  			want:    "cmd:foo",
    89  		},
    90  		{
    91  			name:    "=",
    92  			version: "cmd:scanelf=1.3.4-r0",
    93  			want:    "cmd:scanelf",
    94  		},
    95  		{
    96  			name:    ">=",
    97  			version: "cmd:scanelf>=1.3.4-r0",
    98  			want:    "cmd:scanelf",
    99  		},
   100  		{
   101  			name:    "<",
   102  			version: "cmd:scanelf<1.3.4-r0",
   103  			want:    "cmd:scanelf",
   104  		},
   105  		{
   106  			name:    "ignores file paths",
   107  			version: "/bin/sh",
   108  			want:    "/bin/sh",
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			assert.Equal(t, tt.want, stripVersionSpecifier(tt.version))
   114  		})
   115  	}
   116  }