From 7670c6eaa67bbc7d0eb495e04de8f01dc2e75e45 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Fri, 4 Mar 2022 10:02:31 +0100 Subject: [PATCH] Fix windows tests (#1235) * Fix windows tests Just ignore /../ tests on windows until we have proper suppor. * Remove useless test code This code was basically just testing if tcp works. To test if SO_REUSEPORT works we only have to try to listen on the same addr:port twice. * Fix test --- reuseport/reuseport_test.go | 95 +++++-------------------------------- server_test.go | 12 +++-- uri_test.go | 11 ++++- 3 files changed, 29 insertions(+), 89 deletions(-) diff --git a/reuseport/reuseport_test.go b/reuseport/reuseport_test.go index a79b962a77..93d4295a34 100644 --- a/reuseport/reuseport_test.go +++ b/reuseport/reuseport_test.go @@ -1,17 +1,14 @@ package reuseport import ( - "fmt" - "io/ioutil" "net" "testing" - "time" ) func TestTCP4(t *testing.T) { t.Parallel() - testNewListener(t, "tcp4", "localhost:10081", 20, 1000) + testNewListener(t, "tcp4", "localhost:10081") } func TestTCP6(t *testing.T) { @@ -19,7 +16,7 @@ func TestTCP6(t *testing.T) { // Run this test only if tcp6 interface exists. if hasLocalIPv6(t) { - testNewListener(t, "tcp6", "[::1]:10082", 20, 1000) + testNewListener(t, "tcp6", "[::1]:10082") } } @@ -36,87 +33,17 @@ func hasLocalIPv6(t *testing.T) bool { return false } -func testNewListener(t *testing.T, network, addr string, serversCount, requestsCount int) { - var lns []net.Listener - doneCh := make(chan struct{}, serversCount) - - for i := 0; i < serversCount; i++ { - ln, err := Listen(network, addr) - if err != nil { - t.Fatalf("cannot create listener %d: %s", i, err) - } - go func() { - serveEcho(t, ln) - doneCh <- struct{}{} - }() - lns = append(lns, ln) - } - - for i := 0; i < requestsCount; i++ { - c, err := net.Dial(network, addr) - if err != nil { - t.Fatalf("%d. unexpected error when dialing: %s", i, err) - } - req := fmt.Sprintf("request number %d", i) - if _, err = c.Write([]byte(req)); err != nil { - t.Fatalf("%d. unexpected error when writing request: %s", i, err) - } - if err = c.(*net.TCPConn).CloseWrite(); err != nil { - t.Fatalf("%d. unexpected error when closing write end of the connection: %s", i, err) - } - - var resp []byte - ch := make(chan struct{}) - go func() { - if resp, err = ioutil.ReadAll(c); err != nil { - t.Errorf("%d. unexpected error when reading response: %s", i, err) - } - close(ch) - }() - select { - case <-ch: - case <-time.After(250 * time.Millisecond): - t.Fatalf("%d. timeout when waiting for response", i) - } - - if string(resp) != req { - t.Fatalf("%d. unexpected response %q. Expecting %q", i, resp, req) - } - if err = c.Close(); err != nil { - t.Fatalf("%d. unexpected error when closing connection: %s", i, err) - } - } - - for _, ln := range lns { - if err := ln.Close(); err != nil { - t.Fatalf("unexpected error when closing listener: %s", err) - } +func testNewListener(t *testing.T, network, addr string) { + ln1, err := Listen(network, addr) + if err != nil { + t.Fatalf("cannot create listener %v", err) } - for i := 0; i < serversCount; i++ { - select { - case <-doneCh: - case <-time.After(200 * time.Millisecond): - t.Fatalf("timeout when waiting for servers to be closed") - } + ln2, err := Listen(network, addr) + if err != nil { + t.Fatalf("cannot create listener %v", err) } -} -func serveEcho(t *testing.T, ln net.Listener) { - for { - c, err := ln.Accept() - if err != nil { - break - } - req, err := ioutil.ReadAll(c) - if err != nil { - t.Fatalf("unexpected error when reading request: %s", err) - } - if _, err = c.Write(req); err != nil { - t.Fatalf("unexpected error when writing response: %s", err) - } - if err = c.Close(); err != nil { - t.Fatalf("unexpected error when closing connection: %s", err) - } - } + _ = ln1.Close() + _ = ln2.Close() } diff --git a/server_test.go b/server_test.go index cad3cf1a18..5436606432 100644 --- a/server_test.go +++ b/server_test.go @@ -16,6 +16,7 @@ import ( "os" "reflect" "regexp" + "runtime" "strings" "sync" "testing" @@ -173,7 +174,7 @@ func TestServerPipelineFlush(t *testing.T) { // Since the second request takes 200ms to finish we expect the first one to be flushed earlier. d := time.Since(start) - if d > time.Millisecond*100 { + if d >= time.Millisecond*200 { t.Fatalf("had to wait for %v", d) } @@ -573,12 +574,15 @@ func TestRequestCtxRedirect(t *testing.T) { testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "x.html?b=1#aaa=bbb&cc=ddd", "http://qqq/foo/x.html?b=1#aaa=bbb&cc=ddd") testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html", "http://qqq/x.html") testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html#aaa=bbb&cc=ddd", "http://qqq/x.html#aaa=bbb&cc=ddd") - testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html") - testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html") - testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html") testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "http://foo.bar/baz", "http://foo.bar/baz") testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "https://foo.bar/baz", "https://foo.bar/baz") testRequestCtxRedirect(t, "https://foo.com/bar?aaa", "//google.com/aaa?bb", "https://google.com/aaa?bb") + + if runtime.GOOS != "windows" { + testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html") + testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html") + testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html") + } } func testRequestCtxRedirect(t *testing.T, origURL, redirectURL, expectedURL string) { diff --git a/uri_test.go b/uri_test.go index c7f02817d7..8b441c7366 100644 --- a/uri_test.go +++ b/uri_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "reflect" + "runtime" "testing" "time" ) @@ -120,12 +121,16 @@ func TestURIUpdate(t *testing.T) { // relative uri testURIUpdate(t, "http://example.com/baz/xxx.html?aaa=22#aaa", "bb.html?xx=12#pp", "http://example.com/baz/bb.html?xx=12#pp") - testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34") + testURIUpdate(t, "http://example.com/aaa.html?foo=bar", "?baz=434&aaa#xcv", "http://example.com/aaa.html?baz=434&aaa#xcv") testURIUpdate(t, "http://example.com/baz", "~a/%20b=c,тест?йцу=ке", "http://example.com/~a/%20b=c,%D1%82%D0%B5%D1%81%D1%82?йцу=ке") testURIUpdate(t, "http://example.com/baz", "/qwe#fragment", "http://example.com/qwe#fragment") testURIUpdate(t, "http://example.com/baz/xxx", "aaa.html#bb?cc=dd&ee=dfd", "http://example.com/baz/aaa.html#bb?cc=dd&ee=dfd") + if runtime.GOOS != "windows" { + testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34") + } + // hash testURIUpdate(t, "http://example.com/#fragment1", "#fragment2", "http://example.com/#fragment2") @@ -147,6 +152,10 @@ func testURIUpdate(t *testing.T, base, update, result string) { } func TestURIPathNormalize(t *testing.T) { + if runtime.GOOS == "windows" { + t.SkipNow() + } + t.Parallel() var u URI