Skip to content

Commit

Permalink
add marshal unittest #393
Browse files Browse the repository at this point in the history
  • Loading branch information
wzp committed Nov 9, 2018
1 parent fc976fd commit e4c99a1
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions p2p/host/peerstore/peerinfo_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package peerstore

import (
"encoding/json"
"testing"

"github.com/libp2p/go-libp2p-peer"
Expand Down Expand Up @@ -57,6 +58,88 @@ func TestPeerInfoMarshal(t *testing.T) {
}
}

func TestPeerInfoMarshalWithPointer(t *testing.T) {
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
b := mustAddr(t, "/ip4/1.2.3.8/udp/7777")
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
if err != nil {
t.Fatal(err)
}

pi := PeerInfo{
ID: id,
Addrs: []ma.Multiaddr{a, b},
}

data, err := json.Marshal(&pi)
if err != nil {
t.Fatal(err)
}

pi2 := new(PeerInfo)
if err := json.Unmarshal(data, pi2); err != nil {
t.Fatal(err)
}

if pi2.ID != pi.ID {
t.Fatal("ids didnt match after marshal")
}

if !pi.Addrs[0].Equal(pi2.Addrs[0]) {
t.Fatal("wrong addrs")
}

if !pi.Addrs[1].Equal(pi2.Addrs[1]) {
t.Fatal("wrong addrs")
}

lgbl := pi2.Loggable()
if lgbl["peerID"] != id.Pretty() {
t.Fatal("loggables gave wrong peerID output")
}
}

func TestPeerInfoMarshalWithValue(t *testing.T) {
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
b := mustAddr(t, "/ip4/1.2.3.8/udp/7777")
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
if err != nil {
t.Fatal(err)
}

pi := PeerInfo{
ID: id,
Addrs: []ma.Multiaddr{a, b},
}

data, err := json.Marshal(pi)
if err != nil {
t.Fatal(err)
}

pi2 := new(PeerInfo)
if err := json.Unmarshal(data, pi2); err != nil {
t.Fatal(err)
}

if pi2.ID != pi.ID {
t.Fatal("ids didnt match after marshal")
}

if !pi.Addrs[0].Equal(pi2.Addrs[0]) {
t.Fatal("wrong addrs")
}

if !pi.Addrs[1].Equal(pi2.Addrs[1]) {
t.Fatal("wrong addrs")
}

lgbl := pi2.Loggable()
if lgbl["peerID"] != id.Pretty() {
t.Fatal("loggables gave wrong peerID output")
}
}

func TestP2pAddrParsing(t *testing.T) {
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
if err != nil {
Expand Down

0 comments on commit e4c99a1

Please sign in to comment.