Merge pull request #1835 from nghttp2/integration-lint

integration: Fix lint errors
This commit is contained in:
Tatsuhiro Tsujikawa 2022-11-24 19:05:51 +09:00 committed by GitHub
commit 5195e8be6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 256 additions and 93 deletions

View File

@ -99,6 +99,8 @@ func TestH1H1MultipleRequestCL(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -196,7 +198,10 @@ func TestH1H1GracefulShutdown(t *testing.T) {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
st.cmd.Process.Signal(syscall.SIGQUIT) if err := st.cmd.Process.Signal(syscall.SIGQUIT); err != nil {
t.Fatalf("Error st.cmd.Process.Signal() = %v", err)
}
time.Sleep(150 * time.Millisecond) time.Sleep(150 * time.Millisecond)
res, err = st.http1(requestParam{ res, err = st.http1(requestParam{
@ -264,6 +269,9 @@ func TestH1H1BadHost(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -287,6 +295,9 @@ func TestH1H1BadAuthority(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -310,6 +321,9 @@ func TestH1H1BadScheme(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -335,6 +349,8 @@ func TestH1H1HTTP10(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want { if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -364,6 +380,8 @@ func TestH1H1HTTP10NoHostRewrite(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want { if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -492,20 +510,19 @@ func TestH1H1HeaderFields(t *testing.T) {
func TestH1H1Websocket(t *testing.T) { func TestH1H1Websocket(t *testing.T) {
opts := options{ opts := options{
handler: websocket.Handler(func(ws *websocket.Conn) { handler: websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws) if _, err := io.Copy(ws, ws); err != nil {
t.Fatalf("Error io.Copy() = %v", err)
}
}).ServeHTTP, }).ServeHTTP,
} }
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
content := []byte("hello world") content := []byte("hello world")
res, err := st.websocket(requestParam{ res := st.websocket(requestParam{
name: "TestH1H1Websocket", name: "TestH1H1Websocket",
body: content, body: content,
}) })
if err != nil {
t.Fatalf("Error st.websocket() = %v", err)
}
if got, want := res.body, content; !bytes.Equal(got, want) { if got, want := res.body, content; !bytes.Equal(got, want) {
t.Errorf("echo: %q; want %q", got, want) t.Errorf("echo: %q; want %q", got, want)
} }
@ -766,6 +783,8 @@ func TestH1H2NoHost(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -792,6 +811,8 @@ func TestH1H2HTTP10(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want { if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -822,6 +843,8 @@ func TestH1H2HTTP10NoHostRewrite(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want { if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -1310,6 +1333,8 @@ func TestH1ResponseBeforeRequestEnd(t *testing.T) {
t.Fatalf("Error http.ReadResponse() = %v", err) t.Fatalf("Error http.ReadResponse() = %v", err)
} }
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusNotFound; got != want { if got, want := resp.StatusCode, http.StatusNotFound; got != want {
t.Errorf("status: %v; want %v", got, want) t.Errorf("status: %v; want %v", got, want)
} }
@ -1331,7 +1356,9 @@ func TestH1H1ChunkedEndsPrematurely(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
} }

View File

@ -642,7 +642,9 @@ func TestH2H1BadResponseCL(t *testing.T) {
handler: func(w http.ResponseWriter, r *http.Request) { handler: func(w http.ResponseWriter, r *http.Request) {
// we set content-length: 1024, but only send 3 bytes. // we set content-length: 1024, but only send 3 bytes.
w.Header().Add("Content-Length", "1024") w.Header().Add("Content-Length", "1024")
w.Write([]byte("foo")) if _, err := w.Write([]byte("foo")); err != nil {
t.Fatalf("Error w.Write() = %v", err)
}
}, },
} }
st := newServerTester(t, opts) st := newServerTester(t, opts)
@ -1273,7 +1275,7 @@ func TestH2H1Upgrade(t *testing.T) {
// header field includes obfuscated address even if PROXY protocol // header field includes obfuscated address even if PROXY protocol
// version 1 containing TCP4 entry is accepted. // version 1 containing TCP4 entry is accepted.
func TestH2H1ProxyProtocolV1ForwardedForObfuscated(t *testing.T) { func TestH2H1ProxyProtocolV1ForwardedForObfuscated(t *testing.T) {
pattern := fmt.Sprintf(`^for=_[^;]+$`) pattern := `^for=_[^;]+$`
validFwd := regexp.MustCompile(pattern) validFwd := regexp.MustCompile(pattern)
opts := options{ opts := options{
args: []string{ args: []string{
@ -1291,7 +1293,9 @@ func TestH2H1ProxyProtocolV1ForwardedForObfuscated(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP4 192.168.0.2 192.168.0.100 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP4 192.168.0.2 192.168.0.100 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1ForwardedForObfuscated", name: "TestH2H1ProxyProtocolV1ForwardedForObfuscated",
@ -1329,7 +1333,9 @@ func TestH2H1ProxyProtocolV1TCP4(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP4 192.168.0.2 192.168.0.100 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP4 192.168.0.2 192.168.0.100 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1TCP4", name: "TestH2H1ProxyProtocolV1TCP4",
@ -1367,7 +1373,9 @@ func TestH2H1ProxyProtocolV1TCP6(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 ::1 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 ::1 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1TCP6", name: "TestH2H1ProxyProtocolV1TCP6",
@ -1480,7 +1488,9 @@ func TestH2H1ProxyProtocolV1Unknown(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY UNKNOWN 192.168.0.2 192.168.0.100 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY UNKNOWN 192.168.0.2 192.168.0.100 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1Unknown", name: "TestH2H1ProxyProtocolV1Unknown",
@ -1507,7 +1517,9 @@ func TestH2H1ProxyProtocolV1JustUnknown(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY UNKNOWN\r\n")) if _, err := st.conn.Write([]byte("PROXY UNKNOWN\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1JustUnknown", name: "TestH2H1ProxyProtocolV1JustUnknown",
@ -1534,7 +1546,9 @@ func TestH2H1ProxyProtocolV1TooLongLine(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY UNKNOWN ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535 655350\r\n")) if _, err := st.conn.Write([]byte("PROXY UNKNOWN ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535 655350\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1TooLongLine", name: "TestH2H1ProxyProtocolV1TooLongLine",
@ -1554,7 +1568,9 @@ func TestH2H1ProxyProtocolV1BadLineEnd(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080\r \n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080\r \n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1BadLineEnd", name: "TestH2H1ProxyProtocolV1BadLineEnd",
@ -1574,7 +1590,9 @@ func TestH2H1ProxyProtocolV1NoEnd(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1NoEnd", name: "TestH2H1ProxyProtocolV1NoEnd",
@ -1596,7 +1614,9 @@ func TestH2H1ProxyProtocolV1EmbeddedNULL(t *testing.T) {
b := []byte("PROXY TCP6 ::1*foo ::1 12345 8080\r\n") b := []byte("PROXY TCP6 ::1*foo ::1 12345 8080\r\n")
b[14] = 0 b[14] = 0
st.conn.Write(b) if _, err := st.conn.Write(b); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1EmbeddedNULL", name: "TestH2H1ProxyProtocolV1EmbeddedNULL",
@ -1616,7 +1636,9 @@ func TestH2H1ProxyProtocolV1MissingSrcPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1MissingSrcPort", name: "TestH2H1ProxyProtocolV1MissingSrcPort",
@ -1636,7 +1658,9 @@ func TestH2H1ProxyProtocolV1MissingDstPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 \r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 \r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1MissingDstPort", name: "TestH2H1ProxyProtocolV1MissingDstPort",
@ -1656,7 +1680,9 @@ func TestH2H1ProxyProtocolV1InvalidSrcPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123x 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123x 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidSrcPort", name: "TestH2H1ProxyProtocolV1InvalidSrcPort",
@ -1676,7 +1702,9 @@ func TestH2H1ProxyProtocolV1InvalidDstPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123456 80x\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123456 80x\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidDstPort", name: "TestH2H1ProxyProtocolV1InvalidDstPort",
@ -1697,7 +1725,9 @@ func TestH2H1ProxyProtocolV1LeadingZeroPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 03000 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 03000 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1LeadingZeroPort", name: "TestH2H1ProxyProtocolV1LeadingZeroPort",
@ -1717,7 +1747,9 @@ func TestH2H1ProxyProtocolV1TooLargeSrcPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 65536 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 65536 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1TooLargeSrcPort", name: "TestH2H1ProxyProtocolV1TooLargeSrcPort",
@ -1737,7 +1769,9 @@ func TestH2H1ProxyProtocolV1TooLargeDstPort(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 65536\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 65536\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1TooLargeDstPort", name: "TestH2H1ProxyProtocolV1TooLargeDstPort",
@ -1757,7 +1791,9 @@ func TestH2H1ProxyProtocolV1InvalidSrcAddr(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 192.168.0.1 ::1 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 192.168.0.1 ::1 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidSrcAddr", name: "TestH2H1ProxyProtocolV1InvalidSrcAddr",
@ -1777,7 +1813,9 @@ func TestH2H1ProxyProtocolV1InvalidDstAddr(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY TCP6 ::1 192.168.0.1 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY TCP6 ::1 192.168.0.1 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidDstAddr", name: "TestH2H1ProxyProtocolV1InvalidDstAddr",
@ -1797,7 +1835,9 @@ func TestH2H1ProxyProtocolV1InvalidProtoFamily(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PROXY UNIX ::1 ::1 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PROXY UNIX ::1 ::1 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidProtoFamily", name: "TestH2H1ProxyProtocolV1InvalidProtoFamily",
@ -1817,7 +1857,9 @@ func TestH2H1ProxyProtocolV1InvalidID(t *testing.T) {
st := newServerTester(t, opts) st := newServerTester(t, opts)
defer st.Close() defer st.Close()
st.conn.Write([]byte("PR0XY TCP6 ::1 ::1 12345 8080\r\n")) if _, err := st.conn.Write([]byte("PR0XY TCP6 ::1 ::1 12345 8080\r\n")); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV1InvalidID", name: "TestH2H1ProxyProtocolV1InvalidID",
@ -1852,7 +1894,7 @@ func TestH2H1ProxyProtocolV2TCP4(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("192.168.0.2").To4(), IP: net.ParseIP("192.168.0.2").To4(),
@ -1863,8 +1905,13 @@ func TestH2H1ProxyProtocolV2TCP4(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2TCP4", name: "TestH2H1ProxyProtocolV2TCP4",
@ -1903,7 +1950,7 @@ func TestH2H1ProxyProtocolV2TCP6(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
@ -1914,8 +1961,13 @@ func TestH2H1ProxyProtocolV2TCP6(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2TCP6", name: "TestH2H1ProxyProtocolV2TCP6",
@ -1935,7 +1987,7 @@ func TestH2H1ProxyProtocolV2TCP6(t *testing.T) {
// contains advertised src address. // contains advertised src address.
func TestH2H1ProxyProtocolV2TCP4TLS(t *testing.T) { func TestH2H1ProxyProtocolV2TCP4TLS(t *testing.T) {
var v2Hdr bytes.Buffer var v2Hdr bytes.Buffer
writeProxyProtocolV2(&v2Hdr, proxyProtocolV2{ if err := writeProxyProtocolV2(&v2Hdr, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("192.168.0.2").To4(), IP: net.ParseIP("192.168.0.2").To4(),
@ -1946,7 +1998,9 @@ func TestH2H1ProxyProtocolV2TCP4TLS(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
opts := options{ opts := options{
args: []string{ args: []string{
@ -1987,7 +2041,7 @@ func TestH2H1ProxyProtocolV2TCP4TLS(t *testing.T) {
// contains advertised src address. // contains advertised src address.
func TestH2H1ProxyProtocolV2TCP6TLS(t *testing.T) { func TestH2H1ProxyProtocolV2TCP6TLS(t *testing.T) {
var v2Hdr bytes.Buffer var v2Hdr bytes.Buffer
writeProxyProtocolV2(&v2Hdr, proxyProtocolV2{ if err := writeProxyProtocolV2(&v2Hdr, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
@ -1998,7 +2052,9 @@ func TestH2H1ProxyProtocolV2TCP6TLS(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
opts := options{ opts := options{
args: []string{ args: []string{
@ -2057,7 +2113,7 @@ func TestH2H1ProxyProtocolV2Local(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: proxyProtocolV2CommandLocal, command: proxyProtocolV2CommandLocal,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("192.168.0.2").To4(), IP: net.ParseIP("192.168.0.2").To4(),
@ -2068,8 +2124,13 @@ func TestH2H1ProxyProtocolV2Local(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2Local", name: "TestH2H1ProxyProtocolV2Local",
@ -2094,7 +2155,7 @@ func TestH2H1ProxyProtocolV2UnknownCmd(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: 0xf, command: 0xf,
sourceAddress: &net.TCPAddr{ sourceAddress: &net.TCPAddr{
IP: net.ParseIP("192.168.0.2").To4(), IP: net.ParseIP("192.168.0.2").To4(),
@ -2105,8 +2166,13 @@ func TestH2H1ProxyProtocolV2UnknownCmd(t *testing.T) {
Port: 8080, Port: 8080,
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
_, err := st.http2(requestParam{ _, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2UnknownCmd", name: "TestH2H1ProxyProtocolV2UnknownCmd",
@ -2140,7 +2206,7 @@ func TestH2H1ProxyProtocolV2Unix(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
sourceAddress: &net.UnixAddr{ sourceAddress: &net.UnixAddr{
Name: "/foo", Name: "/foo",
@ -2151,8 +2217,13 @@ func TestH2H1ProxyProtocolV2Unix(t *testing.T) {
Net: "unix", Net: "unix",
}, },
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2Unix", name: "TestH2H1ProxyProtocolV2Unix",
@ -2190,11 +2261,16 @@ func TestH2H1ProxyProtocolV2Unspec(t *testing.T) {
defer st.Close() defer st.Close()
var b bytes.Buffer var b bytes.Buffer
writeProxyProtocolV2(&b, proxyProtocolV2{ if err := writeProxyProtocolV2(&b, proxyProtocolV2{
command: proxyProtocolV2CommandProxy, command: proxyProtocolV2CommandProxy,
additionalData: []byte("foobar"), additionalData: []byte("foobar"),
}) }); err != nil {
st.conn.Write(b.Bytes()) t.Fatalf("Error writeProxyProtocolV2() = %v", err)
}
if _, err := st.conn.Write(b.Bytes()); err != nil {
t.Fatalf("Error st.conn.Write() = %v", err)
}
res, err := st.http2(requestParam{ res, err := st.http2(requestParam{
name: "TestH2H1ProxyProtocolV2Unspec", name: "TestH2H1ProxyProtocolV2Unspec",
@ -2342,7 +2418,9 @@ func TestH2H1Code204CL0(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 204\r\nContent-Length: 0\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 204\r\nContent-Length: 0\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
} }
@ -2381,7 +2459,9 @@ func TestH2H1Code204CLNonzero(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 204\r\nContent-Length: 1\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 204\r\nContent-Length: 1\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
} }
@ -2416,7 +2496,9 @@ func TestH2H1Code204TE(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 204\r\nTransfer-Encoding: chunked\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 204\r\nTransfer-Encoding: chunked\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
} }
@ -2522,7 +2604,10 @@ func TestH2H1GracefulShutdown(t *testing.T) {
} }
// send SIGQUIT signal to nghttpx to perform graceful shutdown // send SIGQUIT signal to nghttpx to perform graceful shutdown
st.cmd.Process.Signal(syscall.SIGQUIT) if err := st.cmd.Process.Signal(syscall.SIGQUIT); err != nil {
t.Fatalf("Error st.cmd.Process.Signal() = %v", err)
}
time.Sleep(150 * time.Millisecond) time.Sleep(150 * time.Millisecond)
// after signal, finish request body // after signal, finish request body
@ -2546,7 +2631,7 @@ func TestH2H1GracefulShutdown(t *testing.T) {
} }
switch f := fr.(type) { switch f := fr.(type) {
case *http2.GoAwayFrame: case *http2.GoAwayFrame:
numGoAway += 1 numGoAway++
want := http2.ErrCodeNo want := http2.ErrCodeNo
if got := f.ErrCode; got != want { if got := f.ErrCode; got != want {
t.Fatalf("f.ErrCode(%v): %v; want %v", numGoAway, got, want) t.Fatalf("f.ErrCode(%v): %v; want %v", numGoAway, got, want)
@ -3530,7 +3615,9 @@ func TestH2H1ChunkedEndsPrematurely(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
} }

View File

@ -180,7 +180,9 @@ func TestH3H1BadResponseCL(t *testing.T) {
handler: func(w http.ResponseWriter, r *http.Request) { handler: func(w http.ResponseWriter, r *http.Request) {
// we set content-length: 1024, but only send 3 bytes. // we set content-length: 1024, but only send 3 bytes.
w.Header().Add("Content-Length", "1024") w.Header().Add("Content-Length", "1024")
w.Write([]byte("foo")) if _, err := w.Write([]byte("foo")); err != nil {
t.Fatalf("Error w.Write() = %v", err)
}
}, },
quic: true, quic: true,
} }
@ -372,7 +374,9 @@ func TestH3H1ChunkedEndsPrematurely(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n") if _, err := bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n"); err != nil {
t.Fatalf("Error bufrw.WriteString() = %v", err)
}
bufrw.Flush() bufrw.Flush()
}, },
quic: true, quic: true,

View File

@ -3,6 +3,7 @@ package nghttp2
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"crypto/tls" "crypto/tls"
"encoding/binary" "encoding/binary"
"errors" "errors"
@ -43,7 +44,6 @@ func pair(name, value string) hpack.HeaderField {
} }
type serverTester struct { type serverTester struct {
args []string // command-line arguments
cmd *exec.Cmd // test frontend server process, which is test subject cmd *exec.Cmd // test frontend server process, which is test subject
url string // test frontend server URL url string // test frontend server URL
t *testing.T t *testing.T
@ -246,16 +246,11 @@ func newServerTester(t *testing.T, opts options) *serverTester {
tlsConn := tls.Client(conn, tlsConfig) tlsConn := tls.Client(conn, tlsConfig)
err = tlsConn.Handshake() err = tlsConn.Handshake()
if err == nil { if err == nil {
cs := tlsConn.ConnectionState()
if !cs.NegotiatedProtocolIsMutual {
st.Close()
st.t.Fatalf("Error negotiated next protocol is not mutual")
}
conn = tlsConn conn = tlsConn
} }
} }
if err != nil { if err != nil {
retry += 1 retry++
if retry >= 100 { if retry >= 100 {
st.Close() st.Close()
st.t.Fatalf("Error server is not responding too long; server command-line arguments may be invalid") st.t.Fatalf("Error server is not responding too long; server command-line arguments may be invalid")
@ -282,16 +277,22 @@ func (st *serverTester) Close() {
if st.cmd != nil { if st.cmd != nil {
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
st.cmd.Wait() if err := st.cmd.Wait(); err != nil {
st.t.Errorf("Error st.cmd.Wait() = %v", err)
}
close(done) close(done)
}() }()
st.cmd.Process.Signal(syscall.SIGQUIT) if err := st.cmd.Process.Signal(syscall.SIGQUIT); err != nil {
st.t.Errorf("Error st.cmd.Process.Signal() = %v", err)
}
select { select {
case <-done: case <-done:
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
st.cmd.Process.Kill() if err := st.cmd.Process.Kill(); err != nil {
st.t.Errorf("Error st.cmd.Process.Kill() = %v", err)
}
<-done <-done
} }
} }
@ -354,7 +355,7 @@ func (cbr *chunkedBodyReader) Read(p []byte) (n int, err error) {
return cbr.body.Read(p) return cbr.body.Read(p)
} }
func (st *serverTester) websocket(rp requestParam) (*serverResponse, error) { func (st *serverTester) websocket(rp requestParam) *serverResponse {
urlstring := st.url + "/echo" urlstring := st.url + "/echo"
config, err := websocket.NewConfig(urlstring, st.url) config, err := websocket.NewConfig(urlstring, st.url)
@ -386,7 +387,7 @@ func (st *serverTester) websocket(rp requestParam) (*serverResponse, error) {
body: msg[:n], body: msg[:n],
} }
return res, nil return res
} }
func (st *serverTester) http3(rp requestParam) (*serverResponse, error) { func (st *serverTester) http3(rp requestParam) (*serverResponse, error) {
@ -425,7 +426,10 @@ func (st *serverTester) http3(rp requestParam) (*serverResponse, error) {
reqURL = u.String() + rp.path reqURL = u.String() + rp.path
} }
req, err := http.NewRequest(method, reqURL, body) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -492,7 +496,10 @@ func (st *serverTester) http1(rp requestParam) (*serverResponse, error) {
reqURL = u.String() + rp.path reqURL = u.String() + rp.path
} }
req, err := http.NewRequest(method, reqURL, body) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -750,7 +757,9 @@ func cloneHeader(h http.Header) http.Header {
} }
func noopHandler(w http.ResponseWriter, r *http.Request) { func noopHandler(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body) if _, err := io.ReadAll(r.Body); err != nil {
http.Error(w, fmt.Sprintf("Error io.ReadAll() = %v", err), http.StatusInternalServerError)
}
} }
type APIResponse struct { type APIResponse struct {
@ -790,9 +799,13 @@ const (
proxyProtocolV2ProtocolDgram proxyProtocolV2Protocol = 0x2 proxyProtocolV2ProtocolDgram proxyProtocolV2Protocol = 0x2
) )
func writeProxyProtocolV2(w io.Writer, hdr proxyProtocolV2) { func writeProxyProtocolV2(w io.Writer, hdr proxyProtocolV2) error {
w.Write([]byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A}) if _, err := w.Write([]byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A}); err != nil {
w.Write([]byte{byte(0x20 | hdr.command)}) return err
}
if _, err := w.Write([]byte{byte(0x20 | hdr.command)}); err != nil {
return err
}
switch srcAddr := hdr.sourceAddress.(type) { switch srcAddr := hdr.sourceAddress.(type) {
case *net.TCPAddr: case *net.TCPAddr:
@ -807,13 +820,25 @@ func writeProxyProtocolV2(w io.Writer, hdr proxyProtocolV2) {
fam = byte(proxyProtocolV2FamilyInet6 << 4) fam = byte(proxyProtocolV2FamilyInet6 << 4)
} }
fam |= byte(proxyProtocolV2ProtocolStream) fam |= byte(proxyProtocolV2ProtocolStream)
w.Write([]byte{fam}) if _, err := w.Write([]byte{fam}); err != nil {
return err
}
length := uint16(len(srcAddr.IP)*2 + 4 + len(hdr.additionalData)) length := uint16(len(srcAddr.IP)*2 + 4 + len(hdr.additionalData))
binary.Write(w, binary.BigEndian, length) if err := binary.Write(w, binary.BigEndian, length); err != nil {
w.Write(srcAddr.IP) return err
w.Write(dstAddr.IP) }
binary.Write(w, binary.BigEndian, uint16(srcAddr.Port)) if _, err := w.Write(srcAddr.IP); err != nil {
binary.Write(w, binary.BigEndian, uint16(dstAddr.Port)) return err
}
if _, err := w.Write(dstAddr.IP); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, uint16(srcAddr.Port)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, uint16(dstAddr.Port)); err != nil {
return err
}
case *net.UnixAddr: case *net.UnixAddr:
dstAddr := hdr.destinationAddress.(*net.UnixAddr) dstAddr := hdr.destinationAddress.(*net.UnixAddr)
if len(srcAddr.Name) > 108 { if len(srcAddr.Name) > 108 {
@ -831,20 +856,40 @@ func writeProxyProtocolV2(w io.Writer, hdr proxyProtocolV2) {
default: default:
fam |= byte(proxyProtocolV2ProtocolUnspec) fam |= byte(proxyProtocolV2ProtocolUnspec)
} }
w.Write([]byte{fam}) if _, err := w.Write([]byte{fam}); err != nil {
return err
}
length := uint16(216 + len(hdr.additionalData)) length := uint16(216 + len(hdr.additionalData))
binary.Write(w, binary.BigEndian, length) if err := binary.Write(w, binary.BigEndian, length); err != nil {
return err
}
zeros := make([]byte, 108) zeros := make([]byte, 108)
w.Write([]byte(srcAddr.Name)) if _, err := w.Write([]byte(srcAddr.Name)); err != nil {
w.Write(zeros[:108-len(srcAddr.Name)]) return err
w.Write([]byte(dstAddr.Name)) }
w.Write(zeros[:108-len(dstAddr.Name)]) if _, err := w.Write(zeros[:108-len(srcAddr.Name)]); err != nil {
return err
}
if _, err := w.Write([]byte(dstAddr.Name)); err != nil {
return err
}
if _, err := w.Write(zeros[:108-len(dstAddr.Name)]); err != nil {
return err
}
default: default:
fam := byte(proxyProtocolV2FamilyUnspec<<4) | byte(proxyProtocolV2ProtocolUnspec) fam := byte(proxyProtocolV2FamilyUnspec<<4) | byte(proxyProtocolV2ProtocolUnspec)
w.Write([]byte{fam}) if _, err := w.Write([]byte{fam}); err != nil {
return err
}
length := uint16(len(hdr.additionalData)) length := uint16(len(hdr.additionalData))
binary.Write(w, binary.BigEndian, length) if err := binary.Write(w, binary.BigEndian, length); err != nil {
return err
}
} }
w.Write(hdr.additionalData) if _, err := w.Write(hdr.additionalData); err != nil {
return err
}
return nil
} }