github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/prog/export_test.go (about)

     1  // Copyright 2017 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package prog
     5  
     6  import (
     7  	"fmt"
     8  	"math/rand"
     9  	"testing"
    10  
    11  	"github.com/google/syzkaller/pkg/testutil"
    12  )
    13  
    14  // Export guts for testing.
    15  
    16  func init() {
    17  	debug = true
    18  }
    19  
    20  var (
    21  	CalcChecksumsCall = calcChecksumsCall
    22  	InitTest          = initTest
    23  	initTargetTest    = InitTargetTest
    24  )
    25  
    26  func initRandomTargetTest(t *testing.T, os, arch string) (*Target, rand.Source, int) {
    27  	target := initTargetTest(t, os, arch)
    28  	return target, testutil.RandSource(t), testutil.IterCount()
    29  }
    30  
    31  func initTest(t *testing.T) (*Target, rand.Source, int) {
    32  	return initRandomTargetTest(t, "linux", "amd64")
    33  }
    34  
    35  func testEachTarget(t *testing.T, fn func(t *testing.T, target *Target)) {
    36  	t.Parallel()
    37  	for _, target := range AllTargets() {
    38  		target := target
    39  		t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
    40  			skipTargetRace(t, target)
    41  			t.Parallel()
    42  			fn(t, target)
    43  		})
    44  	}
    45  }
    46  
    47  func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs rand.Source, iters int)) {
    48  	t.Parallel()
    49  	targets := AllTargets()
    50  	iters := testutil.IterCount()
    51  	iters /= len(targets)
    52  	if iters < 3 {
    53  		iters = 3
    54  	}
    55  	rs0 := testutil.RandSource(t)
    56  	for _, target := range targets {
    57  		target := target
    58  		rs := rand.NewSource(rs0.Int63())
    59  		t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
    60  			skipTargetRace(t, target)
    61  			t.Parallel()
    62  			fn(t, target, rs, iters)
    63  		})
    64  	}
    65  }
    66  
    67  func skipTargetRace(t *testing.T, target *Target) {
    68  	// Race execution is slow and we are getting timeouts on CI.
    69  	// For tests that run for all targets, leave only 2 targets,
    70  	// this should be enough to detect some races.
    71  	if testutil.RaceEnabled && (target.OS != "test" || target.Arch != "64" && target.Arch != "32") {
    72  		t.Skip("skipping all but test/64 targets in race mode")
    73  	}
    74  }
    75  
    76  func initBench(b *testing.B) (*Target, func()) {
    77  	olddebug := debug
    78  	debug = false
    79  	target, err := GetTarget("linux", "amd64")
    80  	if err != nil {
    81  		b.Fatal(err)
    82  	}
    83  	b.ReportAllocs()
    84  	return target, func() { debug = olddebug }
    85  }