@cryptotaxi247 / kubo / commits / 20a20c9e1

updated multiaddr

Juan Batiz-Benet committed Oct 6, 2014 at 04:07 UTC 20a20c9e1a42cff5a45ef02a3529793998d33320
10 files changed +302 -184
Godeps/Godeps.json
+2 -2
@@ -80,8 +80,8 @@
80 },
81 {
82 "ImportPath": "github.com/jbenet/go-multiaddr",
83 - "Comment": "0.1.2-3-g74443fc",
84 - "Rev": "74443fca319c4c2f5e9968b8e268c30a4a74dc64"
83 + "Comment": "0.1.2-9-g1ec9436",
84 + "Rev": "1ec9436b1d642f4f04c0d9e21a0719cda3d659ee"
85 },
86 {
87 "ImportPath": "github.com/jbenet/go-multihash",
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/LICENSE new
+21
@@ -0,0 +1,21 @@
1 +The MIT License (MIT)
2 +
3 +Copyright (c) 2014 Juan Batiz-Benet
4 +
5 +Permission is hereby granted, free of charge, to any person obtaining a copy
6 +of this software and associated documentation files (the "Software"), to deal
7 +in the Software without restriction, including without limitation the rights
8 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom the Software is
10 +furnished to do so, subject to the following conditions:
11 +
12 +The above copyright notice and this permission notice shall be included in
13 +all copies or substantial portions of the Software.
14 +
15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 +THE SOFTWARE.
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/README.md
+20 -18
@@ -7,18 +7,20 @@
7 ### Simple
8
9 ```go
10 -import "github.com/jbenet/go-multiaddr"
10 +import ma "github.com/jbenet/go-multiaddr"
11
12 -m := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
13 -// <Multiaddr /ip4/127.0.0.1/udp/1234>
14 -m.buffer
15 -// <Buffer >
16 -m.String()
17 -// /ip4/127.0.0.1/udp/1234
12 +// construct from a string (err signals parse failure)
13 +m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
14
19 -// construct with Buffer
20 -m = multiaddr.Multiaddr{ Bytes: m.Bytes }
21 -// <Multiaddr /ip4/127.0.0.1/udp/1234>
15 +// construct from bytes (err signals parse failure)
16 +m2, err := ma.NewMultiaddrBytes(m1.Bytes())
17 +
18 +// true
19 +strings.Equal(m1.String(), "/ip4/127.0.0.1/udp/1234")
20 +strings.Equal(m1.String(), m2.String())
21 +bytes.Equal(m1.Bytes(), m2.Bytes())
22 +m1.Equal(m2)
23 +m2.Equal(m1)
24 ```
25
26 ### Protocols
@@ -36,7 +38,7 @@ addr.Protocols()
38
39 ```go
40 // handles the stupid url version too
39 -m = multiaddr.NewUrl("udp4://127.0.0.1:1234")
41 +m = ma.NewUrl("udp4://127.0.0.1:1234")
42 // <Multiaddr /ip4/127.0.0.1/udp/1234>
43 m.Url(buf)
44 // udp4://127.0.0.1:1234
@@ -45,9 +47,9 @@ m.Url(buf)
47 ### En/decapsulate
48
49 ```go
48 -m.Encapsulate(m.NewMultiaddr("/sctp/5678"))
50 +m.Encapsulate(ma.NewMultiaddr("/sctp/5678"))
51 // <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
50 -m.Decapsulate(m.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
52 +m.Decapsulate(ma.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
53 // <Multiaddr /ip4/127.0.0.1>
54 ```
55
@@ -56,11 +58,11 @@ m.Decapsulate(m.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
58 Multiaddr allows expressing tunnels very nicely.
59
60 ```js
59 -printer := multiaddr.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
60 -proxy := multiaddr.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
61 +printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
62 +proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
63 printerOverProxy := proxy.Encapsulate(printer)
62 -// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
64 +// /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80
65
64 -proxyAgain := printerOverProxy.Decapsulate(multiaddr.NewMultiaddr("/ip4"))
65 -// <Multiaddr /ip4/10.20.30.40/tcp/443>
66 +proxyAgain := printerOverProxy.Decapsulate(printer)
67 +// /ip4/10.20.30.40/tcp/443
68 ```
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/doc.go new
+36
@@ -0,0 +1,36 @@
1 +/*
2 +Package multiaddr provides an implementation of the Multiaddr network
3 +address format. Multiaddr emphasizes explicitness, self-description, and
4 +portability. It allows applications to treat addresses as opaque tokens,
5 +and to avoid making assumptions about the address representation (e.g. length).
6 +Learn more at https://github.com/jbenet/multiaddr
7 +
8 +Basic Use:
9 +
10 + import (
11 + "bytes"
12 + "strings"
13 + ma "github.com/jbenet/go-multiaddr"
14 + )
15 +
16 + // construct from a string (err signals parse failure)
17 + m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
18 +
19 + // construct from bytes (err signals parse failure)
20 + m2, err := ma.NewMultiaddrBytes(m1.Bytes())
21 +
22 + // true
23 + strings.Equal(m1.String(), "/ip4/127.0.0.1/udp/1234")
24 + strings.Equal(m1.String(), m2.String())
25 + bytes.Equal(m1.Bytes(), m2.Bytes())
26 + m1.Equal(m2)
27 + m2.Equal(m1)
28 +
29 + // tunneling (en/decap)
30 + printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
31 + proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
32 + printerOverProxy := proxy.Encapsulate(printer)
33 + proxyAgain := printerOverProxy.Decapsulate(printer)
34 +
35 +*/
36 +package multiaddr
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/index.go deleted
-117
@@ -1,117 +0,0 @@
1 -package multiaddr
2 -
3 -import (
4 - "bytes"
5 - "fmt"
6 - "strings"
7 -)
8 -
9 -// Multiaddr is the data structure representing a multiaddr
10 -type Multiaddr struct {
11 - Bytes []byte
12 -}
13 -
14 -// NewMultiaddr parses and validates an input string, returning a *Multiaddr
15 -func NewMultiaddr(s string) (*Multiaddr, error) {
16 - b, err := stringToBytes(s)
17 - if err != nil {
18 - return nil, err
19 - }
20 - return &Multiaddr{Bytes: b}, nil
21 -}
22 -
23 -// Equal tests whether two multiaddrs are equal
24 -func (m *Multiaddr) Equal(m2 *Multiaddr) bool {
25 - return bytes.Equal(m.Bytes, m2.Bytes)
26 -}
27 -
28 -// String returns the string representation of a Multiaddr
29 -func (m *Multiaddr) String() (string, error) {
30 - return bytesToString(m.Bytes)
31 -}
32 -
33 -// Protocols returns the list of protocols this Multiaddr has.
34 -func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
35 -
36 - // panic handler, in case we try accessing bytes incorrectly.
37 - defer func() {
38 - if e := recover(); e != nil {
39 - ret = nil
40 - err = e.(error)
41 - }
42 - }()
43 -
44 - ps := []*Protocol{}
45 - b := m.Bytes[:]
46 - for len(b) > 0 {
47 - p := ProtocolWithCode(int(b[0]))
48 - if p == nil {
49 - return nil, fmt.Errorf("no protocol with code %d", b[0])
50 - }
51 - ps = append(ps, p)
52 - b = b[1+(p.Size/8):]
53 - }
54 - return ps, nil
55 -}
56 -
57 -// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
58 -func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
59 - b := make([]byte, len(m.Bytes)+len(o.Bytes))
60 - b = append(m.Bytes, o.Bytes...)
61 - return &Multiaddr{Bytes: b}
62 -}
63 -
64 -// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
65 -func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
66 - s1, err := m.String()
67 - if err != nil {
68 - return nil, err
69 - }
70 -
71 - s2, err := o.String()
72 - if err != nil {
73 - return nil, err
74 - }
75 -
76 - i := strings.LastIndex(s1, s2)
77 - if i < 0 {
78 - return nil, fmt.Errorf("%s not contained in %s", s2, s1)
79 - }
80 - return NewMultiaddr(s1[:i])
81 -}
82 -
83 -// DialArgs is a convenience function returning arguments for use in net.Dial
84 -func (m *Multiaddr) DialArgs() (string, string, error) {
85 - if !m.IsThinWaist() {
86 - return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
87 - }
88 -
89 - str, err := m.String()
90 - if err != nil {
91 - return "", "", err
92 - }
93 -
94 - parts := strings.Split(str, "/")[1:]
95 - network := parts[2]
96 - host := strings.Join([]string{parts[1], parts[3]}, ":")
97 - return network, host, nil
98 -}
99 -
100 -// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
101 -// This means: /{IP4, IP6}/{TCP, UDP}
102 -func (m *Multiaddr) IsThinWaist() bool {
103 - p, err := m.Protocols()
104 - if err != nil {
105 - return false
106 - }
107 -
108 - if p[0].Code != P_IP4 && p[0].Code != P_IP6 {
109 - return false
110 - }
111 -
112 - if p[1].Code != P_TCP && p[1].Code != P_UDP {
113 - return false
114 - }
115 -
116 - return true
117 -}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/interface.go new
+42
@@ -0,0 +1,42 @@
1 +package multiaddr
2 +
3 +/*
4 +Multiaddr is a cross-protocol, cross-platform format for representing
5 +internet addresses. It emphasizes explicitness and self-description.
6 +Learn more here: https://github.com/jbenet/multiaddr
7 +
8 +Multiaddrs have both a binary and string representation.
9 +
10 + import ma "github.com/jbenet/go-multiaddr"
11 +
12 + addr, err := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/80")
13 + // err non-nil when parsing failed.
14 +
15 +*/
16 +type Multiaddr interface {
17 + // Equal returns whether two Multiaddrs are exactly equal
18 + Equal(Multiaddr) bool
19 +
20 + // Bytes returns the []byte representation of this Multiaddr
21 + Bytes() []byte
22 +
23 + // String returns the string representation of this Multiaddr
24 + // (may panic if internal state is corrupted)
25 + String() string
26 +
27 + // Protocols returns the list of Protocols this Multiaddr includes
28 + // will panic if protocol code incorrect (and bytes accessed incorrectly)
29 + Protocols() []*Protocol
30 +
31 + // Encapsulate wraps this Multiaddr around another. For example:
32 + //
33 + // /ip4/1.2.3.4 encapsulate /tcp/80 = /ip4/1.2.3.4/tcp/80
34 + //
35 + Encapsulate(Multiaddr) Multiaddr
36 +
37 + // Decapsultate removes a Multiaddr wrapping. For example:
38 + //
39 + // /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80
40 + //
41 + Decapsulate(Multiaddr) Multiaddr
42 +}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr.go new
+110
@@ -0,0 +1,110 @@
1 +package multiaddr
2 +
3 +import (
4 + "bytes"
5 + "fmt"
6 + "strings"
7 +)
8 +
9 +// multiaddr is the data structure representing a Multiaddr
10 +type multiaddr struct {
11 + bytes []byte
12 +}
13 +
14 +// NewMultiaddr parses and validates an input string, returning a *Multiaddr
15 +func NewMultiaddr(s string) (Multiaddr, error) {
16 + b, err := stringToBytes(s)
17 + if err != nil {
18 + return nil, err
19 + }
20 + return &multiaddr{bytes: b}, nil
21 +}
22 +
23 +// NewMultiaddrBytes initializes a Multiaddr from a byte representation.
24 +// It validates it as an input string.
25 +func NewMultiaddrBytes(b []byte) (Multiaddr, error) {
26 + s, err := bytesToString(b)
27 + if err != nil {
28 + return nil, err
29 + }
30 + return NewMultiaddr(s)
31 +}
32 +
33 +// Equal tests whether two multiaddrs are equal
34 +func (m *multiaddr) Equal(m2 Multiaddr) bool {
35 + return bytes.Equal(m.bytes, m2.Bytes())
36 +}
37 +
38 +// Bytes returns the []byte representation of this Multiaddr
39 +func (m *multiaddr) Bytes() []byte {
40 + // consider returning copy to prevent changing underneath us?
41 + cpy := make([]byte, len(m.bytes))
42 + copy(cpy, m.bytes)
43 + return cpy
44 +}
45 +
46 +// String returns the string representation of a Multiaddr
47 +func (m *multiaddr) String() string {
48 + s, err := bytesToString(m.bytes)
49 + if err != nil {
50 + panic("multiaddr failed to convert back to string. corrupted?")
51 + }
52 + return s
53 +}
54 +
55 +// Protocols returns the list of protocols this Multiaddr has.
56 +// will panic in case we access bytes incorrectly.
57 +func (m *multiaddr) Protocols() []*Protocol {
58 +
59 + // panic handler, in case we try accessing bytes incorrectly.
60 + defer func() {
61 + if e := recover(); e != nil {
62 + err := e.(error)
63 + panic("Multiaddr.Protocols error: " + err.Error())
64 + }
65 + }()
66 +
67 + ps := []*Protocol{}
68 + b := m.bytes[:]
69 + for len(b) > 0 {
70 + p := ProtocolWithCode(int(b[0]))
71 + if p == nil {
72 + // this is a panic (and not returning err) because this should've been
73 + // caught on constructing the Multiaddr
74 + panic(fmt.Errorf("no protocol with code %d", b[0]))
75 + }
76 + ps = append(ps, p)
77 + b = b[1+(p.Size/8):]
78 + }
79 + return ps
80 +}
81 +
82 +// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
83 +func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
84 + mb := m.bytes
85 + ob := o.Bytes()
86 +
87 + var b bytes.Buffer
88 + b.Write(mb)
89 + b.Write(ob)
90 + return &multiaddr{bytes: b.Bytes()}
91 +}
92 +
93 +// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
94 +func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
95 + s1 := m.String()
96 + s2 := o.String()
97 + i := strings.LastIndex(s1, s2)
98 + if i < 0 {
99 + // if multiaddr not contained, returns a copy.
100 + cpy := make([]byte, len(m.bytes))
101 + copy(cpy, m.bytes)
102 + return &multiaddr{bytes: cpy}
103 + }
104 +
105 + ma, err := NewMultiaddr(s1[:i])
106 + if err != nil {
107 + panic("Multiaddr.Decapsulate incorrect byte boundaries.")
108 + }
109 + return ma
110 +}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr_test.go
+7 -39
@@ -6,7 +6,7 @@ import (
6 "testing"
7 )
8
9 -func newMultiaddr(t *testing.T, a string) *Multiaddr {
9 +func newMultiaddr(t *testing.T, a string) Multiaddr {
10 m, err := NewMultiaddr(a)
11 if err != nil {
12 t.Error(err)
@@ -88,11 +88,7 @@ func TestProtocols(t *testing.T) {
88 t.Error("failed to construct", "/ip4/127.0.0.1/udp/1234")
89 }
90
91 - ps, err := m.Protocols()
92 - if err != nil {
93 - t.Error("failed to get protocols", "/ip4/127.0.0.1/udp/1234")
94 - }
95 -
91 + ps := m.Protocols()
92 if ps[0] != ProtocolWithName("ip4") {
93 t.Error(ps[0], ProtocolWithName("ip4"))
94 t.Error("failed to get ip4 protocol")
@@ -117,47 +113,19 @@ func TestEncapsulate(t *testing.T) {
113 }
114
115 b := m.Encapsulate(m2)
120 - if s, _ := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
116 + if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
117 t.Error("encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed.", s)
118 }
119
120 m3, _ := NewMultiaddr("/udp/5678")
125 - c, err := b.Decapsulate(m3)
126 - if err != nil {
127 - t.Error("decapsulate /udp failed.", err)
128 - }
129 -
130 - if s, _ := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
121 + c := b.Decapsulate(m3)
122 + if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
123 t.Error("decapsulate /udp failed.", "/ip4/127.0.0.1/udp/1234", s)
124 }
125
126 m4, _ := NewMultiaddr("/ip4/127.0.0.1")
135 - d, err := c.Decapsulate(m4)
136 - if err != nil {
137 - t.Error("decapsulate /ip4 failed.", err)
138 - }
139 -
140 - if s, _ := d.String(); s != "" {
127 + d := c.Decapsulate(m4)
128 + if s := d.String(); s != "" {
129 t.Error("decapsulate /ip4 failed.", "/", s)
130 }
131 }
144 -
145 -func TestDialArgs(t *testing.T) {
146 - m, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
147 - if err != nil {
148 - t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
149 - }
150 -
151 - nw, host, err := m.DialArgs()
152 - if err != nil {
153 - t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
154 - }
155 -
156 - if nw != "udp" {
157 - t.Error("failed to get udp network Dial Arg")
158 - }
159 -
160 - if host != "127.0.0.1:1234" {
161 - t.Error("failed to get host:port Dial Arg")
162 - }
163 -}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net.go
+38 -2
@@ -3,12 +3,13 @@ package multiaddr
3 import (
4 "fmt"
5 "net"
6 + "strings"
7 )
8
9 var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
10
11 // FromNetAddr converts a net.Addr type to a Multiaddr.
11 -func FromNetAddr(a net.Addr) (*Multiaddr, error) {
12 +func FromNetAddr(a net.Addr) (Multiaddr, error) {
13 switch a.Network() {
14 case "tcp", "tcp4", "tcp6":
15 ac, ok := a.(*net.TCPAddr)
@@ -65,7 +66,7 @@ func FromNetAddr(a net.Addr) (*Multiaddr, error) {
66 }
67
68 // FromIP converts a net.IP type to a Multiaddr.
68 -func FromIP(ip net.IP) (*Multiaddr, error) {
69 +func FromIP(ip net.IP) (Multiaddr, error) {
70 switch {
71 case ip.To4() != nil:
72 return NewMultiaddr("/ip4/" + ip.String())
@@ -75,3 +76,38 @@ func FromIP(ip net.IP) (*Multiaddr, error) {
76 return nil, errIncorrectNetAddr
77 }
78 }
79 +
80 +// DialArgs is a convenience function returning arguments for use in net.Dial
81 +func DialArgs(m Multiaddr) (string, string, error) {
82 + if !IsThinWaist(m) {
83 + return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
84 + }
85 +
86 + str := m.String()
87 + parts := strings.Split(str, "/")[1:]
88 + network := parts[2]
89 +
90 + var host string
91 + switch parts[0] {
92 + case "ip4":
93 + host = strings.Join([]string{parts[1], parts[3]}, ":")
94 + case "ip6":
95 + host = fmt.Sprintf("[%s]:%s", parts[1], parts[3])
96 + }
97 + return network, host, nil
98 +}
99 +
100 +// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
101 +// This means: /{IP4, IP6}/{TCP, UDP}
102 +func IsThinWaist(m Multiaddr) bool {
103 + p := m.Protocols()
104 + if p[0].Code != P_IP4 && p[0].Code != P_IP6 {
105 + return false
106 + }
107 +
108 + if p[1].Code != P_TCP && p[1].Code != P_UDP {
109 + return false
110 + }
111 +
112 + return true
113 +}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net_test.go
+26 -6
@@ -5,7 +5,7 @@ import (
5 "testing"
6 )
7
8 -type GenFunc func() (*Multiaddr, error)
8 +type GenFunc func() (Multiaddr, error)
9
10 func testConvert(t *testing.T, s string, gen GenFunc) {
11 m, err := gen()
@@ -13,25 +13,25 @@ func testConvert(t *testing.T, s string, gen GenFunc) {
13 t.Fatal("failed to generate.")
14 }
15
16 - if s2, _ := m.String(); err != nil || s2 != s {
16 + if s2 := m.String(); err != nil || s2 != s {
17 t.Fatal("failed to convert: " + s + " != " + s2)
18 }
19 }
20
21 func TestFromIP4(t *testing.T) {
22 - testConvert(t, "/ip4/10.20.30.40", func() (*Multiaddr, error) {
22 + testConvert(t, "/ip4/10.20.30.40", func() (Multiaddr, error) {
23 return FromIP(net.ParseIP("10.20.30.40"))
24 })
25 }
26
27 func TestFromIP6(t *testing.T) {
28 - testConvert(t, "/ip6/2001:4860:0:2001::68", func() (*Multiaddr, error) {
28 + testConvert(t, "/ip6/2001:4860:0:2001::68", func() (Multiaddr, error) {
29 return FromIP(net.ParseIP("2001:4860:0:2001::68"))
30 })
31 }
32
33 func TestFromTCP(t *testing.T) {
34 - testConvert(t, "/ip4/10.20.30.40/tcp/1234", func() (*Multiaddr, error) {
34 + testConvert(t, "/ip4/10.20.30.40/tcp/1234", func() (Multiaddr, error) {
35 return FromNetAddr(&net.TCPAddr{
36 IP: net.ParseIP("10.20.30.40"),
37 Port: 1234,
@@ -40,10 +40,30 @@ func TestFromTCP(t *testing.T) {
40 }
41
42 func TestFromUDP(t *testing.T) {
43 - testConvert(t, "/ip4/10.20.30.40/udp/1234", func() (*Multiaddr, error) {
43 + testConvert(t, "/ip4/10.20.30.40/udp/1234", func() (Multiaddr, error) {
44 return FromNetAddr(&net.UDPAddr{
45 IP: net.ParseIP("10.20.30.40"),
46 Port: 1234,
47 })
48 })
49 }
50 +
51 +func TestDialArgs(t *testing.T) {
52 + m, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
53 + if err != nil {
54 + t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
55 + }
56 +
57 + nw, host, err := DialArgs(m)
58 + if err != nil {
59 + t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
60 + }
61 +
62 + if nw != "udp" {
63 + t.Error("failed to get udp network Dial Arg")
64 + }
65 +
66 + if host != "127.0.0.1:1234" {
67 + t.Error("failed to get host:port Dial Arg")
68 + }
69 +}