2020-03-19 05:53:15 +00:00
|
|
|
package vectors
|
|
|
|
|
|
|
|
import (
|
2020-05-18 18:02:34 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2020-03-19 05:53:15 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-05-18 18:02:34 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-03-19 05:53:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(fi).Decode(out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockHeaderVectors(t *testing.T) {
|
2020-04-08 16:48:15 +00:00
|
|
|
t.Skip("we need to regenerate for beacon")
|
2020-03-19 05:53:15 +00:00
|
|
|
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) {
|
2020-05-18 18:02:34 +00:00
|
|
|
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) {
|
2020-07-30 18:47:12 +00:00
|
|
|
t.Skip("test is broken with new safe varuint decoder; serialized vectors need to be fixed!")
|
|
|
|
|
2020-05-18 18:02:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 05:53:15 +00:00
|
|
|
}
|