diff options
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/manager/diff_test.go | 68 | ||||
| -rw-r--r-- | pkg/osutil/fileutil.go | 16 | ||||
| -rw-r--r-- | pkg/osutil/tar_test.go | 15 |
3 files changed, 86 insertions, 13 deletions
diff --git a/pkg/manager/diff_test.go b/pkg/manager/diff_test.go new file mode 100644 index 000000000..f2408f13b --- /dev/null +++ b/pkg/manager/diff_test.go @@ -0,0 +1,68 @@ +// Copyright 2025 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 manager + +import ( + "testing" + + "github.com/google/syzkaller/pkg/mgrconfig" + "github.com/google/syzkaller/pkg/osutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPatchFocusAreas(t *testing.T) { + cfg := &mgrconfig.Config{ + KernelSrc: t.TempDir(), + } + require.NoError(t, osutil.FillDirectory(cfg.KernelSrc, map[string]string{ + "header.h": `Test`, + "a.c": `#include <header.h> +int main(void) { }`, + "b.c": `int main(void) { }`, + "c.c": `int main(void) { }`, + })) + + PatchFocusAreas(cfg, [][]byte{ + []byte(`diff --git a/b.c b/b.c +index 103167d..fbf7a68 100644 +--- a/b.c ++++ b/b.c +@@ -1 +1 @@ +-int main(void) { } +\ No newline at end of file ++int main(void) { return 1; } +\ No newline at end of file`), + // Also, emulate an update to te header.h. + []byte(`diff --git a/header.h b/header.h +index 103167d..fbf7a68 100644 +--- a/header.h ++++ b/header.h +@@ -1 +1 @@ +-Test +\ No newline at end of file ++Test2 +\ No newline at end of file`), + }) + + assert.Equal(t, []mgrconfig.FocusArea{ + { + Name: modifiedArea, + Filter: mgrconfig.CovFilterCfg{ + Files: []string{"b.c", "header.h"}, + }, + Weight: 3.0, + }, + { + Name: includesArea, + Filter: mgrconfig.CovFilterCfg{ + Files: []string{"a.c"}, + }, + Weight: 2.0, + }, + { + Weight: 1.0, + }, + }, cfg.Experimental.FocusAreas) +} diff --git a/pkg/osutil/fileutil.go b/pkg/osutil/fileutil.go index 850335cfe..cd5b602cf 100644 --- a/pkg/osutil/fileutil.go +++ b/pkg/osutil/fileutil.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "path/filepath" ) // CopyFile atomically copies oldFile to newFile preserving permissions and modification time. @@ -50,6 +51,21 @@ func Rename(oldFile, newFile string) error { return err } +// FillDirectory is used to fill in directory structure for tests. +func FillDirectory(dir string, fileContent map[string]string) error { + for path, content := range fileContent { + fullPath := filepath.Join(dir, path) + dirPath := filepath.Dir(fullPath) + if err := MkdirAll(dirPath); err != nil { + return fmt.Errorf("mkdir %q failed: %w", dirPath, err) + } + if err := WriteFile(fullPath, []byte(content)); err != nil { + return fmt.Errorf("write file failed: %w", err) + } + } + return nil +} + // WriteTempFile writes data to a temp file and returns its name. func WriteTempFile(data []byte) (string, error) { // Note: pkg/report knows about "syzkaller" prefix as it appears in crashes as process name. diff --git a/pkg/osutil/tar_test.go b/pkg/osutil/tar_test.go index a0e5a22bd..432d7c622 100644 --- a/pkg/osutil/tar_test.go +++ b/pkg/osutil/tar_test.go @@ -7,31 +7,20 @@ import ( "archive/tar" "bytes" "io" - "path/filepath" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTarDirectory(t *testing.T) { dir := t.TempDir() - items := map[string]string{ "file1.txt": "first file content", "dir/file2.txt": "second file content", "empty.txt": "", } - - for path, content := range items { - fullPath := filepath.Join(dir, path) - dirPath := filepath.Dir(fullPath) - if err := MkdirAll(dirPath); err != nil { - t.Fatalf("mkdir %q failed: %v", dirPath, err) - } - if err := WriteFile(fullPath, []byte(content)); err != nil { - t.Fatalf("write file failed: %v", err) - } - } + require.NoError(t, FillDirectory(dir, items)) var buf bytes.Buffer err := tarDirectory(dir, &buf) |
