Unverified Commit c9acf9f2 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #60 from multiformats/feat/test-unix

test: test unix addrs
parents a5c136c9 2646b1d6
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"net" "net"
"path/filepath" "path/filepath"
"runtime"
"strings"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
...@@ -192,6 +194,10 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { ...@@ -192,6 +194,10 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
} }
return network, "[" + ip + "]" + ":" + port, nil return network, "[" + ip + "]" + ":" + port, nil
case "unix": case "unix":
if runtime.GOOS == "windows" {
// convert /c:/... to c:\...
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
}
return network, ip, nil return network, ip, nil
default: default:
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m) return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
...@@ -263,6 +269,16 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) { ...@@ -263,6 +269,16 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
if !ok { if !ok {
return nil, errIncorrectNetAddr return nil, errIncorrectNetAddr
} }
cleaned := filepath.Clean(ac.Name)
return ma.NewComponent("unix", cleaned) path := ac.Name
if runtime.GOOS == "windows" {
// Convert c:\foobar\... to c:/foobar/...
path = filepath.ToSlash(path)
}
if len(path) == 0 || path[0] != '/' {
// convert "" and "c:/..." to "/..."
path = "/" + path
}
return ma.NewComponent("unix", path)
} }
...@@ -2,6 +2,7 @@ package manet ...@@ -2,6 +2,7 @@ package manet
import ( import (
"net" "net"
"runtime"
"testing" "testing"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
...@@ -64,6 +65,24 @@ func TestFromIP4(t *testing.T) { ...@@ -64,6 +65,24 @@ func TestFromIP4(t *testing.T) {
}) })
} }
func TestFromUnix(t *testing.T) {
path := "/C:/foo/bar"
if runtime.GOOS == "windows" {
path = `C:\foo\bar`
}
testConvert(t, "/unix/C:/foo/bar", func() (ma.Multiaddr, error) {
return FromNetAddr(&net.UnixAddr{Name: path, Net: "unix"})
})
}
func TestToUnix(t *testing.T) {
path := "/C:/foo/bar"
if runtime.GOOS == "windows" {
path = `C:\foo\bar`
}
testToNetAddr(t, "/unix/C:/foo/bar", "unix", path)
}
func TestFromIP6(t *testing.T) { func TestFromIP6(t *testing.T) {
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) { testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) {
return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")}) return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment