Source file src/os/exec/exec_posix_test.go

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

View as plain text