github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/crypto/ed25519/chainkd/serialize_test.go (about)

     1  package chainkd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestMarshalingFuncs(t *testing.T) {
    12  	xprv, err := NewXPrv(nil)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	want := make([]byte, hex.EncodedLen(len(xprv.Bytes())))
    18  	hex.Encode(want, xprv.Bytes())
    19  
    20  	got, err := json.Marshal(xprv)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	// First and last bytes are "
    25  	if !reflect.DeepEqual(want, got[1:len(got)-1]) {
    26  		t.Errorf("marshaling error: want = %+v, got = %+v", want, got)
    27  	}
    28  
    29  	secXprv := new(XPrv)
    30  	err = json.Unmarshal(got, &secXprv)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if !bytes.Equal(xprv[:], secXprv[:]) {
    35  		t.Errorf("unmarshaling error: want = %+v, got = %+v", xprv, secXprv)
    36  	}
    37  }