From cf5c4c9cbf7bd379038d35caa965e471614866b1 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Sun, 12 May 2024 21:16:59 -0700 Subject: [PATCH] Replace `reflect.DeepEqual` with `[slices/maps].Equal`. They are faster on slice/map comparisons. PiperOrigin-RevId: 633080355 --- pkg/bitmap/bitmap_test.go | 12 +++--- pkg/bits/uint64_test.go | 4 +- pkg/bpf/interpreter_test.go | 3 +- pkg/bpf/optimizer_test.go | 4 +- pkg/buffer/buffer_test.go | 4 +- pkg/fspath/fspath_test.go | 4 +- pkg/gohacks/string_test.go | 4 +- pkg/metric/metric_test.go | 4 +- pkg/p9/p9test/client_test.go | 4 +- pkg/ring0/pagetables/pagetables_test.go | 3 +- .../example/usage/usage_test.go | 3 +- pkg/segment/test/segment_test.go | 8 ++-- pkg/sentry/fsimpl/cgroupfs/bitmap_test.go | 6 +-- pkg/sentry/fsutil/dirty_set_test.go | 4 +- pkg/sentry/memmap/mapping_set_test.go | 37 ++++++++++--------- pkg/tcpip/checker/checker.go | 8 ++-- pkg/tcpip/header/tcp_test.go | 3 +- pkg/tcpip/link/fdbased/endpoint_test.go | 4 +- pkg/tcpip/link/sharedmem/queue/queue_test.go | 8 ++-- .../transport/tcp/test/e2e/tcp_sack_test.go | 6 +-- pkg/unet/unet_test.go | 4 +- pkg/usermem/usermem_test.go | 6 +-- runsc/boot/mount_hints_test.go | 8 ++-- .../portforward/portforward_fd_rw_test.go | 4 +- .../portforward/portforward_hostinet_test.go | 6 +-- runsc/container/container_test.go | 3 +- test/iptables/iptables_test.go | 4 +- test/runtimes/proctor/lib/lib_test.go | 6 +-- 28 files changed, 90 insertions(+), 84 deletions(-) diff --git a/pkg/bitmap/bitmap_test.go b/pkg/bitmap/bitmap_test.go index 45b7f3508..082495aff 100644 --- a/pkg/bitmap/bitmap_test.go +++ b/pkg/bitmap/bitmap_test.go @@ -16,7 +16,7 @@ package bitmap import ( "math" - "reflect" + "slices" "testing" ) @@ -110,7 +110,7 @@ func TestRemove(t *testing.T) { bitmap.Remove(firstSlice[i]) } bitmapSlice := bitmap.ToSlice() - if !reflect.DeepEqual(bitmapSlice, secondSlice) { + if !slices.Equal(bitmapSlice, secondSlice) { t.Errorf("After Remove() firstSlice, remained slice: %v, wanted: %v", bitmapSlice, secondSlice) } @@ -119,7 +119,7 @@ func TestRemove(t *testing.T) { } bitmapSlice = bitmap.ToSlice() emptySlice := make([]uint32, 0) - if !reflect.DeepEqual(bitmapSlice, emptySlice) { + if !slices.Equal(bitmapSlice, emptySlice) { t.Errorf("After Remove secondSlice, remained slice: %v, wanted: %v", bitmapSlice, emptySlice) } @@ -153,7 +153,7 @@ func TestFlipRange(t *testing.T) { bitmap.FlipRange(uint32(tt.flipRangeMin), uint32(tt.flipRangeMax+1)) flipBitmapSlice := bitmap.ToSlice() - if !reflect.DeepEqual(flipFillSlice, flipBitmapSlice) { + if !slices.Equal(flipFillSlice, flipBitmapSlice) { t.Errorf("%v, flipped slice: %v, wanted: %v", tt.name, flipBitmapSlice, flipFillSlice) } }) @@ -186,7 +186,7 @@ func TestClearRange(t *testing.T) { clearedSlice = append(clearedSlice, uint32(i)) } } - if !reflect.DeepEqual(clearedSlice, clearedBitmapSlice) { + if !slices.Equal(clearedSlice, clearedBitmapSlice) { t.Errorf("%v, cleared slice: %v, wanted: %v", tt.name, clearedBitmapSlice, clearedSlice) } }) @@ -402,7 +402,7 @@ func TestGrow(t *testing.T) { want[i] = uint32(i) } } - if !reflect.DeepEqual(bitmap.ToSlice(), want) { + if !slices.Equal(bitmap.ToSlice(), want) { t.Errorf("Grow() got: %v, want: %v", bitmap.ToSlice(), want) } } diff --git a/pkg/bits/uint64_test.go b/pkg/bits/uint64_test.go index 193d1ebcd..725f48756 100644 --- a/pkg/bits/uint64_test.go +++ b/pkg/bits/uint64_test.go @@ -15,7 +15,7 @@ package bits import ( - "reflect" + "slices" "testing" ) @@ -82,7 +82,7 @@ func TestForEachSetBit64(t *testing.T) { ForEachSetBit64(n, func(i int) { got = append(got, i) }) - if !reflect.DeepEqual(got, want) { + if !slices.Equal(got, want) { t.Errorf("ForEachSetBit64(%#x): iterated bits %v, wanted %v", n, got, want) } } diff --git a/pkg/bpf/interpreter_test.go b/pkg/bpf/interpreter_test.go index 0887de734..1b679ba7a 100644 --- a/pkg/bpf/interpreter_test.go +++ b/pkg/bpf/interpreter_test.go @@ -16,6 +16,7 @@ package bpf import ( "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/abi/linux" @@ -886,7 +887,7 @@ func TestValidInstructions(t *testing.T) { if retOptimized.ReturnValue != retFast { t.Fatalf("expected return value from optimized version: got %d, non-optimized execution returned %d", retOptimized.ReturnValue, retFast) } - if !reflect.DeepEqual(retOptimized.InputAccessed, execution.InputAccessed) { + if !slices.Equal(retOptimized.InputAccessed, execution.InputAccessed) { t.Fatalf("expected input read coverage from optimized version: got %s, non-optimized execution was %s", retOptimized.String(), execution.String()) } }) diff --git a/pkg/bpf/optimizer_test.go b/pkg/bpf/optimizer_test.go index 9e480a732..c9d03240b 100644 --- a/pkg/bpf/optimizer_test.go +++ b/pkg/bpf/optimizer_test.go @@ -15,7 +15,7 @@ package bpf import ( - "reflect" + "slices" "strings" "testing" ) @@ -301,7 +301,7 @@ func TestOptimize(t *testing.T) { } else { optimizedInsns = Optimize(optimizedInsns) } - if !reflect.DeepEqual(optimizedInsns, test.want) { + if !slices.Equal(optimizedInsns, test.want) { t.Errorf("got optimized instructions:\n%v\nwant:\n%v\n", prettyInstructions(optimizedInsns), prettyInstructions(test.want)) } }) diff --git a/pkg/buffer/buffer_test.go b/pkg/buffer/buffer_test.go index baa39fb71..e8f18481f 100644 --- a/pkg/buffer/buffer_test.go +++ b/pkg/buffer/buffer_test.go @@ -20,7 +20,7 @@ import ( "fmt" "io" "math/rand" - "reflect" + "slices" "strings" "testing" @@ -625,7 +625,7 @@ func TestBufferPullUp(t *testing.T) { for v := b.data.Front(); v != nil; v = v.Next() { gotLengths = append(gotLengths, v.Size()) } - if !reflect.DeepEqual(gotLengths, tc.lengths) { + if !slices.Equal(gotLengths, tc.lengths) { t.Errorf("lengths = %v; want %v", gotLengths, tc.lengths) } }) diff --git a/pkg/fspath/fspath_test.go b/pkg/fspath/fspath_test.go index d5e9a549a..cc95068e0 100644 --- a/pkg/fspath/fspath_test.go +++ b/pkg/fspath/fspath_test.go @@ -15,7 +15,7 @@ package fspath import ( - "reflect" + "slices" "strings" "testing" ) @@ -126,7 +126,7 @@ func TestParse(t *testing.T) { for pit := p.Begin; pit.Ok(); pit = pit.Next() { pcs = append(pcs, pit.String()) } - if !reflect.DeepEqual(pcs, test.relpath) { + if !slices.Equal(pcs, test.relpath) { t.Errorf("relative path: got %v, wanted %v", pcs, test.relpath) } }) diff --git a/pkg/gohacks/string_test.go b/pkg/gohacks/string_test.go index 8c1c79346..3036108d4 100644 --- a/pkg/gohacks/string_test.go +++ b/pkg/gohacks/string_test.go @@ -15,7 +15,7 @@ package gohacks import ( - "reflect" + "slices" "testing" ) @@ -45,7 +45,7 @@ func TestImmutableBytesFromString(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := ImmutableBytesFromString(tc.input) - if !reflect.DeepEqual(got, tc.want) { + if !slices.Equal(got, tc.want) { t.Errorf("got contents %v (len %d cap %d) want %v (len %d cap %d)", got, len(got), cap(got), tc.want, len(tc.want), cap(tc.want)) } }) diff --git a/pkg/metric/metric_test.go b/pkg/metric/metric_test.go index 0bbdf1ca8..eb5c3846e 100644 --- a/pkg/metric/metric_test.go +++ b/pkg/metric/metric_test.go @@ -21,7 +21,7 @@ import ( "hash/adler32" "math" "os" - "reflect" + "slices" "strconv" "strings" "testing" @@ -729,7 +729,7 @@ func TestTimerMetric(t *testing.T) { } m := emitter[0].(*pb.MetricUpdate).Metrics[0] wantFields := []string{"foo", "quux"} - if !reflect.DeepEqual(m.GetFieldValues(), wantFields) { + if !slices.Equal(m.GetFieldValues(), wantFields) { t.Errorf("%+v: got fields %v want %v", m, m.GetFieldValues(), wantFields) } dv, ok := m.Value.(*pb.MetricValue_DistributionValue) diff --git a/pkg/p9/p9test/client_test.go b/pkg/p9/p9test/client_test.go index aca16cb17..79fdb4866 100644 --- a/pkg/p9/p9test/client_test.go +++ b/pkg/p9/p9test/client_test.go @@ -20,7 +20,7 @@ import ( "io" "math/rand" "os" - "reflect" + "slices" "strings" "testing" "time" @@ -633,7 +633,7 @@ func renameHelper(h *Harness, root p9.File, srcNames []string, dstNames []string // renameSrcPath here? If yes, then this is a mismatch. // We can't rename the src to some subpath of itself. if len(renameDestPath) > len(renameSrcPath) && - reflect.DeepEqual(renameDestPath[:len(renameSrcPath)], renameSrcPath) { + slices.Equal(renameDestPath[:len(renameSrcPath)], renameSrcPath) { renameDestPath = nil renameSrcPath = nil continue diff --git a/pkg/ring0/pagetables/pagetables_test.go b/pkg/ring0/pagetables/pagetables_test.go index df93dcb6a..4efc6ce4c 100644 --- a/pkg/ring0/pagetables/pagetables_test.go +++ b/pkg/ring0/pagetables/pagetables_test.go @@ -15,8 +15,9 @@ package pagetables import ( - "gvisor.dev/gvisor/pkg/hostarch" "testing" + + "gvisor.dev/gvisor/pkg/hostarch" ) type mapping struct { diff --git a/pkg/seccomp/precompiledseccomp/example/usage/usage_test.go b/pkg/seccomp/precompiledseccomp/example/usage/usage_test.go index deee57830..7fdd4ff92 100644 --- a/pkg/seccomp/precompiledseccomp/example/usage/usage_test.go +++ b/pkg/seccomp/precompiledseccomp/example/usage/usage_test.go @@ -17,6 +17,7 @@ package usage import ( "math/rand" "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/bpf" @@ -29,7 +30,7 @@ import ( // match byte-for-byte. If not, it prints them side-by-side. func comparePrograms(t *testing.T, precompiled, freshlyCompiled []bpf.Instruction) { t.Helper() - if !reflect.DeepEqual(precompiled, freshlyCompiled) { + if !slices.Equal(precompiled, freshlyCompiled) { t.Error("Precompiled and freshly-compiled versions of the program do not match:") t.Errorf(" Offset | %-32s | %-32s", "Freshly-compiled", "Compiled") for i := 0; i < max(len(precompiled), len(freshlyCompiled)); i++ { diff --git a/pkg/segment/test/segment_test.go b/pkg/segment/test/segment_test.go index 450b5b828..32d54a4f4 100644 --- a/pkg/segment/test/segment_test.go +++ b/pkg/segment/test/segment_test.go @@ -17,7 +17,7 @@ package segment import ( "fmt" "math/rand" - "reflect" + "slices" "testing" ) @@ -343,7 +343,7 @@ func TestNextLargeEnoughGap(t *testing.T) { } } - if !reflect.DeepEqual(gapArr2, gapArr1) { + if !slices.Equal(gapArr2, gapArr1) { t.Errorf("Search result not correct, got: %v, wanted: %v", gapArr1, gapArr2) } if t.Failed() { @@ -393,7 +393,7 @@ func TestPrevLargeEnoughGap(t *testing.T) { gapArr2 = append(gapArr2, gap.Range().Start) } } - if !reflect.DeepEqual(gapArr2, gapArr1) { + if !slices.Equal(gapArr2, gapArr1) { t.Errorf("Search result not correct, got: %v, wanted: %v", gapArr1, gapArr2) } if t.Failed() { @@ -648,7 +648,7 @@ func TestMutateRange(t *testing.T) { (*seg.ValuePtr())++ return true }) - if got := s.ExportSlice(); !reflect.DeepEqual(got, test.final) { + if got := s.ExportSlice(); !slices.Equal(got, test.final) { t.Errorf("Set mismatch after mutation: got %v, wanted %v", got, test.final) } }) diff --git a/pkg/sentry/fsimpl/cgroupfs/bitmap_test.go b/pkg/sentry/fsimpl/cgroupfs/bitmap_test.go index 5cc56de3b..389329a6a 100644 --- a/pkg/sentry/fsimpl/cgroupfs/bitmap_test.go +++ b/pkg/sentry/fsimpl/cgroupfs/bitmap_test.go @@ -16,7 +16,7 @@ package cgroupfs import ( "fmt" - "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/bitmap" @@ -48,7 +48,7 @@ func TestFormat(t *testing.T) { if err != nil { t.Fatalf("Failed to parse formatted bitmap: %v", err) } - if got, want := b1.ToSlice(), b.ToSlice(); !reflect.DeepEqual(got, want) { + if got, want := b1.ToSlice(), b.ToSlice(); !slices.Equal(got, want) { t.Errorf("Parsing formatted output doesn't result in the original bitmap. Got %v, want %v", got, want) } }) @@ -90,7 +90,7 @@ func TestParse(t *testing.T) { } got := b.ToSlice() - if !reflect.DeepEqual(got, tt.output) { + if !slices.Equal(got, tt.output) { t.Errorf("Parsed bitmap doesn't match what we expected. Got %v, want %v", got, tt.output) } diff --git a/pkg/sentry/fsutil/dirty_set_test.go b/pkg/sentry/fsutil/dirty_set_test.go index 1a1ebe880..bcd0720e8 100644 --- a/pkg/sentry/fsutil/dirty_set_test.go +++ b/pkg/sentry/fsutil/dirty_set_test.go @@ -15,7 +15,7 @@ package fsutil import ( - "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/hostarch" @@ -30,7 +30,7 @@ func TestDirtySet(t *testing.T) { want := []DirtyFlatSegment{ {hostarch.PageSize, 2 * hostarch.PageSize, DirtyInfo{Keep: true}}, } - if got := set.ExportSlice(); !reflect.DeepEqual(got, want) { + if got := set.ExportSlice(); !slices.Equal(got, want) { t.Errorf("set:\n\tgot %v,\n\twant %v", got, want) } } diff --git a/pkg/sentry/memmap/mapping_set_test.go b/pkg/sentry/memmap/mapping_set_test.go index 5cb81fde7..6e7fa78be 100644 --- a/pkg/sentry/memmap/mapping_set_test.go +++ b/pkg/sentry/memmap/mapping_set_test.go @@ -15,9 +15,10 @@ package memmap import ( - "gvisor.dev/gvisor/pkg/hostarch" - "reflect" + "slices" "testing" + + "gvisor.dev/gvisor/pkg/hostarch" ) type testMappingSpace struct { @@ -40,7 +41,7 @@ func TestAddRemoveMapping(t *testing.T) { ms := &testMappingSpace{} mapped := set.AddMapping(ms, hostarch.AddrRange{0x10000, 0x12000}, 0x1000, true) - if got, want := mapped, []MappableRange{{0x1000, 0x3000}}; !reflect.DeepEqual(got, want) { + if got, want := mapped, []MappableRange{{0x1000, 0x3000}}; !slices.Equal(got, want) { t.Errorf("AddMapping: got %+v, wanted %+v", got, want) } @@ -59,7 +60,7 @@ func TestAddRemoveMapping(t *testing.T) { t.Log(&set) mapped = set.AddMapping(ms, hostarch.AddrRange{0x30000, 0x31000}, 0x4000, true) - if got, want := mapped, []MappableRange{{0x4000, 0x5000}}; !reflect.DeepEqual(got, want) { + if got, want := mapped, []MappableRange{{0x4000, 0x5000}}; !slices.Equal(got, want) { t.Errorf("AddMapping: got %+v, wanted %+v", got, want) } @@ -70,7 +71,7 @@ func TestAddRemoveMapping(t *testing.T) { t.Log(&set) mapped = set.AddMapping(ms, hostarch.AddrRange{0x12000, 0x15000}, 0x3000, true) - if got, want := mapped, []MappableRange{{0x3000, 0x4000}, {0x5000, 0x6000}}; !reflect.DeepEqual(got, want) { + if got, want := mapped, []MappableRange{{0x3000, 0x4000}, {0x5000, 0x6000}}; !slices.Equal(got, want) { t.Errorf("AddMapping: got %+v, wanted %+v", got, want) } @@ -83,7 +84,7 @@ func TestAddRemoveMapping(t *testing.T) { t.Log(&set) unmapped := set.RemoveMapping(ms, hostarch.AddrRange{0x10000, 0x11000}, 0x1000, true) - if got, want := unmapped, []MappableRange{{0x1000, 0x2000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x1000, 0x2000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } @@ -106,7 +107,7 @@ func TestAddRemoveMapping(t *testing.T) { t.Log(&set) unmapped = set.RemoveMapping(ms, hostarch.AddrRange{0x11000, 0x15000}, 0x2000, true) - if got, want := unmapped, []MappableRange{{0x2000, 0x4000}, {0x5000, 0x6000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x2000, 0x4000}, {0x5000, 0x6000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } @@ -115,7 +116,7 @@ func TestAddRemoveMapping(t *testing.T) { t.Log(&set) unmapped = set.RemoveMapping(ms, hostarch.AddrRange{0x30000, 0x31000}, 0x4000, true) - if got, want := unmapped, []MappableRange{{0x4000, 0x5000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x4000, 0x5000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } } @@ -129,7 +130,7 @@ func TestInvalidateWholeMapping(t *testing.T) { // [0x10000, 0x11000) => [0, 0x1000) t.Log(&set) set.Invalidate(MappableRange{0, 0x1000}, InvalidateOpts{}) - if got, want := ms.inv, []hostarch.AddrRange{{0x10000, 0x11000}}; !reflect.DeepEqual(got, want) { + if got, want := ms.inv, []hostarch.AddrRange{{Start: 0x10000, End: 0x11000}}; !slices.Equal(got, want) { t.Errorf("Invalidate: got %+v, wanted %+v", got, want) } } @@ -143,7 +144,7 @@ func TestInvalidatePartialMapping(t *testing.T) { // [0x10000, 0x13000) => [0, 0x3000) t.Log(&set) set.Invalidate(MappableRange{0x1000, 0x2000}, InvalidateOpts{}) - if got, want := ms.inv, []hostarch.AddrRange{{0x11000, 0x12000}}; !reflect.DeepEqual(got, want) { + if got, want := ms.inv, []hostarch.AddrRange{{Start: 0x11000, End: 0x12000}}; !slices.Equal(got, want) { t.Errorf("Invalidate: got %+v, wanted %+v", got, want) } } @@ -159,7 +160,7 @@ func TestInvalidateMultipleMappings(t *testing.T) { // [0x12000, 0x13000) => [0x2000, 0x3000) t.Log(&set) set.Invalidate(MappableRange{0, 0x3000}, InvalidateOpts{}) - if got, want := ms.inv, []hostarch.AddrRange{{0x10000, 0x11000}, {0x20000, 0x21000}}; !reflect.DeepEqual(got, want) { + if got, want := ms.inv, []hostarch.AddrRange{{Start: 0x10000, End: 0x11000}, {Start: 0x20000, End: 0x21000}}; !slices.Equal(got, want) { t.Errorf("Invalidate: got %+v, wanted %+v", got, want) } } @@ -176,10 +177,10 @@ func TestInvalidateOverlappingMappings(t *testing.T) { // ms2:[0x11000, 0x13000) => [0x1000, 0x3000) t.Log(&set) set.Invalidate(MappableRange{0x1000, 0x2000}, InvalidateOpts{}) - if got, want := ms1.inv, []hostarch.AddrRange{{0x11000, 0x12000}}; !reflect.DeepEqual(got, want) { + if got, want := ms1.inv, []hostarch.AddrRange{{Start: 0x11000, End: 0x12000}}; !slices.Equal(got, want) { t.Errorf("Invalidate: ms1: got %+v, wanted %+v", got, want) } - if got, want := ms2.inv, []hostarch.AddrRange{{0x20000, 0x21000}}; !reflect.DeepEqual(got, want) { + if got, want := ms2.inv, []hostarch.AddrRange{{Start: 0x20000, End: 0x21000}}; !slices.Equal(got, want) { t.Errorf("Invalidate: ms1: got %+v, wanted %+v", got, want) } } @@ -189,7 +190,7 @@ func TestMixedWritableMappings(t *testing.T) { ms := &testMappingSpace{} mapped := set.AddMapping(ms, hostarch.AddrRange{0x10000, 0x12000}, 0x1000, true) - if got, want := mapped, []MappableRange{{0x1000, 0x3000}}; !reflect.DeepEqual(got, want) { + if got, want := mapped, []MappableRange{{0x1000, 0x3000}}; !slices.Equal(got, want) { t.Errorf("AddMapping: got %+v, wanted %+v", got, want) } @@ -198,7 +199,7 @@ func TestMixedWritableMappings(t *testing.T) { t.Log(&set) mapped = set.AddMapping(ms, hostarch.AddrRange{0x20000, 0x22000}, 0x2000, false) - if got, want := mapped, []MappableRange{{0x3000, 0x4000}}; !reflect.DeepEqual(got, want) { + if got, want := mapped, []MappableRange{{0x3000, 0x4000}}; !slices.Equal(got, want) { t.Errorf("AddMapping: got %+v, wanted %+v", got, want) } @@ -228,7 +229,7 @@ func TestMixedWritableMappings(t *testing.T) { t.Log(&set) unmapped = set.RemoveMapping(ms, hostarch.AddrRange{0x11000, 0x12000}, 0x2000, true) - if got, want := unmapped, []MappableRange{{0x2000, 0x3000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x2000, 0x3000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } @@ -244,7 +245,7 @@ func TestMixedWritableMappings(t *testing.T) { } unmapped = set.RemoveMapping(ms, hostarch.AddrRange{0x10000, 0x12000}, 0x1000, true) - if got, want := unmapped, []MappableRange{{0x1000, 0x2000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x1000, 0x2000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } @@ -253,7 +254,7 @@ func TestMixedWritableMappings(t *testing.T) { t.Log(&set) unmapped = set.RemoveMapping(ms, hostarch.AddrRange{0x21000, 0x22000}, 0x3000, false) - if got, want := unmapped, []MappableRange{{0x3000, 0x4000}}; !reflect.DeepEqual(got, want) { + if got, want := unmapped, []MappableRange{{0x3000, 0x4000}}; !slices.Equal(got, want) { t.Errorf("RemoveMapping: got %+v, wanted %+v", got, want) } } diff --git a/pkg/tcpip/checker/checker.go b/pkg/tcpip/checker/checker.go index 7eaf8ad63..a9355def2 100644 --- a/pkg/tcpip/checker/checker.go +++ b/pkg/tcpip/checker/checker.go @@ -18,7 +18,7 @@ package checker import ( "encoding/binary" - "reflect" + "slices" "testing" "time" @@ -463,7 +463,7 @@ func Raw(want []byte) NetworkChecker { return func(t *testing.T, h []header.Network) { t.Helper() - if got := h[len(h)-1].Payload(); !reflect.DeepEqual(got, want) { + if got := h[len(h)-1].Payload(); !slices.Equal(got, want) { t.Errorf("Wrong payload, got %v, want %v", got, want) } } @@ -917,7 +917,7 @@ func TCPSACKBlockChecker(sackBlocks []header.SACKBlock) TransportChecker { } } - if !reflect.DeepEqual(gotSACKBlocks, sackBlocks) { + if !slices.Equal(gotSACKBlocks, sackBlocks) { t.Errorf("SACKBlocks are not equal, got = %v, want = %v", gotSACKBlocks, sackBlocks) } } @@ -928,7 +928,7 @@ func Payload(want []byte) TransportChecker { return func(t *testing.T, h header.Transport) { t.Helper() - if got := h.Payload(); !reflect.DeepEqual(got, want) { + if got := h.Payload(); !slices.Equal(got, want) { t.Errorf("Wrong payload, got %v, want %v", got, want) } } diff --git a/pkg/tcpip/header/tcp_test.go b/pkg/tcpip/header/tcp_test.go index 4e9a78615..5c8347c5f 100644 --- a/pkg/tcpip/header/tcp_test.go +++ b/pkg/tcpip/header/tcp_test.go @@ -16,6 +16,7 @@ package header_test import ( "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -63,7 +64,7 @@ func TestEncodeSACKBlocks(t *testing.T) { t.Logf("testing: %v", tc) header.EncodeSACKBlocks(tc.sackBlocks, b) opts := header.ParseTCPOptions(b) - if got, want := opts.SACKBlocks, tc.want; !reflect.DeepEqual(got, want) { + if got, want := opts.SACKBlocks, tc.want; !slices.Equal(got, want) { t.Errorf("header.EncodeSACKBlocks(%v, %v), encoded blocks got: %v, want: %v", tc.sackBlocks, b, got, want) } } diff --git a/pkg/tcpip/link/fdbased/endpoint_test.go b/pkg/tcpip/link/fdbased/endpoint_test.go index 353bf4c29..43e205da7 100644 --- a/pkg/tcpip/link/fdbased/endpoint_test.go +++ b/pkg/tcpip/link/fdbased/endpoint_test.go @@ -22,7 +22,7 @@ import ( "fmt" "math/rand" "os" - "reflect" + "slices" "testing" "time" "unsafe" @@ -506,7 +506,7 @@ func TestIovecBuffer(t *testing.T) { buf.Apply(func(v *buffer.View) { lengths = append(lengths, v.Size()) }) - if !reflect.DeepEqual(lengths, c.wantLengths) { + if !slices.Equal(lengths, c.wantLengths) { t.Errorf("Pulled view lengths = %v, want %v", lengths, c.wantLengths) } diff --git a/pkg/tcpip/link/sharedmem/queue/queue_test.go b/pkg/tcpip/link/sharedmem/queue/queue_test.go index 2ee2175c0..15f25a1e5 100644 --- a/pkg/tcpip/link/sharedmem/queue/queue_test.go +++ b/pkg/tcpip/link/sharedmem/queue/queue_test.go @@ -16,7 +16,7 @@ package queue import ( "encoding/binary" - "reflect" + "slices" "testing" "gvisor.dev/gvisor/pkg/atomicbitops" @@ -69,7 +69,7 @@ func TestBasicTxQueue(t *testing.T) { 40, 0, 0, 0, // size 2 } - if !reflect.DeepEqual(want, d) { + if !slices.Equal(want, d) { t.Fatalf("Bad posted packet: got %v, want %v", d, want) } @@ -147,7 +147,7 @@ func TestBasicRxQueue(t *testing.T) { t.Fatalf("Tx pipe is empty after PostBuffers") } - if !reflect.DeepEqual(want[i], d) { + if !slices.Equal(want[i], d) { t.Fatalf("Bad posted packet: got %v, want %v", d, want[i]) } @@ -188,7 +188,7 @@ func TestBasicRxQueue(t *testing.T) { t.Fatalf("Bad packet size: got %v, want %v", n, 100) } - if !reflect.DeepEqual(bufs, b) { + if !slices.Equal(bufs, b) { t.Fatalf("Bad returned buffers: got %v, want %v", bufs, b) } } diff --git a/pkg/tcpip/transport/tcp/test/e2e/tcp_sack_test.go b/pkg/tcpip/transport/tcp/test/e2e/tcp_sack_test.go index ab2f706f3..e010160d1 100644 --- a/pkg/tcpip/transport/tcp/test/e2e/tcp_sack_test.go +++ b/pkg/tcpip/transport/tcp/test/e2e/tcp_sack_test.go @@ -19,7 +19,7 @@ import ( "fmt" "log" "os" - "reflect" + "slices" "testing" "time" @@ -320,7 +320,7 @@ func TestUpdateSACKBlocks(t *testing.T) { copy(sack.Blocks[:], tc.sackBlocks) sack.NumBlocks = len(tc.sackBlocks) tcp.UpdateSACKBlocks(&sack, tc.segStart, tc.segEnd, tc.rcvNxt) - if got, want := sack.Blocks[:sack.NumBlocks], tc.updated; !reflect.DeepEqual(got, want) { + if got, want := sack.Blocks[:sack.NumBlocks], tc.updated; !slices.Equal(got, want) { t.Errorf("UpdateSACKBlocks(%v, %v, %v, %v), got: %v, want: %v", tc.sackBlocks, tc.segStart, tc.segEnd, tc.rcvNxt, got, want) } @@ -349,7 +349,7 @@ func TestTrimSackBlockList(t *testing.T) { copy(sack.Blocks[:], tc.sackBlocks) sack.NumBlocks = len(tc.sackBlocks) tcp.TrimSACKBlockList(&sack, tc.rcvNxt) - if got, want := sack.Blocks[:sack.NumBlocks], tc.trimmed; !reflect.DeepEqual(got, want) { + if got, want := sack.Blocks[:sack.NumBlocks], tc.trimmed; !slices.Equal(got, want) { t.Errorf("TrimSackBlockList(%v, %v), got: %v, want: %v", tc.sackBlocks, tc.rcvNxt, got, want) } } diff --git a/pkg/unet/unet_test.go b/pkg/unet/unet_test.go index a39cef99d..4586e78d5 100644 --- a/pkg/unet/unet_test.go +++ b/pkg/unet/unet_test.go @@ -18,7 +18,7 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" + "slices" "testing" "time" @@ -660,7 +660,7 @@ func TestControlMessage(t *testing.T) { cm.EnableFDs(i) cm.PackFDs(want...) got, err := cm.ExtractFDs() - if err != nil || !reflect.DeepEqual(got, want) { + if err != nil || !slices.Equal(got, want) { t.Errorf("cm.ExtractFDs() = %v, %v, want = %v, %v", got, err, want, nil) } } diff --git a/pkg/usermem/usermem_test.go b/pkg/usermem/usermem_test.go index a5e2fe69e..3d244ea61 100644 --- a/pkg/usermem/usermem_test.go +++ b/pkg/usermem/usermem_test.go @@ -17,7 +17,7 @@ package usermem import ( "bytes" "fmt" - "reflect" + "slices" "strings" "testing" @@ -259,7 +259,7 @@ func TestCopyInt32StringsInVec(t *testing.T) { if n, err := CopyInt32StringsInVec(newContext(), src.IO, src.Addrs, dsts, src.Opts); n != int64(test.n) || err != nil { t.Errorf("CopyInt32StringsInVec: got (%d, %v), wanted (%d, nil)", n, err, test.n) } - if !reflect.DeepEqual(dsts, test.final) { + if !slices.Equal(dsts, test.final) { t.Errorf("dsts: got %v, wanted %v", dsts, test.final) } }) @@ -275,7 +275,7 @@ func TestCopyInt32StringsInVecRequiresOneValidValue(t *testing.T) { if n, err := CopyInt32StringsInVec(newContext(), src.IO, src.Addrs, dsts, src.Opts); !linuxerr.Equals(linuxerr.EINVAL, err) { t.Errorf("CopyInt32StringsInVec: got (%d, %v), wanted (_, %v)", n, err, linuxerr.EINVAL) } - if !reflect.DeepEqual(dsts, initial) { + if !slices.Equal(dsts, initial) { t.Errorf("dsts: got %v, wanted %v", dsts, initial) } }) diff --git a/runsc/boot/mount_hints_test.go b/runsc/boot/mount_hints_test.go index b469d5c54..da8028c55 100644 --- a/runsc/boot/mount_hints_test.go +++ b/runsc/boot/mount_hints_test.go @@ -15,7 +15,7 @@ package boot import ( - "reflect" + "slices" "strings" "testing" @@ -56,7 +56,7 @@ func TestPodMountHintsHappy(t *testing.T) { if want := pod; want != mount1.Share { t.Errorf("mount1 type, want: %q, got: %q", want, mount1.Share) } - if want := []string(nil); !reflect.DeepEqual(want, mount1.Mount.Options) { + if want := []string(nil); !slices.Equal(want, mount1.Mount.Options) { t.Errorf("mount1 type, want: %q, got: %q", want, mount1.Mount.Options) } @@ -73,7 +73,7 @@ func TestPodMountHintsHappy(t *testing.T) { if want := container; want != mount2.Share { t.Errorf("mount2 type, want: %q, got: %q", want, mount2.Share) } - if want := []string{"rw", "private"}; !reflect.DeepEqual(want, mount2.Mount.Options) { + if want := []string{"rw", "private"}; !slices.Equal(want, mount2.Mount.Options) { t.Errorf("mount2 type, want: %q, got: %q", want, mount2.Mount.Options) } } @@ -185,7 +185,7 @@ func TestIgnoreInvalidMountOptions(t *testing.T) { t.Fatalf("newPodMountHints failed: %v", err) } mount1 := podHints.Mounts["mount1"] - if want := []string{"rw", "noexec"}; !reflect.DeepEqual(want, mount1.Mount.Options) { + if want := []string{"rw", "noexec"}; !slices.Equal(want, mount1.Mount.Options) { t.Errorf("mount2 type, want: %q, got: %q", want, mount1.Mount.Options) } } diff --git a/runsc/boot/portforward/portforward_fd_rw_test.go b/runsc/boot/portforward/portforward_fd_rw_test.go index 0e665375f..741049fc2 100644 --- a/runsc/boot/portforward/portforward_fd_rw_test.go +++ b/runsc/boot/portforward/portforward_fd_rw_test.go @@ -18,7 +18,7 @@ import ( "bytes" "fmt" "io" - "reflect" + "slices" "sync" "testing" "time" @@ -320,7 +320,7 @@ func TestReaderWriter(t *testing.T) { want = append(want, buf...) } - if !reflect.DeepEqual(got, want) { + if !slices.Equal(got, want) { t.Fatalf("mismatch types: got: %q want: %q", string(got), string(want)) } diff --git a/runsc/boot/portforward/portforward_hostinet_test.go b/runsc/boot/portforward/portforward_hostinet_test.go index cb87552e9..831124523 100644 --- a/runsc/boot/portforward/portforward_hostinet_test.go +++ b/runsc/boot/portforward/portforward_hostinet_test.go @@ -17,7 +17,7 @@ package portforward import ( "fmt" "net" - "reflect" + "slices" "strings" "sync" "testing" @@ -63,7 +63,7 @@ func TestLocalHostSocket(t *testing.T) { return fmt.Errorf("could not read data: %v", err) } - if !reflect.DeepEqual(data[:recLen], clientData) { + if !slices.Equal(data[:recLen], clientData) { return fmt.Errorf("server mismatch data recieved: got: %s want: %s", data[:recLen], clientData) } @@ -102,7 +102,7 @@ func TestLocalHostSocket(t *testing.T) { dataLen += n } - if !reflect.DeepEqual(data[:dataLen], serverData) { + if !slices.Equal(data[:dataLen], serverData) { return fmt.Errorf("server mismatch data received: got: %s want: %s", data[:dataLen], clientData) } return nil diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 8ef0c9517..b062ad1ea 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -27,6 +27,7 @@ import ( "path/filepath" "reflect" "runtime" + "slices" "strconv" "strings" "testing" @@ -502,7 +503,7 @@ func TestLifecycle(t *testing.T) { SandboxID: args.ID, ContainerID: args.ID, } - if got, want := ids, []FullID{fullID}; !reflect.DeepEqual(got, want) { + if got, want := ids, []FullID{fullID}; !slices.Equal(got, want) { t.Errorf("container list got %v, want %v", got, want) } diff --git a/test/iptables/iptables_test.go b/test/iptables/iptables_test.go index e5a2162a4..2d36fc5c0 100644 --- a/test/iptables/iptables_test.go +++ b/test/iptables/iptables_test.go @@ -19,7 +19,7 @@ import ( "errors" "fmt" "net" - "reflect" + "slices" "sync" "testing" @@ -469,7 +469,7 @@ func TestFilterAddrs(t *testing.T) { } for _, tc := range tcs { - if got := filterAddrs(tc.addrs, tc.ipv6); !reflect.DeepEqual(got, tc.want) { + if got := filterAddrs(tc.addrs, tc.ipv6); !slices.Equal(got, tc.want) { t.Errorf("%v with IPv6 %t: got %v, but wanted %v", tc.addrs, tc.ipv6, got, tc.want) } } diff --git a/test/runtimes/proctor/lib/lib_test.go b/test/runtimes/proctor/lib/lib_test.go index 1193d2e28..83b34938c 100644 --- a/test/runtimes/proctor/lib/lib_test.go +++ b/test/runtimes/proctor/lib/lib_test.go @@ -18,8 +18,8 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "regexp" + "slices" "strings" "testing" @@ -52,7 +52,7 @@ func TestSearchEmptyDir(t *testing.T) { t.Errorf("search error: %v", err) } - if !reflect.DeepEqual(got, want) { + if !slices.Equal(got, want) { t.Errorf("Found %#v; want %#v", got, want) } } @@ -121,7 +121,7 @@ func TestSearch(t *testing.T) { t.Errorf("search error: %v", err) } - if !reflect.DeepEqual(got, want) { + if !slices.Equal(got, want) { t.Errorf("Found %#v; want %#v", got, want) } }