github.com/Aayushi-Bansal/sys@v0.0.0-20180118120756-90d962a959d8/unix/mmap_unix_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package unix_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  func TestMmap(t *testing.T) {
    16  	b, err := unix.Mmap(-1, 0, unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
    17  	if err != nil {
    18  		t.Fatalf("Mmap: %v", err)
    19  	}
    20  	if err := unix.Mprotect(b, unix.PROT_READ|unix.PROT_WRITE); err != nil {
    21  		t.Fatalf("Mprotect: %v", err)
    22  	}
    23  
    24  	b[0] = 42
    25  
    26  	if err := unix.Msync(b, unix.MS_SYNC); err != nil {
    27  		t.Fatalf("Msync: %v", err)
    28  	}
    29  	if err := unix.Madvise(b, unix.MADV_DONTNEED); err != nil {
    30  		t.Fatalf("Madvise: %v", err)
    31  	}
    32  	if err := unix.Munmap(b); err != nil {
    33  		t.Fatalf("Munmap: %v", err)
    34  	}
    35  }