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
5package syscall_test
6
7import (
8	"fmt"
9	"internal/testenv"
10	"os"
11	"runtime"
12	"syscall"
13	"testing"
14)
15
16func testSetGetenv(t *testing.T, key, value string) {
17	err := syscall.Setenv(key, value)
18	if err != nil {
19		t.Fatalf("Setenv failed to set %q: %v", value, err)
20	}
21	newvalue, found := syscall.Getenv(key)
22	if !found {
23		t.Fatalf("Getenv failed to find %v variable (want value %q)", key, value)
24	}
25	if newvalue != value {
26		t.Fatalf("Getenv(%v) = %q; want %q", key, newvalue, value)
27	}
28}
29
30func TestEnv(t *testing.T) {
31	testSetGetenv(t, "TESTENV", "AVALUE")
32	// make sure TESTENV gets set to "", not deleted
33	testSetGetenv(t, "TESTENV", "")
34}
35
36func TestItoa(t *testing.T) {
37	// Make most negative integer: 0x8000...
38	i := 1
39	for i<<1 != 0 {
40		i <<= 1
41	}
42	if i >= 0 {
43		t.Fatal("bad math")
44	}
45	s := syscall.Itoa(i)
46	f := fmt.Sprint(i)
47	if s != f {
48		t.Fatalf("itoa(%d) = %s, want %s", i, s, f)
49	}
50}
51
52// Check that permuting child process fds doesn't interfere with
53// reporting of fork/exec status. See Issue 14979.
54func TestExecErrPermutedFds(t *testing.T) {
55	testenv.MustHaveExec(t)
56
57	attr := &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stderr, os.Stdout}}
58	_, err := os.StartProcess("/", []string{"/"}, attr)
59	if err == nil {
60		t.Fatalf("StartProcess of invalid program returned err = nil")
61	}
62}
63
64func TestGettimeofday(t *testing.T) {
65	if runtime.GOOS == "nacl" || runtime.GOOS == "js" {
66		t.Skip("not implemented on " + runtime.GOOS)
67	}
68	tv := &syscall.Timeval{}
69	if err := syscall.Gettimeofday(tv); err != nil {
70		t.Fatal(err)
71	}
72	if tv.Sec == 0 && tv.Usec == 0 {
73		t.Fatal("Sec and Usec both zero")
74	}
75}
76