1// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package windows_test
6
7import (
8	"io/ioutil"
9	"os"
10	"path/filepath"
11	"syscall"
12	"testing"
13
14	"golang.org/x/sys/windows"
15)
16
17func TestWin32finddata(t *testing.T) {
18	dir, err := ioutil.TempDir("", "go-build")
19	if err != nil {
20		t.Fatalf("failed to create temp directory: %v", err)
21	}
22	defer os.RemoveAll(dir)
23
24	path := filepath.Join(dir, "long_name.and_extension")
25	f, err := os.Create(path)
26	if err != nil {
27		t.Fatalf("failed to create %v: %v", path, err)
28	}
29	f.Close()
30
31	type X struct {
32		fd  windows.Win32finddata
33		got byte
34		pad [10]byte // to protect ourselves
35
36	}
37	var want byte = 2 // it is unlikely to have this character in the filename
38	x := X{got: want}
39
40	pathp, _ := windows.UTF16PtrFromString(path)
41	h, err := windows.FindFirstFile(pathp, &(x.fd))
42	if err != nil {
43		t.Fatalf("FindFirstFile failed: %v", err)
44	}
45	err = windows.FindClose(h)
46	if err != nil {
47		t.Fatalf("FindClose failed: %v", err)
48	}
49
50	if x.got != want {
51		t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
52	}
53}
54
55func TestFormatMessage(t *testing.T) {
56	dll := windows.MustLoadDLL("netevent.dll")
57
58	const TITLE_SC_MESSAGE_BOX uint32 = 0xC0001B75
59	const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
60	buf := make([]uint16, 300)
61	_, err := windows.FormatMessage(flags, uintptr(dll.Handle), TITLE_SC_MESSAGE_BOX, 0, buf, nil)
62	if err != nil {
63		t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, TITLE_SC_MESSAGE_BOX, err)
64	}
65}
66
67func abort(funcname string, err error) {
68	panic(funcname + " failed: " + err.Error())
69}
70
71func ExampleLoadLibrary() {
72	h, err := windows.LoadLibrary("kernel32.dll")
73	if err != nil {
74		abort("LoadLibrary", err)
75	}
76	defer windows.FreeLibrary(h)
77	proc, err := windows.GetProcAddress(h, "GetVersion")
78	if err != nil {
79		abort("GetProcAddress", err)
80	}
81	r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
82	major := byte(r)
83	minor := uint8(r >> 8)
84	build := uint16(r >> 16)
85	print("windows version ", major, ".", minor, " (Build ", build, ")\n")
86}
87
88func TestTOKEN_ALL_ACCESS(t *testing.T) {
89	if windows.TOKEN_ALL_ACCESS != 0xF01FF {
90		t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", windows.TOKEN_ALL_ACCESS)
91	}
92}
93
94func TestCreateWellKnownSid(t *testing.T) {
95	sid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
96	if err != nil {
97		t.Fatalf("Unable to create well known sid for administrators: %v", err)
98	}
99	sidStr, err := sid.String()
100	if err != nil {
101		t.Fatalf("Unable to convert sid into string: %v", err)
102	}
103	if sidStr != "S-1-5-32-544" {
104		t.Fatalf("Expecting administrators to be S-1-5-32-544, but found %s instead", sidStr)
105	}
106}
107