github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/rfork_linux_test.go (about)

     1  // +build linux
     2  
     3  package sh
     4  
     5  import (
     6  	"bytes"
     7  	"strings"
     8  	"syscall"
     9  	"testing"
    10  )
    11  
    12  func getletters() string {
    13  	var a bytes.Buffer
    14  
    15  	for i := 'a'; i < 'z'; i++ {
    16  		a.Write(append([]byte{}, byte(i)))
    17  	}
    18  
    19  	all := string(a.Bytes())
    20  	allCap := strings.ToUpper(all)
    21  	return all + allCap
    22  }
    23  
    24  func getvalid() string {
    25  	return "cumnpsi"
    26  }
    27  
    28  func testTblFlagsOK(flagstr string, expected uintptr, t *testing.T) {
    29  	flags, err := getflags(flagstr)
    30  
    31  	if err != nil {
    32  		t.Error(err)
    33  		return
    34  	}
    35  
    36  	if flags != expected {
    37  		t.Errorf("Flags differ: expected %08x but %08x", expected, flags)
    38  		return
    39  	}
    40  }
    41  
    42  func TestRforkFlags(t *testing.T) {
    43  	_, err := getflags("")
    44  
    45  	if err == nil {
    46  		t.Error("Empty flags should return error")
    47  		return
    48  	}
    49  
    50  	_, err = getflags("a")
    51  
    52  	if err == nil {
    53  		t.Error("Unknow flag a")
    54  		return
    55  	}
    56  
    57  	allchars := getletters()
    58  
    59  	_, err = getflags(allchars)
    60  
    61  	if err == nil {
    62  		t.Error("Should fail")
    63  		return
    64  	}
    65  
    66  	testTblFlagsOK("u", syscall.CLONE_NEWUSER, t)
    67  	testTblFlagsOK("m", syscall.CLONE_NEWNS, t)
    68  	testTblFlagsOK("n", syscall.CLONE_NEWNET, t)
    69  	testTblFlagsOK("i", syscall.CLONE_NEWIPC, t)
    70  	testTblFlagsOK("s", syscall.CLONE_NEWUTS, t)
    71  	testTblFlagsOK("p", syscall.CLONE_NEWPID, t)
    72  	testTblFlagsOK("c", syscall.CLONE_NEWUSER|
    73  		syscall.CLONE_NEWNS|syscall.CLONE_NEWNET|
    74  		syscall.CLONE_NEWIPC|syscall.CLONE_NEWUTS|
    75  		syscall.CLONE_NEWUSER|syscall.CLONE_NEWPID, t)
    76  	testTblFlagsOK("um", syscall.CLONE_NEWUSER|syscall.CLONE_NEWNS, t)
    77  	testTblFlagsOK("umn", syscall.CLONE_NEWUSER|
    78  		syscall.CLONE_NEWNS|
    79  		syscall.CLONE_NEWNET, t)
    80  	testTblFlagsOK("umni", syscall.CLONE_NEWUSER|
    81  		syscall.CLONE_NEWNS|
    82  		syscall.CLONE_NEWNET|
    83  		syscall.CLONE_NEWIPC, t)
    84  	testTblFlagsOK("umnip", syscall.CLONE_NEWUSER|
    85  		syscall.CLONE_NEWNS|
    86  		syscall.CLONE_NEWNET|
    87  		syscall.CLONE_NEWIPC|
    88  		syscall.CLONE_NEWPID, t)
    89  	testTblFlagsOK("umnips", syscall.CLONE_NEWUSER|
    90  		syscall.CLONE_NEWNS|
    91  		syscall.CLONE_NEWNET|
    92  		syscall.CLONE_NEWIPC|
    93  		syscall.CLONE_NEWPID|
    94  		syscall.CLONE_NEWUTS, t)
    95  }