From 1b2246d7bef87a082eec5f4e8b92cf394e694688 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 12 Sep 2019 14:14:50 -0700 Subject: [PATCH 1/4] test: test unix addrs --- convert_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/convert_test.go b/convert_test.go index 33250a0..0e5548d 100644 --- a/convert_test.go +++ b/convert_test.go @@ -64,6 +64,20 @@ func TestFromIP4(t *testing.T) { }) } +func TestFromUnix(t *testing.T) { + testConvert(t, "/unix/c:/foo/bar", func() (ma.Multiaddr, error) { + return FromNetAddr(&net.UnixAddr{Name: "/c:/foo/bar", Net: "unix"}) + }) + testConvert(t, "/unix/foo/bar", func() (ma.Multiaddr, error) { + return FromNetAddr(&net.UnixAddr{Name: "/foo/bar", Net: "unix"}) + }) +} + +func TestToUnix(t *testing.T) { + testToNetAddr(t, "/unix/c:/foo/bar", "unix", "/c:/foo/bar") + testToNetAddr(t, "/unix/foo/bar", "unix", "/foo/bar") +} + func TestFromIP6(t *testing.T) { testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) { return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")}) From d66032c34030b6f9582b5b01ba63b5b294f2c7ae Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 19 Sep 2019 08:59:17 -0700 Subject: [PATCH 2/4] fix unix path handling on windows * Convert to windows paths when converting from a multiaddr to a net.Addr * Convert from windows paths when converting from a net.Addr to a multiaddr Also, don't "clean". `filepath.Clean` is _usually_ correct but not _technically_ correct in all cases. We should leave cleaning to the application. --- convert.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/convert.go b/convert.go index b1e5446..1f936e6 100644 --- a/convert.go +++ b/convert.go @@ -192,7 +192,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { } return network, "[" + ip + "]" + ":" + port, nil case "unix": - return network, ip, nil + return network, filepath.FromSlash(ip), nil default: return "", "", fmt.Errorf("%s is not a 'thin waist' address", m) } @@ -263,6 +263,6 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) { if !ok { return nil, errIncorrectNetAddr } - cleaned := filepath.Clean(ac.Name) - return ma.NewComponent("unix", cleaned) + + return ma.NewComponent("unix", filepath.ToSlash(ac.Name)) } From 611d2a4f644eabf8e19a6e3a0d3d128094251f48 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 27 Sep 2019 17:41:20 -0700 Subject: [PATCH 3/4] address CR --- convert.go | 20 ++++++++++++++++++-- convert_test.go | 17 +++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/convert.go b/convert.go index 1f936e6..11ee7d5 100644 --- a/convert.go +++ b/convert.go @@ -4,6 +4,8 @@ import ( "fmt" "net" "path/filepath" + "runtime" + "strings" ma "github.com/multiformats/go-multiaddr" ) @@ -192,7 +194,11 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { } return network, "[" + ip + "]" + ":" + port, nil case "unix": - return network, filepath.FromSlash(ip), nil + if runtime.GOOS == "windows" { + // convert /c:/... to c:\... + ip = filepath.FromSlash(strings.TrimLeft(ip, "/")) + } + return network, ip, nil default: return "", "", fmt.Errorf("%s is not a 'thin waist' address", m) } @@ -264,5 +270,15 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) { return nil, errIncorrectNetAddr } - return ma.NewComponent("unix", filepath.ToSlash(ac.Name)) + 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) } diff --git a/convert_test.go b/convert_test.go index 0e5548d..71a6312 100644 --- a/convert_test.go +++ b/convert_test.go @@ -2,6 +2,7 @@ package manet import ( "net" + "runtime" "testing" ma "github.com/multiformats/go-multiaddr" @@ -65,17 +66,21 @@ 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: "/c:/foo/bar", Net: "unix"}) - }) - testConvert(t, "/unix/foo/bar", func() (ma.Multiaddr, error) { - return FromNetAddr(&net.UnixAddr{Name: "/foo/bar", Net: "unix"}) + return FromNetAddr(&net.UnixAddr{Name: path, Net: "unix"}) }) } func TestToUnix(t *testing.T) { - testToNetAddr(t, "/unix/c:/foo/bar", "unix", "/c:/foo/bar") - testToNetAddr(t, "/unix/foo/bar", "unix", "/foo/bar") + 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) { From 2646b1d6f3e36845795c241d581276223ec21e4b Mon Sep 17 00:00:00 2001 From: Dominic Della Valle Date: Sat, 28 Sep 2019 16:18:46 -0400 Subject: [PATCH 4/4] fix unescaped back-slash target --- convert_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/convert_test.go b/convert_test.go index 71a6312..21d36ec 100644 --- a/convert_test.go +++ b/convert_test.go @@ -66,21 +66,21 @@ func TestFromIP4(t *testing.T) { } func TestFromUnix(t *testing.T) { - path := "/c:/foo/bar" + path := "/C:/foo/bar" if runtime.GOOS == "windows" { - path = "c:\foo\bar" + path = `C:\foo\bar` } - testConvert(t, "/unix/c:/foo/bar", func() (ma.Multiaddr, error) { + 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" + path := "/C:/foo/bar" if runtime.GOOS == "windows" { - path = "c:\foo\bar" + path = `C:\foo\bar` } - testToNetAddr(t, "/unix/c:/foo/bar", "unix", path) + testToNetAddr(t, "/unix/C:/foo/bar", "unix", path) } func TestFromIP6(t *testing.T) {