-
Notifications
You must be signed in to change notification settings - Fork 43
/
gommap_test.go
153 lines (120 loc) · 3.3 KB
/
gommap_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
// +build !windows
package gommap
import (
"io/ioutil"
"os"
"path"
"syscall"
"testing"
. "gopkg.in/check.v1"
)
func TestAll(t *testing.T) {
TestingT(t)
}
type S struct {
file *os.File
}
var _ = Suite(&S{})
var testData = []byte("0123456789ABCDEF")
func (s *S) SetUpTest(c *C) {
testPath := path.Join(c.MkDir(), "test.txt")
file, err := os.Create(testPath)
if err != nil {
panic(err.Error())
}
s.file = file
s.file.Write(testData)
}
func (s *S) TearDownTest(c *C) {
s.file.Close()
}
func (s *S) TestUnsafeUnmap(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_SHARED)
c.Assert(err, IsNil)
c.Assert(mmap.UnsafeUnmap(), IsNil)
}
func (s *S) TestReadWrite(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_SHARED)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
c.Assert([]byte(mmap), DeepEquals, testData)
mmap[9] = 'X'
mmap.Sync(MS_SYNC)
fileData, err := ioutil.ReadFile(s.file.Name())
c.Assert(err, IsNil)
c.Assert(fileData, DeepEquals, []byte("012345678XABCDEF"))
}
func (s *S) TestSliceMethods(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_SHARED)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
c.Assert([]byte(mmap), DeepEquals, testData)
mmap[9] = 'X'
mmap[7:10].Sync(MS_SYNC)
fileData, err := ioutil.ReadFile(s.file.Name())
c.Assert(err, IsNil)
c.Assert(fileData, DeepEquals, []byte("012345678XABCDEF"))
}
func (s *S) TestProtFlagsAndErr(c *C) {
testPath := s.file.Name()
s.file.Close()
file, err := os.Open(testPath)
c.Assert(err, IsNil)
s.file = file
_, err = Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_SHARED)
// For this to happen, both the error and the protection flag must work.
c.Assert(err, Equals, syscall.EACCES)
}
func (s *S) TestFlags(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_PRIVATE)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
mmap[9] = 'X'
mmap.Sync(MS_SYNC)
fileData, err := ioutil.ReadFile(s.file.Name())
c.Assert(err, IsNil)
// Shouldn't have written, since the map is private.
c.Assert(fileData, DeepEquals, []byte("0123456789ABCDEF"))
}
func (s *S) TestAdvise(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_PRIVATE)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
// A bit tricky to blackbox-test these.
err = mmap.Advise(MADV_RANDOM)
c.Assert(err, IsNil)
err = mmap.Advise(9999)
c.Assert(err, ErrorMatches, "invalid argument")
}
func (s *S) TestProtect(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ, MAP_SHARED)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
c.Assert([]byte(mmap), DeepEquals, testData)
err = mmap.Protect(PROT_READ | PROT_WRITE)
c.Assert(err, IsNil)
// If this operation doesn't blow up tests, the call above worked.
mmap[9] = 'X'
}
func (s *S) TestLock(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_PRIVATE)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
// A bit tricky to blackbox-test these.
err = mmap.Lock()
c.Assert(err, IsNil)
err = mmap.Lock()
c.Assert(err, IsNil)
err = mmap.Unlock()
c.Assert(err, IsNil)
err = mmap.Unlock()
c.Assert(err, IsNil)
}
func (s *S) TestIsResidentUnderOnePage(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_PRIVATE)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()
mapped, err := mmap.IsResident()
c.Assert(err, IsNil)
c.Assert(mapped, DeepEquals, []bool{true})
}