blob: 5390f8aace996bbe364c39fa7891371925d143bb [file] [log] [blame]
Ian Lance Taylorf8f0cb42013-11-06 11:23:33 -08001// 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 (
Ian Lance Taylor93a11022015-01-14 16:11:24 -08008 "fmt"
Ian Lance Taylorb156d712016-07-22 07:41:34 -07009 "internal/testenv"
10 "os"
Ian Lance Taylor0ba45632017-01-13 11:17:23 -080011 "runtime"
Ian Lance Taylorf8f0cb42013-11-06 11:23:33 -080012 "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}
Ian Lance Taylor93a11022015-01-14 16:11:24 -080035
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}
Ian Lance Taylorb156d712016-07-22 07:41:34 -070051
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}
Ian Lance Taylor0ba45632017-01-13 11:17:23 -080063
64func TestGettimeofday(t *testing.T) {
Ian Lance Taylorc2225a72020-01-02 15:05:27 -080065 if runtime.GOOS == "js" {
Ian Lance Taylor7b25b4d2018-08-26 18:14:49 -070066 t.Skip("not implemented on " + runtime.GOOS)
Ian Lance Taylor0ba45632017-01-13 11:17:23 -080067 }
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}