aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/aflow/tool/grepper/grepper_test.go
blob: 76f2beff5a46ed5bd628a3b1c96c8de64c30c657 (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
// Copyright 2026 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.

// Some of these error messages and grep responses are linux-specific.
//go:build linux

package grepper

import (
	"strings"
	"testing"

	"github.com/google/syzkaller/pkg/aflow"
	"github.com/google/syzkaller/pkg/vcs"
	"github.com/stretchr/testify/assert"
)

func TestGrepper(t *testing.T) {
	repo := vcs.MakeTestRepo(t, t.TempDir())
	repo.CommitChangeset("description",
		vcs.FileContent{
			File: "foo.c",
			Content: `
int some_func(void)
{
	line;
	foobar;
	line;
}
			`,
		},
		vcs.FileContent{
			File: "bar.c",
			Content: `
int another_func(int) {
	foobar;
}
			`,
		},
		vcs.FileContent{
			File: "overflow.c",
			Content: strings.Repeat(`
int some_func(int) {
	barfoo;
}
			`, 1000),
		},
	)

	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: "foobar"},
		results{Output: `bar.c=2=int another_func(int) {
bar.c:3:	foobar;
bar.c-4-}
--
foo.c=2=int some_func(void)
--
foo.c-4-	line;
foo.c:5:	foobar;
foo.c-6-	line;
`},
		"")

	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: "barfoo"},
		func(got results) {
			assert.True(t, strings.Contains(got.Output,
				"Full output is too long, showing 500 out of 3999 lines."),
				"%v", got)
			assert.Equal(t, 505, strings.Count(got.Output, "\n"))
		},
		"")

	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: "something that never appears"},
		results{},
		"no matches")

	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: "bad expression ("},
		results{},
		`bad expression: fatal: command line, 'bad expression (': Unmatched ( or \(`)
	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: "->root"},
		results{},
		"no matches")
	aflow.TestTool(t, Tool,
		state{KernelSrc: repo.Dir},
		args{Expression: `-\>root`},
		results{},
		"no matches")
}