gopkg.in/ro-ag/posix.v1@v1.0.6/shm_open_darwin_test.go (about) 1 package posix_test 2 3 import ( 4 "fmt" 5 "gopkg.in/ro-ag/posix.v1" 6 "log" 7 "runtime/debug" 8 "syscall" 9 "testing" 10 "unsafe" 11 ) 12 13 func TestShmAnonymous(t *testing.T) { 14 type args struct { 15 name string 16 } 17 tests := []struct { 18 name string 19 args args 20 wantErr bool 21 }{ 22 {"regular", args{"regular"}, false}, 23 } 24 for _, tt := range tests { 25 t.Run(tt.name, func(t *testing.T) { 26 gotFd, err := posix.ShmAnonymous() 27 if (err != nil) != tt.wantErr { 28 t.Errorf("ShmAnonymous() error = %v, wantErr %v", err, tt.wantErr) 29 return 30 } 31 if gotFd == -1 { 32 t.Errorf("ShmAnonymous() got %v", gotFd) 33 } 34 }) 35 } 36 } 37 38 func TestMemfdCreateParallel(t *testing.T) { 39 for i := 0; i < 100; i++ { 40 name := fmt.Sprintf("name-%.3d", i) 41 t.Run(name, func(t *testing.T) { 42 t.Parallel() 43 fd, err := posix.MemfdCreate("name"+ 44 ""+ 45 "", posix.MFD_ALLOW_SEALING) 46 if err != nil { 47 t.Errorf("MemfdCreate() error = %v %s", err, posix.ErrnoName(err.(syscall.Errno))) 48 return 49 } else { 50 log.Println("Got ", fd) 51 } 52 }) 53 } 54 55 } 56 57 func TestMemfdCreate(t *testing.T) { 58 type args struct { 59 name string 60 flags int 61 } 62 tests := []struct { 63 name string 64 args args 65 wantErr bool 66 }{ 67 {"regular", args{"regular", posix.MFD_ALLOW_SEALING}, false}, 68 {"repeat-1", args{"regular", 0}, false}, 69 {"repeat-2", args{"regular", 0}, false}, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 fd, err := posix.MemfdCreate(tt.args.name, tt.args.flags) 74 if (err != nil) != tt.wantErr { 75 t.Errorf("MemfdCreate() error = %v, wantErr %v", err, tt.wantErr) 76 return 77 } else { 78 log.Println("Got ", fd) 79 } 80 }) 81 } 82 } 83 84 func TestMprotect(t *testing.T) { 85 var buf []byte 86 var err error 87 bts := []byte("test to write") 88 89 defer func() { 90 _ = posix.Munmap(buf) 91 }() 92 protect := func(flags int) { 93 if err = posix.Mprotect(buf, flags); err != nil { 94 t.Log(posix.ErrnoName(err.(posix.Errno))) 95 t.Errorf("Mprotect() error = %v", err) 96 return 97 } 98 } 99 100 write := func(name string, catch bool) { 101 n := 0 102 if catch { 103 debug.SetPanicOnFault(true) 104 defer debug.SetPanicOnFault(false) 105 defer func() { 106 if err := recover(); err != nil { 107 if n != 0 { 108 t.Errorf("%s(write-recoverd): bytes written = %v", name, n) 109 } 110 } else { 111 t.Errorf("%s: Expects system failure", name) 112 } 113 }() 114 } 115 if n = copy(buf, bts); n <= 0 { 116 t.Errorf("%s(write): bytes written = %v", name, n) 117 } 118 } 119 120 read := func(name string, catch bool) { 121 news := "" 122 if catch { 123 debug.SetPanicOnFault(true) 124 defer debug.SetPanicOnFault(false) 125 defer func() { 126 if err := recover(); err != nil { 127 if news != "" { 128 t.Errorf("%s(write-recoverd): bytes read = %v", name, news) 129 } 130 } else { 131 t.Errorf("%s: Expects system failure", name) 132 } 133 }() 134 } 135 136 if news = string(buf[:13]); news != string(bts) { 137 t.Errorf("%s(write): bytes readfail", name) 138 } 139 } 140 141 t.Run("mmap", func(t *testing.T) { 142 if buf, _, err = posix.Mmap(unsafe.Pointer(uintptr(0)), posix.Getpagesize(), posix.PROT_NONE, posix.MAP_ANON|posix.MAP_PRIVATE, 0, 0); err != nil { 143 t.Log(posix.ErrnoName(err.(posix.Errno))) 144 t.Errorf("Mmap() error = %v", err) 145 return 146 } 147 }) 148 149 t.Run("PROT_RDWR", func(t *testing.T) { 150 protect(posix.PROT_RDWR) 151 write("PROT_RDWR", false) 152 read("PROT_RDWR", false) 153 }) 154 155 t.Run("PROT_NONE", func(t *testing.T) { 156 protect(posix.PROT_NONE) 157 write("PROT_NONE", true) 158 write("PROT_NONE", true) 159 }) 160 161 t.Run("PROT_WRITE", func(t *testing.T) { 162 protect(posix.PROT_WRITE) 163 write("PROT_WRITE", false) 164 read("PROT_WRITE", false) 165 }) 166 167 t.Run("PROT_READ", func(t *testing.T) { 168 protect(posix.PROT_READ) 169 write("PROT_READ", true) 170 read("PROT_READ", false) 171 }) 172 }