This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchive_test.go
287 lines (246 loc) · 6.45 KB
/
archive_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
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package archivist
import (
"fmt"
"testing"
"crypto/rand"
"crypto/sha256"
"bytes"
"io/ioutil"
"os"
"math/big"
"github.com/stretchr/testify/assert"
"github.com/stellar/go-stellar-base/xdr"
)
func GetTestS3Archive() *Archive {
mx := big.NewInt(0xffffffff)
r, e := rand.Int(rand.Reader, mx)
if e != nil {
panic(e)
}
return MustConnect(fmt.Sprintf("s3://history-stg.stellar.org/dev/archivist/test-%s", r),
&ConnectOptions{S3Region: "eu-west-1",})
}
func GetTestMockArchive() *Archive {
return MustConnect("mock://test", nil)
}
var tmpdirs []string
func GetTestFileArchive() *Archive {
d, e := ioutil.TempDir("/tmp", "archivist")
if e != nil {
panic(e)
}
if tmpdirs == nil {
tmpdirs = []string{d}
} else {
tmpdirs = append(tmpdirs, d)
}
return MustConnect("file://" + d, nil)
}
func cleanup() {
for _, d := range tmpdirs {
os.RemoveAll(d)
}
}
func GetTestArchive() *Archive {
ty := os.Getenv("ARCHIVIST_TEST_TYPE")
if ty == "file" {
return GetTestFileArchive()
} else if ty == "s3" {
return GetTestS3Archive()
} else {
return GetTestMockArchive()
}
}
func (arch *Archive) AddRandomBucket() (Hash, error) {
var h Hash
buf := make([]byte, 1024)
_, e := rand.Read(buf)
if e != nil {
return h, e
}
h = sha256.Sum256(buf)
pth := BucketPath(h)
e = arch.backend.PutFile(pth, ioutil.NopCloser(bytes.NewReader(buf)))
return h, e
}
func (arch *Archive) AddRandomCheckpointFile(cat string, chk uint32) error {
buf := make([]byte, 1024)
_, e := rand.Read(buf)
if e != nil {
return e
}
pth := CategoryCheckpointPath(cat, chk)
return arch.backend.PutFile(pth, ioutil.NopCloser(bytes.NewReader(buf)))
}
func (arch *Archive) AddRandomCheckpoint(chk uint32) error {
opts := &CommandOptions{Force:true}
for _, cat := range Categories() {
if cat == "history" {
var has HistoryArchiveState
has.CurrentLedger = chk
for i := 0; i < NumLevels; i++ {
curr, e := arch.AddRandomBucket()
if e != nil {
return e
}
snap, e := arch.AddRandomBucket()
if e != nil {
return e
}
next, e := arch.AddRandomBucket()
if e != nil {
return e
}
has.CurrentBuckets[i].Curr = curr.String()
has.CurrentBuckets[i].Snap = snap.String()
has.CurrentBuckets[i].Next.Output = next.String()
}
arch.PutCheckpointHAS(chk, has, opts)
arch.PutRootHAS(has, opts)
} else {
arch.AddRandomCheckpointFile(cat, chk)
}
}
return nil
}
func (arch *Archive) PopulateRandomRange(rng Range) error {
for chk := range rng.Checkpoints() {
if e := arch.AddRandomCheckpoint(chk); e != nil {
return e
}
}
return nil
}
func testRange() Range {
return Range{Low:63, High:0x3bf}
}
func testOptions() *CommandOptions {
return &CommandOptions{Range:testRange(),Concurrency:16}
}
func GetRandomPopulatedArchive() *Archive {
a := GetTestArchive()
a.PopulateRandomRange(testRange())
return a
}
func TestScan(t *testing.T) {
defer cleanup()
opts := testOptions()
GetRandomPopulatedArchive().Scan(opts)
}
func countMissing(arch *Archive, opts *CommandOptions) int {
n := 0
arch.Scan(opts)
for _, missing := range arch.CheckCheckpointFilesMissing(opts) {
n += len(missing)
}
n += len(arch.CheckBucketsMissing())
return n
}
func TestMirror(t *testing.T) {
defer cleanup()
opts := testOptions()
src := GetRandomPopulatedArchive()
dst := GetTestArchive()
Mirror(src, dst, opts)
assert.Equal(t, 0, countMissing(dst, opts))
}
func copyFile(category string, checkpoint uint32, src *Archive, dst *Archive) {
pth := CategoryCheckpointPath(category, checkpoint)
rdr, err := src.backend.GetFile(pth)
if err != nil {
panic(err)
}
if err = dst.backend.PutFile(pth, rdr); err != nil {
panic(err)
}
}
func TestMirrorThenRepair(t *testing.T) {
defer cleanup()
opts := testOptions()
src := GetRandomPopulatedArchive()
dst := GetTestArchive()
Mirror(src, dst, opts)
assert.Equal(t, 0, countMissing(dst, opts))
bad := opts.Range.Low + uint32(opts.Range.Size() / 2)
src.AddRandomCheckpoint(bad)
copyFile("history", bad, src, dst)
assert.NotEqual(t, 0, countMissing(dst, opts))
Repair(src, dst, opts)
assert.Equal(t, 0, countMissing(dst, opts))
}
func TestDryRunNoRepair(t *testing.T) {
defer cleanup()
opts := testOptions()
src := GetRandomPopulatedArchive()
dst := GetTestArchive()
Mirror(src, dst, opts)
assert.Equal(t, 0, countMissing(dst, opts))
bad := opts.Range.Low + uint32(opts.Range.Size() / 2)
src.AddRandomCheckpoint(bad)
copyFile("history", bad, src, dst)
assert.NotEqual(t, 0, countMissing(dst, opts))
opts.DryRun = true;
Repair(src, dst, opts)
assert.NotEqual(t, 0, countMissing(dst, opts))
}
func TestXdrDecode(t *testing.T) {
xdrbytes := []byte {
0, 0, 0, 0, // entry type 0, liveentry
0, 32, 223, 100, // lastmodified 2154340
0, 0, 0, 0, // entry type 0, account
0, 0, 0, 0, // key type 0
23, 140, 68, 253, // ed25519 key (32 bytes)
184, 162, 186, 195,
118, 239, 158, 210,
100, 241, 174, 254,
108, 110, 165, 140,
75, 76, 83, 141,
104, 212, 227, 80,
1, 214, 157, 7,
0, 0, 0, 29, // 64bit balance: 125339976000
46, 216, 65, 64,
0, 0, 129, 170, // 64bit seqnum: 142567144423475
0, 0, 0, 51,
0, 0, 0, 1, // numsubentries: 1
0, 0, 0, 1, // inflationdest type, populated
0, 0, 0, 0, // key type 0
87, 240, 19, 71, // ed25519 key (32 bytes)
52, 91, 9, 62,
213, 239, 178, 85,
161, 119, 108, 251,
168, 90, 76, 116,
12, 48, 134, 248,
115, 255, 117, 50,
19, 18, 170, 203,
0, 0, 0, 0, // flags
0, 0, 0, 19, // homedomain: 19 bytes + 1 null padding
99, 101, 110, 116, // "centaurus.xcoins.de"
97, 117, 114, 117,
115, 46, 120, 99,
111, 105, 110, 115,
46, 100, 101, 0,
1, 0, 0, 0, // thresholds
0, 0, 0, 0, // signers (null)
0, 0, 0, 0, // entry.account.ext.v: 0
0, 0, 0, 0, // entry.ext.v: 0
}
assert.Equal(t, len(xdrbytes), 152)
var tmp xdr.BucketEntry
n, err := xdr.Unmarshal(bytes.NewReader(xdrbytes[:]), &tmp)
fmt.Printf("Decoded %d bytes\n", n)
if err != nil {
panic(err)
}
assert.Equal(t, len(xdrbytes), n)
var out bytes.Buffer
n, err = xdr.Marshal(&out, &tmp)
fmt.Printf("Encoded %d bytes\n", n)
if err != nil {
panic(err)
}
assert.Equal(t, out.Len(), n)
assert.Equal(t, out.Bytes(), xdrbytes)
}