github.com/yukk001/go1.10.8@v0.0.0-20190813125351-6df2d3982e20/src/os/exec/exec_posix_test.go (about)

     1  // Copyright 2017 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  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package exec_test
     8  
     9  import (
    10  	"os/user"
    11  	"strconv"
    12  	"syscall"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestCredentialNoSetGroups(t *testing.T) {
    18  	u, err := user.Current()
    19  	if err != nil {
    20  		t.Fatalf("error getting current user: %v", err)
    21  	}
    22  
    23  	uid, err := strconv.Atoi(u.Uid)
    24  	if err != nil {
    25  		t.Fatalf("error converting Uid=%s to integer: %v", u.Uid, err)
    26  	}
    27  
    28  	gid, err := strconv.Atoi(u.Gid)
    29  	if err != nil {
    30  		t.Fatalf("error converting Gid=%s to integer: %v", u.Gid, err)
    31  	}
    32  
    33  	// If NoSetGroups is true, setgroups isn't called and cmd.Run should succeed
    34  	cmd := helperCommand(t, "echo", "foo")
    35  	cmd.SysProcAttr = &syscall.SysProcAttr{
    36  		Credential: &syscall.Credential{
    37  			Uid:         uint32(uid),
    38  			Gid:         uint32(gid),
    39  			NoSetGroups: true,
    40  		},
    41  	}
    42  
    43  	if err = cmd.Run(); err != nil {
    44  		t.Errorf("Failed to run command: %v", err)
    45  	}
    46  }
    47  
    48  // For issue #19314: make sure that SIGSTOP does not cause the process
    49  // to appear done.
    50  func TestWaitid(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	cmd := helperCommand(t, "sleep")
    54  	if err := cmd.Start(); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	// The sleeps here are unnecessary in the sense that the test
    59  	// should still pass, but they are useful to make it more
    60  	// likely that we are testing the expected state of the child.
    61  	time.Sleep(100 * time.Millisecond)
    62  
    63  	if err := cmd.Process.Signal(syscall.SIGSTOP); err != nil {
    64  		cmd.Process.Kill()
    65  		t.Fatal(err)
    66  	}
    67  
    68  	ch := make(chan error)
    69  	go func() {
    70  		ch <- cmd.Wait()
    71  	}()
    72  
    73  	time.Sleep(100 * time.Millisecond)
    74  
    75  	if err := cmd.Process.Signal(syscall.SIGCONT); err != nil {
    76  		t.Error(err)
    77  		syscall.Kill(cmd.Process.Pid, syscall.SIGCONT)
    78  	}
    79  
    80  	cmd.Process.Kill()
    81  
    82  	<-ch
    83  }