File size: 3,545 Bytes
04dc9f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | // Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix || (js && wasm) || plan9 || wasip1
package syscall_test
import (
"fmt"
"strconv"
"strings"
"syscall"
"testing"
)
type env struct {
name, val string
}
func genDummyEnv(tb testing.TB, size int) []env {
tb.Helper()
envList := make([]env, size)
for idx := range size {
envList[idx] = env{
name: fmt.Sprintf("DUMMY_VAR_%d", idx),
val: fmt.Sprintf("val-%d", idx*100),
}
}
return envList
}
func setDummyEnv(tb testing.TB, envList []env) {
tb.Helper()
for _, env := range envList {
if err := syscall.Setenv(env.name, env.val); err != nil {
tb.Fatalf("setenv %s=%q failed: %v", env.name, env.val, err)
}
}
}
func setupEnvCleanup(tb testing.TB) {
tb.Helper()
originalEnv := map[string]string{}
for _, env := range syscall.Environ() {
fields := strings.SplitN(env, "=", 2)
name, val := fields[0], fields[1]
originalEnv[name] = val
}
tb.Cleanup(func() {
syscall.Clearenv()
for name, val := range originalEnv {
if err := syscall.Setenv(name, val); err != nil {
tb.Fatalf("could not reset env %s=%q: %v", name, val, err)
}
}
})
}
func TestClearenv(t *testing.T) {
setupEnvCleanup(t)
t.Run("DummyVars-4096", func(t *testing.T) {
envList := genDummyEnv(t, 4096)
setDummyEnv(t, envList)
if env := syscall.Environ(); len(env) < 4096 {
t.Fatalf("env is missing dummy variables: %v", env)
}
for idx := range 4096 {
name := fmt.Sprintf("DUMMY_VAR_%d", idx)
if _, ok := syscall.Getenv(name); !ok {
t.Fatalf("env is missing dummy variable %s", name)
}
}
syscall.Clearenv()
if env := syscall.Environ(); len(env) != 0 {
t.Fatalf("clearenv should've cleared all variables: %v still set", env)
}
for idx := range 4096 {
name := fmt.Sprintf("DUMMY_VAR_%d", idx)
if val, ok := syscall.Getenv(name); ok {
t.Fatalf("clearenv should've cleared all variables: %s=%q still set", name, val)
}
}
})
// Test that GODEBUG getting cleared by Clearenv also resets the behaviour.
t.Run("GODEBUG", func(t *testing.T) {
envList := genDummyEnv(t, 100)
setDummyEnv(t, envList)
doNilPanic := func() (ret any) {
defer func() {
ret = recover()
}()
panic(nil)
}
// Allow panic(nil).
if err := syscall.Setenv("GODEBUG", "panicnil=1"); err != nil {
t.Fatalf("setenv GODEBUG=panicnil=1 failed: %v", err)
}
got := doNilPanic()
if got != nil {
t.Fatalf("GODEBUG=panicnil=1 did not allow for nil panic: got %#v", got)
}
// Disallow panic(nil).
syscall.Clearenv()
if env := syscall.Environ(); len(env) != 0 {
t.Fatalf("clearenv should've cleared all variables: %v still set", env)
}
got = doNilPanic()
if got == nil {
t.Fatalf("GODEBUG=panicnil=1 being unset didn't reset panicnil behaviour")
}
if godebug, ok := syscall.Getenv("GODEBUG"); ok {
t.Fatalf("GODEBUG still exists in environment despite being unset: GODEBUG=%q", godebug)
}
})
}
func BenchmarkClearenv(b *testing.B) {
setupEnvCleanup(b)
b.ResetTimer()
for _, size := range []int{100, 1000, 10000} {
b.Run(strconv.Itoa(size), func(b *testing.B) {
envList := genDummyEnv(b, size)
for b.Loop() {
// Ideally we would use b.StopTimer() for the setDummyEnv
// portion, but this causes the benchmark time to get confused
// and take forever. See <https://go.dev/issue/27217>.
setDummyEnv(b, envList)
syscall.Clearenv()
}
})
}
}
|