• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
4package prog
5
6import (
7	"fmt"
8	"math/rand"
9	"testing"
10	"time"
11)
12
13// Export guts for testing.
14
15func init() {
16	debug = true
17}
18
19var (
20	CalcChecksumsCall = calcChecksumsCall
21	InitTest          = initTest
22)
23
24func initTargetTest(t *testing.T, os, arch string) *Target {
25	t.Parallel()
26	target, err := GetTarget(os, arch)
27	if err != nil {
28		t.Fatal(err)
29	}
30	return target
31}
32
33func randSource(t *testing.T) rand.Source {
34	seed := int64(time.Now().UnixNano())
35	t.Logf("seed=%v", seed)
36	return rand.NewSource(seed)
37}
38
39func iterCount() int {
40	iters := 10000
41	if testing.Short() {
42		iters = 100
43	}
44	if raceEnabled {
45		iters /= 10
46	}
47	return iters
48}
49
50func initRandomTargetTest(t *testing.T, os, arch string) (*Target, rand.Source, int) {
51	target := initTargetTest(t, os, arch)
52	return target, randSource(t), iterCount()
53}
54
55func initTest(t *testing.T) (*Target, rand.Source, int) {
56	return initRandomTargetTest(t, "linux", "amd64")
57}
58
59func testEachTarget(t *testing.T, fn func(t *testing.T, target *Target)) {
60	t.Parallel()
61	for _, target := range AllTargets() {
62		target := target
63		t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
64			t.Parallel()
65			fn(t, target)
66		})
67	}
68}
69
70func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs rand.Source, iters int)) {
71	t.Parallel()
72	targets := AllTargets()
73	iters := iterCount()
74	iters /= len(targets)
75	if iters < 3 {
76		iters = 3
77	}
78	rs0 := randSource(t)
79	for _, target := range targets {
80		target := target
81		rs := rand.NewSource(rs0.Int63())
82		t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
83			t.Parallel()
84			fn(t, target, rs, iters)
85		})
86	}
87}
88