mirror of
https://github.com/tailscale/tailscale.git
synced 2026-09-13 22:41:38 -04:00
Prior to this change there were two problems with our fuzzing for oss-fuzz: 1. There was an issue if the fuzzing spanned two files (mingled with the testing). 2. The fuzzing needs to be part of the implementation package (no _test packages). This change fixes that by moving all package fuzzing into a common `fuzz_test.go` within the package. Updates https://github.com/tailscale/corp/issues/46608 Change-Id: I0b95edcd0df946f723eea32f575c679214c0b202 Signed-off-by: Mike Jensen <mikej@tailscale.com>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package hashx
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
"tailscale.com/util/must"
|
|
)
|
|
|
|
func FuzzBlock512(f *testing.F) {
|
|
f.Fuzz(func(t *testing.T, seed int64) {
|
|
c := qt.New(t)
|
|
|
|
execute := func(h hasher, r *rand.Rand) {
|
|
for range r.Intn(256) {
|
|
switch r.Intn(5) {
|
|
case 0:
|
|
n := uint8(r.Uint64())
|
|
h.HashUint8(n)
|
|
case 1:
|
|
n := uint16(r.Uint64())
|
|
h.HashUint16(n)
|
|
case 2:
|
|
n := uint32(r.Uint64())
|
|
h.HashUint32(n)
|
|
case 3:
|
|
n := uint64(r.Uint64())
|
|
h.HashUint64(n)
|
|
case 4:
|
|
b := make([]byte, r.Intn(256))
|
|
r.Read(b)
|
|
h.HashBytes(b)
|
|
}
|
|
}
|
|
}
|
|
|
|
r1 := rand.New(rand.NewSource(seed))
|
|
r2 := rand.New(rand.NewSource(seed))
|
|
|
|
h1 := must.Get(New512(sha256.New()))
|
|
h2 := newNaive()
|
|
|
|
execute(h1, r1)
|
|
execute(h2, r2)
|
|
|
|
c.Assert(h1.Sum(nil), qt.DeepEquals, h2.Sum(nil))
|
|
|
|
execute(h1, r1)
|
|
execute(h2, r2)
|
|
|
|
c.Assert(h1.Sum(nil), qt.DeepEquals, h2.Sum(nil))
|
|
|
|
h1.Reset()
|
|
h2.Reset()
|
|
|
|
execute(h1, r1)
|
|
execute(h2, r2)
|
|
|
|
c.Assert(h1.Sum(nil), qt.DeepEquals, h2.Sum(nil))
|
|
})
|
|
}
|