github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/redhat/dependency_test.go (about)

     1  package redhat
     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.RpmDBEntry{
    23  					Provides: []string{"a-thing"},
    24  					Requires: []string{"b-thing"},
    25  				},
    26  			},
    27  			want: dependency.Specification{
    28  				Provides: []string{"package-c", "a-thing"},
    29  				Requires: []string{"b-thing"},
    30  			},
    31  		},
    32  		{
    33  			name: "strip unsupported keys",
    34  			p: pkg.Package{
    35  				Name: "package-a",
    36  				Metadata: pkg.RpmDBEntry{
    37  					Provides: []string{"libc.so.6(GLIBC_2.11)(64bit)"},
    38  					Requires: []string{"config(bash)", "(llvm if clang)"},
    39  				},
    40  			},
    41  			want: dependency.Specification{
    42  				Provides: []string{"package-a", "libc.so.6(GLIBC_2.11)(64bit)"},
    43  				Requires: []string{"config(bash)"},
    44  			},
    45  		},
    46  		{
    47  			name: "empty dependency data entries",
    48  			p: pkg.Package{
    49  				Name: "package-a",
    50  				Metadata: pkg.RpmDBEntry{
    51  					Provides: []string{""},
    52  					Requires: []string{""},
    53  				},
    54  			},
    55  			want: dependency.Specification{
    56  				Provides: []string{"package-a"},
    57  				Requires: nil,
    58  			},
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			assert.Equal(t, tt.want, dbEntryDependencySpecifier(tt.p))
    64  		})
    65  	}
    66  }
    67  
    68  func Test_isSupportedKey(t *testing.T) {
    69  
    70  	tests := []struct {
    71  		name string
    72  		key  string
    73  		want bool
    74  	}{
    75  		{
    76  			name: "paths allowed",
    77  			key:  "/usr/bin/sh",
    78  			want: true,
    79  		},
    80  		{
    81  			name: "spaces stripped",
    82  			key:  " filesystem ",
    83  			want: true,
    84  		},
    85  		{
    86  			name: "empty key",
    87  			key:  "",
    88  			want: true,
    89  		},
    90  		{
    91  			name: "boolean expression",
    92  			key:  "(pyproject-rpm-macros = 1.9.0-1.el9 if pyproject-rpm-macros)",
    93  			want: false,
    94  		},
    95  		{
    96  			name: "boolean expression with spaces stripped",
    97  			key:  " (llvm if clang)",
    98  			want: false,
    99  		},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			assert.Equal(t, tt.want, isSupportedKey(tt.key))
   104  		})
   105  	}
   106  }