aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/kfuzztest/description_generation_test.go
blob: d36999129968582fb57e781767d9eeeda1cc20e8 (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
109
110
// 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 kfuzztest

import (
	"fmt"
	"os"
	"path"
	"runtime"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/syzkaller/pkg/osutil"
	"github.com/google/syzkaller/sys/targets"
	"github.com/stretchr/testify/require"
)

type testData struct {
	dir  string
	desc string
}

func TestBuildDescriptions(t *testing.T) {
	testCases, err := readTestCases("./testdata")
	require.NoError(t, err)

	target := targets.Get(targets.Linux, targets.AMD64)
	if runtime.GOOS != target.BuildOS {
		t.Skip("we cannot build Linux on this target")
	}
	if target.BrokenCompiler != "" {
		t.Skip("skipping the test due to broken cross-compiler:\n" + target.BrokenCompiler)
	}
	for _, tc := range testCases {
		t.Run(tc.dir, func(t *testing.T) {
			runTest(t, target, tc)
		})
	}
}

// Tests that the description inferred from a compiled binary matches an
// expected description.
func runTest(t *testing.T, target *targets.Target, tc testData) {
	// Compile the C binary containing the metadata.
	cmd := flags(tc.dir)
	out, err := osutil.RunCmd(time.Hour, "", target.CCompiler, cmd...)
	require.NoErrorf(t, err, "Failed to compile: %s", string(out))
	// Cleanup the compiled binary.
	defer func() {
		out, err := osutil.RunCmd(time.Hour, "", "rm", path.Join(tc.dir, "bin"))
		if err != nil {
			require.NoErrorf(t, err, "Failed to cleanup: %s", string(out))
		}
	}()

	binaryPath := path.Join(tc.dir, "bin")
	desc, err := ExtractDescription(binaryPath)
	require.NoError(t, err)

	if diffDesc := cmp.Diff(tc.desc, desc); diffDesc != "" {
		fmt.Print(diffDesc)
		t.Fail()
		return
	}
}

func flags(testDir string) []string {
	return []string{
		"-g",
		"-T",
		path.Join(testDir, "..", "linker.ld"),
		"-o",
		path.Join(testDir, "bin"),
		path.Join(testDir, "prog.c"),
	}
}

func readTestCases(dir string) ([]testData, error) {
	var testCases []testData
	testDirs, err := os.ReadDir(dir)
	if err != nil {
		return nil, err
	}

	for _, subDir := range testDirs {
		if !subDir.IsDir() {
			continue
		}
		testData, err := readTestdata(path.Join(dir, subDir.Name()))
		if err != nil {
			return nil, err
		}
		testCases = append(testCases, testData)
	}

	return testCases, nil
}

func readTestdata(testDir string) (testData, error) {
	content, err := os.ReadFile(path.Join(testDir, "desc.txt"))
	if err != nil {
		return testData{}, err
	}

	return testData{
		dir:  testDir,
		desc: string(content),
	}, nil
}