2020-06-30 02:36:45 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-05-11 19:09:25 +00:00
|
|
|
package deephash
|
2020-06-28 17:58:21 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-21 17:26:04 +00:00
|
|
|
"archive/tar"
|
2021-05-11 20:17:12 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-08-30 17:47:21 +00:00
|
|
|
"crypto/sha256"
|
2021-05-11 20:17:12 +00:00
|
|
|
"fmt"
|
2021-08-03 04:44:13 +00:00
|
|
|
"math"
|
2021-05-11 20:17:12 +00:00
|
|
|
"reflect"
|
2021-08-30 17:47:21 +00:00
|
|
|
"runtime"
|
2020-06-28 17:58:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-02 21:41:56 +00:00
|
|
|
"go4.org/mem"
|
2020-06-28 17:58:21 +00:00
|
|
|
"inet.af/netaddr"
|
2021-05-11 16:45:12 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-08-05 21:05:24 +00:00
|
|
|
"tailscale.com/types/dnstype"
|
2021-07-07 18:58:02 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2021-10-28 00:42:33 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-05-11 16:45:12 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2021-07-07 18:40:28 +00:00
|
|
|
"tailscale.com/version"
|
2021-07-07 18:58:02 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-03-26 00:41:51 +00:00
|
|
|
"tailscale.com/wgengine/router"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2020-06-28 17:58:21 +00:00
|
|
|
)
|
|
|
|
|
2021-07-21 18:29:08 +00:00
|
|
|
type appendBytes []byte
|
|
|
|
|
|
|
|
func (p appendBytes) AppendTo(b []byte) []byte {
|
|
|
|
return append(b, p...)
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:26:04 +00:00
|
|
|
func TestHash(t *testing.T) {
|
2022-03-16 23:27:57 +00:00
|
|
|
type tuple [2]any
|
|
|
|
type iface struct{ X any }
|
2021-08-03 04:44:13 +00:00
|
|
|
type scalars struct {
|
|
|
|
I8 int8
|
|
|
|
I16 int16
|
|
|
|
I32 int32
|
|
|
|
I64 int64
|
|
|
|
I int
|
|
|
|
U8 uint8
|
|
|
|
U16 uint16
|
|
|
|
U32 uint32
|
|
|
|
U64 uint64
|
|
|
|
U uint
|
|
|
|
UP uintptr
|
|
|
|
F32 float32
|
|
|
|
F64 float64
|
|
|
|
C64 complex64
|
|
|
|
C128 complex128
|
|
|
|
}
|
2021-07-21 17:26:04 +00:00
|
|
|
type MyBool bool
|
|
|
|
type MyHeader tar.Header
|
|
|
|
tests := []struct {
|
|
|
|
in tuple
|
|
|
|
wantEq bool
|
|
|
|
}{
|
2021-08-03 04:44:13 +00:00
|
|
|
{in: tuple{false, true}, wantEq: false},
|
|
|
|
{in: tuple{true, true}, wantEq: true},
|
|
|
|
{in: tuple{false, false}, wantEq: true},
|
|
|
|
{
|
|
|
|
in: tuple{
|
|
|
|
scalars{-8, -16, -32, -64, -1234, 8, 16, 32, 64, 1234, 5678, 32.32, 64.64, 32 + 32i, 64 + 64i},
|
|
|
|
scalars{-8, -16, -32, -64, -1234, 8, 16, 32, 64, 1234, 5678, 32.32, 64.64, 32 + 32i, 64 + 64i},
|
|
|
|
},
|
|
|
|
wantEq: true,
|
|
|
|
},
|
|
|
|
{in: tuple{scalars{I8: math.MinInt8}, scalars{I8: math.MinInt8 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{I16: math.MinInt16}, scalars{I16: math.MinInt16 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{I32: math.MinInt32}, scalars{I32: math.MinInt32 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{I64: math.MinInt64}, scalars{I64: math.MinInt64 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{I: -1234}, scalars{I: -1234 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{U8: math.MaxUint8}, scalars{U8: math.MaxUint8 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{U16: math.MaxUint16}, scalars{U16: math.MaxUint16 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{U32: math.MaxUint32}, scalars{U32: math.MaxUint32 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{U64: math.MaxUint64}, scalars{U64: math.MaxUint64 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{U: 1234}, scalars{U: 1234 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{UP: 5678}, scalars{UP: 5678 / 2}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{F32: 32.32}, scalars{F32: math.Nextafter32(32.32, 0)}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{F64: 64.64}, scalars{F64: math.Nextafter(64.64, 0)}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{F32: float32(math.NaN())}, scalars{F32: float32(math.NaN())}}, wantEq: true},
|
|
|
|
{in: tuple{scalars{F64: float64(math.NaN())}, scalars{F64: float64(math.NaN())}}, wantEq: true},
|
|
|
|
{in: tuple{scalars{C64: 32 + 32i}, scalars{C64: complex(math.Nextafter32(32, 0), 32)}}, wantEq: false},
|
|
|
|
{in: tuple{scalars{C128: 64 + 64i}, scalars{C128: complex(math.Nextafter(64, 0), 64)}}, wantEq: false},
|
2021-07-21 18:29:08 +00:00
|
|
|
{in: tuple{[]appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}, []appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}}, wantEq: true},
|
|
|
|
{in: tuple{[]appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}, []appendBytes{{0, 0, 0, 0, 0, 0, 0, 1}, {}}}, wantEq: false},
|
2021-07-21 17:26:04 +00:00
|
|
|
{in: tuple{iface{MyBool(true)}, iface{MyBool(true)}}, wantEq: true},
|
|
|
|
{in: tuple{iface{true}, iface{MyBool(true)}}, wantEq: false},
|
|
|
|
{in: tuple{iface{MyHeader{}}, iface{MyHeader{}}}, wantEq: true},
|
|
|
|
{in: tuple{iface{MyHeader{}}, iface{tar.Header{}}}, wantEq: false},
|
|
|
|
{in: tuple{iface{&MyHeader{}}, iface{&MyHeader{}}}, wantEq: true},
|
|
|
|
{in: tuple{iface{&MyHeader{}}, iface{&tar.Header{}}}, wantEq: false},
|
|
|
|
{in: tuple{iface{[]map[string]MyBool{}}, iface{[]map[string]MyBool{}}}, wantEq: true},
|
|
|
|
{in: tuple{iface{[]map[string]bool{}}, iface{[]map[string]MyBool{}}}, wantEq: false},
|
2021-07-22 22:22:48 +00:00
|
|
|
{
|
|
|
|
in: func() tuple {
|
|
|
|
i1 := 1
|
|
|
|
i2 := 2
|
|
|
|
v1 := [3]*int{&i1, &i2, &i1}
|
|
|
|
v2 := [3]*int{&i1, &i2, &i2}
|
|
|
|
return tuple{v1, v2}
|
|
|
|
}(),
|
|
|
|
wantEq: false,
|
|
|
|
},
|
2021-07-21 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
gotEq := Hash(tt.in[0]) == Hash(tt.in[1])
|
|
|
|
if gotEq != tt.wantEq {
|
|
|
|
t.Errorf("(Hash(%v) == Hash(%v)) = %v, want %v", tt.in[0], tt.in[1], gotEq, tt.wantEq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:16:37 +00:00
|
|
|
func TestDeepHash(t *testing.T) {
|
2020-06-28 17:58:21 +00:00
|
|
|
// v contains the types of values we care about for our current callers.
|
|
|
|
// Mostly we're just testing that we don't panic on handled types.
|
|
|
|
v := getVal()
|
|
|
|
|
2021-07-05 04:25:15 +00:00
|
|
|
hash1 := Hash(v)
|
2020-06-28 17:58:21 +00:00
|
|
|
t.Logf("hash: %v", hash1)
|
|
|
|
for i := 0; i < 20; i++ {
|
2021-07-05 04:25:15 +00:00
|
|
|
hash2 := Hash(getVal())
|
2020-06-28 17:58:21 +00:00
|
|
|
if hash1 != hash2 {
|
|
|
|
t.Error("second hash didn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 18:24:04 +00:00
|
|
|
// Tests that we actually hash map elements. Whoops.
|
|
|
|
func TestIssue4868(t *testing.T) {
|
|
|
|
m1 := map[int]string{1: "foo"}
|
|
|
|
m2 := map[int]string{1: "bar"}
|
|
|
|
if Hash(m1) == Hash(m2) {
|
|
|
|
t.Error("bogus")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
func getVal() []any {
|
|
|
|
return []any{
|
2020-06-28 17:58:21 +00:00
|
|
|
&wgcfg.Config{
|
2021-04-03 01:04:39 +00:00
|
|
|
Name: "foo",
|
2021-05-15 01:07:28 +00:00
|
|
|
Addresses: []netaddr.IPPrefix{netaddr.IPPrefixFrom(netaddr.IPFrom16([16]byte{3: 3}), 5)},
|
2020-06-28 17:58:21 +00:00
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
{
|
2021-10-28 00:42:33 +00:00
|
|
|
PublicKey: key.NodePublic{},
|
2020-06-28 17:58:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&router.Config{
|
2021-04-03 02:24:02 +00:00
|
|
|
Routes: []netaddr.IPPrefix{
|
|
|
|
netaddr.MustParseIPPrefix("1.2.3.0/24"),
|
|
|
|
netaddr.MustParseIPPrefix("1234::/64"),
|
2020-07-14 13:12:00 +00:00
|
|
|
},
|
2020-06-28 17:58:21 +00:00
|
|
|
},
|
2021-05-11 16:45:12 +00:00
|
|
|
map[dnsname.FQDN][]netaddr.IP{
|
|
|
|
dnsname.FQDN("a."): {netaddr.MustParseIP("1.2.3.4"), netaddr.MustParseIP("4.3.2.1")},
|
|
|
|
dnsname.FQDN("b."): {netaddr.MustParseIP("8.8.8.8"), netaddr.MustParseIP("9.9.9.9")},
|
2021-05-18 15:51:46 +00:00
|
|
|
dnsname.FQDN("c."): {netaddr.MustParseIP("6.6.6.6"), netaddr.MustParseIP("7.7.7.7")},
|
|
|
|
dnsname.FQDN("d."): {netaddr.MustParseIP("6.7.6.6"), netaddr.MustParseIP("7.7.7.8")},
|
|
|
|
dnsname.FQDN("e."): {netaddr.MustParseIP("6.8.6.6"), netaddr.MustParseIP("7.7.7.9")},
|
|
|
|
dnsname.FQDN("f."): {netaddr.MustParseIP("6.9.6.6"), netaddr.MustParseIP("7.7.7.0")},
|
2021-05-11 16:45:12 +00:00
|
|
|
},
|
|
|
|
map[dnsname.FQDN][]netaddr.IPPort{
|
|
|
|
dnsname.FQDN("a."): {netaddr.MustParseIPPort("1.2.3.4:11"), netaddr.MustParseIPPort("4.3.2.1:22")},
|
|
|
|
dnsname.FQDN("b."): {netaddr.MustParseIPPort("8.8.8.8:11"), netaddr.MustParseIPPort("9.9.9.9:22")},
|
2021-05-18 15:51:46 +00:00
|
|
|
dnsname.FQDN("c."): {netaddr.MustParseIPPort("8.8.8.8:12"), netaddr.MustParseIPPort("9.9.9.9:23")},
|
|
|
|
dnsname.FQDN("d."): {netaddr.MustParseIPPort("8.8.8.8:13"), netaddr.MustParseIPPort("9.9.9.9:24")},
|
|
|
|
dnsname.FQDN("e."): {netaddr.MustParseIPPort("8.8.8.8:14"), netaddr.MustParseIPPort("9.9.9.9:25")},
|
2021-05-11 16:45:12 +00:00
|
|
|
},
|
2021-11-02 21:41:56 +00:00
|
|
|
map[key.DiscoPublic]bool{
|
|
|
|
key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 31: 0})): true,
|
|
|
|
key.DiscoPublicFromRaw32(mem.B([]byte{1: 2, 31: 0})): false,
|
|
|
|
key.DiscoPublicFromRaw32(mem.B([]byte{1: 3, 31: 0})): true,
|
|
|
|
key.DiscoPublicFromRaw32(mem.B([]byte{1: 4, 31: 0})): false,
|
2020-06-28 17:58:21 +00:00
|
|
|
},
|
2021-07-06 04:21:52 +00:00
|
|
|
&tailcfg.MapResponse{
|
|
|
|
DERPMap: &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
2021-12-15 16:42:25 +00:00
|
|
|
1: {
|
2021-07-06 04:21:52 +00:00
|
|
|
RegionID: 1,
|
|
|
|
RegionCode: "foo",
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
|
|
|
Name: "n1",
|
|
|
|
RegionID: 1,
|
|
|
|
HostName: "foo.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "n2",
|
|
|
|
RegionID: 1,
|
|
|
|
HostName: "bar.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DNSConfig: &tailcfg.DNSConfig{
|
2022-05-03 21:41:58 +00:00
|
|
|
Resolvers: []*dnstype.Resolver{
|
2021-07-06 04:21:52 +00:00
|
|
|
{Addr: "10.0.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PacketFilter: []tailcfg.FilterRule{
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"1.2.3.4"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{
|
|
|
|
IP: "1.2.3.4/32",
|
2021-07-06 04:31:30 +00:00
|
|
|
Ports: tailcfg.PortRange{First: 1, Last: 2},
|
2021-07-06 04:21:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UserProfiles: []tailcfg.UserProfile{
|
|
|
|
{ID: 1, LoginName: "foo@bar.com"},
|
|
|
|
{ID: 2, LoginName: "bar@foo.com"},
|
|
|
|
},
|
|
|
|
},
|
2021-07-07 18:58:02 +00:00
|
|
|
filter.Match{
|
|
|
|
IPProto: []ipproto.Proto{1, 2, 3},
|
|
|
|
},
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 01:05:04 +00:00
|
|
|
|
2021-07-05 04:25:15 +00:00
|
|
|
var sink = Hash("foo")
|
|
|
|
|
2021-05-08 01:05:04 +00:00
|
|
|
func BenchmarkHash(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
v := getVal()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-07-05 04:25:15 +00:00
|
|
|
sink = Hash(v)
|
2021-05-08 01:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 20:17:12 +00:00
|
|
|
|
|
|
|
func TestHashMapAcyclic(t *testing.T) {
|
|
|
|
m := map[int]string{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
m[i] = fmt.Sprint(i)
|
|
|
|
}
|
|
|
|
got := map[string]bool{}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bw := bufio.NewWriter(&buf)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
v := reflect.ValueOf(m)
|
|
|
|
buf.Reset()
|
|
|
|
bw.Reset(&buf)
|
2021-07-22 22:22:48 +00:00
|
|
|
h := &hasher{bw: bw}
|
|
|
|
h.hashMap(v)
|
2021-05-11 20:17:12 +00:00
|
|
|
if got[string(buf.Bytes())] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got[string(buf.Bytes())] = true
|
|
|
|
}
|
|
|
|
if len(got) != 1 {
|
|
|
|
t.Errorf("got %d results; want 1", len(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 05:13:33 +00:00
|
|
|
func TestPrintArray(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
X [32]byte
|
|
|
|
}
|
2021-07-22 22:22:48 +00:00
|
|
|
x := T{X: [32]byte{1: 1, 31: 31}}
|
2021-07-06 05:13:33 +00:00
|
|
|
var got bytes.Buffer
|
|
|
|
bw := bufio.NewWriter(&got)
|
2021-07-22 22:22:48 +00:00
|
|
|
h := &hasher{bw: bw}
|
2021-08-03 04:44:13 +00:00
|
|
|
h.hashValue(reflect.ValueOf(x))
|
2021-07-06 05:13:33 +00:00
|
|
|
bw.Flush()
|
util/deephash: remove unnecessary formatting for structs and slices (#2571)
The index for every struct field or slice element and
the number of fields for the struct is unncessary.
The hashing of Go values is unambiguous because every type (except maps)
encodes in a parsable manner. So long as we know the type information,
we could theoretically decode every value (except for maps).
At a high level:
* numbers are encoded as fixed-width records according to precision.
* strings (and AppendTo output) are encoded with a fixed-width length,
followed by the contents of the buffer.
* slices are prefixed by a fixed-width length, followed by the encoding
of each value. So long as we know the type of each element, we could
theoretically decode each element.
* arrays are encoded just like slices, but elide the length
since it is determined from the Go type.
* maps are encoded first with a byte indicating whether it is a cycle.
If a cycle, it is followed by a fixed-width index for the pointer,
otherwise followed by the SHA-256 hash of its contents. The encoding of maps
is not decodeable, but a SHA-256 hash is sufficient to avoid ambiguities.
* interfaces are encoded first with a byte indicating whether it is nil.
If not nil, it is followed by a fixed-width index for the type,
and then the encoding for the underlying value. Having the type be encoded
first ensures that the value could theoretically be decoded next.
* pointers are encoded first with a byte indicating whether it is
1) nil, 2) a cycle, or 3) newly seen. If a cycle, it is followed by
a fixed-width index for the pointer. If newly seen, it is followed by
the encoding for the pointed-at value.
Removing unnecessary details speeds up hashing:
name old time/op new time/op delta
Hash-8 76.0µs ± 1% 55.8µs ± 2% -26.62% (p=0.000 n=10+10)
HashMapAcyclic-8 61.9µs ± 0% 62.0µs ± 0% ~ (p=0.666 n=9+9)
TailcfgNode-8 10.2µs ± 1% 7.5µs ± 1% -26.90% (p=0.000 n=10+9)
HashArray-8 1.07µs ± 1% 0.70µs ± 1% -34.67% (p=0.000 n=10+9)
Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2021-08-04 03:35:57 +00:00
|
|
|
const want = "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f"
|
2021-07-06 05:13:33 +00:00
|
|
|
if got := got.Bytes(); string(got) != want {
|
|
|
|
t.Errorf("wrong:\n got: %q\nwant: %q\n", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:17:12 +00:00
|
|
|
func BenchmarkHashMapAcyclic(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
m := map[int]string{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
m[i] = fmt.Sprint(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bw := bufio.NewWriter(&buf)
|
|
|
|
v := reflect.ValueOf(m)
|
|
|
|
|
2021-07-22 22:22:48 +00:00
|
|
|
h := &hasher{bw: bw}
|
2021-07-07 05:37:32 +00:00
|
|
|
|
2021-05-11 20:17:12 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Reset()
|
|
|
|
bw.Reset(&buf)
|
2021-07-22 22:22:48 +00:00
|
|
|
h.hashMap(v)
|
2021-05-11 20:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 20:45:23 +00:00
|
|
|
|
2021-07-06 04:21:52 +00:00
|
|
|
func BenchmarkTailcfgNode(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
node := new(tailcfg.Node)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sink = Hash(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 20:45:23 +00:00
|
|
|
func TestExhaustive(t *testing.T) {
|
2021-07-20 05:49:51 +00:00
|
|
|
seen := make(map[Sum]bool)
|
2021-07-02 20:45:23 +00:00
|
|
|
for i := 0; i < 100000; i++ {
|
2021-07-05 04:25:15 +00:00
|
|
|
s := Hash(i)
|
2021-07-02 20:45:23 +00:00
|
|
|
if seen[s] {
|
|
|
|
t.Fatalf("hash collision %v", i)
|
|
|
|
}
|
|
|
|
seen[s] = true
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 04:25:15 +00:00
|
|
|
|
2021-07-07 04:41:18 +00:00
|
|
|
// verify this doesn't loop forever, as it used to (Issue 2340)
|
|
|
|
func TestMapCyclicFallback(t *testing.T) {
|
|
|
|
type T struct {
|
2022-03-16 23:27:57 +00:00
|
|
|
M map[string]any
|
2021-07-07 04:41:18 +00:00
|
|
|
}
|
|
|
|
v := &T{
|
2022-03-16 23:27:57 +00:00
|
|
|
M: map[string]any{},
|
2021-07-07 04:41:18 +00:00
|
|
|
}
|
|
|
|
v.M["m"] = v.M
|
|
|
|
Hash(v)
|
|
|
|
}
|
2021-07-06 05:13:33 +00:00
|
|
|
|
|
|
|
func TestArrayAllocs(t *testing.T) {
|
2021-07-07 18:40:28 +00:00
|
|
|
if version.IsRace() {
|
|
|
|
t.Skip("skipping test under race detector")
|
|
|
|
}
|
2021-08-30 17:47:21 +00:00
|
|
|
|
|
|
|
// In theory, there should be no allocations. However, escape analysis on
|
|
|
|
// certain architectures fails to detect that certain cases do not escape.
|
|
|
|
// This discrepency currently affects sha256.digest.Sum.
|
|
|
|
// Measure the number of allocations in sha256 to ensure that Hash does
|
|
|
|
// not allocate on top of its usage of sha256.
|
|
|
|
// See https://golang.org/issue/48055.
|
|
|
|
var b []byte
|
|
|
|
h := sha256.New()
|
|
|
|
want := int(testing.AllocsPerRun(1000, func() {
|
|
|
|
b = h.Sum(b[:0])
|
|
|
|
}))
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "amd64", "arm64":
|
|
|
|
want = 0 // ensure no allocations on popular architectures
|
|
|
|
}
|
|
|
|
|
2021-07-06 05:13:33 +00:00
|
|
|
type T struct {
|
|
|
|
X [32]byte
|
|
|
|
}
|
|
|
|
x := &T{X: [32]byte{1: 1, 2: 2, 3: 3, 4: 4}}
|
2021-08-30 17:47:21 +00:00
|
|
|
got := int(testing.AllocsPerRun(1000, func() {
|
2021-07-06 05:13:33 +00:00
|
|
|
sink = Hash(x)
|
|
|
|
}))
|
2021-08-30 17:47:21 +00:00
|
|
|
if got > want {
|
|
|
|
t.Errorf("allocs = %v; want %v", got, want)
|
2021-07-06 05:13:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashArray(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
type T struct {
|
|
|
|
X [32]byte
|
|
|
|
}
|
|
|
|
x := &T{X: [32]byte{1: 1, 2: 2, 3: 3, 4: 4}}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sink = Hash(x)
|
|
|
|
}
|
|
|
|
}
|