forked from StackExchange/wmi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wmi_test.go
135 lines (126 loc) · 2.74 KB
/
wmi_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// +build windows
package wmi
import (
"encoding/json"
"fmt"
"reflect"
"runtime/debug"
"testing"
)
func TestQuery(t *testing.T) {
var dst []Win32_Process
q := CreateQuery(&dst, "")
err := Query(q, &dst)
if err != nil {
t.Fatal(err)
}
}
func TestFieldMismatch(t *testing.T) {
type s struct {
Name string
HandleCount uint32
Blah uint32
}
var dst []s
err := Query("SELECT Name, HandleCount FROM Win32_Process", &dst)
if err == nil {
t.Fatal("Expected err field mismatch")
}
expectedErr, ok := err.(ErrFieldMismatch)
if !ok {
t.Fatalf("Unexpected error type; got %T, expected %T", err, expectedErr)
}
if expectedErr.FieldName != "Blah" {
t.Errorf("Unexpected field mismatch error; got error for field %q; expected for %q", expectedErr.FieldName, "Blah")
}
}
func TestStrings(t *testing.T) {
printed := false
f := func() {
var dst []Win32_Process
zeros := 0
q := CreateQuery(&dst, "")
for i := 0; i < 5; i++ {
err := Query(q, &dst)
if err != nil {
t.Fatal(err, q)
}
for _, d := range dst {
v := reflect.ValueOf(d)
for j := 0; j < v.NumField(); j++ {
f := v.Field(j)
if f.Kind() != reflect.String {
continue
}
s := f.Interface().(string)
if len(s) > 0 && s[0] == '\u0000' {
zeros++
if !printed {
printed = true
j, _ := json.MarshalIndent(&d, "", " ")
t.Log("Example with \\u0000:\n", string(j))
}
}
}
}
fmt.Println("iter", i, "zeros:", zeros)
}
if zeros > 0 {
t.Error("> 0 zeros")
}
}
fmt.Println("Disabling GC")
debug.SetGCPercent(-1)
f()
fmt.Println("Enabling GC")
debug.SetGCPercent(100)
f()
}
func TestNamespace(t *testing.T) {
var dst []Win32_Process
q := CreateQuery(&dst, "")
err := QueryNamespace(q, &dst, `root\CIMV2`)
if err != nil {
t.Fatal(err)
}
dst = nil
err = QueryNamespace(q, &dst, `broken\nothing`)
if err == nil {
t.Fatal("expected error")
}
}
func TestCreateQuery(t *testing.T) {
type TestStruct struct {
Name string
Count int
}
var dst []TestStruct
output := "SELECT Name, Count FROM TestStruct WHERE Count > 2"
tests := []interface{}{
&dst,
dst,
TestStruct{},
&TestStruct{},
}
for i, test := range tests {
if o := CreateQuery(test, "WHERE Count > 2"); o != output {
t.Error("bad output on", i, o)
}
}
if CreateQuery(3, "") != "" {
t.Error("expected empty string")
}
}
func TestCreateQueryTags(t *testing.T) {
type TestStruct struct {
Name string
Size int `wmi:"Count"`
UserField string `wmi:"-"`
}
var dst []TestStruct
expected := "SELECT Name, Count FROM TestStruct WHERE Count > 2"
got := CreateQuery(dst, "WHERE Count > 2")
if got != expected {
t.Errorf("Got unexpected query; got %q, expected %q", got, expected)
}
}