Skip to content

Commit 71393c5

Browse files
committed
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.
1 parent 6c039a1 commit 71393c5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

tun/checksum_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,33 @@ type archChecksumDetails struct {
2121
f func([]byte, uint16) uint16
2222
}
2323

24-
func deterministicRandomBytes(seed int64, length int) []byte {
24+
func fillRandomBuffer(seed int64, buf []byte) {
2525
rng := rand.New(rand.NewSource(seed))
26-
buf := make([]byte, length)
2726
n, err := rng.Read(buf)
2827
if err != nil {
2928
panic(err)
3029
}
31-
if n != length {
30+
if n != len(buf) {
3231
panic("incomplete random buffer")
3332
}
33+
}
34+
35+
func deterministicRandomBytes(seed int64, length int) []byte {
36+
buf := make([]byte, length)
37+
fillRandomBuffer(seed, buf)
3438
return buf
3539
}
3640

3741
func getPageAlignedRandomBytes(seed int64, length int) []byte {
3842
alignment := syscall.Getpagesize()
39-
buf := deterministicRandomBytes(seed, length+(alignment-1))
43+
buf := make([]byte, length+(alignment-1))
4044
bufPtr := uintptr(unsafe.Pointer(&buf[0]))
4145
alignedBufPtr := (bufPtr + uintptr(alignment-1)) & ^uintptr(alignment-1)
4246
alignedStart := int(alignedBufPtr - bufPtr)
43-
return buf[alignedStart:]
47+
48+
buf = buf[alignedStart : alignedStart+length]
49+
fillRandomBuffer(seed, buf)
50+
return buf
4451
}
4552

4653
func TestChecksum(t *testing.T) {

0 commit comments

Comments
 (0)