OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / vendor / github.com / bytom / crypto / ed25519 / chainkd / serialize_test.go
diff --git a/vendor/github.com/bytom/crypto/ed25519/chainkd/serialize_test.go b/vendor/github.com/bytom/crypto/ed25519/chainkd/serialize_test.go
new file mode 100644 (file)
index 0000000..142549d
--- /dev/null
@@ -0,0 +1,37 @@
+package chainkd
+
+import (
+       "bytes"
+       "encoding/hex"
+       "encoding/json"
+       "reflect"
+       "testing"
+)
+
+func TestMarshalingFuncs(t *testing.T) {
+       xprv, err := NewXPrv(nil)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       want := make([]byte, hex.EncodedLen(len(xprv.Bytes())))
+       hex.Encode(want, xprv.Bytes())
+
+       got, err := json.Marshal(xprv)
+       if err != nil {
+               t.Fatal(err)
+       }
+       // First and last bytes are "
+       if !reflect.DeepEqual(want, got[1:len(got)-1]) {
+               t.Errorf("marshaling error: want = %+v, got = %+v", want, got)
+       }
+
+       secXprv := new(XPrv)
+       err = json.Unmarshal(got, &secXprv)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(xprv[:], secXprv[:]) {
+               t.Errorf("unmarshaling error: want = %+v, got = %+v", xprv, secXprv)
+       }
+}