Source file src/runtime/signal_windows_test.go

     1  //go:build windows
     2  
     3  package runtime_test
     4  
     5  import (
     6  	"bufio"
     7  	"bytes"
     8  	"fmt"
     9  	"internal/testenv"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"runtime"
    13  	"strings"
    14  	"syscall"
    15  	"testing"
    16  )
    17  
    18  func TestVectoredHandlerDontCrashOnLibrary(t *testing.T) {
    19  	if *flagQuick {
    20  		t.Skip("-quick")
    21  	}
    22  	if runtime.GOARCH != "amd64" {
    23  		t.Skip("this test can only run on windows/amd64")
    24  	}
    25  	testenv.MustHaveGoBuild(t)
    26  	testenv.MustHaveCGO(t)
    27  	testenv.MustHaveExecPath(t, "gcc")
    28  	testprog.Lock()
    29  	defer testprog.Unlock()
    30  	dir := t.TempDir()
    31  
    32  	// build go dll
    33  	dll := filepath.Join(dir, "testwinlib.dll")
    34  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dll, "-buildmode", "c-shared", "testdata/testwinlib/main.go")
    35  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
    36  	if err != nil {
    37  		t.Fatalf("failed to build go library: %s\n%s", err, out)
    38  	}
    39  
    40  	// build c program
    41  	exe := filepath.Join(dir, "test.exe")
    42  	cmd = exec.Command("gcc", "-L"+dir, "-I"+dir, "-ltestwinlib", "-o", exe, "testdata/testwinlib/main.c")
    43  	out, err = testenv.CleanCmdEnv(cmd).CombinedOutput()
    44  	if err != nil {
    45  		t.Fatalf("failed to build c exe: %s\n%s", err, out)
    46  	}
    47  
    48  	// run test program
    49  	cmd = exec.Command(exe)
    50  	out, err = testenv.CleanCmdEnv(cmd).CombinedOutput()
    51  	if err != nil {
    52  		t.Fatalf("failure while running executable: %s\n%s", err, out)
    53  	}
    54  	expectedOutput := "exceptionCount: 1\ncontinueCount: 1\n"
    55  	// cleaning output
    56  	cleanedOut := strings.ReplaceAll(string(out), "\r\n", "\n")
    57  	if cleanedOut != expectedOutput {
    58  		t.Errorf("expected output %q, got %q", expectedOutput, cleanedOut)
    59  	}
    60  }
    61  
    62  func sendCtrlBreak(pid int) error {
    63  	kernel32, err := syscall.LoadDLL("kernel32.dll")
    64  	if err != nil {
    65  		return fmt.Errorf("LoadDLL: %v\n", err)
    66  	}
    67  	generateEvent, err := kernel32.FindProc("GenerateConsoleCtrlEvent")
    68  	if err != nil {
    69  		return fmt.Errorf("FindProc: %v\n", err)
    70  	}
    71  	result, _, err := generateEvent.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid))
    72  	if result == 0 {
    73  		return fmt.Errorf("GenerateConsoleCtrlEvent: %v\n", err)
    74  	}
    75  	return nil
    76  }
    77  
    78  // TestCtrlHandler tests that Go can gracefully handle closing the console window.
    79  // See https://golang.org/issues/41884.
    80  func TestCtrlHandler(t *testing.T) {
    81  	testenv.MustHaveGoBuild(t)
    82  	t.Parallel()
    83  
    84  	// build go program
    85  	exe := filepath.Join(t.TempDir(), "test.exe")
    86  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "testdata/testwinsignal/main.go")
    87  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
    88  	if err != nil {
    89  		t.Fatalf("failed to build go exe: %v\n%s", err, out)
    90  	}
    91  
    92  	// run test program
    93  	cmd = exec.Command(exe)
    94  	var stdout bytes.Buffer
    95  	var stderr bytes.Buffer
    96  	cmd.Stdout = &stdout
    97  	cmd.Stderr = &stderr
    98  	inPipe, err := cmd.StdinPipe()
    99  	if err != nil {
   100  		t.Fatalf("Failed to create stdin pipe: %v", err)
   101  	}
   102  	// keep inPipe alive until the end of the test
   103  	defer inPipe.Close()
   104  
   105  	// in a new command window
   106  	const _CREATE_NEW_CONSOLE = 0x00000010
   107  	cmd.SysProcAttr = &syscall.SysProcAttr{
   108  		CreationFlags: _CREATE_NEW_CONSOLE,
   109  		HideWindow:    true,
   110  	}
   111  	if err := cmd.Start(); err != nil {
   112  		t.Fatalf("Start failed: %v", err)
   113  	}
   114  	defer func() {
   115  		cmd.Process.Kill()
   116  		cmd.Wait()
   117  	}()
   118  
   119  	// check child exited gracefully, did not timeout
   120  	if err := cmd.Wait(); err != nil {
   121  		t.Fatalf("Program exited with error: %v\n%s", err, &stderr)
   122  	}
   123  
   124  	// check child received, handled SIGTERM
   125  	if expected, got := syscall.SIGTERM.String(), strings.TrimSpace(stdout.String()); expected != got {
   126  		t.Fatalf("Expected '%s' got: %s", expected, got)
   127  	}
   128  }
   129  
   130  // TestLibraryCtrlHandler tests that Go DLL allows calling program to handle console control events.
   131  // See https://golang.org/issues/35965.
   132  func TestLibraryCtrlHandler(t *testing.T) {
   133  	if *flagQuick {
   134  		t.Skip("-quick")
   135  	}
   136  	if runtime.GOARCH != "amd64" {
   137  		t.Skip("this test can only run on windows/amd64")
   138  	}
   139  	testenv.MustHaveGoBuild(t)
   140  	testenv.MustHaveCGO(t)
   141  	testenv.MustHaveExecPath(t, "gcc")
   142  	testprog.Lock()
   143  	defer testprog.Unlock()
   144  	dir := t.TempDir()
   145  
   146  	// build go dll
   147  	dll := filepath.Join(dir, "dummy.dll")
   148  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dll, "-buildmode", "c-shared", "testdata/testwinlibsignal/dummy.go")
   149  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
   150  	if err != nil {
   151  		t.Fatalf("failed to build go library: %s\n%s", err, out)
   152  	}
   153  
   154  	// build c program
   155  	exe := filepath.Join(dir, "test.exe")
   156  	cmd = exec.Command("gcc", "-o", exe, "testdata/testwinlibsignal/main.c")
   157  	out, err = testenv.CleanCmdEnv(cmd).CombinedOutput()
   158  	if err != nil {
   159  		t.Fatalf("failed to build c exe: %s\n%s", err, out)
   160  	}
   161  
   162  	// run test program
   163  	cmd = exec.Command(exe)
   164  	var stderr bytes.Buffer
   165  	cmd.Stderr = &stderr
   166  	outPipe, err := cmd.StdoutPipe()
   167  	if err != nil {
   168  		t.Fatalf("Failed to create stdout pipe: %v", err)
   169  	}
   170  	outReader := bufio.NewReader(outPipe)
   171  
   172  	cmd.SysProcAttr = &syscall.SysProcAttr{
   173  		CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
   174  	}
   175  	if err := cmd.Start(); err != nil {
   176  		t.Fatalf("Start failed: %v", err)
   177  	}
   178  
   179  	errCh := make(chan error, 1)
   180  	go func() {
   181  		if line, err := outReader.ReadString('\n'); err != nil {
   182  			errCh <- fmt.Errorf("could not read stdout: %v", err)
   183  		} else if strings.TrimSpace(line) != "ready" {
   184  			errCh <- fmt.Errorf("unexpected message: %v", line)
   185  		} else {
   186  			errCh <- sendCtrlBreak(cmd.Process.Pid)
   187  		}
   188  	}()
   189  
   190  	if err := <-errCh; err != nil {
   191  		t.Fatal(err)
   192  	}
   193  	if err := cmd.Wait(); err != nil {
   194  		t.Fatalf("Program exited with error: %v\n%s", err, &stderr)
   195  	}
   196  }
   197  

View as plain text