-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
168 lines (136 loc) · 3.76 KB
/
file_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
package aferofuse
import (
"fmt"
"io"
"os"
"testing"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/fuse/nodefs"
"github.com/matryer/is"
)
type testableFile struct {
fns []string
err error
n int
read []byte
write []byte
trunc int64
close bool
}
func (file *testableFile) Close() error {
file.fns = append(file.fns, "close")
if file.err != nil {
return file.err
}
file.close = true
return nil
}
// useless (in the context of this package) and unused file functions
func (file *testableFile) Read(p []byte) (n int, err error) { return }
func (file *testableFile) Readdir(p int) (n []os.FileInfo, err error) { return }
func (file *testableFile) Readdirnames(p int) (n []string, err error) { return }
func (file *testableFile) Write(p []byte) (n int, err error) { return }
func (file *testableFile) Seek(int64, int) (n int64, err error) { return }
func (file *testableFile) Stat() (n os.FileInfo, err error) { return }
func (file *testableFile) Sync() (err error) { return }
func (file *testableFile) WriteString(s string) (n int, err error) { return }
func (file *testableFile) Name() (str string) { return }
func (file *testableFile) ReadAt(p []byte, off int64) (n int, err error) {
file.fns = append(file.fns, fmt.Sprintf("readat %v %v", p, off))
n, err = file.n, file.err
if err == nil {
n = copy(p, file.read)
}
return
}
func (file *testableFile) WriteAt(p []byte, off int64) (n int, err error) {
file.fns = append(file.fns, fmt.Sprintf("writeat %v %v", p, off))
n, err = file.n, file.err
if err == nil {
n = copy(file.write, p)
}
return
}
func (file *testableFile) Truncate(size int64) (err error) {
file.fns = append(file.fns, fmt.Sprintf("truncate %d", size))
err = file.err
if err != nil {
return
}
file.trunc = size
return
}
func TestFileRead(t *testing.T) {
is := is.New(t)
file := &testableFile{}
fuseFile := &aferoFuseFile{
File: nodefs.NewDefaultFile(),
file: file,
}
file.err = io.EOF
res, status := fuseFile.Read([]byte{}, 0)
is.Equal(res.Size(), file.n)
is.Equal(fuse.OK, status)
file.err = os.ErrNotExist
file.n = 14
res, status = fuseFile.Read([]byte{}, 0)
is.True(res == nil)
is.Equal(fuse.ToStatus(file.err), status)
file.err = nil
file.read = []byte{1, 2, 3, 4}
aferoBytes := make([]byte, 64)
res, status = fuseFile.Read(aferoBytes, 0)
is.Equal(res.Size(), 64)
fuseBytes, _ := res.Bytes(nil)
is.Equal(aferoBytes[:4], fuseBytes[:4])
is.Equal(fuse.ToStatus(file.err), status)
}
func TestFileWrite(t *testing.T) {
is := is.New(t)
file := &testableFile{}
fuseFile := &aferoFuseFile{
File: nodefs.NewDefaultFile(),
file: file,
}
file.err = io.EOF
n, status := fuseFile.Write([]byte{}, 0)
is.Equal(n, uint32(file.n))
is.Equal(fuse.OK, status)
file.err = os.ErrNotExist
file.n = 14
n, status = fuseFile.Write([]byte{}, 0)
is.Equal(fuse.ToStatus(file.err), status)
file.err = nil
file.write = make([]byte, 64)
aferoBytes := make([]byte, 64)
for i := range aferoBytes {
aferoBytes[i] = byte(i)
}
n, status = fuseFile.Write(aferoBytes, 0)
is.Equal(aferoBytes, file.write)
is.Equal(fuse.OK, status)
}
func TestFileTruncate(t *testing.T) {
is := is.New(t)
file := &testableFile{}
fuseFile := &aferoFuseFile{
File: nodefs.NewDefaultFile(),
file: file,
}
file.err = os.ErrNotExist
is.Equal(fuse.ToStatus(file.err), fuseFile.Truncate(123))
file.err = nil
is.Equal(fuse.ToStatus(file.err), fuseFile.Truncate(123))
is.Equal(file.trunc, int64(123))
}
func TestFileClose(t *testing.T) {
is := is.New(t)
file := &testableFile{}
fuseFile := &aferoFuseFile{
File: nodefs.NewDefaultFile(),
file: file,
}
fuseFile.Release()
is.Equal(file.close, true)
}
func TestEmptyTest(t *testing.T) {}