[master] 9450493 We have rather haphazardly been using "127.0.0.1" in test-cases, asuming that would for ever be the case. This fails on IPv6 only systems and FreeBSD jails where localhost is the "real" IP number.

Poul-Henning Kamp phk at FreeBSD.org
Mon Feb 8 10:49:16 CET 2016


commit 9450493d2e6a32c1034fe37f79912f720bb40070
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Feb 8 09:46:51 2016 +0000

    We have rather haphazardly been using "127.0.0.1" in test-cases,
    asuming that would for ever be the case.  This fails on IPv6 only
    systems and FreeBSD jails where localhost is the "real" IP number.

diff --git a/bin/varnishtest/tests/b00031.vtc b/bin/varnishtest/tests/b00031.vtc
index 9dcb83e..485cebb 100644
--- a/bin/varnishtest/tests/b00031.vtc
+++ b/bin/varnishtest/tests/b00031.vtc
@@ -2,10 +2,10 @@ varnishtest "Test X-Forward-For headers"
 
 server s1 {
 	rxreq
-	expect req.http.X-Forwarded-For == "127.0.0.1"
+	expect req.http.X-Forwarded-For == "${localhost}"
 	txresp
 	rxreq
-	expect req.http.X-Forwarded-For == "1.2.3.4, 127.0.0.1"
+	expect req.http.X-Forwarded-For == "1.2.3.4, ${localhost}"
 	txresp
 } -start
 
diff --git a/bin/varnishtest/tests/c00005.vtc b/bin/varnishtest/tests/c00005.vtc
index 846b283..afa103c 100644
--- a/bin/varnishtest/tests/c00005.vtc
+++ b/bin/varnishtest/tests/c00005.vtc
@@ -11,7 +11,7 @@ server s1 {
 
 varnish v1 -arg "-p vsl_mask=+VCL_trace" -vcl+backend {
 	acl acl1 {
-		"localhost";
+		"${localhost}";
 	}
 
 	sub vcl_recv {
diff --git a/bin/varnishtest/tests/d00003.vtc b/bin/varnishtest/tests/d00003.vtc
index e6182d7..970ed2d 100644
--- a/bin/varnishtest/tests/d00003.vtc
+++ b/bin/varnishtest/tests/d00003.vtc
@@ -21,6 +21,7 @@ server s2 {
 } -start
 
 varnish v1 -vcl+backend {
+	import ${vmod_std};
 	import ${vmod_directors};
 
 	sub vcl_init {
@@ -41,7 +42,7 @@ varnish v1 -vcl+backend {
 		} else if (req.url == "/13") {
 			set req.backend_hint = h1.backend(req.http.Void + "" + req.url);
 		} else if (req.url == "/ip") {
-			set req.backend_hint = h1.backend(client.ip);
+			set req.backend_hint = h1.backend(std.ip("127.0.0.1", client.ip));
 		} else {
 			set req.backend_hint = h1.backend(req.url);
 		}
diff --git a/bin/varnishtest/tests/m00011.vtc b/bin/varnishtest/tests/m00011.vtc
index 930ed8e..45528c9 100644
--- a/bin/varnishtest/tests/m00011.vtc
+++ b/bin/varnishtest/tests/m00011.vtc
@@ -21,8 +21,8 @@ varnish v1 -vcl+backend {
 client c1 {
 	txreq
 	rxresp
-	expect resp.http.foo0 == "127.0.0.1"
-	expect resp.http.foo1 == "127.0.0.1"
+	expect resp.http.foo0 == "${localhost}"
+	expect resp.http.foo1 == "${localhost}"
 	expect resp.http.foo2 == "127.0.0.2"
 	expect resp.http.foo3 == "1.2.3.5"
 	expect resp.http.foo4 == "2001:db8::"
diff --git a/bin/varnishtest/tests/o00001.vtc b/bin/varnishtest/tests/o00001.vtc
index 3d4e2f6..ecf025a 100644
--- a/bin/varnishtest/tests/o00001.vtc
+++ b/bin/varnishtest/tests/o00001.vtc
@@ -4,7 +4,7 @@ server s1 {
 	# The server address is part of the hash-key
 	# so we need three responses
 	rxreq
-	expect req.http.x-forwarded-for == "127.0.0.1"
+	expect req.http.x-forwarded-for == "${localhost}"
 	txresp -hdr "Obj: 1"
 	rxreq
 	expect req.http.x-forwarded-for == "1.2.3.4"
@@ -49,7 +49,7 @@ client c1 {
 	expect resp.status == 200
 	expect resp.http.si == "${v1_addr}"
 	expect resp.http.sp == "${v1_port}"
-	expect resp.http.ci == "127.0.0.1"
+	expect resp.http.ci == "${localhost}"
 } -run
 delay .1
 
@@ -71,7 +71,7 @@ client c1 {
 	expect resp.status == 200
 	expect resp.http.si == "${v1_addr}"
 	expect resp.http.sp == "${v1_port}"
-	expect resp.http.ci == "127.0.0.1"
+	expect resp.http.ci == "${localhost}"
 } -run
 delay .1
 
@@ -84,7 +84,7 @@ client c1 {
 	expect resp.status == 200
 	expect resp.http.si == "${v1_addr}"
 	expect resp.http.sp == "${v1_port}"
-	expect resp.http.ci == "127.0.0.1"
+	expect resp.http.ci == "${localhost}"
 } -run
 delay .1
 
@@ -98,7 +98,7 @@ client c1 {
 	expect resp.status == 200
 	expect resp.http.si == "${v1_addr}"
 	expect resp.http.sp == "${v1_port}"
-	expect resp.http.ci == "127.0.0.1"
+	expect resp.http.ci == "${localhost}"
 } -run
 delay .1
 
@@ -116,7 +116,7 @@ client c1 {
 	expect resp.http.fc == false
 	expect resp.http.si == "${v1_addr}"
 	expect resp.http.sp == "${v1_port}"
-	expect resp.http.ci == "127.0.0.1"
+	expect resp.http.ci == "${localhost}"
 } -run
 delay .1
 
diff --git a/bin/varnishtest/tests/r00921.vtc b/bin/varnishtest/tests/r00921.vtc
index e207c18..7eac5bb 100644
--- a/bin/varnishtest/tests/r00921.vtc
+++ b/bin/varnishtest/tests/r00921.vtc
@@ -2,8 +2,8 @@ varnishtest "VCC type issue in regsub arg 1"
 
 server s1 {
 	rxreq
-	expect req.http.foo == "127.0.0.1"
-	expect req.http.bar == "127.0.0.1"
+	expect req.http.foo == "${localhost}"
+	expect req.http.bar == "${localhost}"
 	txresp
 } -start
 
diff --git a/bin/varnishtest/tests/r01312.vtc b/bin/varnishtest/tests/r01312.vtc
index 05003ea..c39dadc 100644
--- a/bin/varnishtest/tests/r01312.vtc
+++ b/bin/varnishtest/tests/r01312.vtc
@@ -6,6 +6,8 @@ server s1 {
 } -start
 
 varnish v1 -vcl+backend {
+	import ${vmod_std};
+
 	acl foo {
 		"127.0.0.2";
 		"127.0.1"/19;
@@ -15,8 +17,8 @@ varnish v1 -vcl+backend {
 		"127.0.1"/19;
 	}
 	sub vcl_deliver {
-		set resp.http.ACLfoo = client.ip ~ foo;
-		set resp.http.ACLbar = client.ip ~ bar;
+		set resp.http.ACLfoo = std.ip("127.0.0.1", client.ip) ~ foo;
+		set resp.http.ACLbar = std.ip("127.0.0.1", client.ip) ~ bar;
 	}
 } -start
 
diff --git a/bin/varnishtest/tests/r01650.vtc b/bin/varnishtest/tests/r01650.vtc
index e21ceba..ec124bb 100644
--- a/bin/varnishtest/tests/r01650.vtc
+++ b/bin/varnishtest/tests/r01650.vtc
@@ -2,7 +2,7 @@ varnishtest "xff handling discards multiple headers"
 
 server s1 {
 	rxreq
-	expect req.http.X-Forwarded-For == "1.2.3.4, 5.6.7.8, 127.0.0.1"
+	expect req.http.X-Forwarded-For == "1.2.3.4, 5.6.7.8, ${localhost}"
 	txresp
 } -start
 
diff --git a/bin/varnishtest/tests/r01693.vtc b/bin/varnishtest/tests/r01693.vtc
index f16d620..5f051d5 100644
--- a/bin/varnishtest/tests/r01693.vtc
+++ b/bin/varnishtest/tests/r01693.vtc
@@ -16,7 +16,7 @@ logexpect l1 -v v1 {
 	expect 0 1001 Hash "bar"
 	expect 0 1001 Hash "3"
 	expect 0 1001 Hash "/"
-	expect 0 1001 Hash "127.0.0.1"
+	expect 0 1001 Hash "${localhost}"
 } -start
 
 client c1 {
diff --git a/bin/varnishtest/tests/v00008.vtc b/bin/varnishtest/tests/v00008.vtc
index add54ef..95a870f 100644
--- a/bin/varnishtest/tests/v00008.vtc
+++ b/bin/varnishtest/tests/v00008.vtc
@@ -8,7 +8,7 @@ server s1 {
 
 	rxreq
 	expect req.url == "/bar"
-	expect req.http.host == "127.0.0.1"
+	expect req.http.host == "${localhost}"
 	txresp -body "foo1"
 } -start
 
diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index b60deb4..8890a62 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -121,6 +121,12 @@ static pthread_mutex_t		macro_mtx;
 static void
 init_macro(void)
 {
+	struct macro *m;
+
+	/* Dump the extmacros for completeness */
+	VTAILQ_FOREACH(m, &macro_list, list)
+		vtc_log(vltop, 4, "extmacro def %s=%s", m->name, m->val);
+
 	AZ(pthread_mutex_init(&macro_mtx, NULL));
 }
 
@@ -637,15 +643,6 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
 	init_sema();
 	init_server();
 
-	/*
-	 * We need an IP number which will not repond, ever, and that is a
-	 * lot harder than it sounds.  This IP# is from RFC5737 and a
-	 * C-class broadcast at that.
-	 * If tests involving ${bad_ip} fails and you run linux, you should
-	 * check your /proc/sys/net/ipv4/ip_nonlocal_bind setting.
-	 */
-	macro_def(vltop, NULL, "bad_ip", "192.0.2.255");
-
 	/* Move into our tmpdir */
 	AZ(chdir(tmpdir));
 	macro_def(vltop, NULL, "tmpdir", "%s", tmpdir);
diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index 601f245..7272e22 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -427,6 +427,41 @@ dns_works(void)
 }
 
 /**********************************************************************
+ * Figure out what IP related magic
+ */
+
+static void
+ip_magic(void)
+{
+	const char *p;
+	int fd;
+	char abuf[VTCP_ADDRBUFSIZE];
+
+	/*
+	 * In FreeBSD jails localhost/127.0.0.1 becomes the jails IP#
+	 * XXX: IPv6-only hosts would have similar issue, but it is not
+	 * XXX: obvious how to cope.  Ideally "127.0.0.1" would be
+	 * XXX: "localhost", but that doesn't work out of the box.
+	 * XXX: Things like "prefer_ipv6" parameter complicates things.
+	 */
+	fd = VTCP_listen_on("127.0.0.1", NULL, 10, &p);
+	assert(fd >= 0);
+	VTCP_myname(fd, abuf, sizeof abuf, NULL, 0);
+	AZ(close(fd));
+	extmacro_def("localhost", "%s", abuf);
+
+	/*
+	 * We need an IP number which will not repond, ever, and that is a
+	 * lot harder than it sounds.  This IP# is from RFC5737 and a
+	 * C-class broadcast at that.
+	 * If tests involving ${bad_ip} fails and you run linux, you should
+	 * check your /proc/sys/net/ipv4/ip_nonlocal_bind setting.
+	 */
+
+	extmacro_def("bad_ip", "%s", "192.0.2.255");
+}
+
+/**********************************************************************
  * Main
  */
 
@@ -538,6 +573,7 @@ main(int argc, char * const *argv)
 	}
 
 	feature_dns = dns_works();
+	ip_magic();
 
 	if (iflg)
 		i_mode();



More information about the varnish-commit mailing list