github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/vulnsrc/photon/photon_test.go (about)

     1  package photon
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/khulnasoft-lab/tunnel-db/pkg/types"
     8  	"github.com/khulnasoft-lab/tunnel-db/pkg/vulnsrc/vulnerability"
     9  	"github.com/khulnasoft-lab/tunnel-db/pkg/vulnsrctest"
    10  )
    11  
    12  func TestVulnSrc_Update(t *testing.T) {
    13  	tests := []struct {
    14  		name       string
    15  		dir        string
    16  		wantValues []vulnsrctest.WantValues
    17  		wantErr    string
    18  	}{
    19  		{
    20  			name: "happy path",
    21  			dir:  filepath.Join("testdata", "happy"),
    22  			wantValues: []vulnsrctest.WantValues{
    23  				{
    24  					Key: []string{"data-source", "Photon OS 3.0"},
    25  					Value: types.DataSource{
    26  						ID:   vulnerability.Photon,
    27  						Name: "Photon OS CVE metadata",
    28  						URL:  "https://packages.vmware.com/photon/photon_cve_metadata/",
    29  					},
    30  				},
    31  				{
    32  					Key: []string{"advisory-detail", "CVE-2019-0199", "Photon OS 3.0", "apache-tomcat"},
    33  					Value: types.Advisory{
    34  						FixedVersion: "8.5.40-1.ph3",
    35  					},
    36  				},
    37  				{
    38  					Key: []string{"vulnerability-detail", "CVE-2019-0199", "photon"},
    39  					Value: types.VulnerabilityDetail{
    40  						CvssScoreV3: 7.5,
    41  					},
    42  				},
    43  				{
    44  					Key:   []string{"vulnerability-id", "CVE-2019-0199"},
    45  					Value: map[string]interface{}{},
    46  				},
    47  			},
    48  		},
    49  		{
    50  			name:    "sad path (dir doesn't exist)",
    51  			dir:     filepath.Join("testdata", "badPath"),
    52  			wantErr: "no such file or directory",
    53  		},
    54  		{
    55  			name:    "sad path (failed to decode)",
    56  			dir:     filepath.Join("testdata", "sad"),
    57  			wantErr: "failed to decode Photon JSON",
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			vs := NewVulnSrc()
    63  			vulnsrctest.TestUpdate(t, vs, vulnsrctest.TestUpdateArgs{
    64  				Dir:        tt.dir,
    65  				WantValues: tt.wantValues,
    66  				WantErr:    tt.wantErr,
    67  			})
    68  		})
    69  	}
    70  }
    71  
    72  func TestVulnSrc_Get(t *testing.T) {
    73  	tests := []struct {
    74  		name     string
    75  		fixtures []string
    76  		release  string
    77  		pkgName  string
    78  		want     []types.Advisory
    79  		wantErr  string
    80  	}{
    81  		{
    82  			name:     "happy path",
    83  			fixtures: []string{"testdata/fixtures/happy.yaml"},
    84  			release:  "1.0",
    85  			pkgName:  "ansible",
    86  			want: []types.Advisory{
    87  				{
    88  					VulnerabilityID: "CVE-2019-3828",
    89  					FixedVersion:    "2.7.6-2.ph3",
    90  				},
    91  			},
    92  		},
    93  		{
    94  			name:     "no advisories are returned",
    95  			fixtures: []string{"testdata/fixtures/happy.yaml"},
    96  			release:  "2.0",
    97  			pkgName:  "ansible",
    98  			want:     nil,
    99  		},
   100  		{
   101  			name:     "GetAdvisories returns an error",
   102  			fixtures: []string{"testdata/fixtures/sad.yaml"},
   103  			release:  "1.0",
   104  			pkgName:  "ansible",
   105  			wantErr:  "failed to unmarshal advisory JSON",
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			vs := NewVulnSrc()
   111  			vulnsrctest.TestGet(t, vs, vulnsrctest.TestGetArgs{
   112  				Fixtures:   tt.fixtures,
   113  				WantValues: tt.want,
   114  				Release:    tt.release,
   115  				PkgName:    tt.pkgName,
   116  				WantErr:    tt.wantErr,
   117  			})
   118  		})
   119  	}
   120  }