From 71393c576b98c1903cd8d31450b603535586f81e Mon Sep 17 00:00:00 2001 From: Adrian Dewhurst Date: Wed, 31 Jul 2024 16:08:04 -0400 Subject: [PATCH] tun: fix checksum test failures on non-4KiB page sizes When generating page-aligned random bytes, random data started at the beginning of the buffer that will be chopped off. When the page size differs, the start of the returned slice is different than expected for the expected checksums, causing the tests to fail. --- tun/checksum_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tun/checksum_test.go b/tun/checksum_test.go index c40efc9..f5b8f18 100644 --- a/tun/checksum_test.go +++ b/tun/checksum_test.go @@ -21,26 +21,33 @@ type archChecksumDetails struct { f func([]byte, uint16) uint16 } -func deterministicRandomBytes(seed int64, length int) []byte { +func fillRandomBuffer(seed int64, buf []byte) { rng := rand.New(rand.NewSource(seed)) - buf := make([]byte, length) n, err := rng.Read(buf) if err != nil { panic(err) } - if n != length { + if n != len(buf) { panic("incomplete random buffer") } +} + +func deterministicRandomBytes(seed int64, length int) []byte { + buf := make([]byte, length) + fillRandomBuffer(seed, buf) return buf } func getPageAlignedRandomBytes(seed int64, length int) []byte { alignment := syscall.Getpagesize() - buf := deterministicRandomBytes(seed, length+(alignment-1)) + buf := make([]byte, length+(alignment-1)) bufPtr := uintptr(unsafe.Pointer(&buf[0])) alignedBufPtr := (bufPtr + uintptr(alignment-1)) & ^uintptr(alignment-1) alignedStart := int(alignedBufPtr - bufPtr) - return buf[alignedStart:] + + buf = buf[alignedStart : alignedStart+length] + fillRandomBuffer(seed, buf) + return buf } func TestChecksum(t *testing.T) {