github.com/Kalvelign/golang-windows-sys-lib@v0.0.0-20221121121202-63da651435e1/unix/sysvshm_unix_test.go (about)

     1  // Copyright 2021 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  //go:build (darwin && amd64) || linux
     6  // +build darwin,amd64 linux
     7  
     8  package unix_test
     9  
    10  import (
    11  	"runtime"
    12  	"testing"
    13  
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  func TestSysvSharedMemory(t *testing.T) {
    18  	// create ipc
    19  	id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600)
    20  
    21  	// ipc isn't implemented on android, should fail
    22  	if runtime.GOOS == "android" {
    23  		if err != unix.ENOSYS {
    24  			t.Fatalf("expected android to fail, but it didn't")
    25  		}
    26  		return
    27  	}
    28  
    29  	// The kernel may have been built without System V IPC support.
    30  	if err == unix.ENOSYS {
    31  		t.Skip("shmget not supported")
    32  	}
    33  
    34  	if err != nil {
    35  		t.Fatalf("SysvShmGet: %v", err)
    36  	}
    37  	defer func() {
    38  		_, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil)
    39  		if err != nil {
    40  			t.Errorf("Remove failed: %v", err)
    41  		}
    42  	}()
    43  
    44  	// attach
    45  	b1, err := unix.SysvShmAttach(id, 0, 0)
    46  	if err != nil {
    47  		t.Fatalf("Attach: %v", err)
    48  	}
    49  
    50  	if len(b1) != 1024 {
    51  		t.Fatalf("b1 len = %v, want 1024", len(b1))
    52  	}
    53  
    54  	b1[42] = 'x'
    55  
    56  	// attach again
    57  	b2, err := unix.SysvShmAttach(id, 0, 0)
    58  	if err != nil {
    59  		t.Fatalf("Attach: %v", err)
    60  	}
    61  
    62  	if len(b2) != 1024 {
    63  		t.Fatalf("b2 len = %v, want 1024", len(b1))
    64  	}
    65  
    66  	b2[43] = 'y'
    67  	if b2[42] != 'x' || b1[43] != 'y' {
    68  		t.Fatalf("shared memory isn't shared")
    69  	}
    70  
    71  	// detach
    72  	if err = unix.SysvShmDetach(b2); err != nil {
    73  		t.Fatalf("Detach: %v", err)
    74  	}
    75  
    76  	if b1[42] != 'x' || b1[43] != 'y' {
    77  		t.Fatalf("shared memory was invalidated")
    78  	}
    79  }