aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/aflow/tool/codeeditor/codeeditor_test.go
blob: 4ba556f1bd7bf666ddc91a479b79361e8152135c (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
// 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.

package codeeditor

import (
	"path/filepath"
	"testing"

	"github.com/google/syzkaller/pkg/aflow"
	"github.com/google/syzkaller/pkg/osutil"
	"github.com/stretchr/testify/require"
)

func TestCodeeditorEscapingPath(t *testing.T) {
	aflow.TestTool(t, Tool,
		state{
			KernelScratchSrc: "whatever",
		},
		args{
			SourceFile: "../../passwd",
		},
		struct{}{},
		`SourceFile "../../passwd" is outside of the source tree`,
	)
}

func TestCodeeditorMissingPath(t *testing.T) {
	aflow.TestTool(t, Tool,
		state{
			KernelScratchSrc: t.TempDir(),
		},
		args{
			SourceFile: "missing-file",
		},
		struct{}{},
		`SourceFile "missing-file" does not exist`,
	)
}

func TestCodeeditorEmptyCurrentCode(t *testing.T) {
	dir := writeTestFile(t, "foo", "data")
	aflow.TestTool(t, Tool,
		state{
			KernelScratchSrc: dir,
		},
		args{
			SourceFile: "foo",
		},
		struct{}{},
		`CurrentCode snippet is empty`,
	)
}

func writeTestFile(t *testing.T, filename, data string) string {
	dir := t.TempDir()
	if err := osutil.WriteFile(filepath.Join(dir, filename), []byte(data)); err != nil {
		t.Fatal(err)
	}
	return dir
}

func Fuzz(f *testing.F) {
	dir := f.TempDir()
	const filename = "src.c"
	fullFilename := filepath.Join(dir, filename)
	f.Fuzz(func(t *testing.T, fileData []byte, curCode, newCode string) {
		require.NoError(t, osutil.WriteFile(fullFilename, fileData))
		aflow.FuzzTool(t, Tool,
			state{
				KernelScratchSrc: dir,
			},
			args{
				SourceFile:  filename,
				CurrentCode: curCode,
				NewCode:     newCode,
			})
	})
}