1 // Copyright 2013 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.
14 "golang.org/x/sys/windows"
17 func testSetGetenv(t *testing.T, key, value string) {
18 err := windows.Setenv(key, value)
20 t.Fatalf("Setenv failed to set %q: %v", value, err)
22 newvalue, found := windows.Getenv(key)
24 t.Fatalf("Getenv failed to find %v variable (want value %q)", key, value)
26 if newvalue != value {
27 t.Fatalf("Getenv(%v) = %q; want %q", key, newvalue, value)
31 func TestEnv(t *testing.T) {
32 testSetGetenv(t, "TESTENV", "AVALUE")
33 // make sure TESTENV gets set to "", not deleted
34 testSetGetenv(t, "TESTENV", "")
37 func TestGetProcAddressByOrdinal(t *testing.T) {
38 // Attempt calling shlwapi.dll:IsOS, resolving it by ordinal, as
40 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb773795.aspx
41 h, err := windows.LoadLibrary("shlwapi.dll")
43 t.Fatalf("Failed to load shlwapi.dll: %s", err)
45 procIsOS, err := windows.GetProcAddressByOrdinal(h, 437)
47 t.Fatalf("Could not find shlwapi.dll:IsOS by ordinal: %s", err)
50 r, _, _ := syscall.Syscall(procIsOS, 1, OS_NT, 0, 0)
52 t.Error("shlwapi.dll:IsOS(OS_NT) returned 0, expected non-zero value")
56 func TestGetSystemDirectory(t *testing.T) {
57 d, err := windows.GetSystemDirectory()
59 t.Fatalf("Failed to get system directory: %s", err)
61 if !strings.HasSuffix(strings.ToLower(d), "\\system32") {
62 t.Fatalf("System directory does not end in system32: %s", d)
66 func TestGetWindowsDirectory(t *testing.T) {
67 d1, err := windows.GetWindowsDirectory()
69 t.Fatalf("Failed to get Windows directory: %s", err)
71 d2, err := windows.GetSystemWindowsDirectory()
73 t.Fatalf("Failed to get system Windows directory: %s", err)
75 if !strings.HasSuffix(strings.ToLower(d1), `\windows`) {
76 t.Fatalf("Windows directory does not end in windows: %s", d1)
78 if !strings.HasSuffix(strings.ToLower(d2), `\windows`) {
79 t.Fatalf("System Windows directory does not end in windows: %s", d2)
82 func TestFindProcByOrdinal(t *testing.T) {
83 // Attempt calling shlwapi.dll:IsOS, resolving it by ordinal, as
85 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb773795.aspx
86 dll, err := windows.LoadDLL("shlwapi.dll")
88 t.Fatalf("Failed to load shlwapi.dll: %s", err)
90 procIsOS, err := dll.FindProcByOrdinal(437)
92 t.Fatalf("Could not find shlwapi.dll:IsOS by ordinal: %s", err)
94 if procIsOS.Name != "#437" {
95 t.Fatalf("Proc's name is incorrect: %s,expected #437", procIsOS.Name)
98 r, _, _ := syscall.Syscall(procIsOS.Addr(), 1, OS_NT, 0, 0)
100 t.Error("shlwapi.dll:IsOS(OS_NT) returned 0, expected non-zero value")