github.com/google/osv-scalibr@v0.4.1/detector/weakcredentials/winlocal/samreg/userf_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 samreg
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  func TestUserFEnabled(t *testing.T) {
    23  	tests := []struct {
    24  		name        string
    25  		buffer      []byte
    26  		wantEnabled bool
    27  		wantErr     bool
    28  	}{
    29  		{
    30  			name:        "user_is_enabled_returns_true",
    31  			buffer:      []byte(strings.Repeat("A", 56) + "\x00"),
    32  			wantEnabled: true,
    33  		},
    34  		{
    35  			name:        "user_is_disabled_returns_false",
    36  			buffer:      []byte(strings.Repeat("A", 56) + "\x01"),
    37  			wantEnabled: false,
    38  		},
    39  		{
    40  			name:    "userF_structure_parsing_error_returns_error",
    41  			buffer:  []byte(""),
    42  			wantErr: true,
    43  		},
    44  	}
    45  
    46  	for _, tc := range tests {
    47  		t.Run(tc.name, func(t *testing.T) {
    48  			userF := newUserF(tc.buffer, "irrelevant")
    49  			got, gotErr := userF.Enabled()
    50  
    51  			if (gotErr != nil) != tc.wantErr {
    52  				t.Errorf("Enabled(): unexpected error: %v", gotErr)
    53  			}
    54  
    55  			if tc.wantErr {
    56  				return
    57  			}
    58  
    59  			if got != tc.wantEnabled {
    60  				t.Errorf("Enabled(): got %v, want %v", got, tc.wantEnabled)
    61  			}
    62  		})
    63  	}
    64  }