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.
This commit is contained in:
parent
6c039a188c
commit
71393c576b
1 changed files with 12 additions and 5 deletions
|
|
@ -21,26 +21,33 @@ type archChecksumDetails struct {
|
||||||
f func([]byte, uint16) uint16
|
f func([]byte, uint16) uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func deterministicRandomBytes(seed int64, length int) []byte {
|
func fillRandomBuffer(seed int64, buf []byte) {
|
||||||
rng := rand.New(rand.NewSource(seed))
|
rng := rand.New(rand.NewSource(seed))
|
||||||
buf := make([]byte, length)
|
|
||||||
n, err := rng.Read(buf)
|
n, err := rng.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if n != length {
|
if n != len(buf) {
|
||||||
panic("incomplete random buffer")
|
panic("incomplete random buffer")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deterministicRandomBytes(seed int64, length int) []byte {
|
||||||
|
buf := make([]byte, length)
|
||||||
|
fillRandomBuffer(seed, buf)
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPageAlignedRandomBytes(seed int64, length int) []byte {
|
func getPageAlignedRandomBytes(seed int64, length int) []byte {
|
||||||
alignment := syscall.Getpagesize()
|
alignment := syscall.Getpagesize()
|
||||||
buf := deterministicRandomBytes(seed, length+(alignment-1))
|
buf := make([]byte, length+(alignment-1))
|
||||||
bufPtr := uintptr(unsafe.Pointer(&buf[0]))
|
bufPtr := uintptr(unsafe.Pointer(&buf[0]))
|
||||||
alignedBufPtr := (bufPtr + uintptr(alignment-1)) & ^uintptr(alignment-1)
|
alignedBufPtr := (bufPtr + uintptr(alignment-1)) & ^uintptr(alignment-1)
|
||||||
alignedStart := int(alignedBufPtr - bufPtr)
|
alignedStart := int(alignedBufPtr - bufPtr)
|
||||||
return buf[alignedStart:]
|
|
||||||
|
buf = buf[alignedStart : alignedStart+length]
|
||||||
|
fillRandomBuffer(seed, buf)
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChecksum(t *testing.T) {
|
func TestChecksum(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue