-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
backupfile_test.go
276 lines (228 loc) · 7.19 KB
/
backupfile_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
package chanbackup
import (
"bytes"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/lightningnetwork/lnd/lnencrypt"
"github.com/stretchr/testify/require"
)
func makeFakePackedMulti() (PackedMulti, error) {
newPackedMulti := make([]byte, 50)
if _, err := rand.Read(newPackedMulti[:]); err != nil {
return nil, fmt.Errorf("unable to make test backup: %w", err)
}
return PackedMulti(newPackedMulti), nil
}
func assertBackupMatches(t *testing.T, filePath string,
currentBackup PackedMulti) {
t.Helper()
packedBackup, err := os.ReadFile(filePath)
require.NoError(t, err, "unable to test file")
if !bytes.Equal(packedBackup, currentBackup) {
t.Fatalf("backups don't match after first swap: "+
"expected %x got %x", packedBackup[:],
currentBackup)
}
}
func assertFileDeleted(t *testing.T, filePath string) {
t.Helper()
_, err := os.Stat(filePath)
if err == nil {
t.Fatalf("file %v still exists: ", filePath)
}
}
// TestUpdateAndSwap test that we're able to properly swap out old backups on
// disk with new ones. Additionally, after a swap operation succeeds, then each
// time we should only have the main backup file on disk, as the temporary file
// has been removed.
func TestUpdateAndSwap(t *testing.T) {
t.Parallel()
tempTestDir := t.TempDir()
testCases := []struct {
fileName string
tempFileName string
oldTempExists bool
valid bool
}{
// Main file name is blank, should fail.
{
fileName: "",
valid: false,
},
// Old temporary file still exists, should be removed. Only one
// file should remain.
{
fileName: filepath.Join(
tempTestDir, DefaultBackupFileName,
),
tempFileName: filepath.Join(
tempTestDir, DefaultTempBackupFileName,
),
oldTempExists: true,
valid: true,
},
// Old temp doesn't exist, should swap out file, only a single
// file remains.
{
fileName: filepath.Join(
tempTestDir, DefaultBackupFileName,
),
tempFileName: filepath.Join(
tempTestDir, DefaultTempBackupFileName,
),
valid: true,
},
}
for i, testCase := range testCases {
backupFile := NewMultiFile(testCase.fileName)
// To start with, we'll make a random byte slice that'll pose
// as our packed multi backup.
newPackedMulti, err := makeFakePackedMulti()
if err != nil {
t.Fatalf("unable to make test backup: %v", err)
}
// If the old temporary file is meant to exist, then we'll
// create it now as an empty file.
if testCase.oldTempExists {
f, err := os.Create(testCase.tempFileName)
if err != nil {
t.Fatalf("unable to create temp file: %v", err)
}
require.NoError(t, f.Close())
// TODO(roasbeef): mock out fs calls?
}
// With our backup created, we'll now attempt to swap out this
// backup, for the old one.
err = backupFile.UpdateAndSwap(PackedMulti(newPackedMulti))
switch {
// If this is a valid test case, and we failed, then we'll
// return an error.
case err != nil && testCase.valid:
t.Fatalf("#%v, unable to swap file: %v", i, err)
// If this is an invalid test case, and we passed it, then
// we'll return an error.
case err == nil && !testCase.valid:
t.Fatalf("#%v file swap should have failed: %v", i, err)
}
if !testCase.valid {
continue
}
// If we read out the file on disk, then it should match
// exactly what we wrote. The temp backup file should also be
// gone.
assertBackupMatches(t, testCase.fileName, newPackedMulti)
assertFileDeleted(t, testCase.tempFileName)
// Now that we know this is a valid test case, we'll make a new
// packed multi to swap out this current one.
newPackedMulti2, err := makeFakePackedMulti()
if err != nil {
t.Fatalf("unable to make test backup: %v", err)
}
// We'll then attempt to swap the old version for this new one.
err = backupFile.UpdateAndSwap(PackedMulti(newPackedMulti2))
if err != nil {
t.Fatalf("unable to swap file: %v", err)
}
// Once again, the file written on disk should have been
// properly swapped out with the new instance.
assertBackupMatches(t, testCase.fileName, newPackedMulti2)
// Additionally, we shouldn't be able to find the temp backup
// file on disk, as it should be deleted each time.
assertFileDeleted(t, testCase.tempFileName)
}
}
func assertMultiEqual(t *testing.T, a, b *Multi) {
if len(a.StaticBackups) != len(b.StaticBackups) {
t.Fatalf("expected %v backups, got %v", len(a.StaticBackups),
len(b.StaticBackups))
}
for i := 0; i < len(a.StaticBackups); i++ {
assertSingleEqual(t, a.StaticBackups[i], b.StaticBackups[i])
}
}
// TestExtractMulti tests that given a valid packed multi file on disk, we're
// able to read it multiple times repeatedly.
func TestExtractMulti(t *testing.T) {
t.Parallel()
keyRing := &lnencrypt.MockKeyRing{}
// First, as prep, we'll create a single chan backup, then pack that
// fully into a multi backup.
channel, err := genRandomOpenChannelShell()
require.NoError(t, err, "unable to gen chan")
singleBackup := NewSingle(channel, nil)
var b bytes.Buffer
unpackedMulti := Multi{
StaticBackups: []Single{singleBackup},
}
err = unpackedMulti.PackToWriter(&b, keyRing)
require.NoError(t, err, "unable to pack to writer")
packedMulti := PackedMulti(b.Bytes())
// Finally, we'll make a new temporary file, then write out the packed
// multi directly to it.
tempFile, err := os.CreateTemp("", "")
require.NoError(t, err, "unable to create temp file")
t.Cleanup(func() {
require.NoError(t, tempFile.Close())
require.NoError(t, os.Remove(tempFile.Name()))
})
_, err = tempFile.Write(packedMulti)
require.NoError(t, err, "unable to write temp file")
if err := tempFile.Sync(); err != nil {
t.Fatalf("unable to sync temp file: %v", err)
}
testCases := []struct {
fileName string
pass bool
}{
// Main file not read, file name not present.
{
fileName: "",
pass: false,
},
// Main file not read, file name is there, but file doesn't
// exist.
{
fileName: "kek",
pass: false,
},
// Main file not read, should be able to read multiple times.
{
fileName: tempFile.Name(),
pass: true,
},
}
for i, testCase := range testCases {
// First, we'll make our backup file with the specified name.
backupFile := NewMultiFile(testCase.fileName)
// With our file made, we'll now attempt to read out the
// multi-file.
freshUnpackedMulti, err := backupFile.ExtractMulti(keyRing)
switch {
// If this is a valid test case, and we failed, then we'll
// return an error.
case err != nil && testCase.pass:
t.Fatalf("#%v, unable to extract file: %v", i, err)
// If this is an invalid test case, and we passed it, then
// we'll return an error.
case err == nil && !testCase.pass:
t.Fatalf("#%v file extraction should have "+
"failed: %v", i, err)
}
if !testCase.pass {
continue
}
// We'll now ensure that the unpacked multi we read is
// identical to the one we wrote out above.
assertMultiEqual(t, &unpackedMulti, freshUnpackedMulti)
// We should also be able to read the file again, as we have an
// existing handle to it.
freshUnpackedMulti, err = backupFile.ExtractMulti(keyRing)
if err != nil {
t.Fatalf("unable to unpack multi: %v", err)
}
assertMultiEqual(t, &unpackedMulti, freshUnpackedMulti)
}
}