github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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 "fmt" 9 "internal/testenv" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "strings" 14 "syscall" 15 "testing" 16 ) 17 18 func TestOpen_Dir(t *testing.T) { 19 dir := t.TempDir() 20 21 h, err := syscall.Open(dir, syscall.O_RDONLY, 0) 22 if err != nil { 23 t.Fatalf("Open failed: %v", err) 24 } 25 syscall.CloseHandle(h) 26 h, err = syscall.Open(dir, syscall.O_RDONLY|syscall.O_TRUNC, 0) 27 if err == nil { 28 t.Error("Open should have failed") 29 } else { 30 syscall.CloseHandle(h) 31 } 32 h, err = syscall.Open(dir, syscall.O_RDONLY|syscall.O_CREAT, 0) 33 if err == nil { 34 t.Error("Open should have failed") 35 } else { 36 syscall.CloseHandle(h) 37 } 38 } 39 40 func TestWin32finddata(t *testing.T) { 41 dir := t.TempDir() 42 43 path := filepath.Join(dir, "long_name.and_extension") 44 f, err := os.Create(path) 45 if err != nil { 46 t.Fatalf("failed to create %v: %v", path, err) 47 } 48 f.Close() 49 50 type X struct { 51 fd syscall.Win32finddata 52 got byte 53 pad [10]byte // to protect ourselves 54 55 } 56 var want byte = 2 // it is unlikely to have this character in the filename 57 x := X{got: want} 58 59 pathp, _ := syscall.UTF16PtrFromString(path) 60 h, err := syscall.FindFirstFile(pathp, &(x.fd)) 61 if err != nil { 62 t.Fatalf("FindFirstFile failed: %v", err) 63 } 64 err = syscall.FindClose(h) 65 if err != nil { 66 t.Fatalf("FindClose failed: %v", err) 67 } 68 69 if x.got != want { 70 t.Fatalf("memory corruption: want=%d got=%d", want, x.got) 71 } 72 } 73 74 func abort(funcname string, err error) { 75 panic(funcname + " failed: " + err.Error()) 76 } 77 78 func ExampleLoadLibrary() { 79 h, err := syscall.LoadLibrary("kernel32.dll") 80 if err != nil { 81 abort("LoadLibrary", err) 82 } 83 defer syscall.FreeLibrary(h) 84 proc, err := syscall.GetProcAddress(h, "GetVersion") 85 if err != nil { 86 abort("GetProcAddress", err) 87 } 88 r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0) 89 major := byte(r) 90 minor := uint8(r >> 8) 91 build := uint16(r >> 16) 92 print("windows version ", major, ".", minor, " (Build ", build, ")\n") 93 } 94 95 func TestTOKEN_ALL_ACCESS(t *testing.T) { 96 if syscall.TOKEN_ALL_ACCESS != 0xF01FF { 97 t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", syscall.TOKEN_ALL_ACCESS) 98 } 99 } 100 101 func TestStdioAreInheritable(t *testing.T) { 102 testenv.MustHaveGoBuild(t) 103 testenv.MustHaveCGO(t) 104 testenv.MustHaveExecPath(t, "gcc") 105 106 tmpdir := t.TempDir() 107 108 // build go dll 109 const dlltext = ` 110 package main 111 112 import "C" 113 import ( 114 "fmt" 115 ) 116 117 //export HelloWorld 118 func HelloWorld() { 119 fmt.Println("Hello World") 120 } 121 122 func main() {} 123 ` 124 dllsrc := filepath.Join(tmpdir, "helloworld.go") 125 err := os.WriteFile(dllsrc, []byte(dlltext), 0644) 126 if err != nil { 127 t.Fatal(err) 128 } 129 dll := filepath.Join(tmpdir, "helloworld.dll") 130 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dll, "-buildmode", "c-shared", dllsrc) 131 out, err := testenv.CleanCmdEnv(cmd).CombinedOutput() 132 if err != nil { 133 t.Fatalf("failed to build go library: %s\n%s", err, out) 134 } 135 136 // build c exe 137 const exetext = ` 138 #include <stdlib.h> 139 #include <windows.h> 140 int main(int argc, char *argv[]) 141 { 142 system("hostname"); 143 ((void(*)(void))GetProcAddress(LoadLibraryA(%q), "HelloWorld"))(); 144 system("hostname"); 145 return 0; 146 } 147 ` 148 exe := filepath.Join(tmpdir, "helloworld.exe") 149 cmd = exec.Command("gcc", "-o", exe, "-xc", "-") 150 cmd.Stdin = strings.NewReader(fmt.Sprintf(exetext, dll)) 151 out, err = testenv.CleanCmdEnv(cmd).CombinedOutput() 152 if err != nil { 153 t.Fatalf("failed to build c executable: %s\n%s", err, out) 154 } 155 out, err = exec.Command(exe).Output() 156 if err != nil { 157 t.Fatalf("c program execution failed: %v: %v", err, string(out)) 158 } 159 160 hostname, err := os.Hostname() 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 have := strings.ReplaceAll(string(out), "\n", "") 166 have = strings.ReplaceAll(have, "\r", "") 167 want := fmt.Sprintf("%sHello World%s", hostname, hostname) 168 if have != want { 169 t.Fatalf("c program output is wrong: got %q, want %q", have, want) 170 } 171 } 172 173 func FuzzUTF16FromString(f *testing.F) { 174 f.Add("hi") // ASCII 175 f.Add("รข") // latin1 176 f.Add("ใญใ") // plane 0 177 f.Add("๐") // extra Plane 0 178 f.Add("\x90") // invalid byte 179 f.Add("\xe3\x81") // truncated 180 f.Add("\xe3\xc1\x81") // invalid middle byte 181 182 f.Fuzz(func(t *testing.T, tst string) { 183 res, err := syscall.UTF16FromString(tst) 184 if err != nil { 185 if strings.Contains(tst, "\x00") { 186 t.Skipf("input %q contains a NUL byte", tst) 187 } 188 t.Fatalf("UTF16FromString(%q): %v", tst, err) 189 } 190 t.Logf("UTF16FromString(%q) = %04x", tst, res) 191 192 if len(res) < 1 || res[len(res)-1] != 0 { 193 t.Fatalf("missing NUL terminator") 194 } 195 if len(res) > len(tst)+1 { 196 t.Fatalf("len(%04x) > len(%q)+1", res, tst) 197 } 198 }) 199 }