syscall_test.go 2.2 KB

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