-
Notifications
You must be signed in to change notification settings - Fork 5
/
mmap_test.go
676 lines (593 loc) · 16.4 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package mmap
import (
"archive/zip"
"bytes"
"io/ioutil"
"os"
"strings"
"syscall"
"testing"
)
var (
protPage = syscall.PROT_READ | syscall.PROT_WRITE
testData = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
testPath = "/tmp/m.txt"
)
func setup(t *testing.T) {
f, err := os.OpenFile(testPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
if _, err := f.Write(testData); err != nil {
t.Fatalf("error in writing to file :: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}
func tearDown(t *testing.T) {
if err := os.Remove(testPath); err != nil {
t.Fatalf("error in deleting file :: %v", err)
}
}
func TestUnmap(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
if err := m.Unmap(); err != nil {
t.Fatalf("error in unmapping :: %v", err)
}
}
func TestReadWrite(t *testing.T) {
setup(t)
defer tearDown(t)
f, errFile := os.OpenFile(testPath, os.O_RDWR, 0644)
if errFile != nil {
t.Fatalf("error in opening file :: %v", errFile)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, errMmap := NewSharedFileMmap(f, 0, len(testData), protPage)
if errMmap != nil {
t.Fatalf("error in mapping :: %v", errMmap)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
// Read
data := make([]byte, len(testData))
if _, err := m.ReadAt(data, 0); err != nil {
t.Fatalf("error in reading :: %v", err)
}
if !bytes.Equal(testData, data) {
t.Fatalf("mapped data is not equal testData: %v, %v", data, testData)
}
// Read slice bigger than mapped region after offset
lenData := len(testData) + 10
data = make([]byte, lenData)
if _, err := m.ReadAt(data, 0); err != nil {
t.Fatalf("error in reading :: %v", err)
}
if !bytes.Equal(testData, data[:len(testData)]) {
t.Fatalf("mapped data is not equal testData: %v, %v", data[:len(testData)], testData)
}
if !bytes.Equal(data[len(testData):], make([]byte, 10)) {
t.Fatalf("mapped data is not equal testData: %v, %v", data[:len(testData)], testData)
}
// Read offset larger than size of mapped region
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("unexpected error in reading from mmaped region :: %v", err)
}
}()
_, _ = m.ReadAt(data, 100)
}()
// Write
if _, err := m.WriteAt([]byte("a"), 9); err != nil {
t.Fatalf("error in writing to mapped area :: %v", err)
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
f1, errFile := os.OpenFile(testPath, os.O_RDWR, 0644)
if errFile != nil {
t.Fatalf("error in opening file :: %v", errFile)
}
fileData, errFile := ioutil.ReadAll(f1)
if errFile != nil {
t.Fatalf("error in reading file :: %s", errFile)
}
if err := f1.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
if !bytes.Equal(fileData, []byte("012345678aABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
t.Fatalf("no modification in file :: %v", string(fileData))
}
// Write slice bigger than mapped region after offset
if _, err := m.WriteAt([]byte("abc"), 34); err != nil {
t.Fatalf("error in writing to mapped region :: %v", err)
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in flushing mapped region :: %v", err)
}
f2, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
fileData, err = ioutil.ReadAll(f2)
if err != nil {
t.Fatalf("error in reading file :: %s", err)
}
if err := f2.Close(); err != nil {
t.Fatalf("error in closing file :: %s", err)
}
if !bytes.Equal(fileData, []byte("012345678aABCDEFGHIJKLMNOPQRSTUVWXab")) {
t.Fatalf("no modification in file :: %v", string(fileData))
}
// Write offset larger than size of mapped region
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("unexpected error in writing to mmaped region :: %v", err)
}
}()
_, _ = m.WriteAt([]byte("a"), 100)
}()
}
func TestReadStringAt(t *testing.T) {
setup(t)
defer tearDown(t)
f, errFile := os.OpenFile(testPath, os.O_RDWR, 0644)
if errFile != nil {
t.Fatalf("error in opening file :: %v", errFile)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, errMmap := NewSharedFileMmap(f, 0, len(testData), protPage)
if errMmap != nil {
t.Fatalf("error in mapping :: %v", errMmap)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
// Read
sb := &strings.Builder{}
sb.Grow(len(testData))
if n := m.ReadStringAt(sb, 0); n != len(testData) {
t.Fatalf("expected to read string of length %v, read of length %v", len(testData), n)
}
if string(testData) != sb.String() {
t.Fatalf("mapped data is not equal testData: %v, %v", sb.String(), string(testData))
}
// Read data smaller than the mapped region
sb.Reset()
sb.Grow(len(testData) - 2)
if n := m.ReadStringAt(sb, 0); n != len(testData)-2 {
t.Fatalf("expected to read string of length %v, read of length %v", len(testData), n)
}
expectedData := string(testData[:len(testData)-2])
if expectedData != sb.String() {
t.Fatalf("mapped data is not equal testData: %v, %v", sb.String(), expectedData)
}
// Read slice bigger than mapped region after offset
sb.Reset()
sb.Grow(len(testData) + 10)
if n := m.ReadStringAt(sb, 0); n != len(testData) {
t.Fatalf("expected to read string of length %v, read of length %v", len(testData), n)
}
if string(testData) != sb.String() {
t.Fatalf("mapped data is not equal testData: %v, %v", sb.String(), string(testData))
}
// Read offset larger than size of mapped region
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("unexpected error in reading from mmaped region :: %v", err)
}
}()
sb.Reset()
sb.Grow(10)
_ = m.ReadStringAt(sb, 100)
}()
}
func TestWriteStringAt(t *testing.T) {
setup(t)
defer tearDown(t)
f, errFile := os.OpenFile(testPath, os.O_RDWR, 0644)
if errFile != nil {
t.Fatalf("error in opening file :: %v", errFile)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, errMmap := NewSharedFileMmap(f, 0, len(testData), protPage)
if errMmap != nil {
t.Fatalf("error in mapping :: %v", errMmap)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
// Write
_ = m.WriteStringAt("a", 9)
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
f1, errFile := os.OpenFile(testPath, os.O_RDWR, 0644)
if errFile != nil {
t.Fatalf("error in opening file :: %v", errFile)
}
fileData, errFile := ioutil.ReadAll(f1)
if errFile != nil {
t.Fatalf("error in reading file :: %s", errFile)
}
if err := f1.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
if !bytes.Equal(fileData, []byte("012345678aABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
t.Fatalf("no modification in file :: %v", string(fileData))
}
// Write slice bigger than mapped region after offset
_ = m.WriteStringAt("abc", 34)
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in flushing mapped region :: %v", err)
}
f2, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
fileData, err = ioutil.ReadAll(f2)
if err != nil {
t.Fatalf("error in reading file :: %s", err)
}
if err := f2.Close(); err != nil {
t.Fatalf("error in closing file :: %s", err)
}
if !bytes.Equal(fileData, []byte("012345678aABCDEFGHIJKLMNOPQRSTUVWXab")) {
t.Fatalf("no modification in file :: %v", string(fileData))
}
// Write offset larger than size of mapped region
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("unexpected error in writing to mmaped region :: %v", err)
}
}()
_ = m.WriteStringAt("a", 100)
}()
}
func TestAdvise(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
if err := m.Advise(syscall.MADV_SEQUENTIAL); err != nil {
t.Fatalf("error in calling advise :: %v", err)
}
}
func TestLockUnlock(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
if err := m.Lock(); err != nil {
t.Fatalf("error in calling lock on mmap region :: %v", err)
}
if err := m.Unlock(); err != nil {
t.Fatalf("error in calling unlock on mmap region :: %v", err)
}
}
func TestReadUint64At(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
data := []byte{0x00, 0xe4, 0x0b, 0x54, 0x02, 0x00, 0x00, 0x00}
if b, err := m.WriteAt(data, 0); err != nil {
t.Fatalf("error in write :: %v", err)
} else if b != 8 {
t.Fatalf("error in writing, exp: 8, actual: %v", b)
}
expectedNum := uint64(10000000000)
actualNum := m.ReadUint64At(0)
if expectedNum != actualNum {
t.Fatalf("Error in ReadUint64At, expected: %d, actual: %d", expectedNum, actualNum)
}
}
func TestWriteUint64At(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
num := uint64(10000000000)
m.WriteUint64At(num, 0)
expectedSlice := []byte{0x00, 0xe4, 0x0b, 0x54, 0x02, 0x00, 0x00, 0x00}
actualSlice := make([]byte, 8)
if n, err := m.ReadAt(actualSlice, 0); err != nil {
t.Fatalf("error in reading :: %v", err)
} else if n != 8 {
t.Fatalf("error in reading, exp: 8, actual: %v", n)
}
if !bytes.Equal(expectedSlice, actualSlice) {
t.Fatalf("error in TestWriteUint64At, expected: %v, actual: %v",
expectedSlice, actualSlice)
}
}
func TestFailScenarios(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
if _, err := NewSharedFileMmap(f, 0, -1, protPage); err == nil {
t.Fatal("expected error, but found no error")
}
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
// should be safe to close here
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
num := uint64(10000000000)
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("different error than expected in WriteUint64At :: %v", err)
}
}()
m.WriteUint64At(num, int64(len(testData)-4))
}()
func() {
defer func() {
if err := recover(); err != ErrIndexOutOfBound {
t.Fatalf("different error than expected in WriteUint64At :: %v", err)
}
}()
_ = m.ReadUint64At(int64(len(testData) - 7))
}()
m.WriteUint64At(num, 0)
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
buf := make([]byte, 8)
func() {
defer func() {
if err := recover(); err != ErrUnmappedMemory {
t.Fatalf("different error than expected in ReadUint64At :: %v", err)
}
}()
_ = m.ReadUint64At(0)
}()
func() {
defer func() {
if err := recover(); err != ErrUnmappedMemory {
t.Fatalf("different error than expected in WriteUint64At :: %v", err)
}
}()
m.WriteUint64At(num, 0)
}()
func() {
defer func() {
if err := recover(); err != ErrUnmappedMemory {
t.Fatalf("different error than expected in ReadAt :: %v", err)
}
}()
_, _ = m.ReadAt(buf, 0)
}()
func() {
defer func() {
if err := recover(); err != ErrUnmappedMemory {
t.Fatalf("different error than expected in WriteAt :: %v", err)
}
}()
_, _ = m.WriteAt(buf, 0)
}()
}
func TestIOInterfaces(t *testing.T) {
fileName := "/tmp/io.zip"
zipData := append([]byte{80, 75, 05, 06}, bytes.Repeat([]byte{0}, 18)...)
fileSize := len(zipData)
f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
t.Fatalf("error in creating file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
defer func() {
if err := os.Remove(fileName); err != nil {
t.Fatalf("error in removing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, fileSize, protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
if err := os.Truncate(fileName, int64(fileSize)); err != nil {
t.Fatalf("error in truncating file :: %v", err)
}
if _, err := m.WriteAt(zipData, 0); err != nil {
t.Fatalf("error in writing to file :: %v", err)
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling sync :: %v", err)
}
zr, err := zip.NewReader(m, int64(fileSize))
if err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
if len(zr.File) != 0 {
t.Fatalf("zip file not read correctly, contains unexpected file")
}
}
func TestDirtyFlag(t *testing.T) {
setup(t)
defer tearDown(t)
f, err := os.OpenFile(testPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("error in opening file :: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("error in closing file :: %v", err)
}
}()
m, err := NewSharedFileMmap(f, 0, len(testData), protPage)
if err != nil {
t.Fatalf("error in mapping :: %v", err)
}
defer func() {
if err := m.Unmap(); err != nil {
t.Fatalf("error in calling unmap :: %v", err)
}
}()
m.WriteUint64At(0, 0)
if !m.dirty {
t.Fatalf("expected file to be dirty")
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
_ = m.ReadUint64At(0)
// No actual syscall here
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
_ = m.WriteStringAt("string", 0)
if !m.dirty {
t.Fatalf("expected file to be dirty")
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
sb := &strings.Builder{}
sb.Grow(len("string"))
_ = m.ReadStringAt(sb, 0)
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
_, _ = m.WriteAt([]byte{1, 2}, 0)
if !m.dirty {
t.Fatalf("expected file to be dirty")
}
if err := m.Flush(syscall.MS_SYNC); err != nil {
t.Fatalf("error in calling flush :: %v", err)
}
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
bs := make([]byte, 2)
_, _ = m.ReadAt(bs, 0)
if m.dirty {
t.Fatalf("expected file to be not dirty")
}
}