|
1 | 1 | package peerstore
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/libp2p/go-libp2p-peer"
|
@@ -57,6 +58,88 @@ func TestPeerInfoMarshal(t *testing.T) {
|
57 | 58 | }
|
58 | 59 | }
|
59 | 60 |
|
| 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 | + |
60 | 143 | func TestP2pAddrParsing(t *testing.T) {
|
61 | 144 | id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
|
62 | 145 | if err != nil {
|
|
0 commit comments