-
Notifications
You must be signed in to change notification settings - Fork 128
/
mmap_test.go
173 lines (147 loc) · 3.62 KB
/
mmap_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
173
// Copyright 2011 Evan Shaw. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// These tests are adapted from gommap: http://labix.org/gommap
// Copyright (c) 2010, Gustavo Niemeyer <[email protected]>
package mmap
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
var testData = []byte("0123456789ABCDEF")
var testPath = filepath.Join(os.TempDir(), "testdata")
func init() {
f := openFile(os.O_RDWR | os.O_CREATE | os.O_TRUNC)
f.Write(testData)
f.Close()
}
func openFile(flags int) *os.File {
f, err := os.OpenFile(testPath, flags, 0644)
if err != nil {
panic(err.Error())
}
return f
}
func TestUnmap(t *testing.T) {
f := openFile(os.O_RDONLY)
defer f.Close()
mmap, err := Map(f, RDONLY, 0)
if err != nil {
t.Errorf("error mapping: %s", err)
}
if err := mmap.Unmap(); err != nil {
t.Errorf("error unmapping: %s", err)
}
}
func TestReadWrite(t *testing.T) {
f := openFile(os.O_RDWR)
defer f.Close()
mmap, err := Map(f, RDWR, 0)
if err != nil {
t.Errorf("error mapping: %s", err)
}
defer mmap.Unmap()
if !bytes.Equal(testData, mmap) {
t.Errorf("mmap != testData: %q, %q", mmap, testData)
}
mmap[9] = 'X'
mmap.Flush()
fileData, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("error reading file: %s", err)
}
if !bytes.Equal(fileData, []byte("012345678XABCDEF")) {
t.Errorf("file wasn't modified")
}
// leave things how we found them
mmap[9] = '9'
mmap.Flush()
}
func TestProtFlagsAndErr(t *testing.T) {
f := openFile(os.O_RDONLY)
defer f.Close()
if _, err := Map(f, RDWR, 0); err == nil {
t.Errorf("expected error")
}
}
func TestFlags(t *testing.T) {
f := openFile(os.O_RDWR)
defer f.Close()
mmap, err := Map(f, COPY, 0)
if err != nil {
t.Errorf("error mapping: %s", err)
}
defer mmap.Unmap()
mmap[9] = 'X'
mmap.Flush()
fileData, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("error reading file: %s", err)
}
if !bytes.Equal(fileData, testData) {
t.Errorf("file was modified")
}
}
// Test that we can map files from non-0 offsets
// The page size on most Unixes is 4KB, but on Windows it's 64KB
func TestNonZeroOffset(t *testing.T) {
const pageSize = 65536
// Create a 2-page sized file
bigFilePath := filepath.Join(os.TempDir(), "nonzero")
fileobj, err := os.OpenFile(bigFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
panic(err.Error())
}
bigData := make([]byte, 2*pageSize, 2*pageSize)
fileobj.Write(bigData)
fileobj.Close()
// Map the first page by itself
fileobj, err = os.OpenFile(bigFilePath, os.O_RDONLY, 0)
if err != nil {
panic(err.Error())
}
m, err := MapRegion(fileobj, pageSize, RDONLY, 0, 0)
if err != nil {
t.Errorf("error mapping file: %s", err)
}
m.Unmap()
fileobj.Close()
// Map the second page by itself
fileobj, err = os.OpenFile(bigFilePath, os.O_RDONLY, 0)
if err != nil {
panic(err.Error())
}
m, err = MapRegion(fileobj, pageSize, RDONLY, 0, pageSize)
if err != nil {
t.Errorf("error mapping file: %s", err)
}
err = m.Unmap()
if err != nil {
t.Error(err)
}
m, err = MapRegion(fileobj, pageSize, RDONLY, 0, 1)
if err == nil {
t.Error("expect error because offset is not multiple of page size")
}
fileobj.Close()
}
func TestAnonymousMapping(t *testing.T) {
const size = 4 * 1024
// Make an anonymous region
mem, err := MapRegion(nil, size, RDWR, ANON, 0)
if err != nil {
t.Fatalf("failed to allocate memory for buffer: %v", err)
}
// Check memory writable
for i := 0; i < size; i++ {
mem[i] = 0x55
}
// And unmap it
err = mem.Unmap()
if err != nil {
t.Fatalf("failed to unmap memory for buffer: %v", err)
}
}