aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/pkg/fuzzconfig/generate_test.go
blob: 1dd143eafb1cc081c533efd711ff6801084d9bd9 (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 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 fuzzconfig

import (
	"encoding/json"
	"flag"
	"os"
	"path/filepath"
	"testing"

	"github.com/google/syzkaller/pkg/config"
	"github.com/google/syzkaller/pkg/mgrconfig"
	"github.com/google/syzkaller/syz-cluster/pkg/api"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var flagWrite = flag.Bool("write", false, "overwrite out.txt files")

func TestSingularFocus(t *testing.T) {
	focusMap := map[string]struct{}{}
	for _, target := range api.FuzzTargets {
		for _, campaign := range target.Campaigns {
			if campaign.Focus != "" {
				focusMap[campaign.Focus] = struct{}{}
			}
		}
	}
	for focus := range focusMap {
		t.Run(focus, func(t *testing.T) {
			cfg := &api.FuzzConfig{Focus: []string{focus}}
			runTest(t, cfg, filepath.Join("testdata", "singular", focus))
		})
	}
}

func TestNoFocus(t *testing.T) {
	runTest(t, &api.FuzzConfig{}, filepath.Join("testdata", "singular", "default"))
}

func TestMultipleFocus(t *testing.T) {
	runTest(t, &api.FuzzConfig{
		Focus: []string{api.FocusBPF, api.FocusIoUring},
	}, filepath.Join("testdata", "mixed", "bpf_io_uring"))
}

func runTest(t *testing.T, cfg *api.FuzzConfig, baseName string) {
	base, err := GenerateBase(cfg)
	require.NoError(t, err)
	compareOrSave(t, baseName+".base.cfg", base)

	patched, err := GeneratePatched(cfg)
	require.NoError(t, err)
	compareOrSave(t, baseName+".patched.cfg", patched)
}

func compareOrSave(t *testing.T, fileName string, mgrCfg *mgrconfig.Config) {
	targetJSON, err := json.MarshalIndent(mgrCfg, "", "\t")
	require.NoError(t, err)
	if *flagWrite {
		err = os.WriteFile(fileName, targetJSON, 0644)
		require.NoError(t, err)
		return
	}

	var raw json.RawMessage
	err = config.LoadFile(fileName, &raw)
	require.NoError(t, err)

	cfg, err := mgrconfig.LoadPartialData(raw)
	require.NoError(t, err)
	require.NotNil(t, cfg)

	resultJSON, err := json.MarshalIndent(cfg, "", "\t")
	require.NoError(t, err)
	assert.Equal(t, targetJSON, resultJSON)
}