-
Notifications
You must be signed in to change notification settings - Fork 98
/
litefs_test.go
279 lines (247 loc) · 8.17 KB
/
litefs_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
package litefs_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/superfly/litefs"
)
func TestFileType_IsValid(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
for _, typ := range []litefs.FileType{
litefs.FileTypeDatabase,
litefs.FileTypeJournal,
litefs.FileTypeWAL,
litefs.FileTypeSHM,
} {
if !typ.IsValid() {
t.Fatalf("expected valid for %d", typ)
}
}
})
t.Run("Invalid", func(t *testing.T) {
if litefs.FileType(100).IsValid() {
t.Fatalf("expected invalid")
}
})
t.Run("None", func(t *testing.T) {
if litefs.FileTypeNone.IsValid() {
t.Fatalf("expected invalid")
}
})
}
func TestWALReader(t *testing.T) {
t.Run("OK", func(t *testing.T) {
buf := make([]byte, 4096)
b, err := os.ReadFile("testdata/wal-reader/ok/wal")
if err != nil {
t.Fatal(err)
}
// Initialize reader with header info.
r := litefs.NewWALReader(bytes.NewReader(b))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if got, want := r.PageSize(), uint32(4096); got != want {
t.Fatalf("PageSize()=%d, want %d", got, want)
} else if got, want := r.Offset(), int64(0); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
// Read first frame.
if pgno, commit, err := r.ReadFrame(buf); err != nil {
t.Fatal(err)
} else if got, want := pgno, uint32(1); got != want {
t.Fatalf("pgno=%d, want %d", got, want)
} else if got, want := commit, uint32(0); got != want {
t.Fatalf("commit=%d, want %d", got, want)
} else if !bytes.Equal(buf, b[56:4152]) {
t.Fatal("page data mismatch")
} else if got, want := r.Offset(), int64(32); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
// Read second frame. End of transaction.
if pgno, commit, err := r.ReadFrame(buf); err != nil {
t.Fatal(err)
} else if got, want := pgno, uint32(2); got != want {
t.Fatalf("pgno=%d, want %d", got, want)
} else if got, want := commit, uint32(2); got != want {
t.Fatalf("commit=%d, want %d", got, want)
} else if !bytes.Equal(buf, b[4176:8272]) {
t.Fatal("page data mismatch")
} else if got, want := r.Offset(), int64(4152); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
// Read third frame.
if pgno, commit, err := r.ReadFrame(buf); err != nil {
t.Fatal(err)
} else if got, want := pgno, uint32(2); got != want {
t.Fatalf("pgno=%d, want %d", got, want)
} else if got, want := commit, uint32(2); got != want {
t.Fatalf("commit=%d, want %d", got, want)
} else if !bytes.Equal(buf, b[8296:12392]) {
t.Fatal("page data mismatch")
} else if got, want := r.Offset(), int64(8272); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
if _, _, err := r.ReadFrame(buf); err != io.EOF {
t.Fatalf("unexpected error: %s", err)
}
})
t.Run("SaltMismatch", func(t *testing.T) {
buf := make([]byte, 4096)
b, err := os.ReadFile("testdata/wal-reader/salt-mismatch/wal")
if err != nil {
t.Fatal(err)
}
// Initialize reader with header info.
r := litefs.NewWALReader(bytes.NewReader(b))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if got, want := r.PageSize(), uint32(4096); got != want {
t.Fatalf("PageSize()=%d, want %d", got, want)
} else if got, want := r.Offset(), int64(0); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
// Read first frame.
if pgno, commit, err := r.ReadFrame(buf); err != nil {
t.Fatal(err)
} else if got, want := pgno, uint32(1); got != want {
t.Fatalf("pgno=%d, want %d", got, want)
} else if got, want := commit, uint32(0); got != want {
t.Fatalf("commit=%d, want %d", got, want)
} else if !bytes.Equal(buf, b[56:4152]) {
t.Fatal("page data mismatch")
}
// Read second frame. Salt has been altered so it doesn't match header.
if _, _, err := r.ReadFrame(buf); err != io.EOF {
t.Fatalf("unexpected error: %s", err)
}
})
t.Run("FrameChecksumMismatch", func(t *testing.T) {
buf := make([]byte, 4096)
b, err := os.ReadFile("testdata/wal-reader/frame-checksum-mismatch/wal")
if err != nil {
t.Fatal(err)
}
// Initialize reader with header info.
r := litefs.NewWALReader(bytes.NewReader(b))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if got, want := r.PageSize(), uint32(4096); got != want {
t.Fatalf("PageSize()=%d, want %d", got, want)
} else if got, want := r.Offset(), int64(0); got != want {
t.Fatalf("Offset()=%d, want %d", got, want)
}
// Read first frame.
if pgno, commit, err := r.ReadFrame(buf); err != nil {
t.Fatal(err)
} else if got, want := pgno, uint32(1); got != want {
t.Fatalf("pgno=%d, want %d", got, want)
} else if got, want := commit, uint32(0); got != want {
t.Fatalf("commit=%d, want %d", got, want)
} else if !bytes.Equal(buf, b[56:4152]) {
t.Fatal("page data mismatch")
}
// Read second frame. Checksum has been altered so it doesn't match.
if _, _, err := r.ReadFrame(buf); err != io.EOF {
t.Fatalf("unexpected error: %s", err)
}
})
t.Run("ZeroLength", func(t *testing.T) {
r := litefs.NewWALReader(bytes.NewReader(nil))
if err := r.ReadHeader(); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("PartialHeader", func(t *testing.T) {
r := litefs.NewWALReader(bytes.NewReader(make([]byte, 10)))
if err := r.ReadHeader(); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("BadMagic", func(t *testing.T) {
r := litefs.NewWALReader(bytes.NewReader(make([]byte, 32)))
if err := r.ReadHeader(); err == nil || err.Error() != `invalid wal header magic: 0` {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("BadHeaderChecksum", func(t *testing.T) {
data := []byte{
0x37, 0x7f, 0x06, 0x83, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
r := litefs.NewWALReader(bytes.NewReader(data))
if err := r.ReadHeader(); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("BadHeaderVersion", func(t *testing.T) {
data := []byte{
0x37, 0x7f, 0x06, 0x83, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x15, 0x7b, 0x20, 0x92, 0xbb, 0xf8, 0x34, 0x1d}
r := litefs.NewWALReader(bytes.NewReader(data))
if err := r.ReadHeader(); err == nil || err.Error() != `unsupported wal version: 1` {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("ErrBufferSize", func(t *testing.T) {
b, err := os.ReadFile("testdata/wal-reader/ok/wal")
if err != nil {
t.Fatal(err)
}
// Initialize reader with header info.
r := litefs.NewWALReader(bytes.NewReader(b))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
}
if _, _, err := r.ReadFrame(make([]byte, 512)); err == nil || err.Error() != `WALReader.ReadFrame(): buffer size (512) must match page size (4096)` {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("ErrPartialFrameHeader", func(t *testing.T) {
b, err := os.ReadFile("testdata/wal-reader/ok/wal")
if err != nil {
t.Fatal(err)
}
r := litefs.NewWALReader(bytes.NewReader(b[:40]))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if _, _, err := r.ReadFrame(make([]byte, 4096)); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("ErrFrameHeaderOnly", func(t *testing.T) {
b, err := os.ReadFile("testdata/wal-reader/ok/wal")
if err != nil {
t.Fatal(err)
}
r := litefs.NewWALReader(bytes.NewReader(b[:56]))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if _, _, err := r.ReadFrame(make([]byte, 4096)); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
t.Run("ErrPartialFrameData", func(t *testing.T) {
b, err := os.ReadFile("testdata/wal-reader/ok/wal")
if err != nil {
t.Fatal(err)
}
r := litefs.NewWALReader(bytes.NewReader(b[:1000]))
if err := r.ReadHeader(); err != nil {
t.Fatal(err)
} else if _, _, err := r.ReadFrame(make([]byte, 4096)); err != io.EOF {
t.Fatalf("unexpected error: %#v", err)
}
})
}
type errWriter struct{ afterN int }
func (w *errWriter) Write(p []byte) (int, error) {
if w.afterN -= len(p); w.afterN <= 0 {
return 0, fmt.Errorf("write error occurred")
}
return len(p), nil
}