golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/windows/syscall_test.go (about)

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