From 59e906cdade6f29bc7fdffd6da98393f7053ddd9 Mon Sep 17 00:00:00 2001 From: Sergey Egorov Date: Wed, 3 Jun 2020 10:35:38 +0300 Subject: [PATCH] zmq4: fix splitAddr to properly join host and port for ipv6 Fixes #83 Co-authored-by: Sergey Egorov --- utils.go | 2 +- utils_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 utils_test.go diff --git a/utils.go b/utils.go index acfaa58..19af64a 100644 --- a/utils.go +++ b/utils.go @@ -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": diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..5e1d9d1 --- /dev/null +++ b/utils_test.go @@ -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) + } + }) + } +}