github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/dart/pub/pubspec_test.go (about)

     1  package pub
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"runtime"
     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_pubSpecLockAnalyzer_Analyze(t *testing.T) {
    17  	tests := []struct {
    18  		name        string
    19  		dir         string
    20  		pubCacheEnv string
    21  		want        *analyzer.AnalysisResult
    22  		wantErr     assert.ErrorAssertionFunc
    23  	}{
    24  		{
    25  			// Supports only absolute paths for `rootUri` in package_config.json
    26  			// But for this test this field was changed
    27  			name:        "happy path with cache",
    28  			dir:         "testdata/happy",
    29  			pubCacheEnv: "testdata/happy/cache",
    30  			want: &analyzer.AnalysisResult{
    31  				Applications: []types.Application{
    32  					{
    33  						Type:     types.Pub,
    34  						FilePath: "pubspec.lock",
    35  						Libraries: types.Packages{
    36  							{
    37  								ID:       "collection@1.17.0",
    38  								Name:     "collection",
    39  								Version:  "1.17.0",
    40  								Indirect: true,
    41  							},
    42  							{
    43  								ID:      "crypto@3.0.3",
    44  								Name:    "crypto",
    45  								Version: "3.0.3",
    46  								DependsOn: []string{
    47  									"typed_data@1.3.2",
    48  								},
    49  							},
    50  							{
    51  								ID:      "meta@1.11.0",
    52  								Name:    "meta",
    53  								Version: "1.11.0",
    54  							},
    55  							{
    56  								ID:       "typed_data@1.3.2",
    57  								Name:     "typed_data",
    58  								Version:  "1.3.2",
    59  								Indirect: true,
    60  								DependsOn: []string{
    61  									"collection@1.17.0",
    62  								},
    63  							},
    64  						},
    65  					},
    66  				},
    67  			},
    68  			wantErr: assert.NoError,
    69  		},
    70  		{
    71  			name:        "happy path without cache",
    72  			dir:         "testdata/happy",
    73  			pubCacheEnv: "testdata/happy/empty",
    74  			want: &analyzer.AnalysisResult{
    75  				Applications: []types.Application{
    76  					{
    77  						Type:     types.Pub,
    78  						FilePath: "pubspec.lock",
    79  						Libraries: types.Packages{
    80  							{
    81  								ID:       "collection@1.17.0",
    82  								Name:     "collection",
    83  								Version:  "1.17.0",
    84  								Indirect: true,
    85  							},
    86  							{
    87  								ID:      "crypto@3.0.3",
    88  								Name:    "crypto",
    89  								Version: "3.0.3",
    90  							},
    91  							{
    92  								ID:      "meta@1.11.0",
    93  								Name:    "meta",
    94  								Version: "1.11.0",
    95  							},
    96  							{
    97  								ID:       "typed_data@1.3.2",
    98  								Name:     "typed_data",
    99  								Version:  "1.3.2",
   100  								Indirect: true,
   101  							},
   102  						},
   103  					},
   104  				},
   105  			},
   106  			wantErr: assert.NoError,
   107  		},
   108  		{
   109  			name:    "empty file",
   110  			dir:     "testdata/empty",
   111  			want:    &analyzer.AnalysisResult{},
   112  			wantErr: assert.NoError,
   113  		},
   114  		{
   115  			name:    "broken file",
   116  			dir:     "testdata/broken",
   117  			want:    &analyzer.AnalysisResult{},
   118  			wantErr: assert.Error,
   119  		},
   120  	}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			t.Setenv("PUB_CACHE", tt.pubCacheEnv)
   125  			a, err := newPubSpecLockAnalyzer(analyzer.AnalyzerOptions{})
   126  			require.NoError(t, err)
   127  
   128  			got, err := a.PostAnalyze(context.Background(), analyzer.PostAnalysisInput{
   129  				FS: os.DirFS(tt.dir),
   130  			})
   131  
   132  			assert.NoError(t, err)
   133  			assert.Equal(t, tt.want, got)
   134  		})
   135  	}
   136  }
   137  
   138  func Test_pubSpecLockAnalyzer_cacheDir(t *testing.T) {
   139  	tests := []struct {
   140  		name            string
   141  		pubCacheEnv     string
   142  		localAppDataEnv string
   143  		windowsTest     bool
   144  		wantDir         string
   145  	}{
   146  		{
   147  			name:    "default cache dir for Linux/MacOS",
   148  			wantDir: "/root/.pub_cache",
   149  		},
   150  		{
   151  			name:        "default cache dir Windows",
   152  			windowsTest: true,
   153  			wantDir:     "C:\\Users\\User\\AppData\\Local\\Pub\\Cache",
   154  		},
   155  		{
   156  			name:        "PUB_CACHE is used",
   157  			pubCacheEnv: "/root/cache",
   158  			wantDir:     "/root/cache",
   159  		},
   160  		{
   161  			name:        "PUB_CACHE is used in Windows",
   162  			pubCacheEnv: "C:\\Cache",
   163  			windowsTest: true,
   164  			wantDir:     "C:\\Cache",
   165  		},
   166  	}
   167  
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			if runtime.GOOS == "windows" {
   171  				if !tt.windowsTest {
   172  					t.Skipf("This test is not used for %s", runtime.GOOS)
   173  				}
   174  				t.Setenv("LOCALAPPDATA", "C:\\Users\\User\\AppData\\Local")
   175  			} else {
   176  				if tt.windowsTest {
   177  					t.Skipf("This test is not used for %s", runtime.GOOS)
   178  				}
   179  				t.Setenv("HOME", "/root")
   180  			}
   181  
   182  			t.Setenv("PUB_CACHE", tt.pubCacheEnv)
   183  
   184  			dir := cacheDir()
   185  			assert.Equal(t, tt.wantDir, dir)
   186  		})
   187  	}
   188  }
   189  
   190  func Test_pubSpecLockAnalyzer_Required(t *testing.T) {
   191  	tests := []struct {
   192  		name     string
   193  		filePath string
   194  		want     bool
   195  	}{
   196  		{
   197  			name:     "happy path",
   198  			filePath: "pubspec.lock",
   199  			want:     true,
   200  		},
   201  		{
   202  			name:     "sad path",
   203  			filePath: "test.txt",
   204  			want:     false,
   205  		},
   206  	}
   207  	for _, tt := range tests {
   208  		t.Run(tt.name, func(t *testing.T) {
   209  			a := pubSpecLockAnalyzer{}
   210  			got := a.Required(tt.filePath, nil)
   211  			assert.Equal(t, tt.want, got)
   212  		})
   213  	}
   214  }