github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/detector/ospkg/suse/suse_test.go (about)

     1  package suse_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	fake "k8s.io/utils/clock/testing"
    10  
    11  	"github.com/aquasecurity/trivy-db/pkg/db"
    12  	dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
    13  	"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
    14  	"github.com/devseccon/trivy/pkg/dbtest"
    15  	"github.com/devseccon/trivy/pkg/detector/ospkg/suse"
    16  	ftypes "github.com/devseccon/trivy/pkg/fanal/types"
    17  	"github.com/devseccon/trivy/pkg/types"
    18  )
    19  
    20  func TestScanner_Detect(t *testing.T) {
    21  	type args struct {
    22  		osVer string
    23  		pkgs  []ftypes.Package
    24  	}
    25  	tests := []struct {
    26  		name         string
    27  		args         args
    28  		fixtures     []string
    29  		distribution suse.Type
    30  		want         []types.DetectedVulnerability
    31  		wantErr      string
    32  	}{
    33  		{
    34  			name: "happy path",
    35  			fixtures: []string{
    36  				"testdata/fixtures/suse.yaml",
    37  				"testdata/fixtures/data-source.yaml",
    38  			},
    39  			distribution: suse.OpenSUSE,
    40  			args: args{
    41  				osVer: "15.3",
    42  				pkgs: []ftypes.Package{
    43  					{
    44  						Name:       "postgresql",
    45  						Version:    "13",
    46  						Release:    "4.6.6",
    47  						SrcName:    "postgresql",
    48  						SrcVersion: "13",
    49  						SrcRelease: "4.6.6",
    50  						Layer: ftypes.Layer{
    51  							DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
    52  						},
    53  					},
    54  				},
    55  			},
    56  			want: []types.DetectedVulnerability{
    57  				{
    58  					PkgName:          "postgresql",
    59  					VulnerabilityID:  "SUSE-SU-2021:0175-1",
    60  					InstalledVersion: "13-4.6.6",
    61  					FixedVersion:     "13-4.6.7",
    62  					Layer: ftypes.Layer{
    63  						DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
    64  					},
    65  					DataSource: &dbTypes.DataSource{
    66  						ID:   vulnerability.SuseCVRF,
    67  						Name: "SUSE CVRF",
    68  						URL:  "https://ftp.suse.com/pub/projects/security/cvrf/",
    69  					},
    70  				},
    71  			},
    72  		},
    73  		{
    74  			name: "broken bucket",
    75  			fixtures: []string{
    76  				"testdata/fixtures/invalid.yaml",
    77  				"testdata/fixtures/data-source.yaml",
    78  			},
    79  			distribution: suse.SUSEEnterpriseLinux,
    80  			args: args{
    81  				osVer: "15.3",
    82  				pkgs: []ftypes.Package{
    83  					{
    84  						Name:       "jq",
    85  						Version:    "1.6-r0",
    86  						SrcName:    "jq",
    87  						SrcVersion: "1.6-r0",
    88  					},
    89  				},
    90  			},
    91  			wantErr: "failed to get SUSE advisories",
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			_ = dbtest.InitDB(t, tt.fixtures)
    97  			defer db.Close()
    98  
    99  			s := suse.NewScanner(tt.distribution)
   100  			got, err := s.Detect(tt.args.osVer, nil, tt.args.pkgs)
   101  			if tt.wantErr != "" {
   102  				require.Error(t, err)
   103  				assert.Contains(t, err.Error(), tt.wantErr)
   104  				return
   105  			}
   106  			assert.NoError(t, err)
   107  			assert.Equal(t, tt.want, got)
   108  		})
   109  	}
   110  }
   111  
   112  func TestScanner_IsSupportedVersion(t *testing.T) {
   113  	type args struct {
   114  		osFamily ftypes.OSType
   115  		osVer    string
   116  	}
   117  	tests := []struct {
   118  		name         string
   119  		now          time.Time
   120  		distribution suse.Type
   121  		args         args
   122  		want         bool
   123  	}{
   124  		{
   125  			name: "opensuse.leap42.3",
   126  			now:  time.Date(2019, 5, 31, 23, 59, 59, 0, time.UTC),
   127  			args: args{
   128  				osFamily: "opensuse.leap",
   129  				osVer:    "42.3",
   130  			},
   131  			distribution: suse.OpenSUSE,
   132  			want:         true,
   133  		},
   134  		{
   135  			name: "sles12.3",
   136  			now:  time.Date(2019, 5, 31, 23, 59, 59, 0, time.UTC),
   137  			args: args{
   138  				osFamily: "suse linux enterprise server",
   139  				osVer:    "12.3",
   140  			},
   141  			distribution: suse.SUSEEnterpriseLinux,
   142  			want:         false,
   143  		},
   144  		{
   145  			name: "latest",
   146  			now:  time.Date(2019, 5, 2, 23, 59, 59, 0, time.UTC),
   147  			args: args{
   148  				osFamily: "opensuse.leap",
   149  				osVer:    "999.0",
   150  			},
   151  			want: true,
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		t.Run(tt.name, func(t *testing.T) {
   156  			s := suse.NewScanner(tt.distribution, suse.WithClock(fake.NewFakeClock(tt.now)))
   157  			got := s.IsSupportedVersion(tt.args.osFamily, tt.args.osVer)
   158  			assert.Equal(t, tt.want, got)
   159  		})
   160  	}
   161  }