--- /dev/null
+#REGTEST_TYPE=devel
+
+# This reg-test verifies the effects of "tune.h2.fe.enabled" by comparing two
+# haproxy instances, "hon" with the default setting and "hoff" with HTTP/2
+# disabled on frontends. On "hoff":
+# - "h2" is never negotiated via ALPN on "bind" lines, whether it comes from
+# the default ALPN, an explicit "alpn" setting (even "h2" alone) or a
+# crt-list entry;
+# - an HTTP/1 connection starting with the HTTP/2 preface gets a 405
+# instead of being upgraded;
+# - a "bind" line forcing "proto h2" closes its connections upon accept;
+# - the h2_enabled sample fetch returns false.
+# The same requests are sent to "hon", where HTTP/2 must work as usual.
+# It requires OpenSSL >= 1.0.2 for ALPN.
+
+varnishtest "Test the 'tune.h2.fe.enabled' setting and the 'h2_enabled' sample fetch"
+feature cmd "$HAPROXY_PROGRAM -cc 'feature(H2) && feature(OPENSSL) && openssl_version_atleast(1.0.2)'"
+feature ignore_unknown_macro
+
+haproxy hon -conf {
+ global
+ .if feature(THREAD)
+ thread-groups 1
+ .endif
+ chroot /
+ crt-base ${testdir}/certs
+
+ .if !ssllib_name_startswith(AWS-LC)
+ tune.ssl.default-dh-param 2048
+ .endif
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ # the first digit is the client ALPN (2: "h2", 3: "h2,http/1.1"), the
+ # second digit is the bind (0: default, 2: "h2", 3: "h2,http/1.1",
+ # 5: crt-list with "h2,http/1.1"). /up sends the H2 preface in clear to
+ # the HTTP/1 frontend, which must then be upgraded.
+ listen px-clr
+ bind "fd@${clr}"
+ default-server ssl verify none
+ use-server s20 if { path /20 }
+ use-server s30 if { path /30 }
+ use-server s32 if { path /32 }
+ use-server s33 if { path /33 }
+ use-server s35 if { path /35 }
+ use-server sup if { path /up }
+ server s20 "${tmpdir}/on-ssl0.sock" alpn h2
+ server s30 "${tmpdir}/on-ssl0.sock" alpn h2,http/1.1
+ server s32 "${tmpdir}/on-ssl2.sock" alpn h2,http/1.1
+ server s33 "${tmpdir}/on-ssl3.sock" alpn h2,http/1.1
+ server s35 "${tmpdir}/on-ssl5.sock" alpn h2,http/1.1
+ server sup ${hon_feh1_addr}:${hon_feh1_port} no-ssl proto h2
+
+ frontend fe-ssl
+ bind "${tmpdir}/on-ssl0.sock" ssl crt ${testdir}/certs/common.pem
+ bind "${tmpdir}/on-ssl2.sock" ssl crt ${testdir}/certs/common.pem alpn h2
+ bind "${tmpdir}/on-ssl3.sock" ssl crt ${testdir}/certs/common.pem alpn h2,http/1.1
+ bind "${tmpdir}/on-ssl5.sock" ssl crt-list ${testdir}/certs/h2_fe_enabled.crt-list
+ http-request return status 200 hdr x-alpn _%[ssl_fc_alpn] hdr x-ver _%[req.ver]
+
+ frontend fe-h1
+ bind "fd@${feh1}"
+ http-request return status 200 hdr x-ver _%[req.ver] hdr x-h2 %[h2_enabled]
+
+ frontend fe-h2
+ bind "fd@${feh2}" proto h2
+ http-request return status 200 hdr x-ver _%[req.ver]
+} -start
+
+haproxy hoff -conf {
+ global
+ .if feature(THREAD)
+ thread-groups 1
+ .endif
+ chroot /
+ crt-base ${testdir}/certs
+ tune.h2.fe.enabled off
+
+ .if !ssllib_name_startswith(AWS-LC)
+ tune.ssl.default-dh-param 2048
+ .endif
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ # same as above
+ listen px-clr
+ bind "fd@${clr}"
+ default-server ssl verify none
+ use-server s20 if { path /20 }
+ use-server s30 if { path /30 }
+ use-server s32 if { path /32 }
+ use-server s33 if { path /33 }
+ use-server s35 if { path /35 }
+ use-server sup if { path /up }
+ server s20 "${tmpdir}/off-ssl0.sock" alpn h2
+ server s30 "${tmpdir}/off-ssl0.sock" alpn h2,http/1.1
+ server s32 "${tmpdir}/off-ssl2.sock" alpn h2,http/1.1
+ server s33 "${tmpdir}/off-ssl3.sock" alpn h2,http/1.1
+ server s35 "${tmpdir}/off-ssl5.sock" alpn h2,http/1.1
+ server sup ${hoff_feh1_addr}:${hoff_feh1_port} no-ssl proto h2
+
+ frontend fe-ssl
+ bind "${tmpdir}/off-ssl0.sock" ssl crt ${testdir}/certs/common.pem
+ bind "${tmpdir}/off-ssl2.sock" ssl crt ${testdir}/certs/common.pem alpn h2
+ bind "${tmpdir}/off-ssl3.sock" ssl crt ${testdir}/certs/common.pem alpn h2,http/1.1
+ bind "${tmpdir}/off-ssl5.sock" ssl crt-list ${testdir}/certs/h2_fe_enabled.crt-list
+ http-request return status 200 hdr x-alpn _%[ssl_fc_alpn] hdr x-ver _%[req.ver]
+
+ frontend fe-h1
+ bind "fd@${feh1}"
+ http-request return status 200 hdr x-ver _%[req.ver] hdr x-h2 %[h2_enabled]
+
+ frontend fe-h2
+ bind "fd@${feh2}" proto h2
+ http-request return status 200 hdr x-ver _%[req.ver]
+} -start
+
+# ALPN negotiation: "h2" is selected whenever possible on "hon"
+client c1 -connect ${hon_clr_sock} {
+ txreq -url "/20"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_h2"
+ expect resp.http.x-ver == "_2.0"
+
+ txreq -url "/30"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_h2"
+ expect resp.http.x-ver == "_2.0"
+
+ txreq -url "/32"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_h2"
+ expect resp.http.x-ver == "_2.0"
+
+ txreq -url "/33"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_h2"
+ expect resp.http.x-ver == "_2.0"
+
+ txreq -url "/35"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_h2"
+ expect resp.http.x-ver == "_2.0"
+} -run
+
+# ALPN negotiation: "h2" is never selected on "hoff", "alpn h2" alone becomes
+# equivalent to "no-alpn"
+client c2 -connect ${hoff_clr_sock} {
+ txreq -url "/20"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_"
+ expect resp.http.x-ver == "_1.1"
+
+ txreq -url "/30"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_http/1.1"
+ expect resp.http.x-ver == "_1.1"
+
+ txreq -url "/32"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_"
+ expect resp.http.x-ver == "_1.1"
+
+ txreq -url "/33"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_http/1.1"
+ expect resp.http.x-ver == "_1.1"
+
+ txreq -url "/35"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-alpn == "_http/1.1"
+ expect resp.http.x-ver == "_1.1"
+} -run
+
+# h2_enabled reflects the setting
+client c3 -connect ${hon_feh1_sock} {
+ txreq -url "/"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-ver == "_1.1"
+ expect resp.http.x-h2 == "1"
+} -run
+
+client c4 -connect ${hoff_feh1_sock} {
+ txreq -url "/"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-ver == "_1.1"
+ expect resp.http.x-h2 == "0"
+} -run
+
+# H2 preface on an HTTP/1 frontend: upgraded on "hon". Haproxy is used as the
+# H2 client because vtest emits the preface again on the first "stream", and
+# its "txpri" may split it after the first empty line, which the H1 mux would
+# then parse as an HTTP/1 request.
+client c5 -connect ${hon_clr_sock} {
+ txreq -url "/up"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-ver == "_2.0"
+ expect resp.http.x-h2 == "1"
+} -run
+
+# H2 preface on an HTTP/1 frontend: no upgrade on "hoff", the server responds
+# a 405 which the H2 client cannot parse, hence a 502
+client c6 -connect ${hoff_clr_sock} {
+ txreq -url "/up"
+ rxresp
+ expect resp.status == 502
+} -run
+
+# same on "hoff" with a direct client, which must get the 405
+client c7 -connect ${hoff_feh1_sock} {
+ send "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
+ rxresp
+ expect resp.status == 405
+ expect_close
+} -run
+
+# "proto h2" frontend: works on "hon"
+client c8 -connect ${hon_feh2_sock} {
+ txpri
+ stream 0 {
+ txsettings
+ rxsettings
+ txsettings -ack
+ rxsettings
+ expect settings.ack == true
+ } -run
+
+ stream 1 {
+ txreq -url "/"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-ver == "_2.0"
+ } -run
+} -run
+
+# "proto h2" frontend: the connection is closed upon accept on "hoff", without
+# even waiting for the preface (sending it could thus fail on a reset)
+client c9 -connect ${hoff_feh2_sock} {
+ expect_close
+} -run