mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2024-12-26 23:30:27 +08:00
07fbce0b46
Avoids circular module dependency.
84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package peer_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
. "github.com/libp2p/go-libp2p-core/test"
|
|
)
|
|
|
|
func TestPeerSerdePB(t *testing.T) {
|
|
id, err := RandPeerID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := id.Marshal()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var id2 peer.ID
|
|
if err = id2.Unmarshal(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id != id2 {
|
|
t.Error("expected equal ids in circular serde test")
|
|
}
|
|
}
|
|
|
|
func TestPeerSerdeJSON(t *testing.T) {
|
|
id, err := RandPeerID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := id.MarshalJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var id2 peer.ID
|
|
if err = id2.UnmarshalJSON(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id != id2 {
|
|
t.Error("expected equal ids in circular serde test")
|
|
}
|
|
}
|
|
|
|
func TestBinaryMarshaler(t *testing.T) {
|
|
id, err := RandPeerID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := id.MarshalBinary()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var id2 peer.ID
|
|
if err = id2.UnmarshalBinary(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id != id2 {
|
|
t.Error("expected equal ids in circular serde test")
|
|
}
|
|
}
|
|
|
|
func TestTextMarshaler(t *testing.T) {
|
|
id, err := RandPeerID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := id.MarshalText()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var id2 peer.ID
|
|
if err = id2.UnmarshalText(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id != id2 {
|
|
t.Error("expected equal ids in circular serde test")
|
|
}
|
|
}
|