go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/users/ps1getlocalusers_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package users_test
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"go.mondoo.com/cnquery/providers/os/resources/users"
    12  )
    13  
    14  func TestWindowsLocalUsersParser(t *testing.T) {
    15  	data, err := os.Open("./testdata/windows.json")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	localUsers, err := users.ParseWindowsLocalUsers(data)
    21  	assert.Nil(t, err)
    22  	assert.Equal(t, 5, len(localUsers))
    23  
    24  	expected := &users.WindowsLocalUser{
    25  		Name:            "chris",
    26  		Description:     "Built-in account for administering the computer/domain",
    27  		PrincipalSource: 1,
    28  		SID: users.WindowsSID{
    29  			BinaryLength:     28,
    30  			AccountDomainSid: pointer("S-1-5-21-2356735557-1575748656-448136971"),
    31  			Value:            "S-1-5-21-2356735557-1575748656-448136971-500",
    32  		},
    33  		ObjectClass:            "User",
    34  		Enabled:                true,
    35  		FullName:               "",
    36  		PasswordRequired:       true,
    37  		UserMayChangePassword:  true,
    38  		AccountExpires:         nil,
    39  		PasswordChangeableDate: pointer("/Date(1586981519962)/"),
    40  		PasswordExpires:        pointer("/Date(1590610319962)/"),
    41  		PasswordLastSet:        pointer("/Date(1586981519962)/"),
    42  		LastLogon:              pointer("/Date(1587041759064)/"),
    43  	}
    44  	found := findWindowsUser(localUsers, "chris")
    45  	assert.EqualValues(t, expected, found)
    46  }
    47  
    48  func pointer(val string) *string {
    49  	return &val
    50  }
    51  
    52  func findWindowsUser(users []users.WindowsLocalUser, username string) *users.WindowsLocalUser {
    53  	for i := range users {
    54  		if users[i].Name == username {
    55  			return &users[i]
    56  		}
    57  	}
    58  	return nil
    59  }