Home | History | Annotate | Download | only in runtest
      1 // Copyright 2018 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 runtest
      5 
      6 import (
      7 	"os"
      8 	"path/filepath"
      9 	"runtime"
     10 	"testing"
     11 
     12 	"github.com/google/syzkaller/pkg/csource"
     13 	"github.com/google/syzkaller/pkg/host"
     14 	"github.com/google/syzkaller/prog"
     15 	"github.com/google/syzkaller/sys/targets"
     16 	_ "github.com/google/syzkaller/sys/test/gen" // pull in the test target
     17 )
     18 
     19 func Test(t *testing.T) {
     20 	for _, sysTarget := range targets.List["test"] {
     21 		sysTarget1 := targets.Get(sysTarget.OS, sysTarget.Arch)
     22 		t.Run(sysTarget1.Arch, func(t *testing.T) {
     23 			t.Parallel()
     24 			test(t, sysTarget1)
     25 		})
     26 	}
     27 }
     28 
     29 func test(t *testing.T, sysTarget *targets.Target) {
     30 	target, err := prog.GetTarget(sysTarget.OS, sysTarget.Arch)
     31 	if err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	if testing.Short() && target.PtrSize == 4 {
     35 		// Building 32-bit binaries fails on travis (see comments in Makefile).
     36 		t.Skip("skipping in short mode")
     37 	}
     38 	executor, err := csource.BuildFile(target, filepath.FromSlash("../../executor/executor.cc"))
     39 	if err != nil {
     40 		t.Fatal(err)
     41 	}
     42 	defer os.Remove(executor)
     43 	features, err := host.Check(target)
     44 	if err != nil {
     45 		t.Fatalf("failed to detect host features: %v", err)
     46 	}
     47 	calls, _, err := host.DetectSupportedSyscalls(target, "none")
     48 	if err != nil {
     49 		t.Fatalf("failed to detect supported syscalls: %v", err)
     50 	}
     51 	enabledCalls := map[string]map[*prog.Syscall]bool{
     52 		"":     calls,
     53 		"none": calls,
     54 	}
     55 	requests := make(chan *RunRequest, 2*runtime.GOMAXPROCS(0))
     56 	go func() {
     57 		for req := range requests {
     58 			RunTest(req, executor)
     59 			close(req.Done)
     60 		}
     61 	}()
     62 	ctx := &Context{
     63 		Dir:          filepath.Join("..", "..", "sys", target.OS, "test"),
     64 		Target:       target,
     65 		Features:     features,
     66 		EnabledCalls: enabledCalls,
     67 		Requests:     requests,
     68 		LogFunc: func(text string) {
     69 			t.Helper()
     70 			t.Logf(text)
     71 		},
     72 	}
     73 	if err := ctx.Run(); err != nil {
     74 		t.Fatal(err)
     75 	}
     76 }
     77