Source file src/cmd/go/go_unix_test.go

     1  // Copyright 2015 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  //go:build unix
     6  
     7  package main_test
     8  
     9  import (
    10  	"bufio"
    11  	"context"
    12  	"internal/testenv"
    13  	"io"
    14  	"os"
    15  	"os/exec"
    16  	"slices"
    17  	"strings"
    18  	"syscall"
    19  	"testing"
    20  )
    21  
    22  func TestGoBuildUmask(t *testing.T) {
    23  	// Do not use tg.parallel; avoid other tests seeing umask manipulation.
    24  	mask := syscall.Umask(0077) // prohibit low bits
    25  	defer syscall.Umask(mask)
    26  
    27  	tg := testgo(t)
    28  	defer tg.cleanup()
    29  	tg.tempFile("x.go", `package main; func main() {}`)
    30  
    31  	// We have set a umask, but if the parent directory happens to have a default
    32  	// ACL, the umask may be ignored. To prevent spurious failures from an ACL,
    33  	// we compare the file created by "go build" against a file written explicitly
    34  	// by os.WriteFile.
    35  	//
    36  	// (See https://go.dev/issue/62724, https://go.dev/issue/17909.)
    37  	control := tg.path("control")
    38  	tg.creatingTemp(control)
    39  	if err := os.WriteFile(control, []byte("#!/bin/sh\nexit 0"), 0777); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	cfi, err := os.Stat(control)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	exe := tg.path("x")
    48  	tg.creatingTemp(exe)
    49  	tg.run("build", "-o", exe, tg.path("x.go"))
    50  	fi, err := os.Stat(exe)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	got, want := fi.Mode(), cfi.Mode()
    55  	if got == want {
    56  		t.Logf("wrote x with mode %v", got)
    57  	} else {
    58  		t.Fatalf("wrote x with mode %v, wanted no 0077 bits (%v)", got, want)
    59  	}
    60  }
    61  
    62  // TestTestInterrupt verifies the fix for issue #60203.
    63  //
    64  // If the whole process group for a 'go test' invocation receives
    65  // SIGINT (as would be sent by pressing ^C on a console),
    66  // it should return quickly, not deadlock.
    67  func TestTestInterrupt(t *testing.T) {
    68  	if testing.Short() {
    69  		t.Skipf("skipping in short mode: test executes many subprocesses")
    70  	}
    71  	// Don't run this test in parallel, for the same reason.
    72  
    73  	tg := testgo(t)
    74  	defer tg.cleanup()
    75  	tg.setenv("GOROOT", testGOROOT)
    76  
    77  	ctx, cancel := context.WithCancel(context.Background())
    78  	cmd := testenv.CommandContext(t, ctx, tg.goTool(), "test", "std", "-short", "-count=1")
    79  	cmd.Dir = tg.execDir
    80  
    81  	// Override $TMPDIR when running the tests: since we're terminating the tests
    82  	// with a signal they might fail to clean up some temp files, and we don't
    83  	// want that to cause an "unexpected files" failure at the end of the run.
    84  	cmd.Env = append(slices.Clip(tg.env), tempEnvName()+"="+t.TempDir())
    85  
    86  	cmd.SysProcAttr = &syscall.SysProcAttr{
    87  		Setpgid: true,
    88  	}
    89  	cmd.Cancel = func() error {
    90  		pgid := cmd.Process.Pid
    91  		return syscall.Kill(-pgid, syscall.SIGINT)
    92  	}
    93  
    94  	pipe, err := cmd.StdoutPipe()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	t.Logf("running %v", cmd)
   100  	if err := cmd.Start(); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	stdout := new(strings.Builder)
   105  	r := bufio.NewReader(pipe)
   106  	line, err := r.ReadString('\n')
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	stdout.WriteString(line)
   111  
   112  	// The output line for some test was written, so we know things are in progress.
   113  	//
   114  	// Cancel the rest of the run by sending SIGINT to the process group:
   115  	// it should finish up and exit with a nonzero status,
   116  	// not have to be killed with SIGKILL.
   117  	cancel()
   118  
   119  	io.Copy(stdout, r)
   120  	if stdout.Len() > 0 {
   121  		t.Logf("stdout:\n%s", stdout)
   122  	}
   123  	err = cmd.Wait()
   124  
   125  	ee, _ := err.(*exec.ExitError)
   126  	if ee == nil {
   127  		t.Fatalf("unexpectedly finished with nonzero status")
   128  	}
   129  	if len(ee.Stderr) > 0 {
   130  		t.Logf("stderr:\n%s", ee.Stderr)
   131  	}
   132  	if !ee.Exited() {
   133  		t.Fatalf("'go test' did not exit after interrupt: %v", err)
   134  	}
   135  
   136  	t.Logf("interrupted tests without deadlocking")
   137  }
   138  

View as plain text