diff options
| author | Alexander Potapenko <glider@google.com> | 2024-04-16 15:09:36 +0200 |
|---|---|---|
| committer | Alexander Potapenko <glider@google.com> | 2024-04-17 14:36:52 +0000 |
| commit | bd38b692cfb82df95728b979e5f3572112aa005b (patch) | |
| tree | 7e7d68b6ad9179e6621b1cc58c1b83fdd4776f41 /pkg | |
| parent | acc528cbf40c42eb112d2ce66c5f778bd4a397fb (diff) | |
pkg/ifuzz: test Generate()
Make sure random text generation works as a whole, not just for single instructions.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/ifuzz/ifuzz_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
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) |
