| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"hash/maphash" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
// Test_HashFold pins the contract: keys EqualFold reports equal hash equal, |
| 13 |
// as strings and as bytes, at every length across the word boundaries, and |
| 14 |
// keys it tells apart, by case of a non-letter, by a byte past ASCII or by |
| 15 |
// length alone, hash apart. |
| 16 |
func Test_HashFold(t *testing.T) { |
| 17 |
t.Parallel() |
| 18 |
|
| 19 |
for _, pair := range [][2]string{ |
| 20 |
{"", ""}, |
| 21 |
{"a", "A"}, |
| 22 |
{"id", "ID"}, |
| 23 |
{"Content-Type", "content-type"}, |
| 24 |
{"X-Forwarded-For", "x-FORWARDED-for"}, |
| 25 |
{"hobby", "HoBbY"}, |
| 26 |
{"nested.field.name", "NESTED.Field.NAME"}, |
| 27 |
{"caf\xc3\xa9", "CAF\xc3\xa9"}, |
| 28 |
} { |
| 29 |
require.True(t, EqualFold(pair[0], pair[1])) |
| 30 |
require.Equal(t, HashFold(pair[0]), HashFold(pair[1]), "%q and %q", pair[0], pair[1]) |
| 31 |
require.Equal(t, HashFold(pair[0]), HashFold([]byte(pair[1])), "%q as bytes", pair[1]) |
| 32 |
} |
| 33 |
|
| 34 |
for _, pair := range [][2]string{ |
| 35 |
{"a", "b"}, |
| 36 |
{"ab", "ba"}, |
| 37 |
{"a", "a\x00"}, |
| 38 |
{"", "\x00"}, |
| 39 |
{"@", "`"}, |
| 40 |
{"[", "{"}, |
| 41 |
{"caf\xc3\xa9", "caf\xc3\x89"}, |
| 42 |
{"abcd", "abce"}, |
| 43 |
{"abcdefg", "abcdefh"}, |
| 44 |
{"abcdefgh", "abcdefgi"}, |
| 45 |
{"abcdefghi", "abcdefghj"}, |
| 46 |
{"abcdefghijklmnop", "abcdefghijklmnoq"}, |
| 47 |
// keys that pack into the same words, told apart by length alone |
| 48 |
{"b", "ab"}, |
| 49 |
{"b", "bb"}, |
| 50 |
{"bb", "bbb"}, |
| 51 |
{"aaaa", "aaaaa"}, |
| 52 |
{"````", "a````"}, |
| 53 |
{"aaaaaaaa", "aaaaaaaaa"}, |
| 54 |
{"~bcdefghijklmno", "abcdefghijklmno\x00"}, |
| 55 |
} { |
| 56 |
require.False(t, EqualFold(pair[0], pair[1])) |
| 57 |
require.NotEqual(t, HashFold(pair[0]), HashFold(pair[1]), "%q and %q", pair[0], pair[1]) |
| 58 |
} |
| 59 |
|
| 60 |
// One byte repeated, at every length across the word boundaries. |
| 61 |
for _, c := range "a-\x00" { |
| 62 |
seen := make(map[uint64]int) |
| 63 |
for n := range 64 { |
| 64 |
h := HashFold(strings.Repeat(string(c), n)) |
| 65 |
other, ok := seen[h] |
| 66 |
require.False(t, ok, "%q repeated %d and %d times share hash %#x", c, other, n, h) |
| 67 |
seen[h] = n |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Every length across the word boundaries, each byte position flipped in |
| 72 |
// case in turn. |
| 73 |
for n := range 40 { |
| 74 |
key := []byte(strings.Repeat("aB3-", n/4+1)[:n]) |
| 75 |
want := HashFold(string(key)) |
| 76 |
for i := range key { |
| 77 |
flipped := append([]byte(nil), key...) |
| 78 |
if c := flipped[i]; c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' { |
| 79 |
flipped[i] ^= 0x20 |
| 80 |
} |
| 81 |
require.Equal(t, want, HashFold(flipped), "length %d, byte %d", n, i) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Test_HashFold_NoCollisions hashes over a hundred thousand keys EqualFold |
| 87 |
// tells apart, short and long, and requires that no two share a hash. |
| 88 |
func Test_HashFold_NoCollisions(t *testing.T) { |
| 89 |
t.Parallel() |
| 90 |
|
| 91 |
seen := make(map[uint64]string) |
| 92 |
add := func(key string) { |
| 93 |
h := HashFold(key) |
| 94 |
if other, ok := seen[h]; ok && !EqualFold(other, key) { |
| 95 |
t.Fatalf("%q and %q share hash %#x", other, key, h) |
| 96 |
} |
| 97 |
seen[h] = key |
| 98 |
} |
| 99 |
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-_." |
| 100 |
add("") |
| 101 |
for _, a := range alphabet { |
| 102 |
add(string(a)) |
| 103 |
for _, b := range alphabet { |
| 104 |
add(string(a) + string(b)) |
| 105 |
for _, c := range alphabet[:16] { |
| 106 |
add(string(a) + string(b) + string(c)) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
for i := range 40000 { |
| 111 |
add("field" + strconv.Itoa(i)) |
| 112 |
add("X-Header-" + strconv.Itoa(i) + "-Name") |
| 113 |
if i%4 == 0 { |
| 114 |
add("user.addresses." + strconv.Itoa(i) + ".street.line.number") |
| 115 |
} |
| 116 |
} |
| 117 |
require.Greater(t, len(seen), 110000) |
| 118 |
} |
| 119 |
|
| 120 |
func FuzzHashFold(f *testing.F) { |
| 121 |
f.Add("Content-Type", "content-type") |
| 122 |
f.Add("abcdefghi", "ABCDEFGHI") |
| 123 |
f.Add("a", "b") |
| 124 |
f.Add("@[`{", "`{@[") |
| 125 |
f.Add("\xc9abc", "\xe9abc") |
| 126 |
f.Fuzz(func(t *testing.T, a, b string) { |
| 127 |
ha := HashFold(a) |
| 128 |
if HashFold([]byte(a)) != ha { |
| 129 |
t.Fatalf("HashFold(%q) differs between string and bytes", a) |
| 130 |
} |
| 131 |
if EqualFold(a, b) && HashFold(b) != ha { |
| 132 |
t.Fatalf("HashFold(%q) != HashFold(%q) though EqualFold holds", a, b) |
| 133 |
} |
| 134 |
// Swapping the case of every letter is still EqualFold. |
| 135 |
swapped := []byte(a) |
| 136 |
for i, c := range swapped { |
| 137 |
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' { |
| 138 |
swapped[i] = c ^ 0x20 |
| 139 |
} |
| 140 |
} |
| 141 |
if HashFold(swapped) != ha { |
| 142 |
t.Fatalf("HashFold(%q) != HashFold(%q)", a, swapped) |
| 143 |
} |
| 144 |
}) |
| 145 |
} |
| 146 |
|
| 147 |
// go test -run=^$ -bench=Benchmark_HashFold -benchmem -count=4 |
| 148 |
func Benchmark_HashFold(b *testing.B) { |
| 149 |
seed := maphash.MakeSeed() |
| 150 |
for _, key := range []string{"id", "Name", "Content-Type", "X-Forwarded-Proto", strings.Repeat("Header-Name-", 4)} { |
| 151 |
b.Run(strconv.Itoa(len(key))+"B/fiber", func(b *testing.B) { |
| 152 |
var sink uint64 |
| 153 |
for b.Loop() { |
| 154 |
sink += HashFold(key) |
| 155 |
} |
| 156 |
_ = sink |
| 157 |
}) |
| 158 |
b.Run(strconv.Itoa(len(key))+"B/default", func(b *testing.B) { |
| 159 |
// The case-insensitive hash the standard library offers: fold a |
| 160 |
// copy, then hash it. |
| 161 |
var sink uint64 |
| 162 |
for b.Loop() { |
| 163 |
sink += maphash.String(seed, strings.ToLower(key)) |
| 164 |
} |
| 165 |
_ = sink |
| 166 |
}) |
| 167 |
} |
| 168 |
} |