forked from StackExchange/wmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmi_test.go
316 lines (304 loc) · 9.29 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// +build windows
package wmi
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"runtime/debug"
"sync"
"testing"
"time"
)
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 || err.Error() != `wmi: cannot load field "Blah" into a "uint32": no such struct field` {
t.Error("Expected err field mismatch")
}
}
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 _TestMany(t *testing.T) {
limit := 5000
fmt.Println("running until:", limit)
fmt.Println("No panics mean it succeeded. Other errors are OK.")
runtime.GOMAXPROCS(2)
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
for i := 0; i < limit; i++ {
if i%25 == 0 {
fmt.Println(i)
}
var dst []Win32_PerfRawData_PerfDisk_LogicalDisk
q := CreateQuery(&dst, "")
err := Query(q, &dst)
if err != nil {
fmt.Println("ERROR disk", err)
}
}
wg.Done()
}()
go func() {
for i := 0; i > -limit; i-- {
if i%25 == 0 {
fmt.Println(i)
}
var dst []Win32_OperatingSystem
q := CreateQuery(&dst, "")
err := Query(q, &dst)
if err != nil {
fmt.Println("ERROR OS", err)
}
}
wg.Done()
}()
wg.Wait()
}
type Win32_Process struct {
CSCreationClassName string
CSName string
Caption *string
CommandLine *string
CreationClassName string
CreationDate *time.Time
Description *string
ExecutablePath *string
ExecutionState *uint16
Handle string
HandleCount uint32
InstallDate *time.Time
KernelModeTime uint64
MaximumWorkingSetSize *uint32
MinimumWorkingSetSize *uint32
Name string
OSCreationClassName string
OSName string
OtherOperationCount uint64
OtherTransferCount uint64
PageFaults uint32
PageFileUsage uint32
ParentProcessId uint32
PeakPageFileUsage uint32
PeakVirtualSize uint64
PeakWorkingSetSize uint32
Priority uint32
PrivatePageCount uint64
ProcessId uint32
QuotaNonPagedPoolUsage uint32
QuotaPagedPoolUsage uint32
QuotaPeakNonPagedPoolUsage uint32
QuotaPeakPagedPoolUsage uint32
ReadOperationCount uint64
ReadTransferCount uint64
SessionId uint32
Status *string
TerminationDate *time.Time
ThreadCount uint32
UserModeTime uint64
VirtualSize uint64
WindowsVersion string
WorkingSetSize uint64
WriteOperationCount uint64
WriteTransferCount uint64
}
type Win32_PerfRawData_PerfDisk_LogicalDisk struct {
AvgDiskBytesPerRead uint64
AvgDiskBytesPerRead_Base uint32
AvgDiskBytesPerTransfer uint64
AvgDiskBytesPerTransfer_Base uint32
AvgDiskBytesPerWrite uint64
AvgDiskBytesPerWrite_Base uint32
AvgDiskQueueLength uint64
AvgDiskReadQueueLength uint64
AvgDiskSecPerRead uint32
AvgDiskSecPerRead_Base uint32
AvgDiskSecPerTransfer uint32
AvgDiskSecPerTransfer_Base uint32
AvgDiskSecPerWrite uint32
AvgDiskSecPerWrite_Base uint32
AvgDiskWriteQueueLength uint64
Caption *string
CurrentDiskQueueLength uint32
Description *string
DiskBytesPerSec uint64
DiskReadBytesPerSec uint64
DiskReadsPerSec uint32
DiskTransfersPerSec uint32
DiskWriteBytesPerSec uint64
DiskWritesPerSec uint32
FreeMegabytes uint32
Frequency_Object uint64
Frequency_PerfTime uint64
Frequency_Sys100NS uint64
Name string
PercentDiskReadTime uint64
PercentDiskReadTime_Base uint64
PercentDiskTime uint64
PercentDiskTime_Base uint64
PercentDiskWriteTime uint64
PercentDiskWriteTime_Base uint64
PercentFreeSpace uint32
PercentFreeSpace_Base uint32
PercentIdleTime uint64
PercentIdleTime_Base uint64
SplitIOPerSec uint32
Timestamp_Object uint64
Timestamp_PerfTime uint64
Timestamp_Sys100NS uint64
}
type Win32_OperatingSystem struct {
BootDevice string
BuildNumber string
BuildType string
Caption *string
CodeSet string
CountryCode string
CreationClassName string
CSCreationClassName string
CSDVersion *string
CSName string
CurrentTimeZone int16
DataExecutionPrevention_Available bool
DataExecutionPrevention_32BitApplications bool
DataExecutionPrevention_Drivers bool
DataExecutionPrevention_SupportPolicy *uint8
Debug bool
Description *string
Distributed bool
EncryptionLevel uint32
ForegroundApplicationBoost *uint8
FreePhysicalMemory uint64
FreeSpaceInPagingFiles uint64
FreeVirtualMemory uint64
InstallDate time.Time
LargeSystemCache *uint32
LastBootUpTime time.Time
LocalDateTime time.Time
Locale string
Manufacturer string
MaxNumberOfProcesses uint32
MaxProcessMemorySize uint64
MUILanguages *[]string
Name string
NumberOfLicensedUsers *uint32
NumberOfProcesses uint32
NumberOfUsers uint32
OperatingSystemSKU uint32
Organization string
OSArchitecture string
OSLanguage uint32
OSProductSuite uint32
OSType uint16
OtherTypeDescription *string
PAEEnabled *bool
PlusProductID *string
PlusVersionNumber *string
PortableOperatingSystem bool
Primary bool
ProductType uint32
RegisteredUser string
SerialNumber string
ServicePackMajorVersion uint16
ServicePackMinorVersion uint16
SizeStoredInPagingFiles uint64
Status string
SuiteMask uint32
SystemDevice string
SystemDirectory string
SystemDrive string
TotalSwapSpaceSize *uint64
TotalVirtualMemorySize uint64
TotalVisibleMemorySize uint64
Version string
WindowsDirectory string
}