-
Notifications
You must be signed in to change notification settings - Fork 29
/
verify.js
253 lines (238 loc) · 7.37 KB
/
verify.js
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
'use strict'
const BB = require('bluebird')
const contentPath = require('../lib/content/path')
const index = require('../lib/entry-index')
const fs = BB.promisifyAll(require('graceful-fs'))
const path = require('path')
const Tacks = require('tacks')
const test = require('tap').test
const testDir = require('./util/test-dir')(__filename)
const ssri = require('ssri')
const CacheContent = require('./util/cache-content')
const CACHE = path.join(testDir, 'cache')
const CONTENT = Buffer.from('foobarbaz', 'utf8')
const KEY = 'my-test-key'
const INTEGRITY = ssri.fromData(CONTENT)
const METADATA = { foo: 'bar' }
const BUCKET = index._bucketPath(CACHE, KEY)
const verify = require('..').verify
function mockCache () {
const fixture = new Tacks(CacheContent({
[INTEGRITY]: CONTENT
}))
fixture.create(CACHE)
return fs.mkdirAsync(path.join(CACHE, 'tmp')).then(() => {
return index.insert(CACHE, KEY, INTEGRITY, {
metadata: METADATA
})
})
}
test('removes corrupted index entries from buckets', t => {
return mockCache().then(() => {
return fs.readFileAsync(BUCKET, 'utf8').then(BUCKETDATA => {
// traaaaash
return fs.appendFileAsync(BUCKET, '\n234uhhh').then(() => {
return verify(CACHE)
}).then(stats => {
t.equal(stats.missingContent, 0, 'content valid because of good entry')
t.equal(stats.totalEntries, 1, 'only one entry counted')
return fs.readFileAsync(BUCKET, 'utf8')
}).then(bucketData => {
const bucketEntry = JSON.parse(bucketData.split('\t')[1])
const targetEntry = JSON.parse(BUCKETDATA.split('\t')[1])
targetEntry.time = bucketEntry.time // different timestamps
t.deepEqual(
bucketEntry, targetEntry, 'bucket only contains good entry')
})
})
})
})
test('removes shadowed index entries from buckets', t => {
return mockCache().then(() => {
return index.insert(CACHE, KEY, INTEGRITY, {
metadata: 'meh'
}).then(newEntry => {
return verify(CACHE).then(stats => {
t.equal(stats.missingContent, 0, 'content valid because of good entry')
t.equal(stats.totalEntries, 1, 'only one entry counted')
return fs.readFileAsync(BUCKET, 'utf8')
}).then(bucketData => {
const stringified = JSON.stringify({
key: newEntry.key,
integrity: newEntry.integrity.toString(),
time: +(bucketData.match(/"time":([0-9]+)/)[1]),
metadata: newEntry.metadata
})
t.equal(
bucketData,
`\n${index._hashEntry(stringified)}\t${stringified}`,
'only the most recent entry is still in the bucket'
)
})
})
})
})
test('accepts function for custom user filtering of index entries', t => {
const KEY2 = KEY + 'aaa'
const KEY3 = KEY + 'bbb'
return mockCache().then(() => {
return Promise.all([
index.insert(CACHE, KEY2, INTEGRITY, {
metadata: 'haayyyy'
}),
index.insert(CACHE, KEY3, INTEGRITY, {
metadata: 'haayyyy again'
})
]).then(([entryA, entryB]) => ({
[entryA.key]: entryA,
[entryB.key]: entryB
}))
}).then(newEntries => {
return verify(CACHE, {
filter (entry) {
return entry.key.length === KEY2.length
}
}).then(stats => {
t.deepEqual({
verifiedContent: stats.verifiedContent,
rejectedEntries: stats.rejectedEntries,
totalEntries: stats.totalEntries
}, {
verifiedContent: 1,
rejectedEntries: 1,
totalEntries: 2
}, 'reported relevant changes')
return index.ls(CACHE)
}).then(entries => {
entries[KEY2].time = newEntries[KEY2].time
entries[KEY3].time = newEntries[KEY3].time
t.deepEqual(entries, newEntries, 'original entry not included')
})
})
})
test('removes corrupted content', t => {
const cpath = contentPath(CACHE, INTEGRITY)
return mockCache().then(() => {
return fs.truncateAsync(cpath, CONTENT.length - 1)
}).then(() => {
return verify(CACHE)
}).then(stats => {
delete stats.startTime
delete stats.runTime
delete stats.endTime
t.deepEqual(stats, {
verifiedContent: 0,
reclaimedCount: 1,
reclaimedSize: CONTENT.length - 1,
badContentCount: 1,
keptSize: 0,
missingContent: 1,
rejectedEntries: 1,
totalEntries: 0
}, 'reported correct collection counts')
return fs.statAsync(cpath).then(() => {
throw new Error('expected a failure')
}).catch((err) => {
if (err.code === 'ENOENT') {
t.match(err.message, /no such file/, 'content no longer in cache')
return
}
throw err
})
})
})
test('removes content not referenced by any entries', t => {
const fixture = new Tacks(CacheContent({
[INTEGRITY]: CONTENT
}))
fixture.create(CACHE)
return verify(CACHE).then(stats => {
delete stats.startTime
delete stats.runTime
delete stats.endTime
t.deepEqual(stats, {
verifiedContent: 0,
reclaimedCount: 1,
reclaimedSize: CONTENT.length,
badContentCount: 0,
keptSize: 0,
missingContent: 0,
rejectedEntries: 0,
totalEntries: 0
}, 'reported correct collection counts')
})
})
test('cleans up contents of tmp dir', t => {
const tmpFile = path.join(CACHE, 'tmp', 'x')
const misc = path.join(CACHE, 'y')
return mockCache().then(() => {
return Promise.all([
fs.writeFileAsync(tmpFile, ''),
fs.writeFileAsync(misc, '')
]).then(() => verify(CACHE))
}).then(() => {
return Promise.all([
fs.statAsync(tmpFile).catch((err) => {
if (err.code === 'ENOENT') {
return err
}
throw err
}),
fs.statAsync(misc)
]).then(([err, stat]) => {
t.equal(err.code, 'ENOENT', 'tmp file was blown away')
t.ok(stat, 'misc file was not touched')
})
})
})
test('writes a file with last verification time', t => {
return verify(CACHE).then(() => {
return Promise.all([
verify.lastRun(CACHE),
fs.readFileAsync(
path.join(CACHE, '_lastverified'), 'utf8'
).then(data => {
return new Date(parseInt(data))
})
]).then(([fromLastRun, fromFile]) => {
t.equal(+fromLastRun, +fromFile, 'last verified was writen')
})
})
})
test('fixes permissions and users on cache contents')
test('re-builds the index with the size parameter', t => {
const KEY2 = KEY + 'aaa'
const KEY3 = KEY + 'bbb'
return mockCache().then(() => {
return Promise.all([
index.insert(CACHE, KEY2, INTEGRITY, {
metadata: 'haayyyy',
size: 20
}),
index.insert(CACHE, KEY3, INTEGRITY, {
metadata: 'haayyyy again',
size: 30
})])
}).then(() => {
return index.ls(CACHE).then((newEntries) => {
return verify(CACHE)
.then(stats => {
t.deepEqual({
verifiedContent: stats.verifiedContent,
rejectedEntries: stats.rejectedEntries,
totalEntries: stats.totalEntries
}, {
verifiedContent: 1,
rejectedEntries: 0,
totalEntries: 3
}, 'reported relevant changes')
return index.ls(CACHE)
}).then(entries => {
entries[KEY].time = newEntries[KEY].time
entries[KEY2].time = newEntries[KEY2].time
entries[KEY3].time = newEntries[KEY3].time
t.deepEqual(entries, newEntries, 'original index entries not preserved')
})
})
})
})