Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/exec/exec_posix_test.go

Documentation: os/exec

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

View as plain text