github.com/google/osv-scalibr@v0.4.1/annotator/cachedir/cachedir_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cachedir_test
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/osv-scalibr/annotator/cachedir"
    23  	"github.com/google/osv-scalibr/extractor"
    24  	"github.com/google/osv-scalibr/inventory"
    25  	"github.com/google/osv-scalibr/inventory/vex"
    26  )
    27  
    28  func TestIsInsideCacheDir(t *testing.T) {
    29  	// Define test cases with different platform-specific paths
    30  	testCases := []struct {
    31  		inputPath           string
    32  		separator           rune // defaulting to '/'
    33  		wantCacheAnnotation bool
    34  	}{
    35  		// Linux/Unix
    36  		{inputPath: "/tmp/somefile", wantCacheAnnotation: true},
    37  		{inputPath: "/var/cache/apt/archives", wantCacheAnnotation: true},
    38  		{inputPath: "/home/user/.local/share/Trash/files/file.txt", wantCacheAnnotation: true},
    39  		{inputPath: "/home/user/.cache/thumbnails", wantCacheAnnotation: true},
    40  		{inputPath: "/root/.cache/pip", wantCacheAnnotation: true},
    41  		{inputPath: "/home/user/projects/code", wantCacheAnnotation: false},
    42  
    43  		// macOS
    44  		{inputPath: "/Users/username/Library/Caches/com.apple.Safari", wantCacheAnnotation: true},
    45  		{inputPath: "/private/tmp/mytmpfile", wantCacheAnnotation: true},
    46  		{inputPath: "/System/Volumes/Data/private/var/tmp/file", wantCacheAnnotation: true},
    47  		{inputPath: "/System/Volumes/Data/private/tmp/file", wantCacheAnnotation: true},
    48  		{inputPath: "/Users/username/Documents", wantCacheAnnotation: false},
    49  
    50  		// Windows
    51  		{inputPath: "C:\\Users\\testuser\\AppData\\Local\\Temp\\tempfile.txt", separator: '\\', wantCacheAnnotation: true},
    52  		{inputPath: "C:\\Windows\\Temp\\log.txt", separator: '\\', wantCacheAnnotation: true},
    53  		{inputPath: "C:\\Program Files\\MyApp", separator: '\\', wantCacheAnnotation: false},
    54  
    55  		// Edge cases
    56  		{inputPath: "", wantCacheAnnotation: false},
    57  		{inputPath: "some/relative/path", wantCacheAnnotation: false},
    58  	}
    59  
    60  	for _, tt := range testCases {
    61  		t.Run(tt.inputPath, func(t *testing.T) {
    62  			if tt.separator == 0 {
    63  				tt.separator = '/'
    64  			}
    65  
    66  			if os.PathSeparator != tt.separator {
    67  				t.Skipf("Skipping IsInsideCacheDir(%q)", tt.inputPath)
    68  			}
    69  
    70  			inv := &inventory.Inventory{
    71  				Packages: []*extractor.Package{&extractor.Package{
    72  					Locations: []string{tt.inputPath},
    73  				}},
    74  			}
    75  			if err := cachedir.New().Annotate(t.Context(), nil, inv); err != nil {
    76  				t.Errorf("Annotate(%v): %v", inv, err)
    77  			}
    78  			var want []*vex.PackageExploitabilitySignal
    79  			if tt.wantCacheAnnotation {
    80  				want = []*vex.PackageExploitabilitySignal{&vex.PackageExploitabilitySignal{
    81  					Plugin:          cachedir.Name,
    82  					Justification:   vex.ComponentNotPresent,
    83  					MatchesAllVulns: true,
    84  				}}
    85  			}
    86  
    87  			got := inv.Packages[0].ExploitabilitySignals
    88  			if diff := cmp.Diff(want, got); diff != "" {
    89  				t.Errorf("Annotate(%v) (-want +got):\n%s", inv, diff)
    90  			}
    91  		})
    92  	}
    93  }