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