Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit 00679cc

Browse files
authored
Merge pull request #45 from zhizouxiao/fix/peerinfo-unmarshal
PeerInfo UnMarshal Error #393
2 parents 545f62f + de5683c commit 00679cc

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

peerinfo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (pi *PeerInfo) Loggable() map[string]interface{} {
7777
}
7878
}
7979

80-
func (pi *PeerInfo) MarshalJSON() ([]byte, error) {
80+
func (pi PeerInfo) MarshalJSON() ([]byte, error) {
8181
out := make(map[string]interface{})
8282
out["ID"] = pi.ID.Pretty()
8383
var addrs []string

peerinfo_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package peerstore
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/libp2p/go-libp2p-peer"
@@ -57,6 +58,88 @@ func TestPeerInfoMarshal(t *testing.T) {
5758
}
5859
}
5960

61+
func TestPeerInfoMarshalWithPointer(t *testing.T) {
62+
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
63+
b := mustAddr(t, "/ip4/1.2.3.8/udp/7777")
64+
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
69+
pi := PeerInfo{
70+
ID: id,
71+
Addrs: []ma.Multiaddr{a, b},
72+
}
73+
74+
data, err := json.Marshal(&pi)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
79+
pi2 := new(PeerInfo)
80+
if err := json.Unmarshal(data, pi2); err != nil {
81+
t.Fatal(err)
82+
}
83+
84+
if pi2.ID != pi.ID {
85+
t.Fatal("ids didnt match after marshal")
86+
}
87+
88+
if !pi.Addrs[0].Equal(pi2.Addrs[0]) {
89+
t.Fatal("wrong addrs")
90+
}
91+
92+
if !pi.Addrs[1].Equal(pi2.Addrs[1]) {
93+
t.Fatal("wrong addrs")
94+
}
95+
96+
lgbl := pi2.Loggable()
97+
if lgbl["peerID"] != id.Pretty() {
98+
t.Fatal("loggables gave wrong peerID output")
99+
}
100+
}
101+
102+
func TestPeerInfoMarshalWithValue(t *testing.T) {
103+
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
104+
b := mustAddr(t, "/ip4/1.2.3.8/udp/7777")
105+
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
pi := PeerInfo{
111+
ID: id,
112+
Addrs: []ma.Multiaddr{a, b},
113+
}
114+
115+
data, err := json.Marshal(pi)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
pi2 := new(PeerInfo)
121+
if err := json.Unmarshal(data, pi2); err != nil {
122+
t.Fatal(err)
123+
}
124+
125+
if pi2.ID != pi.ID {
126+
t.Fatal("ids didnt match after marshal")
127+
}
128+
129+
if !pi.Addrs[0].Equal(pi2.Addrs[0]) {
130+
t.Fatal("wrong addrs")
131+
}
132+
133+
if !pi.Addrs[1].Equal(pi2.Addrs[1]) {
134+
t.Fatal("wrong addrs")
135+
}
136+
137+
lgbl := pi2.Loggable()
138+
if lgbl["peerID"] != id.Pretty() {
139+
t.Fatal("loggables gave wrong peerID output")
140+
}
141+
}
142+
60143
func TestP2pAddrParsing(t *testing.T) {
61144
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
62145
if err != nil {

0 commit comments

Comments
 (0)