github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/secrets/mysqlmylogin/mysqlmylogin_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 mysqlmylogin_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/google/go-cmp/cmp/cmpopts"
    22  	"github.com/google/osv-scalibr/extractor/filesystem"
    23  	"github.com/google/osv-scalibr/extractor/filesystem/secrets/mysqlmylogin"
    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  )
    28  
    29  func TestFileRequired(t *testing.T) {
    30  	tests := []struct {
    31  		name         string
    32  		path         string
    33  		wantRequired bool
    34  	}{
    35  		{
    36  			name:         "valid .mylogin.cnf",
    37  			path:         "/foo/.mylogin.cnf",
    38  			wantRequired: true,
    39  		},
    40  		{
    41  			name:         "valid .mylogin.cnf",
    42  			path:         ".mylogin.cnf",
    43  			wantRequired: true,
    44  		},
    45  		{
    46  			name:         "invalid .mylogin.cnf",
    47  			path:         "/foo.mylogin.cnf",
    48  			wantRequired: false,
    49  		},
    50  		{
    51  			name:         "invalid .mylogin.cnf",
    52  			path:         "/foo.mylogin.cnf.ext",
    53  			wantRequired: false,
    54  		},
    55  		{
    56  			name:         "invalid .mylogin.cnf",
    57  			path:         "/foo_mysqlmylogin_zoo.ext",
    58  			wantRequired: false,
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			var e filesystem.Extractor = mysqlmylogin.Extractor{}
    65  			if got := e.FileRequired(simplefileapi.New(tt.path, nil)); got != tt.wantRequired {
    66  				t.Fatalf("FileRequired(%s): got %v, want %v", tt.path, got, tt.wantRequired)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestExtract(t *testing.T) {
    73  	tests := []struct {
    74  		Name        string
    75  		InputConfig extracttest.ScanInputMockConfig
    76  		WantSecrets []*inventory.Secret
    77  		WantErr     error
    78  	}{
    79  		{
    80  			Name: "valid .mylogin.cnf file",
    81  			InputConfig: extracttest.ScanInputMockConfig{
    82  				Path: "testdata/valid",
    83  			},
    84  			WantSecrets: []*inventory.Secret{
    85  				{
    86  					Secret: mysqlmylogin.Section{
    87  						SectionName: "local",
    88  						User:        "root",
    89  						Password:    "google",
    90  						Host:        "localhost",
    91  						Port:        "1234",
    92  						Socket:      "socket"},
    93  					Location: "testdata/valid",
    94  				},
    95  			},
    96  		}, {
    97  			Name: "valid .mylogin.cnf file with multiple sections",
    98  			InputConfig: extracttest.ScanInputMockConfig{
    99  				Path: "testdata/valid_multiple_sections",
   100  			},
   101  			WantSecrets: []*inventory.Secret{
   102  				{
   103  					Secret: mysqlmylogin.Section{
   104  						SectionName: "local",
   105  						User:        "root",
   106  						Password:    "google",
   107  						Host:        "localhost",
   108  						Port:        "1234",
   109  						Socket:      "socket"},
   110  					Location: "testdata/valid_multiple_sections",
   111  				}, {
   112  					Secret: mysqlmylogin.Section{
   113  						SectionName: "client",
   114  						User:        "admin",
   115  						Password:    "password_client",
   116  						Host:        "127.0.0.1",
   117  						Port:        "4321",
   118  						Socket:      "s"},
   119  					Location: "testdata/valid_multiple_sections",
   120  				},
   121  			},
   122  		},
   123  		{
   124  			Name: "invalid .mylogin.cnf file",
   125  			InputConfig: extracttest.ScanInputMockConfig{
   126  				Path: "testdata/invalid",
   127  			},
   128  			WantSecrets: nil,
   129  			WantErr:     cmpopts.AnyError,
   130  		},
   131  	}
   132  
   133  	for _, tt := range tests {
   134  		t.Run(tt.Name, func(t *testing.T) {
   135  			e := mysqlmylogin.New()
   136  
   137  			scanInput := extracttest.GenerateScanInputMock(t, tt.InputConfig)
   138  			defer extracttest.CloseTestScanInput(t, scanInput)
   139  
   140  			got, err := e.Extract(t.Context(), &scanInput)
   141  
   142  			if diff := cmp.Diff(tt.WantErr, err, cmpopts.EquateErrors()); diff != "" {
   143  				t.Errorf("%s.Extract(%q) error diff (-want +got):\n%s", e.Name(), tt.InputConfig.Path, diff)
   144  				return
   145  			}
   146  
   147  			wantInv := inventory.Inventory{Secrets: tt.WantSecrets}
   148  			if diff := cmp.Diff(wantInv, got); diff != "" {
   149  				t.Errorf("%s.Extract(%q) diff (-want +got):\n%s", e.Name(), tt.InputConfig.Path, diff)
   150  			}
   151  		})
   152  	}
   153  }