Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz compatibility with parity lib #49

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.out
*.test
bin
testdata/
53 changes: 53 additions & 0 deletions compat/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package compat

/*
#include <stdlib.h>
#include "./rust/compat.h"
#cgo LDFLAGS: ${SRCDIR}/rust/target/release/libcompat.a
*/
import "C"

import (
"errors"
"unsafe"
)

//go:generate scalegen

type Struct struct {
Field1 uint16
Field2 [3]byte
}

type Byte32 struct {
Array [32]byte
}

type Compat struct {
Field1 uint8
Field2 uint16
Field3 uint32
Field4 uint64
Field5 [8]byte
Field6 bool
Field7 *Struct
Field8 Struct
Field9 [4]Struct
Field10 []byte
Field11 []Byte32
Field12 [][]byte
Field13 []Struct
}

func RoundTrip(input []byte) ([]byte, error) {
ptr := C.CBytes(input)
defer C.free(ptr)
response := C.round_trip((*C.uchar)(ptr), C.size_t(len(input)))
defer C.free_response(response)
buf := make([]byte, response.len)
copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(response.ptr)), response.len))
if response.code == 1 {
return nil, errors.New(string(buf))
}
return buf, nil
}
267 changes: 267 additions & 0 deletions compat/compat_scale.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions compat/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package compat

import (
"bytes"
"testing"

fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-scale"
)

func FuzzRoundTrip(f *testing.F) {
f.Add([]byte("0"))
f.Add([]byte("321321dssae12312asada3421"))
f.Fuzz(func(t *testing.T, data []byte) {
fuzzer := fuzz.NewFromGoFuzz(data)

compat := Compat{}
fuzzer.Fuzz(&compat)
buf := bytes.NewBuffer(nil)
_, err := compat.EncodeScale(scale.NewEncoder(buf))
require.NoError(t, err)

output, err := RoundTrip(buf.Bytes())
require.NoError(t, err)
require.Equal(t, buf.Bytes(), output)

result := Compat{}
_, err = result.DecodeScale(scale.NewDecoder(bytes.NewReader(output)))
require.NoError(t, err)
require.Equal(t, compat, result)
})
}
2 changes: 2 additions & 0 deletions compat/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
compat.h