mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-03 05:23:08 +00:00
The error in String should not actually ocurr, as the multiaddr should have been valid to be constructed successfully, and thus should be encoded back to its string rep correctly. This will be bolstered by creating an interface (to prevent messing with the internal bytes)
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package multiaddr
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
type GenFunc func() (*Multiaddr, error)
|
|
|
|
func testConvert(t *testing.T, s string, gen GenFunc) {
|
|
m, err := gen()
|
|
if err != nil {
|
|
t.Fatal("failed to generate.")
|
|
}
|
|
|
|
if s2 := m.String(); err != nil || s2 != s {
|
|
t.Fatal("failed to convert: " + s + " != " + s2)
|
|
}
|
|
}
|
|
|
|
func TestFromIP4(t *testing.T) {
|
|
testConvert(t, "/ip4/10.20.30.40", func() (*Multiaddr, error) {
|
|
return FromIP(net.ParseIP("10.20.30.40"))
|
|
})
|
|
}
|
|
|
|
func TestFromIP6(t *testing.T) {
|
|
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (*Multiaddr, error) {
|
|
return FromIP(net.ParseIP("2001:4860:0:2001::68"))
|
|
})
|
|
}
|
|
|
|
func TestFromTCP(t *testing.T) {
|
|
testConvert(t, "/ip4/10.20.30.40/tcp/1234", func() (*Multiaddr, error) {
|
|
return FromNetAddr(&net.TCPAddr{
|
|
IP: net.ParseIP("10.20.30.40"),
|
|
Port: 1234,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestFromUDP(t *testing.T) {
|
|
testConvert(t, "/ip4/10.20.30.40/udp/1234", func() (*Multiaddr, error) {
|
|
return FromNetAddr(&net.UDPAddr{
|
|
IP: net.ParseIP("10.20.30.40"),
|
|
Port: 1234,
|
|
})
|
|
})
|
|
}
|