2017-09-05 11:31:14 +00:00
|
|
|
// Copyright 2017 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 prog
|
|
|
|
|
|
|
|
import (
|
2018-01-26 11:34:02 +00:00
|
|
|
"fmt"
|
2017-09-05 11:31:14 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Export guts for testing.
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
debug = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
CalcChecksumsCall = calcChecksumsCall
|
2017-11-22 10:42:10 +00:00
|
|
|
InitTest = initTest
|
2017-09-05 11:31:14 +00:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:49:08 +00:00
|
|
|
func initTargetTest(t *testing.T, os, arch string) *Target {
|
2017-09-05 11:31:14 +00:00
|
|
|
t.Parallel()
|
2017-12-13 12:49:08 +00:00
|
|
|
target, err := GetTarget(os, arch)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
2018-02-19 18:35:04 +00:00
|
|
|
func randSource(t *testing.T) rand.Source {
|
|
|
|
seed := int64(time.Now().UnixNano())
|
|
|
|
t.Logf("seed=%v", seed)
|
|
|
|
return rand.NewSource(seed)
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:06:00 +00:00
|
|
|
func iterCount() int {
|
2017-09-05 11:31:14 +00:00
|
|
|
iters := 10000
|
|
|
|
if testing.Short() {
|
|
|
|
iters = 100
|
|
|
|
}
|
2018-05-04 18:06:00 +00:00
|
|
|
if raceEnabled {
|
|
|
|
iters /= 10
|
|
|
|
}
|
|
|
|
return iters
|
|
|
|
}
|
|
|
|
|
|
|
|
func initRandomTargetTest(t *testing.T, os, arch string) (*Target, rand.Source, int) {
|
|
|
|
target := initTargetTest(t, os, arch)
|
|
|
|
return target, randSource(t), iterCount()
|
2017-09-05 11:31:14 +00:00
|
|
|
}
|
2017-12-13 12:49:08 +00:00
|
|
|
|
|
|
|
func initTest(t *testing.T) (*Target, rand.Source, int) {
|
|
|
|
return initRandomTargetTest(t, "linux", "amd64")
|
|
|
|
}
|
2018-01-26 11:34:02 +00:00
|
|
|
|
2018-05-03 11:40:21 +00:00
|
|
|
func testEachTarget(t *testing.T, fn func(t *testing.T, target *Target)) {
|
2018-05-04 18:06:00 +00:00
|
|
|
t.Parallel()
|
2018-05-03 11:40:21 +00:00
|
|
|
for _, target := range AllTargets() {
|
|
|
|
target := target
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
fn(t, target)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:34:02 +00:00
|
|
|
func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs rand.Source, iters int)) {
|
2018-05-04 18:06:00 +00:00
|
|
|
t.Parallel()
|
2018-01-26 11:34:02 +00:00
|
|
|
targets := AllTargets()
|
2018-05-04 18:06:00 +00:00
|
|
|
iters := iterCount()
|
2018-01-26 11:34:02 +00:00
|
|
|
iters /= len(targets)
|
2018-05-04 18:06:00 +00:00
|
|
|
if iters < 3 {
|
|
|
|
iters = 3
|
|
|
|
}
|
2018-02-19 18:35:04 +00:00
|
|
|
rs0 := randSource(t)
|
2018-01-26 11:34:02 +00:00
|
|
|
for _, target := range targets {
|
|
|
|
target := target
|
|
|
|
rs := rand.NewSource(rs0.Int63())
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", target.OS, target.Arch), func(t *testing.T) {
|
2018-01-24 18:28:36 +00:00
|
|
|
t.Parallel()
|
2018-01-26 11:34:02 +00:00
|
|
|
fn(t, target, rs, iters)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|