From caddc6cbcef9933a4539a06714df006e0c5ac7b2 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 22 Nov 2022 12:03:55 +0100 Subject: pkg/testutil: add RandSource helper The code to send rand source is dublicated in several packages. Move it to testutil package. --- pkg/csource/csource_test.go | 8 +------- pkg/ifuzz/ifuzz_test.go | 10 ++-------- pkg/ipc/ipc_test.go | 8 ++------ pkg/repro/repro_test.go | 11 ++--------- pkg/testutil/testutil.go | 24 ++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 pkg/testutil/testutil.go (limited to 'pkg') diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index bfeb8f6a7..2d047cc10 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -12,7 +12,6 @@ import ( "strings" "sync/atomic" "testing" - "time" "github.com/google/syzkaller/pkg/testutil" "github.com/google/syzkaller/prog" @@ -59,12 +58,7 @@ func TestGenerate(t *testing.T) { } func testTarget(t *testing.T, target *prog.Target, full bool) { - seed := time.Now().UnixNano() - if os.Getenv("CI") != "" { - seed = 0 // required for deterministic coverage reports - } - rs := rand.NewSource(seed) - t.Logf("seed=%v", seed) + rs := testutil.RandSource(t) p := target.Generate(rs, 10, target.DefaultChoiceTable()) // Turns out that fully minimized program can trigger new interesting warnings, // e.g. about NULL arguments for functions that require non-NULL arguments in syz_ functions. diff --git a/pkg/ifuzz/ifuzz_test.go b/pkg/ifuzz/ifuzz_test.go index eee8afe00..11f57216b 100644 --- a/pkg/ifuzz/ifuzz_test.go +++ b/pkg/ifuzz/ifuzz_test.go @@ -6,11 +6,10 @@ package ifuzz import ( "encoding/hex" "math/rand" - "os" "testing" - "time" "github.com/google/syzkaller/pkg/ifuzz/iset" + "github.com/google/syzkaller/pkg/testutil" ) var allArches = []string{ArchX86, ArchPowerPC} @@ -53,12 +52,7 @@ func testDecode(t *testing.T, arch string) { if _, err := insnset.DecodeExt(0, nil); err == nil { xedEnabled = true } - seed := time.Now().UnixNano() - if os.Getenv("CI") != "" { - seed = 0 // required for deterministic coverage reports - } - t.Logf("seed=%v", seed) - r := rand.New(rand.NewSource(seed)) + r := rand.New(testutil.RandSource(t)) for repeat := 0; repeat < 10; repeat++ { for mode := iset.Mode(0); mode < iset.ModeLast; mode++ { diff --git a/pkg/ipc/ipc_test.go b/pkg/ipc/ipc_test.go index 3456852c5..0734fbe6e 100644 --- a/pkg/ipc/ipc_test.go +++ b/pkg/ipc/ipc_test.go @@ -16,6 +16,7 @@ import ( . "github.com/google/syzkaller/pkg/ipc" "github.com/google/syzkaller/pkg/ipc/ipcconfig" "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/pkg/testutil" "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" "github.com/google/syzkaller/sys/targets" @@ -36,12 +37,6 @@ func initTest(t *testing.T) (*prog.Target, rand.Source, int, bool, bool, targets if testing.Short() { iters = 10 } - seed := time.Now().UnixNano() - if os.Getenv("CI") != "" { - seed = 0 // required for deterministic coverage reports - } - rs := rand.NewSource(seed) - t.Logf("seed=%v", seed) target, err := prog.GetTarget(runtime.GOOS, runtime.GOARCH) if err != nil { t.Fatal(err) @@ -50,6 +45,7 @@ func initTest(t *testing.T) (*prog.Target, rand.Source, int, bool, bool, targets if err != nil { t.Fatal(err) } + rs := testutil.RandSource(t) return target, rs, iters, cfg.UseShmem, cfg.UseForkServer, cfg.Timeouts } diff --git a/pkg/repro/repro_test.go b/pkg/repro/repro_test.go index d02ea6d67..486dc963b 100644 --- a/pkg/repro/repro_test.go +++ b/pkg/repro/repro_test.go @@ -5,11 +5,10 @@ package repro import ( "math/rand" - "os" "testing" - "time" "github.com/google/syzkaller/pkg/csource" + "github.com/google/syzkaller/pkg/testutil" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/sys/targets" ) @@ -19,13 +18,7 @@ func initTest(t *testing.T) (*rand.Rand, int) { if testing.Short() { iters = 100 } - seed := time.Now().UnixNano() - if os.Getenv("CI") != "" { - seed = 0 // required for deterministic coverage reports - } - rs := rand.NewSource(seed) - t.Logf("seed=%v", seed) - return rand.New(rs), iters + return rand.New(testutil.RandSource(t)), iters } func TestBisect(t *testing.T) { diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go new file mode 100644 index 000000000..9a8de26bd --- /dev/null +++ b/pkg/testutil/testutil.go @@ -0,0 +1,24 @@ +// Copyright 2022 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package testutil + +import ( + "math/rand" + "os" + "strconv" + "testing" + "time" +) + +func RandSource(t *testing.T) rand.Source { + seed := time.Now().UnixNano() + if fixed := os.Getenv("SYZ_SEED"); fixed != "" { + seed, _ = strconv.ParseInt(fixed, 0, 64) + } + if os.Getenv("CI") != "" { + seed = 0 // required for deterministic coverage reports + } + t.Logf("seed=%v", seed) + return rand.NewSource(seed) +} -- cgit mrf-deployment