Skip to content

Commit c9acf9f

Browse files
authored
Merge pull request #60 from multiformats/feat/test-unix
test: test unix addrs
2 parents a5c136c + 2646b1d commit c9acf9f

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

convert.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"net"
66
"path/filepath"
7+
"runtime"
8+
"strings"
79

810
ma "github.com/multiformats/go-multiaddr"
911
)
@@ -192,6 +194,10 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
192194
}
193195
return network, "[" + ip + "]" + ":" + port, nil
194196
case "unix":
197+
if runtime.GOOS == "windows" {
198+
// convert /c:/... to c:\...
199+
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
200+
}
195201
return network, ip, nil
196202
default:
197203
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
@@ -263,6 +269,16 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
263269
if !ok {
264270
return nil, errIncorrectNetAddr
265271
}
266-
cleaned := filepath.Clean(ac.Name)
267-
return ma.NewComponent("unix", cleaned)
272+
273+
path := ac.Name
274+
if runtime.GOOS == "windows" {
275+
// Convert c:\foobar\... to c:/foobar/...
276+
path = filepath.ToSlash(path)
277+
}
278+
if len(path) == 0 || path[0] != '/' {
279+
// convert "" and "c:/..." to "/..."
280+
path = "/" + path
281+
}
282+
283+
return ma.NewComponent("unix", path)
268284
}

convert_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package manet
22

33
import (
44
"net"
5+
"runtime"
56
"testing"
67

78
ma "github.com/multiformats/go-multiaddr"
@@ -64,6 +65,24 @@ func TestFromIP4(t *testing.T) {
6465
})
6566
}
6667

68+
func TestFromUnix(t *testing.T) {
69+
path := "/C:/foo/bar"
70+
if runtime.GOOS == "windows" {
71+
path = `C:\foo\bar`
72+
}
73+
testConvert(t, "/unix/C:/foo/bar", func() (ma.Multiaddr, error) {
74+
return FromNetAddr(&net.UnixAddr{Name: path, Net: "unix"})
75+
})
76+
}
77+
78+
func TestToUnix(t *testing.T) {
79+
path := "/C:/foo/bar"
80+
if runtime.GOOS == "windows" {
81+
path = `C:\foo\bar`
82+
}
83+
testToNetAddr(t, "/unix/C:/foo/bar", "unix", path)
84+
}
85+
6786
func TestFromIP6(t *testing.T) {
6887
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) {
6988
return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")})

0 commit comments

Comments
 (0)