| 1 |
// Copyright 2021 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 godebug_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
. "internal/godebug" |
| 10 |
"internal/race" |
| 11 |
"internal/testenv" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"runtime/metrics" |
| 15 |
"slices" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
func TestGet(t *testing.T) { |
| 21 |
foo := New("#foo") |
| 22 |
tests := []struct { |
| 23 |
godebug string |
| 24 |
setting *Setting |
| 25 |
want string |
| 26 |
}{ |
| 27 |
{"", New("#"), ""}, |
| 28 |
{"", foo, ""}, |
| 29 |
{"foo=bar", foo, "bar"}, |
| 30 |
{"foo=bar,after=x", foo, "bar"}, |
| 31 |
{"before=x,foo=bar,after=x", foo, "bar"}, |
| 32 |
{"before=x,foo=bar", foo, "bar"}, |
| 33 |
{",,,foo=bar,,,", foo, "bar"}, |
| 34 |
{"foodecoy=wrong,foo=bar", foo, "bar"}, |
| 35 |
{"foo=", foo, ""}, |
| 36 |
{"foo", foo, ""}, |
| 37 |
{",foo", foo, ""}, |
| 38 |
{"foo=bar,baz", New("#loooooooong"), ""}, |
| 39 |
} |
| 40 |
for _, tt := range tests { |
| 41 |
t.Setenv("GODEBUG", tt.godebug) |
| 42 |
got := tt.setting.Value() |
| 43 |
if got != tt.want { |
| 44 |
t.Errorf("get(%q, %q) = %q; want %q", tt.godebug, tt.setting.Name(), got, tt.want) |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestMetrics(t *testing.T) { |
| 50 |
const name = "http2client" // must be a real name so runtime will accept it |
| 51 |
|
| 52 |
var m [1]metrics.Sample |
| 53 |
m[0].Name = "/godebug/non-default-behavior/" + name + ":events" |
| 54 |
metrics.Read(m[:]) |
| 55 |
if kind := m[0].Value.Kind(); kind != metrics.KindUint64 { |
| 56 |
t.Fatalf("NonDefault kind = %v, want uint64", kind) |
| 57 |
} |
| 58 |
|
| 59 |
s := New(name) |
| 60 |
s.Value() |
| 61 |
s.IncNonDefault() |
| 62 |
s.IncNonDefault() |
| 63 |
s.IncNonDefault() |
| 64 |
metrics.Read(m[:]) |
| 65 |
if kind := m[0].Value.Kind(); kind != metrics.KindUint64 { |
| 66 |
t.Fatalf("NonDefault kind = %v, want uint64", kind) |
| 67 |
} |
| 68 |
if count := m[0].Value.Uint64(); count != 3 { |
| 69 |
t.Fatalf("NonDefault value = %d, want 3", count) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// TestPanicNilRace checks for a race in the runtime caused by use of runtime |
| 74 |
// atomics (not visible to usual race detection) to install the counter for |
| 75 |
// non-default panic(nil) semantics. For #64649. |
| 76 |
func TestPanicNilRace(t *testing.T) { |
| 77 |
if !race.Enabled { |
| 78 |
t.Skip("Skipping test intended for use with -race.") |
| 79 |
} |
| 80 |
if os.Getenv("GODEBUG") != "panicnil=1" { |
| 81 |
cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestPanicNilRace$", "-test.v", "-test.parallel=2", "-test.count=1")) |
| 82 |
cmd.Env = append(cmd.Env, "GODEBUG=panicnil=1") |
| 83 |
out, err := cmd.CombinedOutput() |
| 84 |
t.Logf("output:\n%s", out) |
| 85 |
|
| 86 |
if err != nil { |
| 87 |
t.Errorf("Was not expecting a crash") |
| 88 |
} |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
test := func(t *testing.T) { |
| 93 |
t.Parallel() |
| 94 |
defer func() { |
| 95 |
recover() |
| 96 |
}() |
| 97 |
panic(nil) |
| 98 |
} |
| 99 |
t.Run("One", test) |
| 100 |
t.Run("Two", test) |
| 101 |
} |
| 102 |
|
| 103 |
func TestCmdBisect(t *testing.T) { |
| 104 |
testenv.MustHaveGoRun(t) |
| 105 |
out, err := exec.Command(testenv.GoToolPath(t), "run", "cmd/vendor/golang.org/x/tools/cmd/bisect", "GODEBUG=buggy=1#PATTERN", os.Args[0], "-test.run=^TestBisectTestCase$").CombinedOutput() |
| 106 |
if err != nil { |
| 107 |
t.Fatalf("exec bisect: %v\n%s", err, out) |
| 108 |
} |
| 109 |
|
| 110 |
var want []string |
| 111 |
src, err := os.ReadFile("godebug_test.go") |
| 112 |
if err != nil { |
| 113 |
t.Fatal(err) |
| 114 |
} |
| 115 |
for i, line := range strings.Split(string(src), "\n") { |
| 116 |
if strings.Contains(line, "BISECT"+" "+"BUG") { |
| 117 |
want = append(want, fmt.Sprintf("godebug_test.go:%d", i+1)) |
| 118 |
} |
| 119 |
} |
| 120 |
slices.Sort(want) |
| 121 |
|
| 122 |
var have []string |
| 123 |
for _, line := range strings.Split(string(out), "\n") { |
| 124 |
if strings.Contains(line, "godebug_test.go:") { |
| 125 |
have = append(have, line[strings.LastIndex(line, "godebug_test.go:"):]) |
| 126 |
} |
| 127 |
} |
| 128 |
slices.Sort(have) |
| 129 |
|
| 130 |
if !slices.Equal(have, want) { |
| 131 |
t.Errorf("bad bisect output:\nhave %v\nwant %v\ncomplete output:\n%s", have, want, string(out)) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// This test does nothing by itself, but you can run |
| 136 |
// |
| 137 |
// bisect 'GODEBUG=buggy=1#PATTERN' go test -run='^TestBisectTestCase$' |
| 138 |
// |
| 139 |
// to see that the GODEBUG bisect support is working. |
| 140 |
// TestCmdBisect above does exactly that. |
| 141 |
func TestBisectTestCase(t *testing.T) { |
| 142 |
s := New("#buggy") |
| 143 |
for i := 0; i < 10; i++ { |
| 144 |
a := s.Value() == "1" |
| 145 |
b := s.Value() == "1" |
| 146 |
c := s.Value() == "1" // BISECT BUG |
| 147 |
d := s.Value() == "1" // BISECT BUG |
| 148 |
e := s.Value() == "1" // BISECT BUG |
| 149 |
|
| 150 |
if a { |
| 151 |
t.Log("ok") |
| 152 |
} |
| 153 |
if b { |
| 154 |
t.Log("ok") |
| 155 |
} |
| 156 |
if c { |
| 157 |
t.Error("bug") |
| 158 |
} |
| 159 |
if d && |
| 160 |
e { |
| 161 |
t.Error("bug") |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestImmutable(t *testing.T) { |
| 167 |
defer func(godebug string) { |
| 168 |
os.Setenv("GODEBUG", godebug) |
| 169 |
}(os.Getenv("GODEBUG")) |
| 170 |
|
| 171 |
setting := New("fips140") |
| 172 |
value := setting.Value() |
| 173 |
|
| 174 |
os.Setenv("GODEBUG", "fips140=off") |
| 175 |
if setting.Value() != value { |
| 176 |
t.Errorf("Value() changed after setting GODEBUG=fips140=off") |
| 177 |
} |
| 178 |
|
| 179 |
os.Setenv("GODEBUG", "fips140=on") |
| 180 |
if setting.Value() != value { |
| 181 |
t.Errorf("Value() changed after setting GODEBUG=fips140=on") |
| 182 |
} |
| 183 |
|
| 184 |
os.Setenv("GODEBUG", "fips140=") |
| 185 |
if setting.Value() != value { |
| 186 |
t.Errorf("Value() changed after setting GODEBUG=fips140=") |
| 187 |
} |
| 188 |
|
| 189 |
os.Setenv("GODEBUG", "") |
| 190 |
if setting.Value() != value { |
| 191 |
t.Errorf("Value() changed after setting GODEBUG=") |
| 192 |
} |
| 193 |
} |