From 8904ff96b59a4f9433eae0897cce551fb0f1613d Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 24 Sep 2016 11:46:43 +0200 Subject: prog: add a simple test for exec encoding --- prog/encodingexec_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 prog/encodingexec_test.go (limited to 'prog/encodingexec_test.go') diff --git a/prog/encodingexec_test.go b/prog/encodingexec_test.go new file mode 100644 index 000000000..8c64581f9 --- /dev/null +++ b/prog/encodingexec_test.go @@ -0,0 +1,48 @@ +// Copyright 2016 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 prog + +import ( + "bytes" + "encoding/binary" + "fmt" + "testing" + + "github.com/google/syzkaller/sys" +) + +func TestSerializeForExecRandom(t *testing.T) { + rs, iters := initTest(t) + for i := 0; i < iters; i++ { + p := Generate(rs, 10, nil) + p.SerializeForExec() + } +} + +func TestSerializeForExec(t *testing.T) { + tests := []struct { + prog string + serialized []uint64 + }{ + { + "getpid()", + []uint64{uint64(sys.CallMap["getpid"].ID), 0, uint64(ExecInstrEOF)}, + }, + } + for i, test := range tests { + p, err := Deserialize([]byte(test.prog)) + if err != nil { + t.Fatalf("failed to deserialize prog %v: %v", i, err) + } + t.Run(fmt.Sprintf("%v:%v", i, p.String()), func(t *testing.T) { + data := p.SerializeForExec() + w := new(bytes.Buffer) + binary.Write(w, binary.LittleEndian, test.serialized) + if !bytes.Equal(data, w.Bytes()) { + t.Fatalf("want %+q, got %+q", w.Bytes(), data) + } + + }) + } +} -- cgit mrf-deployment