syscall_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. }