Source file src/syscall/syscall_windows_test.go

     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 TestWin32finddata(t *testing.T) {
    19  	dir := t.TempDir()
    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  }
    78  
    79  func TestStdioAreInheritable(t *testing.T) {
    80  	testenv.MustHaveGoBuild(t)
    81  	testenv.MustHaveCGO(t)
    82  	testenv.MustHaveExecPath(t, "gcc")
    83  
    84  	tmpdir := t.TempDir()
    85  
    86  	// build go dll
    87  	const dlltext = `
    88  package main
    89  
    90  import "C"
    91  import (
    92  	"fmt"
    93  )
    94  
    95  //export HelloWorld
    96  func HelloWorld() {
    97  	fmt.Println("Hello World")
    98  }
    99  
   100  func main() {}
   101  `
   102  	dllsrc := filepath.Join(tmpdir, "helloworld.go")
   103  	err := os.WriteFile(dllsrc, []byte(dlltext), 0644)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	dll := filepath.Join(tmpdir, "helloworld.dll")
   108  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dll, "-buildmode", "c-shared", dllsrc)
   109  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   110  	if err != nil {
   111  		t.Fatalf("failed to build go library: %s\n%s", err, out)
   112  	}
   113  
   114  	// build c exe
   115  	const exetext = `
   116  #include <stdlib.h>
   117  #include <windows.h>
   118  int main(int argc, char *argv[])
   119  {
   120  	system("hostname");
   121  	((void(*)(void))GetProcAddress(LoadLibraryA(%q), "HelloWorld"))();
   122  	system("hostname");
   123  	return 0;
   124  }
   125  `
   126  	exe := filepath.Join(tmpdir, "helloworld.exe")
   127  	cmd = exec.Command("gcc", "-o", exe, "-xc", "-")
   128  	cmd.Stdin = strings.NewReader(fmt.Sprintf(exetext, dll))
   129  	out, err = testenv.CleanCmdEnv(cmd).CombinedOutput()
   130  	if err != nil {
   131  		t.Fatalf("failed to build c executable: %s\n%s", err, out)
   132  	}
   133  	out, err = exec.Command(exe).Output()
   134  	if err != nil {
   135  		t.Fatalf("c program execution failed: %v: %v", err, string(out))
   136  	}
   137  
   138  	hostname, err := os.Hostname()
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	have := strings.ReplaceAll(string(out), "\n", "")
   144  	have = strings.ReplaceAll(have, "\r", "")
   145  	want := fmt.Sprintf("%sHello World%s", hostname, hostname)
   146  	if have != want {
   147  		t.Fatalf("c program output is wrong: got %q, want %q", have, want)
   148  	}
   149  }
   150  

View as plain text