lotus/chain/vectors/vectors_test.go
Steven Allen 5733c71c50 Lint everything
We were ignoring quite a few error cases, and had one case where we weren't
actually updating state where we wanted to. Unfortunately, if the linter doesn't
pass, nobody has any reason to actually check lint failures in CI.

There are three remaining XXXs marked in the code for lint.
2020-08-20 20:46:36 -07:00

89 lines
1.8 KiB
Go

package vectors
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/filecoin-project/lotus/chain/types"
)
func LoadVector(t *testing.T, f string, out interface{}) {
p := filepath.Join("../../extern/serialization-vectors", f)
fi, err := os.Open(p)
if err != nil {
t.Fatal(err)
}
defer fi.Close() //nolint:errcheck
if err := json.NewDecoder(fi).Decode(out); err != nil {
t.Fatal(err)
}
}
func TestBlockHeaderVectors(t *testing.T) {
t.Skip("we need to regenerate for beacon")
var headers []HeaderVector
LoadVector(t, "block_headers.json", &headers)
for i, hv := range headers {
if hv.Block.Cid().String() != hv.Cid {
t.Fatalf("CID mismatch in test vector %d", i)
}
data, err := hv.Block.Serialize()
if err != nil {
t.Fatal(err)
}
if fmt.Sprintf("%x", data) != hv.CborHex {
t.Fatalf("serialized data mismatched for test vector %d", i)
}
}
}
func TestMessageSigningVectors(t *testing.T) {
var msvs []MessageSigningVector
LoadVector(t, "message_signing.json", &msvs)
for i, msv := range msvs {
smsg := &types.SignedMessage{
Message: *msv.Unsigned,
Signature: *msv.Signature,
}
if smsg.Cid().String() != msv.Cid {
t.Fatalf("cid of message in vector %d mismatches", i)
}
// TODO: check signature
}
}
func TestUnsignedMessageVectors(t *testing.T) {
t.Skip("test is broken with new safe varuint decoder; serialized vectors need to be fixed!")
var msvs []UnsignedMessageVector
LoadVector(t, "unsigned_messages.json", &msvs)
for i, msv := range msvs {
b, err := msv.Message.Serialize()
if err != nil {
t.Fatal(err)
}
dec, err := hex.DecodeString(msv.HexCbor)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, dec) {
t.Fatalf("serialization vector %d mismatches bytes", i)
}
}
}