123 lines
2.2 KiB
Go
123 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/kr/pretty"
|
|
)
|
|
|
|
func testWasmFile(t *testing.T, fname string) *Process {
|
|
fin, err := os.Open(fname)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close()
|
|
|
|
p, err := NewProcess(fin, fname)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.name = fname
|
|
|
|
ret, err := p.Main()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if ret != 0 {
|
|
t.Fatalf("expected return code to be 0, got: %d", ret)
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
func TestHelloWorld(t *testing.T) {
|
|
fin, err := os.Open("./testdata/hello.wasm")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close()
|
|
|
|
p, err := NewProcess(fin, "hello")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ret, err := p.Main()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if ret != 0 {
|
|
t.Fatalf("expected return code to be 0, got: %d", ret)
|
|
}
|
|
|
|
data := p.readMem(200) // should be "Hello"
|
|
if string(data) != "Hello" {
|
|
t.Fatalf("wanted \"Hello\", got: %q", string(data))
|
|
}
|
|
|
|
_, err = p.writeMem(200, []byte("goodbye"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
data = p.readMem(200)
|
|
if string(data) != "goodbye" {
|
|
t.Fatalf("wanted \"goodbye\", got: %q", string(data))
|
|
}
|
|
}
|
|
|
|
func TestWriteFile(t *testing.T) {
|
|
testWasmFile(t, "./testdata/writefile.wasm")
|
|
}
|
|
|
|
func TestFileOps(t *testing.T) {
|
|
p := testWasmFile(t, "./testdata/fileops.wasm")
|
|
data := p.readMem(255)
|
|
if string(data) != "Hello, world!\n" {
|
|
t.Fatalf("wanted \"Hello, world\", got: %q", string(data))
|
|
}
|
|
}
|
|
|
|
func TestArbint(t *testing.T) {
|
|
p := testWasmFile(t, "./testdata/arbint.wasm")
|
|
pretty.Println(p.vm.Memory()[0x200:0x204])
|
|
}
|
|
|
|
type structFromC struct {
|
|
Bar int32
|
|
Baz int32
|
|
}
|
|
|
|
func TestStruct(t *testing.T) {
|
|
p := testWasmFile(t, "./testdata/struct.wasm")
|
|
pretty.Println(p.vm.Memory()[0x200:0x208])
|
|
|
|
foo := structFromC{}
|
|
if err := binary.Read(bytes.NewReader(p.vm.Memory()[0x200:0x208]), binary.LittleEndian, &foo); err != nil {
|
|
t.Fatalf("can't read memory as the struct: %v", err)
|
|
}
|
|
|
|
t.Logf("%#v", foo)
|
|
|
|
if foo.Bar != 31337 {
|
|
t.Fatalf("wanted foo.bar to be 31337, got: %d", foo.Bar)
|
|
}
|
|
|
|
if foo.Baz != 255 {
|
|
t.Fatalf("wanted foo.baz to be 255, got: %d", foo.Baz)
|
|
}
|
|
}
|
|
|
|
func TestCHelloWorld(t *testing.T) {
|
|
testWasmFile(t, "./testdata/helloworld.wasm")
|
|
}
|
|
|
|
func TestFizzbuzz(t *testing.T) {
|
|
testWasmFile(t, "./testdata/fizzbuzz.wasm")
|
|
}
|