land/cmd/land/process_test.go

123 lines
2.2 KiB
Go
Raw Normal View History

2018-06-08 02:02:12 +00:00
package main
import (
2018-06-20 14:58:19 +00:00
"bytes"
"encoding/binary"
2018-06-08 02:02:12 +00:00
"os"
"testing"
2018-06-20 14:58:19 +00:00
"github.com/kr/pretty"
2018-06-08 02:02:12 +00:00
)
2018-06-18 01:48:10 +00:00
func testWasmFile(t *testing.T, fname string) *Process {
2018-06-18 01:16:24 +00:00
fin, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
defer fin.Close()
2018-06-18 01:48:10 +00:00
p, err := NewProcess(fin, fname)
2018-06-18 01:16:24 +00:00
if err != nil {
t.Fatal(err)
}
2018-06-21 03:06:35 +00:00
p.name = fname
2018-06-18 01:16:24 +00:00
ret, err := p.Main()
if err != nil {
t.Fatal(err)
}
if ret != 0 {
t.Fatalf("expected return code to be 0, got: %d", ret)
}
2018-06-18 01:48:10 +00:00
return p
2018-06-18 01:16:24 +00:00
}
2018-06-08 02:02:12 +00:00
func TestHelloWorld(t *testing.T) {
fin, err := os.Open("./testdata/hello.wasm")
if err != nil {
t.Fatal(err)
}
defer fin.Close()
2018-06-18 01:48:10 +00:00
p, err := NewProcess(fin, "hello")
2018-06-08 02:02:12 +00:00
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)
}
2018-06-18 00:38:05 +00:00
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))
}
2018-06-08 02:02:12 +00:00
}
2018-06-18 01:16:24 +00:00
func TestWriteFile(t *testing.T) {
testWasmFile(t, "./testdata/writefile.wasm")
}
2018-06-18 01:48:10 +00:00
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))
}
}
2018-06-20 14:58:19 +00:00
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)
}
}
2018-06-21 03:06:35 +00:00
func TestCHelloWorld(t *testing.T) {
testWasmFile(t, "./testdata/helloworld.wasm")
}
2018-06-22 01:43:03 +00:00
func TestFizzbuzz(t *testing.T) {
testWasmFile(t, "./testdata/fizzbuzz.wasm")
}