From e530ec1befe3153020d601f5066939d984d790ac Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 22 Jul 2019 10:20:51 +0200 Subject: pkg/csource: test sys/*/test programs Running sys/*/test programs requires real machines and kernels for each OS. We can't do that in unit tests, but at least try to deserialize these programs so that they don't get rotten. --- pkg/csource/csource_test.go | 63 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 11 deletions(-) (limited to 'pkg/csource/csource_test.go') diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index 592f4fe19..e31347357 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -5,13 +5,17 @@ package csource import ( "fmt" + "io/ioutil" "math/rand" "os" "os/exec" + "path/filepath" "runtime" + "strings" "testing" "time" + "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" "github.com/google/syzkaller/sys/targets" @@ -48,6 +52,17 @@ func TestGenerate(t *testing.T) { } } +// This is the main configuration used by executor, so we want to test it as well. +var executorOpts = Options{ + Threaded: true, + Collide: true, + Repeat: true, + Procs: 2, + Sandbox: "none", + Repro: true, + UseTmpDir: true, +} + func testTarget(t *testing.T, target *prog.Target, full bool) { seed := time.Now().UnixNano() if os.Getenv("TRAVIS") != "" { @@ -67,17 +82,7 @@ func testTarget(t *testing.T, target *prog.Target, full bool) { if !full || testing.Short() { p.Calls = append(p.Calls, syzProg.Calls...) opts = allOptionsSingle(target.OS) - // This is the main configuration used by executor, - // so we want to test it as well. - opts = append(opts, Options{ - Threaded: true, - Collide: true, - Repeat: true, - Procs: 2, - Sandbox: "none", - Repro: true, - UseTmpDir: true, - }) + opts = append(opts, executorOpts) } else { minimized, _ := prog.Minimize(syzProg, -1, false, func(p *prog.Prog, call int) bool { return len(p.Calls) == len(syzProg.Calls) @@ -107,3 +112,39 @@ func testOne(t *testing.T, p *prog.Prog, opts Options) { } defer os.Remove(bin) } + +func TestSysTests(t *testing.T) { + t.Parallel() + for _, target := range prog.AllTargets() { + target := target + t.Run(target.OS+"/"+target.Arch, func(t *testing.T) { + t.Parallel() + dir := filepath.Join("..", "..", "sys", target.OS, "test") + if !osutil.IsExist(dir) { + return + } + files, err := ioutil.ReadDir(dir) + if err != nil { + t.Fatalf("failed to read %v: %v", dir, err) + } + for _, finfo := range files { + file := filepath.Join(dir, finfo.Name()) + if strings.HasSuffix(file, "~") || strings.HasSuffix(file, ".swp") { + continue + } + data, err := ioutil.ReadFile(file) + if err != nil { + t.Fatalf("failed to read %v: %v", file, err) + } + p, err := target.Deserialize(data, prog.Strict) + if err != nil { + t.Fatalf("failed to parse program %v: %v", file, err) + } + _, err = Write(p, executorOpts) + if err != nil { + t.Fatalf("failed to generate C source for %v: %v", file, err) + } + } + }) + } +} -- cgit mrf-deployment