-
Notifications
You must be signed in to change notification settings - Fork 7
/
winapi_test.go
184 lines (162 loc) · 4.48 KB
/
winapi_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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package winapi_test
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"testing"
"unsafe"
"github.com/alexbrainman/winapi"
)
func TestGlobalMemoryStatusEx(t *testing.T) {
var m winapi.MEMORYSTATUSEX
m.Length = uint32(unsafe.Sizeof(m))
err := winapi.GlobalMemoryStatusEx(&m)
if err != nil {
t.Fatal(err)
}
t.Logf("MEMORYSTATUSEX is %+v", m)
}
func TestGetProcessHandleCount(t *testing.T) {
h, err := syscall.GetCurrentProcess()
if err != nil {
t.Fatal(err)
}
var count uint32
err = winapi.GetProcessHandleCount(h, &count)
if err != nil {
t.Fatal(err)
}
t.Logf("Handle count is %v", count)
}
func TestGetVersionEx(t *testing.T) {
var vi winapi.OSVERSIONINFOEX
vi.OSVersionInfoSize = uint32(unsafe.Sizeof(vi))
err := winapi.GetVersionEx(&vi)
if err != nil {
t.Fatal(err)
}
t.Logf("OSVERSIONINFOEX is %+v", vi)
t.Logf("OSVERSIONINFOEX.CSDVersion is %v", syscall.UTF16ToString(vi.CSDVersion[:]))
}
func testTlsThread(t *testing.T, tlsidx uint32) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
threadId := winapi.GetCurrentThreadId()
want := uintptr(threadId)
err := winapi.TlsSetValue(tlsidx, want)
if err != nil {
t.Fatal(err)
}
have, err := winapi.TlsGetValue(tlsidx)
if err != nil {
t.Fatal(err)
}
if want != have {
t.Errorf("threadid=%d: unexpected tls data %d, want %d", threadId, have, want)
}
}
func TestTls(t *testing.T) {
tlsidx, err := winapi.TlsAlloc()
if err != nil {
t.Fatal(err)
}
defer func() {
err := winapi.TlsFree(tlsidx)
if err != nil {
t.Fatal(err)
}
}()
const threadCount = 20
done := make(chan bool)
for i := 0; i < threadCount; i++ {
go func() {
defer func() {
done <- true
}()
testTlsThread(t, tlsidx)
}()
}
for i := 0; i < threadCount; i++ {
<-done
}
}
func runIcacls(t *testing.T, args ...string) string {
t.Helper()
out, err := exec.Command("icacls", args...).CombinedOutput()
if err != nil {
t.Fatalf("icacls failed: %v\n%v", err, string(out))
}
return string(out)
}
func adjustACL(t *testing.T, path string) {
// as described in
// https://stackoverflow.com/questions/17536692/resetting-file-security-to-inherit-after-a-movefile-operation
var acl winapi.ACL
err := winapi.InitializeAcl(&acl, uint32(unsafe.Sizeof(acl)), winapi.ACL_REVISION)
if err != nil {
t.Fatal(err)
}
errno := winapi.SetNamedSecurityInfo(syscall.StringToUTF16Ptr(path), winapi.SE_FILE_OBJECT, winapi.DACL_SECURITY_INFORMATION|winapi.UNPROTECTED_DACL_SECURITY_INFORMATION, nil, nil, &acl, nil)
if errno != 0 {
t.Fatalf("SetNamedSecurityInfo failed: %v", syscall.Errno(errno))
}
}
func runGetACL(t *testing.T, path string) string {
t.Helper()
cmd := fmt.Sprintf(`Get-Acl "%s" | Select -expand AccessToString`, path)
out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
if err != nil {
t.Fatalf("Get-Acl failed: %v\n%v", err, string(out))
}
return string(out)
}
func TestIssue22343(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "TestIssue22343")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
newtmpdir := filepath.Join(tmpdir, "tmp")
err = os.Mkdir(newtmpdir, 0777)
if err != nil {
t.Fatal(err)
}
// When TestIssue22343/tmp directory is created, it will have
// the same security attributes as TestIssue22343.
// Add Guest account full access to TestIssue22343/tmp - this
// will make all files created in TestIssue22343/tmp have different
// security attributes to the files created in TestIssue22343.
runIcacls(t, newtmpdir,
"/inheritance:r", // breaks the inheritance from the directory above
"/grant:r", // clears the ACL
"guest:(oi)(ci)f", // Guest user will have full access
)
src := filepath.Join(tmpdir, "main.go")
err = ioutil.WriteFile(src, []byte("package main; func main() { }\n"), 0644)
if err != nil {
t.Fatal(err)
}
exe := filepath.Join(tmpdir, "main.exe")
cmd := exec.Command("go", "build", "-o", exe, src)
cmd.Env = append(os.Environ(),
"TMP="+newtmpdir,
"TEMP="+newtmpdir,
)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go command failed: %v\n%v", err, string(out))
}
adjustACL(t, exe)
// exe file is expected to have the same security atributes as the src.
if got, expected := runGetACL(t, exe), runGetACL(t, src); got != expected {
t.Fatalf("expected Get-Acl output of \n%v\n, got \n%v\n", expected, got)
}
}