Files
sys/unix/mremap_test.go
T
Anton Kuklin e0c3b6e6ae unix: add Mremap for linux
For golang/go#60409

Change-Id: I75a9732ee996f0aeb91599d80803f96ada468c27
GitHub-Last-Rev: c348b6194d9fbe92f4f912c66709b0af75810e99
GitHub-Pull-Request: golang/sys#164
Reviewed-on: https://go-review.googlesource.com/c/sys/+/502715
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-06-16 19:37:35 +00:00

48 lines
1.1 KiB
Go

// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux
// +build linux
package unix_test
import (
"testing"
"golang.org/x/sys/unix"
)
func TestMremap(t *testing.T) {
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap: %v", err)
}
if err := unix.Mprotect(b, unix.PROT_READ|unix.PROT_WRITE); err != nil {
t.Fatalf("Mprotect: %v", err)
}
b[0] = 42
bNew, err := unix.Mremap(b, unix.Getpagesize()*2, unix.MREMAP_MAYMOVE)
if err != nil {
t.Fatalf("Mremap2: %v", err)
}
bNew[unix.Getpagesize()+1] = 84 // checks
if bNew[0] != 42 {
t.Fatal("first element value was changed")
}
if len(bNew) != unix.Getpagesize()*2 {
t.Fatal("new memory len not equal to specified len")
}
if cap(bNew) != unix.Getpagesize()*2 {
t.Fatal("new memory cap not equal to specified len")
}
_, err = unix.Mremap(b, unix.Getpagesize(), unix.MREMAP_FIXED)
if err != unix.EINVAL {
t.Fatalf("unix.MREMAP_FIXED should be forbidden")
}
}