]> Cypherpunks repositories - gostls13.git/commitdiff
net: use slices and maps to clean up tests
authorapocelipes <seve3r@outlook.com>
Wed, 24 Jul 2024 10:39:48 +0000 (10:39 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 25 Jul 2024 00:20:13 +0000 (00:20 +0000)
Replace reflect.DeepEqual with slices.Equal/maps.Equal, which is
much faster.

Change-Id: I54600fb63a56460c11d3d5af9072da585e31b1a2
GitHub-Last-Rev: 08c1445ad5be94d071e8ceb4b060b8f4ab0d77ba
GitHub-Pull-Request: golang/go#67606
Reviewed-on: https://go-review.googlesource.com/c/go/+/587816
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

17 files changed:
src/net/dnsclient_unix_test.go
src/net/dnsconfig_unix_test.go
src/net/hosts_test.go
src/net/http/cgi/host_test.go
src/net/http/clientserver_test.go
src/net/http/fs_test.go
src/net/http/http_test.go
src/net/http/httputil/reverseproxy_test.go
src/net/http/request_test.go
src/net/http/serve_test.go
src/net/http/sniff_test.go
src/net/http/transport_test.go
src/net/lookup_test.go
src/net/lookup_windows_test.go
src/net/mail/message_test.go
src/net/resolverdialfunc_test.go
src/net/textproto/reader_test.go

index 31677573c0b22a5d89eb5717c092600a6bc27494..c4e5194a34084598f81d51cf3822db0e627f7688 100644 (file)
@@ -10,6 +10,7 @@ import (
        "context"
        "errors"
        "fmt"
+       "maps"
        "os"
        "path"
        "path/filepath"
@@ -429,7 +430,7 @@ func TestUpdateResolvConf(t *testing.T) {
                        wg.Wait()
                }
                servers := conf.servers()
-               if !reflect.DeepEqual(servers, tt.servers) {
+               if !slices.Equal(servers, tt.servers) {
                        t.Errorf("#%d: got %v; want %v", i, servers, tt.servers)
                        continue
                }
@@ -1154,7 +1155,7 @@ func testRotate(t *testing.T, rotate bool, nameservers, wantServers []string) {
                }
        }
 
-       if !reflect.DeepEqual(usedServers, wantServers) {
+       if !slices.Equal(usedServers, wantServers) {
                t.Errorf("rotate=%t got used servers:\n%v\nwant:\n%v", rotate, usedServers, wantServers)
        }
 }
@@ -1433,7 +1434,7 @@ func TestStrictErrorsLookupIP(t *testing.T) {
                                        wantIPs[ip] = struct{}{}
                                }
                        }
-                       if !reflect.DeepEqual(gotIPs, wantIPs) {
+                       if !maps.Equal(gotIPs, wantIPs) {
                                t.Errorf("#%d (%s) strict=%v: got ips %v; want %v", i, tt.desc, strict, gotIPs, wantIPs)
                        }
                }
@@ -1940,7 +1941,7 @@ func TestPTRandNonPTR(t *testing.T) {
        if err != nil {
                t.Fatalf("LookupAddr: %v", err)
        }
-       if want := []string{"golang.org."}; !reflect.DeepEqual(names, want) {
+       if want := []string{"golang.org."}; !slices.Equal(names, want) {
                t.Errorf("names = %q; want %q", names, want)
        }
 }
@@ -2207,14 +2208,14 @@ func TestCVE202133195(t *testing.T) {
                                if err.Error() != expectedErr.Error() {
                                        t.Fatalf("unexpected error: %s", err)
                                }
-                               if !reflect.DeepEqual(records, expected) {
+                               if !slices.Equal(records, expected) {
                                        t.Error("Unexpected record set")
                                }
                                records, err = LookupAddr("192.0.2.42")
                                if err.Error() != expectedErr.Error() {
                                        t.Fatalf("unexpected error: %s", err)
                                }
-                               if !reflect.DeepEqual(records, expected) {
+                               if !slices.Equal(records, expected) {
                                        t.Error("Unexpected record set")
                                }
                        },
index 0aae2ba85b8c32377c55c01d5f18715846711597..4db1c5a4af9e5336fb41ae254550b1dff520732a 100644 (file)
@@ -11,6 +11,7 @@ import (
        "io/fs"
        "os"
        "reflect"
+       "slices"
        "strings"
        "testing"
        "time"
@@ -250,7 +251,7 @@ func TestDNSDefaultSearch(t *testing.T) {
        for _, tt := range dnsDefaultSearchTests {
                getHostname = func() (string, error) { return tt.name, tt.err }
                got := dnsDefaultSearch()
-               if !reflect.DeepEqual(got, tt.want) {
+               if !slices.Equal(got, tt.want) {
                        t.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt.name, tt.err, got, tt.want)
                }
        }
index 5f22920765bd7678c385e84975425500319e4951..2661e79d993d203641b8f4e169ec8e8cbacdd959 100644 (file)
@@ -5,7 +5,7 @@
 package net
 
 import (
-       "reflect"
+       "slices"
        "strings"
        "testing"
 )
@@ -73,7 +73,7 @@ func testStaticHost(t *testing.T, hostsPath string, ent staticHostEntry) {
        ins := []string{ent.in, absDomainName(ent.in), strings.ToLower(ent.in), strings.ToUpper(ent.in)}
        for _, in := range ins {
                addrs, _ := lookupStaticHost(in)
-               if !reflect.DeepEqual(addrs, ent.out) {
+               if !slices.Equal(addrs, ent.out) {
                        t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", hostsPath, in, addrs, ent.out)
                }
        }
@@ -143,7 +143,7 @@ func testStaticAddr(t *testing.T, hostsPath string, ent staticHostEntry) {
        for i := range ent.out {
                ent.out[i] = absDomainName(ent.out[i])
        }
-       if !reflect.DeepEqual(hosts, ent.out) {
+       if !slices.Equal(hosts, ent.out) {
                t.Errorf("%s, lookupStaticAddr(%s) = %v; want %v", hostsPath, ent.in, hosts, ent.out)
        }
 }
index 7fe0e6257dbeae16ca5feb0134586ce5c141c1d3..8ecfa19f6bac2d11336f947b4cc9ba56ae818923 100644 (file)
@@ -16,9 +16,9 @@ import (
        "net/http/httptest"
        "os"
        "path/filepath"
-       "reflect"
        "regexp"
        "runtime"
+       "slices"
        "strings"
        "testing"
        "time"
@@ -510,7 +510,7 @@ func TestRemoveLeadingDuplicates(t *testing.T) {
        }
        for _, tt := range tests {
                got := removeLeadingDuplicates(tt.env)
-               if !reflect.DeepEqual(got, tt.want) {
+               if !slices.Equal(got, tt.want) {
                        t.Errorf("removeLeadingDuplicates(%q) = %q; want %q", tt.env, got, tt.want)
                }
        }
index 3dc440dde1584e1fd5fd85ad1bbddfe667adc20c..0c2142a0639c74f1786794f54500de929330302f 100644 (file)
@@ -274,7 +274,7 @@ func testChunkedResponseHeaders(t *testing.T, mode testMode) {
        if mode == http2Mode {
                wantTE = nil
        }
-       if !reflect.DeepEqual(res.TransferEncoding, wantTE) {
+       if !slices.Equal(res.TransferEncoding, wantTE) {
                t.Errorf("TransferEncoding = %v; want %v", res.TransferEncoding, wantTE)
        }
        if got, haveCL := res.Header["Content-Length"]; haveCL {
index 2ffffbf0b33c9705fdd22f2199f7ee71f6b4c9d3..3149ca35acd2a297f323dd66f3e6dd6769d12902 100644 (file)
@@ -24,9 +24,9 @@ import (
        "os/exec"
        "path"
        "path/filepath"
-       "reflect"
        "regexp"
        "runtime"
+       "slices"
        "strconv"
        "strings"
        "testing"
@@ -516,7 +516,7 @@ func testServeFileContentType(t *testing.T, mode testMode) {
                if err != nil {
                        t.Fatal(err)
                }
-               if h := resp.Header["Content-Type"]; !reflect.DeepEqual(h, want) {
+               if h := resp.Header["Content-Type"]; !slices.Equal(h, want) {
                        t.Errorf("Content-Type mismatch: got %v, want %v", h, want)
                }
                resp.Body.Close()
@@ -1448,7 +1448,7 @@ func TestFileServerCleanPath(t *testing.T) {
                rr := httptest.NewRecorder()
                req, _ := NewRequest("GET", "http://foo.localhost"+tt.path, nil)
                FileServer(fileServerCleanPathDir{&log}).ServeHTTP(rr, req)
-               if !reflect.DeepEqual(log, tt.wantOpen) {
+               if !slices.Equal(log, tt.wantOpen) {
                        t.Logf("For %s: Opens = %q; want %q", tt.path, log, tt.wantOpen)
                }
                if rr.Code != tt.wantCode {
index 2e7e024e2057f39ada5778752ce4ae78d96fa043..df9812fc94747c1a8b6d40f6e1dd89044ba873d1 100644 (file)
@@ -12,8 +12,8 @@ import (
        "io/fs"
        "net/url"
        "os"
-       "reflect"
        "regexp"
+       "slices"
        "strings"
        "testing"
 )
@@ -41,7 +41,7 @@ func TestForeachHeaderElement(t *testing.T) {
                foreachHeaderElement(tt.in, func(v string) {
                        got = append(got, v)
                })
-               if !reflect.DeepEqual(got, tt.want) {
+               if !slices.Equal(got, tt.want) {
                        t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
                }
        }
index eac8b7ec81d6ecff6cfe7612d939cb5ae46f27e0..67d0e50593a2eb884c3e1d270ce9fce9d36e3574 100644 (file)
@@ -205,7 +205,7 @@ func TestReverseProxyStripHeadersPresentInConnection(t *testing.T) {
                slices.Sort(cf)
                expectedValues := []string{"Upgrade", someConnHeader, fakeConnectionToken}
                slices.Sort(expectedValues)
-               if !reflect.DeepEqual(cf, expectedValues) {
+               if !slices.Equal(cf, expectedValues) {
                        t.Errorf("handler modified header %q = %q; want %q", "Connection", cf, expectedValues)
                }
        }))
@@ -765,7 +765,7 @@ func TestReverseProxyGetPutBuffer(t *testing.T) {
        wantLog := []string{"getBuf", "putBuf-" + strconv.Itoa(size)}
        mu.Lock()
        defer mu.Unlock()
-       if !reflect.DeepEqual(log, wantLog) {
+       if !slices.Equal(log, wantLog) {
                t.Errorf("Log events = %q; want %q", log, wantLog)
        }
 }
index 9b6eb6e1a8bc99364bc28e1dd1f93a53db6bdac5..37b888313d2abf8d8aadd19bfa18d01e51738ae4 100644 (file)
@@ -23,6 +23,7 @@ import (
        "os"
        "reflect"
        "regexp"
+       "slices"
        "strings"
        "testing"
 )
@@ -69,22 +70,22 @@ func TestParseFormQuery(t *testing.T) {
        if bz := req.PostFormValue("z"); bz != "post" {
                t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
        }
-       if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
+       if qs := req.Form["q"]; !slices.Equal(qs, []string{"foo", "bar"}) {
                t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
        }
-       if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
+       if both := req.Form["both"]; !slices.Equal(both, []string{"y", "x"}) {
                t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
        }
        if prio := req.FormValue("prio"); prio != "2" {
                t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
        }
-       if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
+       if orphan := req.Form["orphan"]; !slices.Equal(orphan, []string{"", "nope"}) {
                t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
        }
-       if empty := req.Form["empty"]; !reflect.DeepEqual(empty, []string{"", "not"}) {
+       if empty := req.Form["empty"]; !slices.Equal(empty, []string{"", "not"}) {
                t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
        }
-       if nokey := req.Form[""]; !reflect.DeepEqual(nokey, []string{"nokey"}) {
+       if nokey := req.Form[""]; !slices.Equal(nokey, []string{"nokey"}) {
                t.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey)
        }
 }
@@ -765,7 +766,7 @@ func TestRequestWriteBufferedWriter(t *testing.T) {
                "User-Agent: " + DefaultUserAgent + "\r\n",
                "\r\n",
        }
-       if !reflect.DeepEqual(got, want) {
+       if !slices.Equal(got, want) {
                t.Errorf("Writes = %q\n  Want = %q", got, want)
        }
 }
@@ -785,7 +786,7 @@ func TestRequestBadHostHeader(t *testing.T) {
                "User-Agent: " + DefaultUserAgent + "\r\n",
                "\r\n",
        }
-       if !reflect.DeepEqual(got, want) {
+       if !slices.Equal(got, want) {
                t.Errorf("Writes = %q\n  Want = %q", got, want)
        }
 }
@@ -804,7 +805,7 @@ func TestRequestBadUserAgent(t *testing.T) {
                "User-Agent: evil  X-Evil: evil\r\n",
                "\r\n",
        }
-       if !reflect.DeepEqual(got, want) {
+       if !slices.Equal(got, want) {
                t.Errorf("Writes = %q\n  Want = %q", got, want)
        }
 }
index b2858ba8f2b55956331e2f66c6552c4a33c66d66..cc485d3b89471c5b1ffbec44bea2a9409da4d22c 100644 (file)
@@ -34,6 +34,7 @@ import (
        "reflect"
        "regexp"
        "runtime"
+       "slices"
        "strconv"
        "strings"
        "sync"
@@ -4010,7 +4011,7 @@ func testHTTP10ConnectionHeader(t *testing.T, mode testMode) {
                resp.Body.Close()
 
                got := resp.Header["Connection"]
-               if !reflect.DeepEqual(got, tt.expect) {
+               if !slices.Equal(got, tt.expect) {
                        t.Errorf("wrong Connection headers for request %q. Got %q expect %q", tt.req, got, tt.expect)
                }
        }
@@ -4329,7 +4330,7 @@ func testServerConnState(t *testing.T, mode testMode) {
 
                <-complete
                sl := <-activeLog
-               if !reflect.DeepEqual(sl.got, sl.want) {
+               if !slices.Equal(sl.got, sl.want) {
                        t.Errorf("Request(s) produced unexpected state sequence.\nGot:  %v\nWant: %v", sl.got, sl.want)
                }
                // Don't return sl to activeLog: we don't expect any further states after
@@ -4355,7 +4356,7 @@ func testServerConnState(t *testing.T, mode testMode) {
                                return
                        }
                        sl.got = append(sl.got, state)
-                       if sl.complete != nil && (len(sl.got) >= len(sl.want) || !reflect.DeepEqual(sl.got, sl.want[:len(sl.got)])) {
+                       if sl.complete != nil && (len(sl.got) >= len(sl.want) || !slices.Equal(sl.got, sl.want[:len(sl.got)])) {
                                close(sl.complete)
                                sl.complete = nil
                        }
index d6ef40905e6892dcd3f2e098f8dd99c1edec38a9..68c8a6af1e11fcc1e8030d6d764b1abd0e9cb571 100644 (file)
@@ -10,7 +10,7 @@ import (
        "io"
        "log"
        . "net/http"
-       "reflect"
+       "slices"
        "strconv"
        "strings"
        "testing"
@@ -144,7 +144,7 @@ func testServerIssue5953(t *testing.T, mode testMode) {
 
        got := resp.Header["Content-Type"]
        want := []string{""}
-       if !reflect.DeepEqual(got, want) {
+       if !slices.Equal(got, want) {
                t.Errorf("Content-Type = %q; want %q", got, want)
        }
        resp.Body.Close()
index ae7159dab00d399c4d440321e00b6af972c7ea1b..2389284249a9b0f5df460f43003e0296852f9b01 100644 (file)
@@ -36,6 +36,7 @@ import (
        "os"
        "reflect"
        "runtime"
+       "slices"
        "strconv"
        "strings"
        "sync"
@@ -4453,7 +4454,7 @@ func TestTransportFlushesBodyChunks(t *testing.T) {
                "5\r\nnum2\n\r\n",
                "0\r\n\r\n",
        }
-       if !reflect.DeepEqual(lw.writes, want) {
+       if !slices.Equal(lw.writes, want) {
                t.Errorf("Writes differed.\n Got: %q\nWant: %q\n", lw.writes, want)
        }
 }
@@ -5284,7 +5285,7 @@ func testTransportMaxIdleConns(t *testing.T, mode testMode) {
                "|http|host-2.dns-is-faked.golang:" + port,
                "|http|host-3.dns-is-faked.golang:" + port,
        }
-       if got := tr.IdleConnKeysForTesting(); !reflect.DeepEqual(got, want) {
+       if got := tr.IdleConnKeysForTesting(); !slices.Equal(got, want) {
                t.Fatalf("idle conn keys mismatch.\n got: %q\nwant: %q\n", got, want)
        }
 
@@ -5296,7 +5297,7 @@ func testTransportMaxIdleConns(t *testing.T, mode testMode) {
                "|http|host-3.dns-is-faked.golang:" + port,
                "|http|host-4.dns-is-faked.golang:" + port,
        }
-       if got := tr.IdleConnKeysForTesting(); !reflect.DeepEqual(got, want) {
+       if got := tr.IdleConnKeysForTesting(); !slices.Equal(got, want) {
                t.Fatalf("idle conn keys mismatch after 5th host.\n got: %q\nwant: %q\n", got, want)
        }
 }
index 7052f3c9fc77e05ebd25caa00a7edd43d699d5bb..d106f98eef18a9242b056e3180aa19b6b26c7669 100644 (file)
@@ -433,7 +433,7 @@ func TestLookupLongTXT(t *testing.T) {
                strings.Repeat("abcdefghijklmnopqrstuvwxyABCDEFGHJIKLMNOPQRSTUVWXY", 10),
                "gophers rule",
        }
-       if !reflect.DeepEqual(txts, want) {
+       if !slices.Equal(txts, want) {
                t.Fatalf("LookupTXT golang.rsc.io incorrect\nhave %q\nwant %q", txts, want)
        }
 }
index 8f6e4b238f3979aba7ed8274921ff234696d86ea..4f287901855653f65570b826d541b887b5fe8f9c 100644 (file)
@@ -144,7 +144,7 @@ func TestNSLookupTXT(t *testing.T) {
                }
                slices.Sort(expected)
                slices.Sort(txt)
-               if !reflect.DeepEqual(expected, txt) {
+               if !slices.Equal(expected, txt) {
                        t.Errorf("different results %s:\texp:%v\tgot:%v", server, toJson(expected), toJson(txt))
                }
        })
@@ -170,7 +170,7 @@ func TestLookupLocalPTR(t *testing.T) {
        }
        slices.Sort(expected)
        slices.Sort(names)
-       if !reflect.DeepEqual(expected, names) {
+       if !slices.Equal(expected, names) {
                t.Errorf("different results %s:\texp:%v\tgot:%v", addr, toJson(expected), toJson(names))
        }
 }
@@ -201,7 +201,7 @@ func TestLookupPTR(t *testing.T) {
                }
                slices.Sort(expected)
                slices.Sort(names)
-               if !reflect.DeepEqual(expected, names) {
+               if !slices.Equal(expected, names) {
                        t.Errorf("different results %s:\texp:%v\tgot:%v", addr, toJson(expected), toJson(names))
                }
        }
index 012d51c3df1304cea105b17c85d24f3e1d021bff..dad9c367f3e4510eebba93afe87c3801466d7408 100644 (file)
@@ -9,6 +9,7 @@ import (
        "io"
        "mime"
        "reflect"
+       "slices"
        "strings"
        "testing"
        "time"
@@ -115,7 +116,7 @@ func headerEq(a, b Header) bool {
                if !ok {
                        return false
                }
-               if !reflect.DeepEqual(as, bs) {
+               if !slices.Equal(as, bs) {
                        return false
                }
        }
index 9b45cdbcb0f526b3345e15705af93a416b8538cd..3e8d77526619fe15aac0bfb4a73b713b902da4a3 100644 (file)
@@ -59,7 +59,7 @@ func TestResolverDialFunc(t *testing.T) {
                if err != nil {
                        t.Fatal(err)
                }
-               if got, want := sortedIPStrings(ips), []string{"0:200::e00", "1.2.3.4", "1::f", "5.6.7.8"}; !reflect.DeepEqual(got, want) {
+               if got, want := sortedIPStrings(ips), []string{"0:200::e00", "1.2.3.4", "1::f", "5.6.7.8"}; !slices.Equal(got, want) {
                        t.Errorf("LookupIP wrong.\n got: %q\nwant: %q\n", got, want)
                }
        })
index f794879bd739fc14288ce36a39b9d56d4fb2ff2d..d510f9b338673f1406b83952d1679aa88dc9bb2f 100644 (file)
@@ -11,6 +11,7 @@ import (
        "net"
        "reflect"
        "runtime"
+       "slices"
        "strings"
        "sync"
        "testing"
@@ -95,13 +96,13 @@ func TestReadDotLines(t *testing.T) {
        r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanother\n")
        s, err := r.ReadDotLines()
        want := []string{"dotlines", "foo", ".bar", "..baz", "quux", ""}
-       if !reflect.DeepEqual(s, want) || err != nil {
+       if !slices.Equal(s, want) || err != nil {
                t.Fatalf("ReadDotLines: %v, %v", s, err)
        }
 
        s, err = r.ReadDotLines()
        want = []string{"another"}
-       if !reflect.DeepEqual(s, want) || err != io.ErrUnexpectedEOF {
+       if !slices.Equal(s, want) || err != io.ErrUnexpectedEOF {
                t.Fatalf("ReadDotLines2: %v, %v", s, err)
        }
 }
@@ -110,13 +111,13 @@ func TestReadDotBytes(t *testing.T) {
        r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n")
        b, err := r.ReadDotBytes()
        want := []byte("dotlines\nfoo\n.bar\n..baz\nquux\n\n")
-       if !reflect.DeepEqual(b, want) || err != nil {
+       if !slices.Equal(b, want) || err != nil {
                t.Fatalf("ReadDotBytes: %q, %v", b, err)
        }
 
        b, err = r.ReadDotBytes()
        want = []byte("anot.her\n")
-       if !reflect.DeepEqual(b, want) || err != io.ErrUnexpectedEOF {
+       if !slices.Equal(b, want) || err != io.ErrUnexpectedEOF {
                t.Fatalf("ReadDotBytes2: %q, %v", b, err)
        }
 }