Skip to content

Commit

Permalink
zmq4: fix splitAddr to properly join host and port for ipv6
Browse files Browse the repository at this point in the history
Fixes #83 

Co-authored-by: Sergey Egorov <[email protected]>
  • Loading branch information
egorse and Sergey Egorov authored Jun 3, 2020
1 parent de8318e commit 59e906c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func splitAddr(v string) (network, addr string, err error) {
case "", "*":
host = "0.0.0.0"
}
addr = host + ":" + port
addr = net.JoinHostPort(host, port)
return network, addr, err

case "ipc":
Expand Down
56 changes: 56 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zmq4

import (
"fmt"
"testing"
)

func TestSplitAddr(t *testing.T) {
testCases := []struct {
desc string
v string
network string
addr string
err error
}{
{
desc: "tcp wild",
v: "tcp://*:5000",
network: "tcp",
addr: "0.0.0.0:5000",
err: nil,
},
{
desc: "tcp ipv4",
v: "tcp://127.0.0.1:6000",
network: "tcp",
addr: "127.0.0.1:6000",
err: nil,
},
{
desc: "tcp ipv6",
v: "tcp://[::1]:7000",
network: "tcp",
addr: "[::1]:7000",
err: nil,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
network, addr, err := splitAddr(tc.v)
if network != tc.network {
t.Fatalf("unexpected network: got=%v, want=%v", network, tc.network)
}
if addr != tc.addr {
t.Fatalf("unexpected address: got=%q, want=%q", addr, tc.addr)
}
if fmt.Sprintf("%+v", err) != fmt.Sprintf("%+v", tc.err) { // nil-safe comparison errors by value
t.Fatalf("unexpected error: got=%+v, want=%+v", err, tc.err)
}
})
}
}

0 comments on commit 59e906c

Please sign in to comment.