aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/aflow/test_tool.go
blob: b2f47be743efbb472bd9dbdec28a669bc590b95e (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
// 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 aflow

import (
	"testing"

	"github.com/stretchr/testify/require"
)

// TestTool runs the given tool on provided initState/initArgs and compares results/error
// with the provided wantResults/wantError.
// wantResults can be either the tool return struct, or a function that accepts the tool
// return struct. In the latter case, the function is executed with the actual results,
// and is supposed to do assertions on the value.
func TestTool(t *testing.T, tool Tool, initState, initArgs, wantResults any, wantError string) {
	type tester interface {
		testVerify(t *testing.T, ctx *verifyContext, state, args, results any) (
			map[string]any, map[string]any, func(map[string]any))
	}
	vctx := newVerifyContext()
	state, args, resultChecker := tool.(tester).testVerify(t, vctx, initState, initArgs, wantResults)
	require.NoError(t, vctx.finalize())
	// Just ensure it does not crash.
	_ = tool.declaration()
	// We don't init all fields, init more, if necessary.
	ctx := &Context{
		state: state,
	}
	defer ctx.Close()
	gotResults, err := tool.execute(ctx, args)
	gotError := ""
	if err != nil {
		gotError = err.Error()
	}
	require.Equal(t, wantError, gotError)
	resultChecker(gotResults)
}

func FuzzTool(t *testing.T, tool Tool, initState, initArgs any) (map[string]any, error) {
	type toolFuzzer interface {
		checkFuzzTypes(t *testing.T, state, args any) (map[string]any, map[string]any)
	}
	state, args := tool.(toolFuzzer).checkFuzzTypes(t, initState, initArgs)
	ctx := &Context{
		state: state,
	}
	defer ctx.Close()
	return tool.execute(ctx, args)
}