From 6a60f4058eea7264ee4475503e4dcd53776b763c Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Wed, 26 Apr 2017 01:44:38 +0200 Subject: [PATCH 1/2] Add func to construct /ipfs addrs from PeerInfo --- peerinfo.go | 13 +++++++++++++ peerinfo_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/peerinfo.go b/peerinfo.go index 3226a50..2b86f08 100644 --- a/peerinfo.go +++ b/peerinfo.go @@ -50,6 +50,19 @@ func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) { }, nil } +func InfoToP2pAddrs(pi *PeerInfo) ([]ma.Multiaddr, error) { + addrs := []ma.Multiaddr{} + tpl := "/" + ma.ProtocolWithCode(ma.P_IPFS).Name + "/" + for _, addr := range pi.Addrs { + p2paddr, err := ma.NewMultiaddr(tpl + peer.IDB58Encode(pi.ID)) + if err != nil { + return nil, err + } + addrs = append(addrs, addr.Encapsulate(p2paddr)) + } + return addrs, nil +} + func (pi *PeerInfo) Loggable() map[string]interface{} { return map[string]interface{}{ "peerID": pi.ID.Pretty(), diff --git a/peerinfo_test.go b/peerinfo_test.go index edad247..11d0737 100644 --- a/peerinfo_test.go +++ b/peerinfo_test.go @@ -83,3 +83,42 @@ func TestP2pAddrParsing(t *testing.T) { t.Fatal("didnt get expected address") } } + +func TestP2pAddrConstruction(t *testing.T) { + id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ") + if err != nil { + t.Error(err) + } + addr := ma.StringCast("/ip4/1.2.3.4/tcp/4536") + p2paddr := ma.Join(addr, ma.StringCast("/ipfs/"+peer.IDB58Encode(id))) + + pi := &PeerInfo{ID: id, Addrs: []ma.Multiaddr{addr}} + p2paddrs, err := InfoToP2pAddrs(pi) + if err != nil { + t.Error(err) + } + + if len(p2paddrs) != 1 { + t.Fatalf("expected 1 addr, got %d", len(p2paddrs)) + } + + if !p2paddr.Equal(p2paddrs[0]) { + t.Fatalf("expected [%s], got [%s]", p2paddr, p2paddrs[0]) + } + + pi = &PeerInfo{ID: id} + p2paddrs, err = InfoToP2pAddrs(pi) + if err != nil { + t.Error(err) + } + + if len(p2paddrs) > 0 { + t.Fatalf("expected 0 addrs, got %d", len(p2paddrs)) + } + + pi = &PeerInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/4536")}} + _, err = InfoToP2pAddrs(pi) + if err == nil { + t.Fatalf("expected error, got none") + } +} From 2ce0773687cf1c37c1ad0f51df23f46f0a04836a Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Wed, 26 Apr 2017 01:58:43 +0200 Subject: [PATCH 2/2] Allow for parsing otherwise empty /ipfs addrs, improve tests --- peerinfo.go | 13 +++++++++--- peerinfo_test.go | 51 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/peerinfo.go b/peerinfo.go index 2b86f08..cb21626 100644 --- a/peerinfo.go +++ b/peerinfo.go @@ -31,12 +31,13 @@ func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) { return nil, ErrInvalidAddr } - ipfspart := parts[len(parts)-1] // last part + // TODO(lgierth): we shouldn't assume /ipfs is the last part + ipfspart := parts[len(parts)-1] if ipfspart.Protocols()[0].Code != ma.P_IPFS { return nil, ErrInvalidAddr } - // make sure 'ipfs id' parses as a peer.ID + // make sure the /ipfs value parses as a peer.ID peerIdParts := strings.Split(ipfspart.String(), "/") peerIdStr := peerIdParts[len(peerIdParts)-1] id, err := peer.IDB58Decode(peerIdStr) @@ -44,9 +45,15 @@ func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) { return nil, err } + // we might have received just an /ipfs part, which means there's no addr. + var addrs []ma.Multiaddr + if len(parts) > 1 { + addrs = append(addrs, ma.Join(parts[:len(parts)-1]...)) + } + return &PeerInfo{ ID: id, - Addrs: []ma.Multiaddr{ma.Join(parts[:len(parts)-1]...)}, + Addrs: addrs, }, nil } diff --git a/peerinfo_test.go b/peerinfo_test.go index 11d0737..57ca5cc 100644 --- a/peerinfo_test.go +++ b/peerinfo_test.go @@ -58,29 +58,54 @@ func TestPeerInfoMarshal(t *testing.T) { } func TestP2pAddrParsing(t *testing.T) { - a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536") id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ") if err != nil { - t.Fatal(err) + t.Error(err) } + addr := ma.StringCast("/ip4/1.2.3.4/tcp/4536") + p2paddr := ma.Join(addr, ma.StringCast("/ipfs/"+peer.IDB58Encode(id))) - p2pa := a.String() + "/ipfs/" + id.Pretty() - p2pma, err := ma.NewMultiaddr(p2pa) + pinfo, err := InfoFromP2pAddr(p2paddr) if err != nil { - t.Fatal(err) - } - - pinfo, err := InfoFromP2pAddr(p2pma) - if err != nil { - t.Fatal(err) + t.Error(err) } if pinfo.ID != id { - t.Fatal("didnt get expected peerID") + t.Fatalf("expected PeerID [%s], got [%s]", id, pinfo.ID) } - if !a.Equal(pinfo.Addrs[0]) { - t.Fatal("didnt get expected address") + if len(pinfo.Addrs) != 1 { + t.Fatalf("expected 1 addr, got %d", len(pinfo.Addrs)) + } + + if !addr.Equal(pinfo.Addrs[0]) { + t.Fatalf("expected addr [%s], got [%s]", addr, pinfo.Addrs[0]) + } + + addr = ma.StringCast("/ipfs/" + peer.IDB58Encode(id)) + pinfo, err = InfoFromP2pAddr(addr) + if err != nil { + t.Error(err) + } + + if pinfo.ID != id { + t.Fatalf("expected PeerID [%s], got [%s]", id, pinfo.ID) + } + + if len(pinfo.Addrs) > 0 { + t.Fatalf("expected 0 addrs, got %d", len(pinfo.Addrs)) + } + + addr = ma.StringCast("/ip4/1.2.3.4/tcp/4536") + pinfo, err = InfoFromP2pAddr(addr) + if err == nil { + t.Fatalf("expected error, got none") + } + + addr = ma.StringCast("/ip4/1.2.3.4/tcp/4536/http") + pinfo, err = InfoFromP2pAddr(addr) + if err == nil { + t.Fatalf("expected error, got none") } }