|
| 1 | +// Copyright 2026 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 codeeditor |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/syzkaller/pkg/aflow" |
| 11 | + "github.com/google/syzkaller/pkg/osutil" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCodeeditorEscapingPath(t *testing.T) { |
| 16 | + aflow.TestTool(t, Tool, |
| 17 | + state{ |
| 18 | + KernelScratchSrc: "whatever", |
| 19 | + }, |
| 20 | + args{ |
| 21 | + SourceFile: "../../passwd", |
| 22 | + }, |
| 23 | + struct{}{}, |
| 24 | + `SourceFile "../../passwd" is outside of the source tree`, |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +func TestCodeeditorMissingPath(t *testing.T) { |
| 29 | + aflow.TestTool(t, Tool, |
| 30 | + state{ |
| 31 | + KernelScratchSrc: t.TempDir(), |
| 32 | + }, |
| 33 | + args{ |
| 34 | + SourceFile: "missing-file", |
| 35 | + }, |
| 36 | + struct{}{}, |
| 37 | + `SourceFile "missing-file" does not exist`, |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +func TestCodeeditorEmptyCurrentCode(t *testing.T) { |
| 42 | + dir := writeTestFile(t, "foo", "data") |
| 43 | + aflow.TestTool(t, Tool, |
| 44 | + state{ |
| 45 | + KernelScratchSrc: dir, |
| 46 | + }, |
| 47 | + args{ |
| 48 | + SourceFile: "foo", |
| 49 | + }, |
| 50 | + struct{}{}, |
| 51 | + `CurrentCode snippet is empty`, |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +func writeTestFile(t *testing.T, filename, data string) string { |
| 56 | + dir := t.TempDir() |
| 57 | + if err := osutil.WriteFile(filepath.Join(dir, filename), []byte(data)); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + return dir |
| 61 | +} |
| 62 | + |
| 63 | +func Fuzz(f *testing.F) { |
| 64 | + dir := f.TempDir() |
| 65 | + const filename = "src.c" |
| 66 | + fullFilename := filepath.Join(dir, filename) |
| 67 | + f.Fuzz(func(t *testing.T, fileData []byte, curCode, newCode string) { |
| 68 | + require.NoError(t, osutil.WriteFile(fullFilename, fileData)) |
| 69 | + aflow.FuzzTool(t, Tool, |
| 70 | + state{ |
| 71 | + KernelScratchSrc: dir, |
| 72 | + }, |
| 73 | + args{ |
| 74 | + SourceFile: filename, |
| 75 | + CurrentCode: curCode, |
| 76 | + NewCode: newCode, |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
0 commit comments