github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/image/oci_test.go (about)

     1  package image
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestTryOCI(t *testing.T) {
    11  	tests := []struct {
    12  		name         string
    13  		ociImagePath string
    14  		wantErr      string
    15  	}{
    16  		{
    17  			name:         "correct path to index without tag",
    18  			ociImagePath: filepath.Join("testdata", "multi"),
    19  			wantErr:      "",
    20  		},
    21  		{
    22  			name:         "correct path to index with correct tag",
    23  			ociImagePath: filepath.Join("testdata", "multi:tg11"),
    24  			wantErr:      "",
    25  		},
    26  		{
    27  			name:         "correct path to index with incorrect tag",
    28  			ociImagePath: filepath.Join("testdata", "multi:tg12"),
    29  			wantErr:      "invalid OCI image ref",
    30  		},
    31  		{
    32  			name:         "correct path to manifest without tag",
    33  			ociImagePath: filepath.Join("testdata", "single"),
    34  			wantErr:      "",
    35  		},
    36  		{
    37  			name:         "correct path to manifest with correct tag",
    38  			ociImagePath: filepath.Join("testdata", "single:3.14"),
    39  			wantErr:      "",
    40  		},
    41  		{
    42  			name:         "correct path to manifest with incorrect tag",
    43  			ociImagePath: filepath.Join("testdata", "single:3.11"),
    44  			wantErr:      "invalid OCI image ref",
    45  		},
    46  		{
    47  			name: "correct path to manifest with correct digest",
    48  			ociImagePath: filepath.Join("testdata",
    49  				"single@sha256:56ae38f2f5c54b98311b8b2463d4861368c451ac17098f4227d84946b42ab96d"),
    50  			wantErr: "",
    51  		},
    52  		{
    53  			name: "correct path to manifest with incorrect digest",
    54  			ociImagePath: filepath.Join("testdata",
    55  				"single@sha256:1111111111111111111111111111111111111111111111111111111111111111"),
    56  			wantErr: "invalid OCI image ref",
    57  		},
    58  	}
    59  
    60  	for _, test := range tests {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			_, err := tryOCI(test.ociImagePath)
    63  			if test.wantErr != "" {
    64  				assert.NotNil(t, err)
    65  				assert.Contains(t, err.Error(), test.wantErr, err)
    66  			} else {
    67  				assert.NoError(t, err)
    68  			}
    69  		})
    70  	}
    71  }