github.com/rafaeltorres324/go/src@v0.0.0-20210519164414-9fdf653a9838/syscall/syscall_windows_test.go (about) 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 5 package syscall_test 6 7 import ( 8 "os" 9 "path/filepath" 10 "syscall" 11 "testing" 12 ) 13 14 func TestWin32finddata(t *testing.T) { 15 dir, err := os.MkdirTemp("", "go-build") 16 if err != nil { 17 t.Fatalf("failed to create temp directory: %v", err) 18 } 19 defer os.RemoveAll(dir) 20 21 path := filepath.Join(dir, "long_name.and_extension") 22 f, err := os.Create(path) 23 if err != nil { 24 t.Fatalf("failed to create %v: %v", path, err) 25 } 26 f.Close() 27 28 type X struct { 29 fd syscall.Win32finddata 30 got byte 31 pad [10]byte // to protect ourselves 32 33 } 34 var want byte = 2 // it is unlikely to have this character in the filename 35 x := X{got: want} 36 37 pathp, _ := syscall.UTF16PtrFromString(path) 38 h, err := syscall.FindFirstFile(pathp, &(x.fd)) 39 if err != nil { 40 t.Fatalf("FindFirstFile failed: %v", err) 41 } 42 err = syscall.FindClose(h) 43 if err != nil { 44 t.Fatalf("FindClose failed: %v", err) 45 } 46 47 if x.got != want { 48 t.Fatalf("memory corruption: want=%d got=%d", want, x.got) 49 } 50 } 51 52 func abort(funcname string, err error) { 53 panic(funcname + " failed: " + err.Error()) 54 } 55 56 func ExampleLoadLibrary() { 57 h, err := syscall.LoadLibrary("kernel32.dll") 58 if err != nil { 59 abort("LoadLibrary", err) 60 } 61 defer syscall.FreeLibrary(h) 62 proc, err := syscall.GetProcAddress(h, "GetVersion") 63 if err != nil { 64 abort("GetProcAddress", err) 65 } 66 r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0) 67 major := byte(r) 68 minor := uint8(r >> 8) 69 build := uint16(r >> 16) 70 print("windows version ", major, ".", minor, " (Build ", build, ")\n") 71 } 72 73 func TestTOKEN_ALL_ACCESS(t *testing.T) { 74 if syscall.TOKEN_ALL_ACCESS != 0xF01FF { 75 t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", syscall.TOKEN_ALL_ACCESS) 76 } 77 }