diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2022-11-22 12:03:55 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-11-23 09:09:39 +0100 |
| commit | caddc6cbcef9933a4539a06714df006e0c5ac7b2 (patch) | |
| tree | a1c058c702d31527eb9a183f8115f73d6945776b | |
| parent | 75740b3f90c81d4c226b0e30eec947df4fbd6832 (diff) | |
pkg/testutil: add RandSource helper
The code to send rand source is dublicated in several packages.
Move it to testutil package.
| -rw-r--r-- | pkg/csource/csource_test.go | 8 | ||||
| -rw-r--r-- | pkg/ifuzz/ifuzz_test.go | 10 | ||||
| -rw-r--r-- | pkg/ipc/ipc_test.go | 8 | ||||
| -rw-r--r-- | pkg/repro/repro_test.go | 11 | ||||
| -rw-r--r-- | pkg/testutil/testutil.go | 24 | ||||
| -rw-r--r-- | prog/alloc_test.go | 4 | ||||
| -rw-r--r-- | prog/compression_test.go | 6 | ||||
| -rw-r--r-- | prog/export_test.go | 19 | ||||
| -rw-r--r-- | prog/heatmap_test.go | 4 | ||||
| -rw-r--r-- | prog/prog_test.go | 2 |
10 files changed, 44 insertions, 52 deletions
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) +} diff --git a/prog/alloc_test.go b/prog/alloc_test.go index 757dbd8ef..c1d0317a5 100644 --- a/prog/alloc_test.go +++ b/prog/alloc_test.go @@ -6,6 +6,8 @@ package prog import ( "fmt" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestMemAlloc(t *testing.T) { @@ -85,7 +87,7 @@ func TestVmaAlloc(t *testing.T) { if err != nil { t.Fatal(err) } - r := newRand(target, randSource(t)) + r := newRand(target, testutil.RandSource(t)) va := newVmaAlloc(1000) for i := 0; i < 30; i++ { size := r.rand(4) + 1 diff --git a/prog/compression_test.go b/prog/compression_test.go index b46f70999..120e5d60f 100644 --- a/prog/compression_test.go +++ b/prog/compression_test.go @@ -8,10 +8,12 @@ import ( "fmt" "math/rand" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestCompress(t *testing.T) { - r := rand.New(randSource(t)) + r := rand.New(testutil.RandSource(t)) err := testRoundTrip(r, Compress, Decompress) if err != nil { t.Fatalf("compress/decompress %v", err) @@ -19,7 +21,7 @@ func TestCompress(t *testing.T) { } func TestEncode(t *testing.T) { - r := rand.New(randSource(t)) + r := rand.New(testutil.RandSource(t)) err := testRoundTrip(r, EncodeB64, DecodeB64) if err != nil { t.Fatalf("encode/decode Base64 %v", err) diff --git a/prog/export_test.go b/prog/export_test.go index 4a116dc49..e544103c1 100644 --- a/prog/export_test.go +++ b/prog/export_test.go @@ -6,10 +6,7 @@ package prog import ( "fmt" "math/rand" - "os" - "strconv" "testing" - "time" "github.com/google/syzkaller/pkg/testutil" ) @@ -26,18 +23,6 @@ var ( initTargetTest = InitTargetTest ) -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) -} - func iterCount() int { iters := 1000 if testing.Short() { @@ -51,7 +36,7 @@ func iterCount() int { func initRandomTargetTest(t *testing.T, os, arch string) (*Target, rand.Source, int) { target := initTargetTest(t, os, arch) - return target, randSource(t), iterCount() + return target, testutil.RandSource(t), iterCount() } func initTest(t *testing.T) (*Target, rand.Source, int) { @@ -78,7 +63,7 @@ func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs if iters < 3 { iters = 3 } - rs0 := randSource(t) + rs0 := testutil.RandSource(t) for _, target := range targets { target := target rs := rand.NewSource(rs0.Int63()) diff --git a/prog/heatmap_test.go b/prog/heatmap_test.go index cbda3ce43..4103a4f97 100644 --- a/prog/heatmap_test.go +++ b/prog/heatmap_test.go @@ -7,6 +7,8 @@ import ( "math/rand" "sort" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestGenericHeatmap(t *testing.T) { @@ -48,7 +50,7 @@ func TestGenericHeatmap(t *testing.T) { const tries = 10 iters := iterCount() / tries - r := rand.New(randSource(t)) + r := rand.New(testutil.RandSource(t)) for _, test := range testData { data, err := DecodeB64(test.data) if err != nil { diff --git a/prog/prog_test.go b/prog/prog_test.go index 9e347b4e8..8da8d773c 100644 --- a/prog/prog_test.go +++ b/prog/prog_test.go @@ -180,7 +180,7 @@ func TestCrossTarget(t *testing.T) { func testCrossTarget(t *testing.T, target *Target, crossTargets []*Target) { ct := target.DefaultChoiceTable() - rs := randSource(t) + rs := testutil.RandSource(t) iters := 100 if testing.Short() { iters /= 10 |
