WireGuard Archive on lore.kernel.org
 help / color / Atom feed
From: Sina Siadat <siadat@gmail.com>
To: wireguard@lists.zx2c4.com
Cc: Sina Siadat <siadat@gmail.com>
Subject: [PATCH] device ping test: get free port when testing
Date: Thu, 30 Jul 2020 20:50:49 +0430
Message-ID: <20200730162049.2869-1-siadat@gmail.com> (raw)

Signed-off-by: Sina Siadat <siadat@gmail.com>
---
 device/device_test.go | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/device/device_test.go b/device/device_test.go
index 5ea5410..016a0ca 100644
--- a/device/device_test.go
+++ b/device/device_test.go
@@ -8,6 +8,7 @@ package device
 import (
 	"bufio"
 	"bytes"
+	"fmt"
 	"net"
 	"strings"
 	"testing"
@@ -16,16 +17,30 @@ import (
 	"golang.zx2c4.com/wireguard/tun/tuntest"
 )
 
+func getFreePort(t *testing.T) string {
+	l, err := net.ListenPacket("udp", "localhost:0")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer l.Close()
+	return fmt.Sprintf("%d", l.LocalAddr().(*net.UDPAddr).Port)
+}
+
 func TestTwoDevicePing(t *testing.T) {
-	// TODO(crawshaw): pick unused ports on localhost
+	port1 := getFreePort(t)
+	port2 := getFreePort(t)
+
 	cfg1 := `private_key=481eb0d8113a4a5da532d2c3e9c14b53c8454b34ab109676f6b58c2245e37b58
-listen_port=53511
+listen_port={{PORT1}}
 replace_peers=true
 public_key=f70dbb6b1b92a1dde1c783b297016af3f572fef13b0abb16a2623d89a58e9725
 protocol_version=1
 replace_allowed_ips=true
 allowed_ip=1.0.0.2/32
-endpoint=127.0.0.1:53512`
+endpoint=127.0.0.1:{{PORT2}}`
+	cfg1 = strings.ReplaceAll(cfg1, "{{PORT1}}", port1)
+	cfg1 = strings.ReplaceAll(cfg1, "{{PORT2}}", port2)
+
 	tun1 := tuntest.NewChannelTUN()
 	dev1 := NewDevice(tun1.TUN(), NewLogger(LogLevelDebug, "dev1: "))
 	dev1.Up()
@@ -35,13 +50,16 @@ endpoint=127.0.0.1:53512`
 	}
 
 	cfg2 := `private_key=98c7989b1661a0d64fd6af3502000f87716b7c4bbcf00d04fc6073aa7b539768
-listen_port=53512
+listen_port={{PORT2}}
 replace_peers=true
 public_key=49e80929259cebdda4f322d6d2b1a6fad819d603acd26fd5d845e7a123036427
 protocol_version=1
 replace_allowed_ips=true
 allowed_ip=1.0.0.1/32
-endpoint=127.0.0.1:53511`
+endpoint=127.0.0.1:{{PORT1}}`
+	cfg2 = strings.ReplaceAll(cfg2, "{{PORT1}}", port1)
+	cfg2 = strings.ReplaceAll(cfg2, "{{PORT2}}", port2)
+
 	tun2 := tuntest.NewChannelTUN()
 	dev2 := NewDevice(tun2.TUN(), NewLogger(LogLevelDebug, "dev2: "))
 	dev2.Up()
-- 
2.24.3 (Apple Git-128)


             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-30 16:20 Sina Siadat [this message]
2020-07-31 14:19 ` Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200730162049.2869-1-siadat@gmail.com \
    --to=siadat@gmail.com \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

WireGuard Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/wireguard/0 wireguard/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 wireguard wireguard/ https://lore.kernel.org/wireguard \
		wireguard@lists.zx2c4.com
	public-inbox-index wireguard

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/com.zx2c4.lists.wireguard


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git