syscall_windows_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2012 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. package windows_test
  5. import (
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "syscall"
  10. "testing"
  11. "golang.org/x/sys/windows"
  12. )
  13. func TestWin32finddata(t *testing.T) {
  14. dir, err := ioutil.TempDir("", "go-build")
  15. if err != nil {
  16. t.Fatalf("failed to create temp directory: %v", err)
  17. }
  18. defer os.RemoveAll(dir)
  19. path := filepath.Join(dir, "long_name.and_extension")
  20. f, err := os.Create(path)
  21. if err != nil {
  22. t.Fatalf("failed to create %v: %v", path, err)
  23. }
  24. f.Close()
  25. type X struct {
  26. fd windows.Win32finddata
  27. got byte
  28. pad [10]byte // to protect ourselves
  29. }
  30. var want byte = 2 // it is unlikely to have this character in the filename
  31. x := X{got: want}
  32. pathp, _ := windows.UTF16PtrFromString(path)
  33. h, err := windows.FindFirstFile(pathp, &(x.fd))
  34. if err != nil {
  35. t.Fatalf("FindFirstFile failed: %v", err)
  36. }
  37. err = windows.FindClose(h)
  38. if err != nil {
  39. t.Fatalf("FindClose failed: %v", err)
  40. }
  41. if x.got != want {
  42. t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
  43. }
  44. }
  45. func TestFormatMessage(t *testing.T) {
  46. dll := windows.MustLoadDLL("netevent.dll")
  47. const TITLE_SC_MESSAGE_BOX uint32 = 0xC0001B75
  48. const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
  49. buf := make([]uint16, 300)
  50. _, err := windows.FormatMessage(flags, uintptr(dll.Handle), TITLE_SC_MESSAGE_BOX, 0, buf, nil)
  51. if err != nil {
  52. t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, TITLE_SC_MESSAGE_BOX, err)
  53. }
  54. }
  55. func abort(funcname string, err error) {
  56. panic(funcname + " failed: " + err.Error())
  57. }
  58. func ExampleLoadLibrary() {
  59. h, err := windows.LoadLibrary("kernel32.dll")
  60. if err != nil {
  61. abort("LoadLibrary", err)
  62. }
  63. defer windows.FreeLibrary(h)
  64. proc, err := windows.GetProcAddress(h, "GetVersion")
  65. if err != nil {
  66. abort("GetProcAddress", err)
  67. }
  68. r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
  69. major := byte(r)
  70. minor := uint8(r >> 8)
  71. build := uint16(r >> 16)
  72. print("windows version ", major, ".", minor, " (Build ", build, ")\n")
  73. }
  74. func TestTOKEN_ALL_ACCESS(t *testing.T) {
  75. if windows.TOKEN_ALL_ACCESS != 0xF01FF {
  76. t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", windows.TOKEN_ALL_ACCESS)
  77. }
  78. }
  79. func TestCreateWellKnownSid(t *testing.T) {
  80. sid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
  81. if err != nil {
  82. t.Fatalf("Unable to create well known sid for administrators: %v", err)
  83. }
  84. sidStr, err := sid.String()
  85. if err != nil {
  86. t.Fatalf("Unable to convert sid into string: %v", err)
  87. }
  88. if sidStr != "S-1-5-32-544" {
  89. t.Fatalf("Expecting administrators to be S-1-5-32-544, but found %s instead", sidStr)
  90. }
  91. }