You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 lines
3.0 KiB

  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. "unsafe"
  12. "golang.org/x/sys/windows"
  13. )
  14. func TestWin32finddata(t *testing.T) {
  15. dir, err := ioutil.TempDir("", "go-build")
  16. if err != nil {
  17. t.Fatalf("failed to create temp directory: %v", err)
  18. }
  19. defer os.RemoveAll(dir)
  20. path := filepath.Join(dir, "long_name.and_extension")
  21. f, err := os.Create(path)
  22. if err != nil {
  23. t.Fatalf("failed to create %v: %v", path, err)
  24. }
  25. f.Close()
  26. type X struct {
  27. fd windows.Win32finddata
  28. got byte
  29. pad [10]byte // to protect ourselves
  30. }
  31. var want byte = 2 // it is unlikely to have this character in the filename
  32. x := X{got: want}
  33. pathp, _ := windows.UTF16PtrFromString(path)
  34. h, err := windows.FindFirstFile(pathp, &(x.fd))
  35. if err != nil {
  36. t.Fatalf("FindFirstFile failed: %v", err)
  37. }
  38. err = windows.FindClose(h)
  39. if err != nil {
  40. t.Fatalf("FindClose failed: %v", err)
  41. }
  42. if x.got != want {
  43. t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
  44. }
  45. }
  46. func TestFormatMessage(t *testing.T) {
  47. dll := windows.MustLoadDLL("pdh.dll")
  48. pdhOpenQuery := func(datasrc *uint16, userdata uint32, query *windows.Handle) (errno uintptr) {
  49. r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhOpenQueryW").Addr(), 3, uintptr(unsafe.Pointer(datasrc)), uintptr(userdata), uintptr(unsafe.Pointer(query)))
  50. return r0
  51. }
  52. pdhCloseQuery := func(query windows.Handle) (errno uintptr) {
  53. r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhCloseQuery").Addr(), 1, uintptr(query), 0, 0)
  54. return r0
  55. }
  56. var q windows.Handle
  57. name, err := windows.UTF16PtrFromString("no_such_source")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. errno := pdhOpenQuery(name, 0, &q)
  62. if errno == 0 {
  63. pdhCloseQuery(q)
  64. t.Fatal("PdhOpenQuery succeeded, but expected to fail.")
  65. }
  66. const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
  67. buf := make([]uint16, 300)
  68. _, err = windows.FormatMessage(flags, uintptr(dll.Handle), uint32(errno), 0, buf, nil)
  69. if err != nil {
  70. t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, errno, err)
  71. }
  72. }
  73. func abort(funcname string, err error) {
  74. panic(funcname + " failed: " + err.Error())
  75. }
  76. func ExampleLoadLibrary() {
  77. h, err := windows.LoadLibrary("kernel32.dll")
  78. if err != nil {
  79. abort("LoadLibrary", err)
  80. }
  81. defer windows.FreeLibrary(h)
  82. proc, err := windows.GetProcAddress(h, "GetVersion")
  83. if err != nil {
  84. abort("GetProcAddress", err)
  85. }
  86. r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
  87. major := byte(r)
  88. minor := uint8(r >> 8)
  89. build := uint16(r >> 16)
  90. print("windows version ", major, ".", minor, " (Build ", build, ")\n")
  91. }
  92. func TestTOKEN_ALL_ACCESS(t *testing.T) {
  93. if windows.TOKEN_ALL_ACCESS != 0xF01FF {
  94. t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", windows.TOKEN_ALL_ACCESS)
  95. }
  96. }