github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/c/conan/conan_test.go (about)

     1  package conan
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    13  	"github.com/devseccon/trivy/pkg/fanal/types"
    14  )
    15  
    16  func Test_conanLockAnalyzer_Analyze(t *testing.T) {
    17  	tests := []struct {
    18  		name      string
    19  		inputFile string
    20  		want      *analyzer.AnalysisResult
    21  	}{
    22  		{
    23  			name:      "happy path",
    24  			inputFile: "testdata/happy.lock",
    25  			want: &analyzer.AnalysisResult{
    26  				Applications: []types.Application{
    27  					{
    28  						Type:     types.Conan,
    29  						FilePath: "testdata/happy.lock",
    30  						Libraries: types.Packages{
    31  							{
    32  								ID:      "openssl/3.0.5",
    33  								Name:    "openssl",
    34  								Version: "3.0.5",
    35  								DependsOn: []string{
    36  									"zlib/1.2.12",
    37  								},
    38  								Locations: []types.Location{
    39  									{
    40  										StartLine: 12,
    41  										EndLine:   21,
    42  									},
    43  								},
    44  							},
    45  							{
    46  								ID:       "zlib/1.2.12",
    47  								Name:     "zlib",
    48  								Version:  "1.2.12",
    49  								Indirect: true,
    50  								Locations: []types.Location{
    51  									{
    52  										StartLine: 22,
    53  										EndLine:   28,
    54  									},
    55  								},
    56  							},
    57  						},
    58  					},
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name:      "empty file",
    64  			inputFile: "testdata/empty.lock",
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			f, err := os.Open(tt.inputFile)
    71  			require.NoError(t, err)
    72  			defer f.Close()
    73  
    74  			a := conanLockAnalyzer{}
    75  			got, err := a.Analyze(nil, analyzer.AnalysisInput{
    76  				FilePath: tt.inputFile,
    77  				Content:  f,
    78  			})
    79  
    80  			if got != nil {
    81  				for _, app := range got.Applications {
    82  					sort.Sort(app.Libraries)
    83  				}
    84  			}
    85  
    86  			assert.NoError(t, err)
    87  			assert.Equal(t, tt.want, got)
    88  		})
    89  	}
    90  }
    91  
    92  func Test_conanLockAnalyzer_Required(t *testing.T) {
    93  	tests := []struct {
    94  		name     string
    95  		filePath string
    96  		want     bool
    97  	}{
    98  		{
    99  			name:     "default name",
   100  			filePath: "conan.lock",
   101  			want:     true,
   102  		},
   103  		{
   104  			name:     "name with prefix",
   105  			filePath: "pkga_deps.lock",
   106  			want:     false,
   107  		},
   108  		{
   109  			name:     "txt",
   110  			filePath: "test.txt",
   111  			want:     false,
   112  		},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			dir := t.TempDir()
   117  			f, err := os.Create(filepath.Join(dir, tt.filePath))
   118  			require.NoError(t, err)
   119  			defer f.Close()
   120  
   121  			fi, err := f.Stat()
   122  			require.NoError(t, err)
   123  
   124  			a := conanLockAnalyzer{}
   125  			got := a.Required("", fi)
   126  			assert.Equal(t, tt.want, got)
   127  		})
   128  	}
   129  }