Ian Lance Taylor | f8f0cb4 | 2013-11-06 11:23:33 -0800 | [diff] [blame] | 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 ( |
Ian Lance Taylor | 93a1102 | 2015-01-14 16:11:24 -0800 | [diff] [blame] | 8 | "fmt" |
Ian Lance Taylor | b156d71 | 2016-07-22 07:41:34 -0700 | [diff] [blame] | 9 | "internal/testenv" |
| 10 | "os" |
Ian Lance Taylor | 0ba4563 | 2017-01-13 11:17:23 -0800 | [diff] [blame] | 11 | "runtime" |
Ian Lance Taylor | f8f0cb4 | 2013-11-06 11:23:33 -0800 | [diff] [blame] | 12 | "syscall" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func 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 | |
| 30 | func TestEnv(t *testing.T) { |
| 31 | testSetGetenv(t, "TESTENV", "AVALUE") |
| 32 | // make sure TESTENV gets set to "", not deleted |
| 33 | testSetGetenv(t, "TESTENV", "") |
| 34 | } |
Ian Lance Taylor | 93a1102 | 2015-01-14 16:11:24 -0800 | [diff] [blame] | 35 | |
| 36 | func 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 | } |
Ian Lance Taylor | b156d71 | 2016-07-22 07:41:34 -0700 | [diff] [blame] | 51 | |
| 52 | // Check that permuting child process fds doesn't interfere with |
| 53 | // reporting of fork/exec status. See Issue 14979. |
| 54 | func 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 | } |
Ian Lance Taylor | 0ba4563 | 2017-01-13 11:17:23 -0800 | [diff] [blame] | 63 | |
| 64 | func TestGettimeofday(t *testing.T) { |
Ian Lance Taylor | c2225a7 | 2020-01-02 15:05:27 -0800 | [diff] [blame] | 65 | if runtime.GOOS == "js" { |
Ian Lance Taylor | 7b25b4d | 2018-08-26 18:14:49 -0700 | [diff] [blame] | 66 | t.Skip("not implemented on " + runtime.GOOS) |
Ian Lance Taylor | 0ba4563 | 2017-01-13 11:17:23 -0800 | [diff] [blame] | 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 | } |