Source file src/syscall/syscall_test.go

     1  // Copyright 2013 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  package syscall_test
     6  
     7  import (
     8  	"internal/testenv"
     9  	"os"
    10  	"runtime"
    11  	"syscall"
    12  	"testing"
    13  )
    14  
    15  // Errno must implement error on all platforms, including plan9.
    16  var _ error = syscall.Errno(0)
    17  
    18  func testSetGetenv(t *testing.T, key, value string) {
    19  	err := syscall.Setenv(key, value)
    20  	if err != nil {
    21  		t.Fatalf("Setenv failed to set %q: %v", value, err)
    22  	}
    23  	newvalue, found := syscall.Getenv(key)
    24  	if !found {
    25  		t.Fatalf("Getenv failed to find %v variable (want value %q)", key, value)
    26  	}
    27  	if newvalue != value {
    28  		t.Fatalf("Getenv(%v) = %q; want %q", key, newvalue, value)
    29  	}
    30  }
    31  
    32  func TestEnv(t *testing.T) {
    33  	testSetGetenv(t, "TESTENV", "AVALUE")
    34  	// make sure TESTENV gets set to "", not deleted
    35  	testSetGetenv(t, "TESTENV", "")
    36  }
    37  
    38  // Check that permuting child process fds doesn't interfere with
    39  // reporting of fork/exec status. See Issue 14979.
    40  func TestExecErrPermutedFds(t *testing.T) {
    41  	testenv.MustHaveExec(t)
    42  
    43  	attr := &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stderr, os.Stdout}}
    44  	_, err := os.StartProcess("/", []string{"/"}, attr)
    45  	if err == nil {
    46  		t.Fatalf("StartProcess of invalid program returned err = nil")
    47  	}
    48  }
    49  
    50  func TestGettimeofday(t *testing.T) {
    51  	if runtime.GOOS == "js" {
    52  		t.Skip("not implemented on " + runtime.GOOS)
    53  	}
    54  	tv := &syscall.Timeval{}
    55  	if err := syscall.Gettimeofday(tv); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if tv.Sec == 0 && tv.Usec == 0 {
    59  		t.Fatal("Sec and Usec both zero")
    60  	}
    61  }
    62  

View as plain text