github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/secrets/gitbasicauth/codecatalyst/codecatalyst_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 codecatalyst_test
    16  
    17  import (
    18  	"runtime"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	"github.com/google/osv-scalibr/extractor/filesystem/secrets/gitbasicauth/codecatalyst"
    24  	"github.com/google/osv-scalibr/extractor/filesystem/simplefileapi"
    25  	"github.com/google/osv-scalibr/inventory"
    26  	"github.com/google/osv-scalibr/testing/extracttest"
    27  	codecatalystdetector "github.com/google/osv-scalibr/veles/secrets/gitbasicauth/codecatalyst"
    28  )
    29  
    30  func TestExtractor_FileRequired(t *testing.T) {
    31  	tests := []struct {
    32  		inputPath string
    33  		want      bool
    34  		isWindows bool
    35  	}{
    36  		{inputPath: "", want: false},
    37  
    38  		// linux
    39  		{inputPath: `/Users/example-user/folder/.git/config`, want: true},
    40  		{inputPath: `/Users/example-user/.git-credentials`, want: true},
    41  		{inputPath: `/Users/example-user/.zsh_history`, want: true},
    42  		{inputPath: `/Users/example-user/bad/path`, want: false},
    43  
    44  		// windows
    45  		{inputPath: `C:\Users\USERNAME\folder\.git\config`, isWindows: true, want: true},
    46  		{inputPath: `C:\Users\YourUserName\.git-credentials`, isWindows: true, want: true},
    47  		{inputPath: `C:\Users\USERNAME\another\bad\path`, isWindows: true, want: false},
    48  	}
    49  
    50  	for _, tt := range tests {
    51  		t.Run(tt.inputPath, func(t *testing.T) {
    52  			if tt.isWindows && runtime.GOOS != "windows" {
    53  				t.Skipf("Skipping test %q for %q", t.Name(), runtime.GOOS)
    54  			}
    55  			e := codecatalyst.New()
    56  			got := e.FileRequired(simplefileapi.New(tt.inputPath, nil))
    57  			if got != tt.want {
    58  				t.Errorf("FileRequired(%s) got = %v, want %v", tt.inputPath, got, tt.want)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestExtractor_Extract(t *testing.T) {
    65  	tests := []*struct {
    66  		Name        string
    67  		Path        string
    68  		WantSecrets []*inventory.Secret
    69  		WantErr     error
    70  	}{
    71  		{
    72  			Name:        "empty",
    73  			Path:        "empty",
    74  			WantSecrets: nil,
    75  		},
    76  		{
    77  			Name: "git_credentials",
    78  			Path: "git_credentials",
    79  			WantSecrets: []*inventory.Secret{
    80  				{
    81  					Secret: codecatalystdetector.Credentials{
    82  						FullURL: `https://user:password@git.region.codecatalyst.aws/v1/space/project/repo`,
    83  					},
    84  					Location: "git_credentials",
    85  				},
    86  			},
    87  		},
    88  		{
    89  			Name: "git_config",
    90  			Path: "git_config",
    91  			WantSecrets: []*inventory.Secret{
    92  				{
    93  					Secret: codecatalystdetector.Credentials{
    94  						FullURL: `https://user:password@git.region.codecatalyst.aws/v1/space/project/repo`,
    95  					},
    96  					Location: "git_config",
    97  				},
    98  			},
    99  		},
   100  		{
   101  			Name: "history_file",
   102  			Path: ".zsh_history",
   103  			WantSecrets: []*inventory.Secret{
   104  				{
   105  					Secret: codecatalystdetector.Credentials{
   106  						FullURL: `https://user:password@git.region.codecatalyst.aws/v1/space/project/test-repo`,
   107  					},
   108  					Location: ".zsh_history",
   109  				},
   110  			},
   111  		},
   112  		{
   113  			Name:        "random_content",
   114  			Path:        "random_content",
   115  			WantSecrets: nil,
   116  		},
   117  	}
   118  
   119  	for _, tt := range tests {
   120  		t.Run(tt.Name, func(t *testing.T) {
   121  			extr := codecatalyst.New()
   122  
   123  			inputCfg := extracttest.ScanInputMockConfig{
   124  				Path:         tt.Path,
   125  				FakeScanRoot: "testdata",
   126  			}
   127  
   128  			scanInput := extracttest.GenerateScanInputMock(t, inputCfg)
   129  			defer extracttest.CloseTestScanInput(t, scanInput)
   130  
   131  			got, err := extr.Extract(t.Context(), &scanInput)
   132  
   133  			if diff := cmp.Diff(tt.WantErr, err, cmpopts.EquateErrors()); diff != "" {
   134  				t.Errorf("%s.Extract(%q) error diff (-want +got):\n%s", extr.Name(), tt.Path, diff)
   135  				return
   136  			}
   137  
   138  			wantInv := inventory.Inventory{Secrets: tt.WantSecrets}
   139  			opts := []cmp.Option{cmpopts.SortSlices(extracttest.PackageCmpLess), cmpopts.EquateEmpty()}
   140  			if diff := cmp.Diff(wantInv, got, opts...); diff != "" {
   141  				t.Errorf("%s.Extract(%q) diff (-want +got):\n%s", extr.Name(), tt.Path, diff)
   142  			}
   143  		})
   144  	}
   145  }