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.
4
5//go:build windows
6// +build windows
7
8package windows_test
9
10import (
11	"strings"
12	"syscall"
13	"testing"
14
15	"golang.org/x/sys/windows"
16)
17
18func testSetGetenv(t *testing.T, key, value string) {
19	err := windows.Setenv(key, value)
20	if err != nil {
21		t.Fatalf("Setenv failed to set %q: %v", value, err)
22	}
23	newvalue, found := windows.Getenv(key)
24	if !found {
25		t.Fatalf("Getenv failed to find %v variable (want value %q)", key, value)
26	}
27	if newvalue != value {
28		t.Fatalf("Getenv(%v) = %q; want %q", key, newvalue, value)
29	}
30}
31
32func TestEnv(t *testing.T) {
33	testSetGetenv(t, "TESTENV", "AVALUE")
34	// make sure TESTENV gets set to "", not deleted
35	testSetGetenv(t, "TESTENV", "")
36}
37
38func TestGetProcAddressByOrdinal(t *testing.T) {
39	// Attempt calling shlwapi.dll:IsOS, resolving it by ordinal, as
40	// suggested in
41	// https://msdn.microsoft.com/en-us/library/windows/desktop/bb773795.aspx
42	h, err := windows.LoadLibrary("shlwapi.dll")
43	if err != nil {
44		t.Fatalf("Failed to load shlwapi.dll: %s", err)
45	}
46	procIsOS, err := windows.GetProcAddressByOrdinal(h, 437)
47	if err != nil {
48		t.Fatalf("Could not find shlwapi.dll:IsOS by ordinal: %s", err)
49	}
50	const OS_NT = 1
51	r, _, _ := syscall.Syscall(procIsOS, 1, OS_NT, 0, 0)
52	if r == 0 {
53		t.Error("shlwapi.dll:IsOS(OS_NT) returned 0, expected non-zero value")
54	}
55}
56
57func TestGetSystemDirectory(t *testing.T) {
58	d, err := windows.GetSystemDirectory()
59	if err != nil {
60		t.Fatalf("Failed to get system directory: %s", err)
61	}
62	if !strings.HasSuffix(strings.ToLower(d), "\\system32") {
63		t.Fatalf("System directory does not end in system32: %s", d)
64	}
65}
66
67func TestGetWindowsDirectory(t *testing.T) {
68	d1, err := windows.GetWindowsDirectory()
69	if err != nil {
70		t.Fatalf("Failed to get Windows directory: %s", err)
71	}
72	d2, err := windows.GetSystemWindowsDirectory()
73	if err != nil {
74		t.Fatalf("Failed to get system Windows directory: %s", err)
75	}
76	if !strings.HasSuffix(strings.ToLower(d1), `\windows`) {
77		t.Fatalf("Windows directory does not end in windows: %s", d1)
78	}
79	if !strings.HasSuffix(strings.ToLower(d2), `\windows`) {
80		t.Fatalf("System Windows directory does not end in windows: %s", d2)
81	}
82}
83func TestFindProcByOrdinal(t *testing.T) {
84	// Attempt calling shlwapi.dll:IsOS, resolving it by ordinal, as
85	// suggested in
86	// https://msdn.microsoft.com/en-us/library/windows/desktop/bb773795.aspx
87	dll, err := windows.LoadDLL("shlwapi.dll")
88	if err != nil {
89		t.Fatalf("Failed to load shlwapi.dll: %s", err)
90	}
91	procIsOS, err := dll.FindProcByOrdinal(437)
92	if err != nil {
93		t.Fatalf("Could not find shlwapi.dll:IsOS by ordinal: %s", err)
94	}
95	if procIsOS.Name != "#437" {
96		t.Fatalf("Proc's name is incorrect: %s,expected #437", procIsOS.Name)
97	}
98	const OS_NT = 1
99	r, _, _ := syscall.Syscall(procIsOS.Addr(), 1, OS_NT, 0, 0)
100	if r == 0 {
101		t.Error("shlwapi.dll:IsOS(OS_NT) returned 0, expected non-zero value")
102	}
103}
104