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-05-11 20:17:12 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-07-05 04:25:15 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2021-05-11 20:17:12 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2020-06-28 17:58:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
2021-05-11 16:45:12 +00:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/util/dnsname"
|
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-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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVal() []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
&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-04-30 23:45:36 +00:00
|
|
|
Endpoints: wgcfg.Endpoints{
|
|
|
|
IPPorts: wgcfg.NewIPPortSet(netaddr.MustParseIPPort("42.42.42.42:5")),
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
map[tailcfg.DiscoKey]bool{
|
|
|
|
{1: 1}: true,
|
|
|
|
{1: 2}: false,
|
2021-05-18 15:51:46 +00:00
|
|
|
{2: 3}: true,
|
|
|
|
{3: 4}: false,
|
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++ {
|
|
|
|
visited := map[uintptr]bool{}
|
2021-05-19 18:51:21 +00:00
|
|
|
scratch := make([]byte, 0, 64)
|
2021-05-11 20:17:12 +00:00
|
|
|
v := reflect.ValueOf(m)
|
|
|
|
buf.Reset()
|
|
|
|
bw.Reset(&buf)
|
2021-05-19 18:51:21 +00:00
|
|
|
if !hashMapAcyclic(bw, v, visited, scratch) {
|
2021-05-11 20:17:12 +00:00
|
|
|
t.Fatal("returned false")
|
|
|
|
}
|
|
|
|
if got[string(buf.Bytes())] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got[string(buf.Bytes())] = true
|
|
|
|
}
|
|
|
|
if len(got) != 1 {
|
|
|
|
t.Errorf("got %d results; want 1", len(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
visited := map[uintptr]bool{}
|
2021-05-19 18:51:21 +00:00
|
|
|
scratch := make([]byte, 0, 64)
|
2021-05-11 20:17:12 +00:00
|
|
|
v := reflect.ValueOf(m)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Reset()
|
|
|
|
bw.Reset(&buf)
|
2021-05-19 18:51:21 +00:00
|
|
|
if !hashMapAcyclic(bw, v, visited, scratch) {
|
2021-05-11 20:17:12 +00:00
|
|
|
b.Fatal("returned false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 20:45:23 +00:00
|
|
|
|
|
|
|
func TestExhaustive(t *testing.T) {
|
2021-07-05 04:25:15 +00:00
|
|
|
seen := make(map[[sha256.Size]byte]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
|
|
|
|
|
|
|
func TestSHA256EqualHex(t *testing.T) {
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
sum := Hash(i)
|
|
|
|
hx := hex.EncodeToString(sum[:])
|
|
|
|
if !sha256EqualHex(sum, hx) {
|
|
|
|
t.Fatal("didn't match, should've")
|
|
|
|
}
|
|
|
|
if sha256EqualHex(sum, hx[:len(hx)-1]) {
|
|
|
|
t.Fatal("matched on wrong length")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|