forked from StackExchange/wmi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection_test.go
88 lines (74 loc) · 2.16 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// +build windows
package wmi
import (
"os/user"
"strings"
"testing"
)
// Just a smoke test of SWbemServicesConnection API. More detailed ones has
// been done at the upper levels of abstraction.
func TestSWbemServicesConnection(t *testing.T) {
s, err := ConnectSWbemServices()
if err != nil {
t.Fatalf("ConnectSWbemServices: %s", err)
}
var dst []Win32_OperatingSystem
q := CreateQuery(&dst, "")
errQuery := s.Query(q, &dst)
if errQuery != nil {
t.Fatalf("Query: %s", errQuery)
}
count := len(dst)
if count < 1 {
t.Fatalf("Query: no results found for Win32_OperatingSystem")
}
errClose := s.Close()
if errClose != nil {
t.Fatalf("Close: %s", errClose)
}
}
type userAccount struct {
SID string
Name string
Domain string
}
func TestSWbemServicesConnection_Get(t *testing.T) {
s, err := ConnectSWbemServices()
if err != nil {
t.Fatalf("InitializeSWbemServices: %s", err)
}
// https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-loggedonuser
var loggedUsers []struct {
AccountRef string `wmi:"Antecedent"`
}
if err := s.Query("SELECT * from Win32_LoggedOnUser", &loggedUsers); err != nil {
t.Fatalf("Failed to query Win32_LoggedOnUser; %s", err)
}
osUser, err := user.Current()
if err != nil {
t.Fatalf("Failed to query current u; %s", err)
}
// Try to find current user account by its SID.
var currentUserAccount userAccount
for _, u := range loggedUsers {
var account userAccount
if err := s.Get(u.AccountRef, &account); err != nil {
t.Errorf("Failed to get Win32_Account using ref %q; %s", u, err)
continue
}
if account.SID == osUser.Uid {
currentUserAccount = account
break
}
}
t.Logf("Expected user session; %+v", currentUserAccount)
// And now check domain/username to ensure we got the right user.
parts := strings.SplitN(osUser.Username, `\`, 2)
osUserDomain, osUserName := parts[0], parts[1]
if currentUserAccount.Name != osUserName {
t.Errorf("Got unexpected user Name; got %q, expected %q", currentUserAccount.Name, osUserName)
}
if currentUserAccount.Domain != osUserDomain {
t.Errorf("Got unexpected user Domain; got %q, expected %q", currentUserAccount.Domain, osUserDomain)
}
}