From bd38b692cfb82df95728b979e5f3572112aa005b Mon Sep 17 00:00:00 2001 From: Alexander Potapenko Date: Tue, 16 Apr 2024 15:09:36 +0200 Subject: pkg/ifuzz: test Generate() Make sure random text generation works as a whole, not just for single instructions. --- pkg/ifuzz/ifuzz_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'pkg') diff --git a/pkg/ifuzz/ifuzz_test.go b/pkg/ifuzz/ifuzz_test.go index 11f57216b..79d7bab5d 100644 --- a/pkg/ifuzz/ifuzz_test.go +++ b/pkg/ifuzz/ifuzz_test.go @@ -116,6 +116,42 @@ func testDecode(t *testing.T, arch string) { } } +func TestGenerate(t *testing.T) { + for _, arch := range allArches { + t.Run(arch, func(t *testing.T) { + testGenerate(t, arch) + }) + } +} + +func testGenerate(t *testing.T, arch string) { + insnset := iset.Arches[arch] + r := rand.New(testutil.RandSource(t)) + for mode := iset.Mode(0); mode < iset.ModeLast; mode++ { + for repeat := 1; repeat < 10; repeat++ { + if len(insnset.GetInsns(mode, iset.TypeUser)) == 0 { + continue + } + cfg := &iset.Config{ + Arch: arch, + Mode: mode, + Priv: true, + Exec: true, + Len: repeat, + } + text := Generate(cfg, r) + for len(text) != 0 { + size, err := insnset.Decode(mode, text) + if size == 0 || err != nil { + t.Errorf("failed to decode text: %v", text) + break + } + text = text[size:] + } + } + } +} + func allInsns(arch string, mode iset.Mode, priv, exec bool) []iset.Insn { insnset := iset.Arches[arch] insns := insnset.GetInsns(mode, iset.TypeUser) -- cgit mrf-deployment