Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.22.x, 1.23.x]
go-version: [1.23.x, 1.24.x]
venv: [windows-2019, windows-2022]
fips: [1, 0]
runs-on: ${{ matrix.venv }}
Expand Down
55 changes: 55 additions & 0 deletions cng/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,61 @@ func TestHash_OneShot(t *testing.T) {
}
}

func TestHashAllocations(t *testing.T) {
msg := []byte("testing")
n := int(testing.AllocsPerRun(10, func() {
sink ^= cng.MD4(msg)[0]
sink ^= cng.MD5(msg)[0]
sink ^= cng.SHA1(msg)[0]
sink ^= cng.SHA256(msg)[0]
sink ^= cng.SHA384(msg)[0]
sink ^= cng.SHA512(msg)[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it help in some way to put all of these in the same test?

Or could these be separate tests (perhaps using t.Run) to make it easier to figure out what went wrong when something changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good recommendation. We have so many tests with this approach so I actually used the same code for most of the tests, I'll see if we can cut any lines or make it look simpler with that approach on the other repositories too.

}))
want := 0
if n > want {
t.Errorf("allocs = %d, want %d", n, want)
}
}

func TestHashStructAllocations(t *testing.T) {
msg := []byte("testing")

md4Hash := cng.NewMD4()
md5Hash := cng.NewMD5()
sha1Hash := cng.NewSHA1()
sha256Hash := cng.NewSHA256()
sha384Hash := cng.NewSHA384()
sha512Hash := cng.NewSHA512()

sum := make([]byte, sha512Hash.Size())
n := int(testing.AllocsPerRun(10, func() {
md4Hash.Write(msg)
md5Hash.Write(msg)
sha1Hash.Write(msg)
sha256Hash.Write(msg)
sha384Hash.Write(msg)
sha512Hash.Write(msg)

md4Hash.Sum(sum[:0])
md5Hash.Sum(sum[:0])
sha1Hash.Sum(sum[:0])
sha256Hash.Sum(sum[:0])
sha384Hash.Sum(sum[:0])
sha512Hash.Sum(sum[:0])

md4Hash.Reset()
md5Hash.Reset()
sha1Hash.Reset()
sha256Hash.Reset()
sha384Hash.Reset()
sha512Hash.Reset()
}))
want := 0
if n > want {
t.Errorf("allocs = %d, want %d", n, want)
}
}

func BenchmarkSHA256_8Bytes(b *testing.B) {
b.StopTimer()
h := cng.NewSHA256()
Expand Down