-
Notifications
You must be signed in to change notification settings - Fork 52
/
readonly_test.go
93 lines (78 loc) · 1.92 KB
/
readonly_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
package vfs
import (
"errors"
"os"
"testing"
)
var (
errDummy = errors.New("Not implemented")
// Complete dummy base
baseFSDummy = Dummy(errDummy)
ro = ReadOnly(baseFSDummy)
)
func TestROInterface(t *testing.T) {
_ = Filesystem(ro)
}
func TestROOpenFileFlags(t *testing.T) {
_, err := ro.OpenFile("name", os.O_CREATE, 0666)
if err != ErrReadOnly {
t.Errorf("Create error expected")
}
_, err = ro.OpenFile("name", os.O_APPEND, 0666)
if err != ErrReadOnly {
t.Errorf("Append error expected")
}
_, err = ro.OpenFile("name", os.O_WRONLY, 0666)
if err != ErrReadOnly {
t.Errorf("WROnly error expected")
}
_, err = ro.OpenFile("name", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != ErrReadOnly {
t.Errorf("Error expected")
}
// os.O_RDWR is allowed, dummy error is returned
_, err = ro.OpenFile("name", os.O_RDWR, 0)
if err != errDummy {
t.Errorf("Expected dummy error")
}
}
func TestRORemove(t *testing.T) {
err := ro.Remove("test")
if err != ErrReadOnly {
t.Errorf("Remove error expected")
}
}
func TestRORename(t *testing.T) {
err := ro.Rename("old", "new")
if err != ErrReadOnly {
t.Errorf("Rename error expected")
}
}
func TestMkDir(t *testing.T) {
err := ro.Mkdir("test", 0777)
if err != ErrReadOnly {
t.Errorf("Mkdir error expected")
}
}
type writeDummyFS struct {
Filesystem
}
// Opens a dummyfile instead of error
func (fs writeDummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
return DummyFile(errDummy), nil
}
func TestROOpenFileWrite(t *testing.T) {
// Dummy base with mocked OpenFile for write test
roWriteMock := ReadOnly(writeDummyFS{Dummy(errDummy)})
f, err := roWriteMock.OpenFile("name", os.O_RDWR, 0)
if err != nil {
t.Errorf("No OpenFile error expected: %s", err)
}
written, err := f.Write([]byte("test"))
if err != ErrReadOnly {
t.Errorf("Error expected: %s", err)
}
if written > 0 {
t.Errorf("Written expected 0: %d", written)
}
}