github.com/afumu/libc@v0.0.6/unix_test.go (about)

     1  // Copyright 2023 The Libc 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  //go:build unix
     6  // +build unix
     7  
     8  package libc // import "github.com/afumu/libc"
     9  
    10  import (
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  // https://gitlab.com/cznic/libc/-/issues/29
    19  func TestIssue29(t *testing.T) {
    20  	dir := t.TempDir()
    21  
    22  	fn := filepath.Join(dir, "test")
    23  	if err := os.WriteFile(fn, make([]byte, 1<<20), 0644); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	f, err := os.OpenFile(fn, os.O_RDWR, 0644)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	defer f.Close()
    33  
    34  	d := Xmmap(nil, 0, 4096, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED, int32(uintptr(f.Fd())), 0)
    35  	if d == 0 {
    36  		t.Fatal("mmap failed")
    37  	}
    38  
    39  	if rc := Xmunmap(nil, d, 4096); rc != 0 {
    40  		t.Fatalf("munmap failed: %v", rc)
    41  	}
    42  }