From d359cb66c8ea9693fea57c76219a74234c58ab31 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Sat, 21 Nov 2026 23:44:39 +0100 Subject: [PATCH 1/2] fix: do not report a transfer that got no data as a result A download or upload URL that answered with an error status was timed as if it carried data and requested again without a pause, and a download whose requests all failed was reported as 0 Mbps. Such a server now fails like one that is down. --- defs/server.go | 148 ++++++++++++++++--- defs/transfer_status_test.go | 272 +++++++++++++++++++++++++++++++++++ speedtest/helper.go | 70 ++++++--- transfer_status_test.go | 101 +++++++++++++ 4 files changed, 557 insertions(+), 34 deletions(-) create mode 100644 defs/transfer_status_test.go create mode 100644 transfer_status_test.go diff --git a/defs/server.go b/defs/server.go index c5349d0..ee62092 100644 --- a/defs/server.go +++ b/defs/server.go @@ -19,6 +19,7 @@ import ( "slices" "strconv" "sync" + "sync/atomic" "time" "github.com/briandowns/spinner" @@ -401,23 +402,56 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu downloadDone := make(chan struct{}, requests) var wg sync.WaitGroup + // whether any request was answered with data, and why the last one that + // was not failed + var succeeded atomic.Bool + var lastErr atomic.Pointer[error] + var failed bool + + // fetch makes one download request. An error means it brought no test + // data: the request failed, or the answer was not a success. + fetch := func() error { + resp, err := http.DefaultClient.Do(req.Clone(ctx)) + if err != nil { + return err + } + defer resp.Body.Close() - doDownload := func() { - defer wg.Done() + if err := refusal("download", resp); err != nil { + // an error page is not test data, so it is not counted + return err + } - reqClone := req.Clone(ctx) - resp, err := http.DefaultClient.Do(reqClone) - if err != nil { - if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { - output.WriteDebug("Failed when making HTTP request: %s\n", err) + n, err := io.Copy(io.Discard, io.TeeReader(resp.Body, counter)) + if n == 0 { + // success without a single byte is not test data either + if err == nil { + err = errors.New("the download request returned no data") } - return + return err } - defer resp.Body.Close() + // A body that was cut short counts all the same, whoever cut it: + // the end of the test, --timeout or the server. What arrived did + // cross the link, and on a slow link no request gets its whole body. + succeeded.Store(true) + if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + output.WriteDebug("Failed when reading HTTP response: %s\n", err) + } + return nil + } - if _, err = io.Copy(io.Discard, io.TeeReader(resp.Body, counter)); err != nil { - if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { - output.WriteDebug("Failed when reading HTTP response: %s\n", err) + doDownload := func() { + defer wg.Done() + + if err := fetch(); err != nil { + // cancellation is how the test ends, so it is not a failure + if ctx.Err() != nil { + return + } + output.WriteDebug("Download request failed: %s\n", err) + lastErr.Store(&err) + if !pauseBeforeRetry(ctx) { + return } } @@ -452,6 +486,9 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu // not a terminal defer func() { pb.Stop() + if failed { + return + } if useBytes { output.WriteUI("Download rate:\t%s\n", counter.AvgHumanize()) } else { @@ -485,6 +522,15 @@ Loop: // result doesn't change under us while it's being reported wg.Wait() + // No request answered with data is not a result of 0 Mbps. Without an + // error either, they were all still waiting when the test ended. + if !succeeded.Load() { + failed = true + if err := lastErr.Load(); err != nil { + return 0, 0, *err + } + return 0, 0, errors.New("no download request was answered before the test ended") + } return counter.AvgMbps(), counter.Total(), nil } @@ -520,6 +566,11 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int uploadDone := make(chan struct{}, requests) var wg sync.WaitGroup + // whether any request was accepted, and why the last one that was not + // was refused + var succeeded atomic.Bool + var lastErr atomic.Pointer[error] + var failed bool doUpload := func() { defer wg.Done() @@ -561,12 +612,28 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int } return } - defer resp.Body.Close() - if _, err := io.Copy(io.Discard, resp.Body); err != nil { - // cancellation is how the test ends, so it is not a failure - if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { - output.WriteDebug("Failed when reading HTTP response: %s\n", err) + // A request is accepted once the server answers it with success: it + // has the payload then, whatever becomes of the rest of its answer. + if err = refusal("upload", resp); err == nil { + succeeded.Store(true) + if _, err := io.Copy(io.Discard, resp.Body); err != nil { + // cancellation is how the test ends, so it is not a failure + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + output.WriteDebug("Failed when reading HTTP response: %s\n", err) + } + } + } + // closed before the pause below, not after it + resp.Body.Close() + + if err != nil { + // What was sent of the body stays counted: it did cross the + // link. A test where no request is accepted fails all the same. + output.WriteDebug("Upload request failed: %s\n", err) + lastErr.Store(&err) + if !pauseBeforeRetry(ctx) { + return } } @@ -601,6 +668,9 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int // not a terminal defer func() { pb.Stop() + if failed { + return + } if useBytes { output.WriteUI("Upload rate:\t%s\n", counter.AvgHumanize()) } else { @@ -634,9 +704,53 @@ Loop: // result doesn't change under us while it's being reported wg.Wait() + // nothing but refused requests is not a result + if err := lastErr.Load(); err != nil && !succeeded.Load() { + failed = true + return 0, 0, *err + } return counter.AvgMbps(), counter.Total(), nil } +// retryDelay spaces the requests that follow a failed one, so that a wrong +// URL is not asked for as fast as the server can refuse it. +const retryDelay = 200 * time.Millisecond + +// errorPageLimit is how much of a refused answer is read before it is closed. +const errorPageLimit = 4 << 10 + +// refusal reports a transfer request that was not answered with success. +// Any 2xx status is success. Everything else comes with an error page or +// nothing, not with test data, and that includes a redirect the client did +// not follow. +// +// A short error page is read to its end, so that the retry can use the +// connection again and does not open a new one each time. Only newer Go +// versions do that on their own. A page that does not say how long it is +// is left alone: reading it could wait for a server that never ends it. +func refusal(phase string, resp *http.Response) error { + if resp.StatusCode >= 200 && resp.StatusCode <= 299 { + return nil + } + if n := resp.ContentLength; n > 0 && n <= errorPageLimit { + io.CopyN(io.Discard, resp.Body, n) + } + if text := http.StatusText(resp.StatusCode); text != "" { + return fmt.Errorf("the %s request returned HTTP %d %s", phase, resp.StatusCode, text) + } + return fmt.Errorf("the %s request returned HTTP %d", phase, resp.StatusCode) +} + +// pauseBeforeRetry waits retryDelay and reports whether the test is still on. +func pauseBeforeRetry(ctx context.Context) bool { + select { + case <-time.After(retryDelay): + return true + case <-ctx.Done(): + return false + } +} + // GetIPInfo accesses the backend's getIP.php endpoint and get current client's IP information func (s *Server) GetIPInfo(distanceUnit string) (*GetIPResult, error) { t := time.Now() diff --git a/defs/transfer_status_test.go b/defs/transfer_status_test.go new file mode 100644 index 0000000..6ccf77f --- /dev/null +++ b/defs/transfer_status_test.go @@ -0,0 +1,272 @@ +package defs + +import ( + "io" + "net" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "sync/atomic" + "testing" + "time" +) + +// refusingServer answers every transfer request with an error page and +// counts the requests. +func refusingServer(t *testing.T, status int) (*Server, *atomic.Int64) { + t.Helper() + + var requests atomic.Int64 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + io.Copy(io.Discard, r.Body) + w.WriteHeader(status) + w.Write([]byte(strings.Repeat("

this is an error page

\n", 100))) + })) + t.Cleanup(ts.Close) + + return &Server{Server: ts.URL, DownloadURL: "garbage.php", UploadURL: "empty.php"}, &requests +} + +// A download URL that answers 404 gives nothing to measure. The test must +// fail with the status instead of timing the error pages, and must not ask +// again as fast as the server can refuse. +func TestDownloadFailsWhenEveryRequestIsRefused(t *testing.T) { + s, requests := refusingServer(t, http.StatusNotFound) + + _, total, err := s.Download(true, false, false, 2, 1, time.Second) + + if err == nil || err.Error() != "the download request returned HTTP 404 Not Found" { + t.Errorf("error = %v, want the 404 named", err) + } + if total != 0 { + t.Errorf("counted %d bytes of error pages as downloaded", total) + } + if n := requests.Load(); n > 50 { + t.Errorf("%d requests in a second, want a pause between refused ones", n) + } +} + +func TestUploadFailsWhenEveryRequestIsRefused(t *testing.T) { + s, requests := refusingServer(t, http.StatusRequestEntityTooLarge) + + _, _, err := s.Upload(false, true, false, false, 2, 16, time.Second) + + if err == nil || err.Error() != "the upload request returned HTTP 413 Request Entity Too Large" { + t.Errorf("error = %v, want the 413 named", err) + } + if n := requests.Load(); n > 50 { + t.Errorf("%d requests in a second, want a pause between refused ones", n) + } +} + +// A refused request is asked again after a pause. The retries must not each +// open a connection of their own: a short error page that says how long it +// is gets read, so the connection it came over can be used again. +func TestRefusedRequestsKeepTheirConnections(t *testing.T) { + const streams = 2 + + for _, phase := range []string{"download", "upload"} { + t.Run(phase, func(t *testing.T) { + var requests, connections atomic.Int64 + page := strings.Repeat("

this is an error page

\n", 100) + ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + io.Copy(io.Discard, r.Body) + w.Header().Set("Content-Length", strconv.Itoa(len(page))) + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(page)) + })) + ts.Config.ConnState = func(_ net.Conn, state http.ConnState) { + if state == http.StateNew { + connections.Add(1) + } + } + ts.Start() + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php", UploadURL: "empty.php"} + if phase == "download" { + s.Download(true, false, false, streams, 1, time.Second) + } else { + s.Upload(false, true, false, false, streams, 16, time.Second) + } + + // one more than the streams leaves room for a connection that + // was being opened when another one became free + if r, c := requests.Load(), connections.Load(); r <= streams+1 || c > streams+1 { + t.Errorf("%d requests over %d connections, want the %d streams to keep theirs", r, c, streams) + } + }) + } +} + +// One refused request among good ones does not fail the test, and its error +// page is not counted as data. +func TestDownloadDoesNotCountAnErrorPage(t *testing.T) { + const data = "0123456789" + var requests atomic.Int64 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if requests.Add(1) == 1 { + w.WriteHeader(http.StatusServiceUnavailable) + // a length the data cannot add up to + w.Write([]byte(strings.Repeat("x", 4096+3))) + return + } + w.Write([]byte(data)) + })) + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php"} + _, total, err := s.Download(true, false, false, 1, 1, time.Second) + + if err != nil { + t.Fatalf("Download: %v", err) + } + if total == 0 || total%uint64(len(data)) != 0 { + t.Errorf("counted %d bytes, want only whole %d-byte answers", total, len(data)) + } +} + +// A download URL whose connections are dropped gives nothing to measure +// either. The test fails with the cause instead of reporting 0 Mbps. +func TestDownloadFailsWhenEveryRequestIsDropped(t *testing.T) { + var requests atomic.Int64 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + conn, _, err := w.(http.Hijacker).Hijack() + if err != nil { + t.Errorf("hijack: %v", err) + return + } + conn.(*net.TCPConn).SetLinger(0) + conn.Close() + })) + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php"} + _, total, err := s.Download(true, false, false, 2, 1, time.Second) + + if err == nil { + t.Error("no error, want the dropped connection reported") + } + if total != 0 { + t.Errorf("counted %d bytes", total) + } + if n := requests.Load(); n > 50 { + t.Errorf("%d requests in a second, want a pause between failed ones", n) + } +} + +// A download whose answers are cut short is a result all the same: what +// arrived did cross the link. On a slow link the end of the test cuts every +// request, and none ever gets its whole body. +func TestDownloadCountsAnswersThatAreCutShort(t *testing.T) { + chunk := make([]byte, 16<<10) + + for _, tc := range []struct { + name string + send http.HandlerFunc + }{ + {"by the end of the test", func(w http.ResponseWriter, r *http.Request) { + for r.Context().Err() == nil { + w.Write(chunk) + w.(http.Flusher).Flush() + time.Sleep(20 * time.Millisecond) + } + }}, + {"by the server", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Length", "1048576") + w.Write(chunk) + w.(http.Flusher).Flush() + // each aborted answer costs a connection, so they are spaced + time.Sleep(5 * time.Millisecond) + panic(http.ErrAbortHandler) + }}, + } { + t.Run(tc.name, func(t *testing.T) { + ts := httptest.NewServer(tc.send) + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php"} + _, total, err := s.Download(true, false, false, 2, 1, 500*time.Millisecond) + + if err != nil { + t.Errorf("Download returned error: %v", err) + } + if total == 0 { + t.Error("nothing was counted") + } + }) + } +} + +// An upload is accepted once the server answers it with success. The server +// has the payload then, so an answer that is cut short after that does not +// make the request a failed one. +func TestUploadCountsARequestWhoseAnswerIsCutShort(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.Copy(io.Discard, r.Body) + w.Header().Set("Content-Length", "100") + w.Write([]byte("ok")) + w.(http.Flusher).Flush() + // each aborted answer costs a connection, so they are spaced + time.Sleep(5 * time.Millisecond) + panic(http.ErrAbortHandler) + })) + defer ts.Close() + + s := &Server{Server: ts.URL, UploadURL: "empty.php"} + _, total, err := s.Upload(false, true, false, false, 2, 16, 500*time.Millisecond) + + if err != nil { + t.Errorf("Upload returned error: %v", err) + } + if total == 0 { + t.Error("nothing was counted") + } +} + +// A download URL that accepts the requests and never answers them leaves +// nothing to measure when the test ends. That is a failure too, even though +// no request got as far as an error. +func TestDownloadFailsWhenNoRequestIsAnswered(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + <-r.Context().Done() + })) + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php"} + _, total, err := s.Download(true, false, false, 2, 1, time.Second) + + if err == nil || err.Error() != "no download request was answered before the test ended" { + t.Errorf("error = %v, want the missing answers reported", err) + } + if total != 0 { + t.Errorf("counted %d bytes", total) + } +} + +// A download URL that answers with success and no body has nothing to +// measure either, and is not asked again without a pause. +func TestDownloadFailsWhenTheAnswersAreEmpty(t *testing.T) { + var requests atomic.Int64 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + })) + defer ts.Close() + + s := &Server{Server: ts.URL, DownloadURL: "garbage.php"} + _, total, err := s.Download(true, false, false, 2, 1, time.Second) + + if err == nil || err.Error() != "the download request returned no data" { + t.Errorf("error = %v, want the empty answers reported", err) + } + if total != 0 { + t.Errorf("counted %d bytes", total) + } + if n := requests.Load(); n > 50 { + t.Errorf("%d requests in a second, want a pause between empty ones", n) + } +} diff --git a/speedtest/helper.go b/speedtest/helper.go index b980261..7dc539b 100644 --- a/speedtest/helper.go +++ b/speedtest/helper.go @@ -9,7 +9,9 @@ import ( "math" "mime/multipart" "net/http" + "net/url" "os" + "slices" "strconv" "strings" "time" @@ -42,9 +44,17 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel var reps_json []report.JSONReport var reps_csv []report.CSVReport var measured bool - // each server that did not answer its probe, and why + // each server that gave no result, and why: it did not answer its probe, + // or its download or upload test failed var down []downServer + // add a new line after each test if testing multiple servers + separate := func() { + if len(servers) > 1 && !silent { + output.WriteUIBlank() + } + } + // fetch current user's IP info for _, currentServer := range servers { // get telemetry level @@ -121,8 +131,13 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel download, br, err := currentServer.Download(silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent), c.Int(defs.OptionChunks), time.Duration(c.Int(defs.OptionDuration))*time.Second) if err != nil { - output.WriteError("Failed to get download speed: %s\n", err) - return err + // with one server the error that ends the run says it + if len(servers) > 1 { + output.WriteError("Failed to get download speed: %s\n", output.Sanitize(err.Error())) + } + down = append(down, failedServer(currentServer, u, err, true)) + separate() + continue } downloadValue = download bytesRead = br @@ -143,8 +158,13 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel upload, bw, err := currentServer.Upload(c.Bool(defs.OptionNoPreAllocate), silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent), c.Int(defs.OptionUploadSize), time.Duration(c.Int(defs.OptionDuration))*time.Second) if err != nil { - output.WriteError("Failed to get upload speed: %s\n", err) - return err + // with one server the error that ends the run says it + if len(servers) > 1 { + output.WriteError("Failed to get upload speed: %s\n", output.Sanitize(err.Error())) + } + down = append(down, failedServer(currentServer, u, err, true)) + separate() + continue } uploadValue = upload bytesWritten = bw @@ -236,20 +256,14 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel measured = true } else { output.WriteUI("Selected server %s (%s) is not responding at the moment, try again later\n", output.Sanitize(currentServer.Name), output.Sanitize(u.Hostname())) - down = append(down, downServer{ - server: fmt.Sprintf("%s (%s)", output.Sanitize(currentServer.Name), output.Sanitize(u.Hostname())), - reason: output.Sanitize(upErr.Error()), - }) + down = append(down, failedServer(currentServer, u, upErr, false)) } - //add a new line after each test if testing multiple servers - if len(servers) > 1 && !silent { - output.WriteUIBlank() - } + separate() } - // No server answered: fail instead of printing an empty report, which - // with exit status 0 would pass for a result. + // No server gave a result: fail instead of printing an empty report, + // which with exit status 0 would pass for a result. if !measured && len(down) > 0 { return noneResponding(down) } @@ -276,22 +290,44 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel return nil } -// downServer is a selected server that did not answer its probe. +// downServer is a selected server that gave no result. type downServer struct { server string // name and host, ready to print reason string + // answered tells a server that answered its probe and then failed its + // download or upload test from one that did not answer at all + answered bool +} + +// failedServer notes a server that gave no result, and why. +func failedServer(s defs.Server, u *url.URL, err error, answered bool) downServer { + return downServer{ + server: fmt.Sprintf("%s (%s)", output.Sanitize(s.Name), output.Sanitize(u.Hostname())), + reason: output.Sanitize(err.Error()), + answered: answered, + } } // noneResponding names every selected server with its cause. It is printed -// after "Terminated due to error: ", hence the capital letter. +// after "Terminated due to error: ", hence the capital letter. A server that +// answered its probe did respond, so with one of those among them the text +// speaks of results instead. func noneResponding(down []downServer) error { + answered := slices.ContainsFunc(down, func(d downServer) bool { return d.answered }) + if len(down) == 1 { + if answered { + return fmt.Errorf("Selected server %s gave no result: %s", down[0].server, down[0].reason) + } return fmt.Errorf("Selected server %s is not responding: %s", down[0].server, down[0].reason) } causes := make([]string, 0, len(down)) for _, d := range down { causes = append(causes, d.server+": "+d.reason) } + if answered { + return fmt.Errorf("None of the %d selected servers gave a result: %s", len(down), strings.Join(causes, "; ")) + } return fmt.Errorf("None of the %d selected servers is responding: %s", len(down), strings.Join(causes, "; ")) } diff --git a/transfer_status_test.go b/transfer_status_test.go new file mode 100644 index 0000000..8e1a1eb --- /dev/null +++ b/transfer_status_test.go @@ -0,0 +1,101 @@ +package main + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// transferBackend answers the probe and the IP lookup, and its download URL +// with the given status. +func transferBackend(t *testing.T, downloadStatus int) *httptest.Server { + t.Helper() + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/empty.php": + case "/getIP.php": + w.Write([]byte(`{"processedString":"127.0.0.1 - localhost","rawIspInfo":""}`)) + case "/garbage.php": + w.WriteHeader(downloadStatus) + w.Write([]byte(strings.Repeat("x", 64<<10))) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + return srv +} + +// A server that answers its probe but whose download URL does not exist gives +// nothing to measure. The run fails and names the status in every mode, not +// reporting the speed at which the error pages arrived. +func TestMissingDownloadURLFailsTheRun(t *testing.T) { + backend := transferBackend(t, http.StatusNotFound) + list := writeServerList(t, listedServer{1, "Up", backend.URL, "empty.php"}) + + for _, mode := range outputModes { + t.Run("mode="+mode, func(t *testing.T) { + r := runCLI(t, withMode(mode, "--local-json", list, "--server", "1", "--duration", "1", "--no-icmp", "--no-upload")...) + + if r.code != 1 { + t.Errorf("exit status %d, want 1, stderr:\n%s", r.code, r.stderr) + } + // the event stream has announced its phases by then + if strings.Contains(r.stdout, "Up") || strings.Contains(r.stdout, "rate") { + t.Errorf("stdout = %q, want no report", r.stdout) + } + if msg, want := r.failure(t), "Selected server Up (127.0.0.1) gave no result: the download request returned HTTP 404 Not Found"; msg != want { + t.Errorf("error = %q, want %q", msg, want) + } + if strings.Contains(r.stderr, "Download rate") { + t.Errorf("a rate is printed for the failed test:\n%s", r.stderr) + } + if n := strings.Count(r.stderr, "HTTP 404 Not Found"); n != 1 { + t.Errorf("the cause is on stderr %d times, want once:\n%s", n, r.stderr) + } + }) + } +} + +// With more than one server, one whose download fails does not take the +// results of the others with it. +func TestFailedDownloadLeavesTheRunToTheOtherServers(t *testing.T) { + good := transferBackend(t, http.StatusOK) + bad := transferBackend(t, http.StatusNotFound) + list := writeServerList(t, + listedServer{1, "Bad", bad.URL, "empty.php"}, + listedServer{2, "Good", good.URL, "empty.php"}, + ) + args := []string{"--local-json", list, "--server", "1", "--server", "2", "--duration", "1", "--no-icmp", "--no-upload"} + + r := runCLI(t, append(args, "--json")...) + + if r.code != 0 { + t.Errorf("exit status %d, want 0, stderr:\n%s", r.code, r.stderr) + } + var reports []struct { + Server struct { + Name string `json:"name"` + } `json:"server"` + Download float64 `json:"download"` + } + if err := json.Unmarshal([]byte(r.stdout), &reports); err != nil { + t.Fatalf("stdout is not a JSON report: %v\n%s", err, r.stdout) + } + if len(reports) != 1 || reports[0].Server.Name != "Good" || reports[0].Download <= 0 { + t.Errorf("reports = %+v, want the one of Good with a download rate", reports) + } + const cause = "Failed to get download speed: the download request returned HTTP 404 Not Found\n" + if !strings.Contains(r.stderr, cause) { + t.Errorf("stderr does not say why Bad failed:\n%s", r.stderr) + } + + // the failed server's output is set apart from the next one's like any other + r = runCLI(t, args...) + if !strings.Contains(r.stderr, cause+"\nSelected server: Good ") { + t.Errorf("no blank line between the failed server and the next:\n%s", r.stderr) + } +} From 45a48a84a5b2519530d2a4b553e586e0edc0456e Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Sun, 22 Nov 2026 01:21:30 +0100 Subject: [PATCH 2/2] fix: refuse a redirect from HTTPS to plain HTTP under --secure A server could answer with a redirect to an http:// URL, and the probe, the IP lookup, the transfers and the telemetry request followed it. A run that asked for HTTPS was then measured, or its result sent, over plain HTTP. --- defs/server.go | 27 +++--- secure_redirect_test.go | 177 +++++++++++++++++++++++++++++++++++++ speedtest/redirect_test.go | 47 ++++++++++ speedtest/speedtest.go | 22 +++++ 4 files changed, 262 insertions(+), 11 deletions(-) create mode 100644 secure_redirect_test.go create mode 100644 speedtest/redirect_test.go diff --git a/defs/server.go b/defs/server.go index ee62092..35c631e 100644 --- a/defs/server.go +++ b/defs/server.go @@ -606,26 +606,31 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int } resp, err := http.DefaultClient.Do(uploadReq) - if err != nil { + if err != nil && resp == nil { if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { output.WriteDebug("Failed when making HTTP request: %s\n", err) } return } - // A request is accepted once the server answers it with success: it - // has the payload then, whatever becomes of the rest of its answer. - if err = refusal("upload", resp); err == nil { - succeeded.Store(true) - if _, err := io.Copy(io.Discard, resp.Body); err != nil { - // cancellation is how the test ends, so it is not a failure - if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { - output.WriteDebug("Failed when reading HTTP response: %s\n", err) + // An error that comes with a response is a redirect the client did + // not accept. That is a refusal too, and the body is closed already. + if err == nil { + // A request is accepted once the server answers it with success: + // it has the payload then, whatever becomes of the rest of its + // answer. + if err = refusal("upload", resp); err == nil { + succeeded.Store(true) + if _, err := io.Copy(io.Discard, resp.Body); err != nil { + // cancellation is how the test ends, so it is not a failure + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + output.WriteDebug("Failed when reading HTTP response: %s\n", err) + } } } + // closed before the pause below, not after it + resp.Body.Close() } - // closed before the pause below, not after it - resp.Body.Close() if err != nil { // What was sent of the body stays counted: it did cross the diff --git a/secure_redirect_test.go b/secure_redirect_test.go new file mode 100644 index 0000000..3357042 --- /dev/null +++ b/secure_redirect_test.go @@ -0,0 +1,177 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strconv" + "strings" + "sync/atomic" + "testing" +) + +// redirectingFront is an HTTPS server that sends the requests for the given +// paths on to a plain HTTP backend, and answers the others itself. +func redirectingFront(t *testing.T, backend *httptest.Server, paths ...string) *httptest.Server { + t.Helper() + + front := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for _, path := range paths { + if r.URL.Path == path { + http.Redirect(w, r, backend.URL+r.URL.RequestURI(), http.StatusTemporaryRedirect) + return + } + } + if r.URL.Path == "/getIP.php" { + w.Write([]byte(`{"processedString":"127.0.0.1 - localhost","rawIspInfo":""}`)) + } + })) + t.Cleanup(front.Close) + return front +} + +const refusedRedirect = "the server redirected to plain HTTP, which --secure does not allow" + +// --secure asks for HTTPS to every test server. A server that answers with a +// redirect to plain HTTP must not move the test there. +func TestSecureRefusesARedirectToPlainHTTP(t *testing.T) { + backend := transferBackend(t, http.StatusOK) + front := redirectingFront(t, backend, "/empty.php", "/getIP.php", "/garbage.php") + + list := writeServerList(t, listedServer{1, "Front", front.URL, "empty.php"}) + args := []string{"--local-json", list, "--server", "1", "--skip-cert-verify", + "--duration", "1", "--no-icmp", "--no-download", "--no-upload", "--json"} + + // without --secure the redirect is followed, as it always was + if r := runCLI(t, args...); r.code != 0 { + t.Errorf("without --secure: exit status %d, want 0, stderr:\n%s", r.code, r.stderr) + } + + r := runCLI(t, append(args, "--secure")...) + if r.code != 1 { + t.Errorf("exit status %d, want 1, stderr:\n%s", r.code, r.stderr) + } + if r.stdout != "" { + t.Errorf("stdout = %q, want no report", r.stdout) + } + if msg := r.failure(t); !strings.HasSuffix(msg, refusedRedirect) { + t.Errorf("error = %q, want it to end in %q", msg, refusedRedirect) + } +} + +// The same holds when only the download URL is redirected: the download is +// neither measured over plain HTTP nor reported as 0 Mbps. +func TestSecureRefusesARedirectedDownload(t *testing.T) { + backend := transferBackend(t, http.StatusOK) + front := redirectingFront(t, backend, "/garbage.php") + + list := writeServerList(t, listedServer{1, "Front", front.URL, "empty.php"}) + r := runCLI(t, "--local-json", list, "--server", "1", "--skip-cert-verify", "--secure", + "--duration", "1", "--no-icmp", "--no-upload", "--json") + + if r.code != 1 { + t.Errorf("exit status %d, want 1, stderr:\n%s", r.code, r.stderr) + } + if r.stdout != "" { + t.Errorf("stdout = %q, want no report", r.stdout) + } + if msg := r.failure(t); !strings.HasSuffix(msg, refusedRedirect) { + t.Errorf("error = %q, want it to end in %q", msg, refusedRedirect) + } +} + +// And when only the upload is redirected. A POST answered with 302 is one +// the client would follow, as a GET of the new URL, so the refusal reaches +// the upload as a failed request. A 307 asks for the same POST again, which +// the client does not do, so that one arrives as a status. Neither is a +// result, and neither sends anything on over plain HTTP. +func TestSecureRefusesARedirectedUpload(t *testing.T) { + for _, tc := range []struct { + status int + want string + }{ + {http.StatusFound, refusedRedirect}, + {http.StatusTemporaryRedirect, "the upload request returned HTTP 307 Temporary Redirect"}, + } { + t.Run(strconv.Itoa(tc.status), func(t *testing.T) { + var overPlainHTTP atomic.Int64 + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + overPlainHTTP.Add(1) + })) + t.Cleanup(backend.Close) + + front := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost: + http.Redirect(w, r, backend.URL+r.URL.RequestURI(), tc.status) + case r.URL.Path == "/getIP.php": + w.Write([]byte(`{"processedString":"127.0.0.1 - localhost","rawIspInfo":""}`)) + } + })) + t.Cleanup(front.Close) + + list := writeServerList(t, listedServer{1, "Front", front.URL, "empty.php"}) + // the payload is small enough for the server to take in whole, so + // that it has no unread body to close the connection on + r := runCLI(t, "--local-json", list, "--server", "1", "--skip-cert-verify", "--secure", + "--duration", "1", "--no-icmp", "--no-download", "--upload-size", "16", "--json") + + if r.code != 1 { + t.Errorf("exit status %d, want 1, stderr:\n%s", r.code, r.stderr) + } + if r.stdout != "" { + t.Errorf("stdout = %q, want no report", r.stdout) + } + if msg := r.failure(t); !strings.HasSuffix(msg, tc.want) { + t.Errorf("error = %q, want it to end in %q", msg, tc.want) + } + if n := overPlainHTTP.Load(); n != 0 { + t.Errorf("%d requests went on over plain HTTP, want none", n) + } + }) + } +} + +// The telemetry request is made after the test with the same client, so +// --secure covers it as well. A telemetry server that sends the result on to +// plain HTTP gets an error line and the result stays off plain HTTP. The run +// itself has its result and is not failed by that. +func TestSecureRefusesARedirectedTelemetry(t *testing.T) { + var overPlainHTTP atomic.Int64 + plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + overPlainHTTP.Add(1) + })) + t.Cleanup(plain.Close) + + telemetry := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, plain.URL+r.URL.RequestURI(), http.StatusTemporaryRedirect) + })) + t.Cleanup(telemetry.Close) + + // a test server that redirects nothing + front := redirectingFront(t, plain) + list := writeServerList(t, listedServer{1, "Front", front.URL, "empty.php"}) + args := []string{"--local-json", list, "--server", "1", "--skip-cert-verify", + "--duration", "1", "--no-icmp", "--no-download", "--no-upload", "--json", + "--telemetry-server", telemetry.URL} + + // without --secure the redirect is followed, as it always was + if r := runCLI(t, args...); r.code != 0 || overPlainHTTP.Load() == 0 { + t.Errorf("without --secure: exit status %d and %d requests over plain HTTP, want 0 and some, stderr:\n%s", + r.code, overPlainHTTP.Load(), r.stderr) + } + overPlainHTTP.Store(0) + + r := runCLI(t, append(args, "--secure")...) + if r.code != 0 { + t.Errorf("exit status %d, want 0, stderr:\n%s", r.code, r.stderr) + } + if !strings.Contains(r.stdout, `"name":"Front"`) { + t.Errorf("stdout = %q, want the report", r.stdout) + } + if !strings.Contains(r.stderr, "Error when sending telemetry data: ") || !strings.Contains(r.stderr, refusedRedirect) { + t.Errorf("stderr does not name the refused telemetry redirect:\n%s", r.stderr) + } + if n := overPlainHTTP.Load(); n != 0 { + t.Errorf("%d requests went on over plain HTTP, want none", n) + } +} diff --git a/speedtest/redirect_test.go b/speedtest/redirect_test.go new file mode 100644 index 0000000..513328e --- /dev/null +++ b/speedtest/redirect_test.go @@ -0,0 +1,47 @@ +package speedtest + +import ( + "net/http" + "net/url" + "testing" +) + +func TestRefuseDowngrade(t *testing.T) { + request := func(rawURL string) *http.Request { + u, err := url.Parse(rawURL) + if err != nil { + t.Fatalf("parse %q: %v", rawURL, err) + } + return &http.Request{URL: u} + } + + cases := []struct { + name string + from string + to string + wantErr bool + }{ + {"HTTPS to HTTPS", "https://a.example/empty.php", "https://b.example/empty.php", false}, + {"HTTPS to plain HTTP", "https://a.example/empty.php", "http://a.example/empty.php", true}, + {"plain HTTP to plain HTTP", "http://a.example/telemetry", "http://b.example/telemetry", false}, + {"plain HTTP to HTTPS", "http://a.example/telemetry", "https://a.example/telemetry", false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + err := refuseDowngrade(request(c.to), []*http.Request{request(c.from)}) + if (err != nil) != c.wantErr { + t.Errorf("error = %v, want an error: %t", err, c.wantErr) + } + }) + } + + t.Run("too many redirects", func(t *testing.T) { + via := make([]*http.Request, 10) + for i := range via { + via[i] = request("https://a.example/empty.php") + } + if err := refuseDowngrade(request("https://a.example/empty.php"), via); err == nil { + t.Error("followed an eleventh redirect") + } + }) +} diff --git a/speedtest/speedtest.go b/speedtest/speedtest.go index e06c5ba..a139ef8 100644 --- a/speedtest/speedtest.go +++ b/speedtest/speedtest.go @@ -312,6 +312,15 @@ func SpeedTest(c *cli.Context) error { return nil } + // From here on the requests go to the test servers and, with telemetry + // on, to its server. Under --secure a redirect may not take any of them + // from HTTPS to plain HTTP. A telemetry server given with http:// is + // still used as given, and the server list was fetched the way its own + // URL says, which --secure leaves alone. + if forceScheme == forceHttps { + http.DefaultClient.CheckRedirect = refuseDowngrade + } + // if --server is given, do speed tests with all of them if len(c.IntSlice(defs.OptionServer)) > 0 { return doSpeedTest(c, servers, telemetryServer, network, silent, noICMP) @@ -422,6 +431,19 @@ func wellKnownServerURL(serverURL string) string { return serverURL + "/.well-known/librespeed" } +// refuseDowngrade is the redirect policy under --secure: a server asked over +// HTTPS may not send the client on to plain HTTP. The limit of ten redirects +// is the one Go applies when no policy is set. +func refuseDowngrade(req *http.Request, via []*http.Request) error { + if len(via) >= 10 { + return errors.New("stopped after 10 redirects") + } + if via[len(via)-1].URL.Scheme == "https" && req.URL.Scheme != "https" { + return errors.New("the server redirected to plain HTTP, which --secure does not allow") + } + return nil +} + // getServerList fetches the server JSON from a remote server func getServerList(forceScheme int, serverList string, excludes, specific []int, filter bool) ([]defs.Server, error) { // --exclude and --server cannot be used at the same time