aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs/linux_test.go
blob: a583e56aaca18b8dab73571e1b09a2da1a804480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2019 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 vcs

import (
	"bytes"
	"io/ioutil"
	"os"
	"strings"
	"testing"

	"github.com/google/syzkaller/pkg/osutil"
)

type MinimizationTest struct {
	config         string
	baselineConfig string
	// Output contains expected config option
	expectedConfig string
	// Minimization is expected to pass or fail
	passing bool
}

func createTestLinuxRepo(t *testing.T) string {
	baseDir, err := ioutil.TempDir("", "syz-config-bisect-test")
	if err != nil {
		t.Fatal(err)
	}
	repo := CreateTestRepo(t, baseDir, "")
	repo.CommitChange("commit")
	repo.SetTag("v4.1")
	err = os.MkdirAll(baseDir+"/tools/testing/ktest", 0755)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(baseDir+"/scripts/kconfig", 0755)
	if err != nil {
		t.Fatal(err)
	}

	// Copy stubbed scripts used by config bisect
	err = osutil.CopyFile("testdata/linux/config-bisect.pl",
		baseDir+"/tools/testing/ktest/config-bisect.pl")
	if err != nil {
		t.Fatal(err)
	}
	err = osutil.CopyFile("testdata/linux/merge_config.sh",
		baseDir+"/scripts/kconfig/merge_config.sh")
	if err != nil {
		t.Fatal(err)
	}

	return baseDir
}

func TestMinimizationResults(t *testing.T) {
	tests := []MinimizationTest{
		{
			config:         "CONFIG_ORIGINAL=y",
			baselineConfig: "CONFIG_FAILING=y",
			expectedConfig: "CONFIG_ORIGINAL=y",
			passing:        false,
		},
		{
			config:         "CONFIG_ORIGINAL=y",
			baselineConfig: "CONFIG_REPRODUCES_CRASH=y",
			expectedConfig: "CONFIG_REPRODUCES_CRASH=y",
			passing:        true,
		},
		{
			config:         configBisectTag,
			baselineConfig: "CONFIG_NOT_REPRODUCE_CRASH=y",
			expectedConfig: configBisectTag,
			passing:        true,
		},
	}

	trace := new(bytes.Buffer)
	baseDir := createTestLinuxRepo(t)
	repo, err := NewRepo("linux", "64", baseDir)
	if err != nil {
		t.Fatalf("Unable to create repository")
	}
	pred := func(test []byte) (BisectResult, error) {
		if strings.Contains(string(test), "CONFIG_REPRODUCES_CRASH=y") {
			return BisectBad, nil
		}
		return BisectGood, nil
	}

	minimizer, ok := repo.(ConfigMinimizer)
	if !ok {
		t.Fatalf("Config minimization is not implemented")
	}
	for _, test := range tests {
		outConfig, err := minimizer.Minimize([]byte(test.config),
			[]byte(test.baselineConfig), trace, pred)
		if test.passing && err != nil {
			t.Fatalf("failed to run Minimize: %v", err)
		} else if test.passing && !strings.Contains(string(outConfig),
			test.expectedConfig) {
			t.Fatalf("output is not expected %v vs. %v", string(outConfig),
				test.expectedConfig)
		}
	}
	t.Log(trace.String())
}