-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
391 lines (347 loc) · 13.2 KB
/
test.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* global describe, it, beforeEach, afterEach */
'use strict'
const isEqual = require('lodash.isequal')
const assert = require('assert')
const PouchDB = require('pouchdb')
const Crypt = require('garbados-crypt')
PouchDB.plugin(require('pouchdb-adapter-memory'))
PouchDB.plugin(require('.'))
const {
COUCH_URL,
USE_COUCH
} = process.env
describe('ComDB', function () {
this.timeout(10 * 1000)
beforeEach(async function () {
this.password = 'hello-world'
this.name = '.comdb-test'
this.db = new PouchDB(this.name)
await this.db.setPassword(this.password, {
name: COUCH_URL && USE_COUCH && `${COUCH_URL}/comdb-test`
})
})
afterEach(function () {
return this.db.destroy()
})
it('should encrypt writes', async function () {
const { id } = await this.db.post({ hello: 'world' })
const doc = await this.db.get(id)
const { rows } = await this.db._encrypted.allDocs({ include_docs: true })
const { payload } = rows[0].doc
const json = await this.db._crypt.decrypt(payload)
const plainDoc = JSON.parse(json)
assert.equal(doc.hello, plainDoc.hello, 'Unencrypted and decrypted documents differ.')
})
it('should decrypt and handle deletions', async function () {
// delete a document by inserting a payload into the encrypted db
const { id } = await this.db.post({ hello: 'galaxy' })
const doc = await this.db.get(id)
doc._deleted = true
const { id: encryptedId } = await this.db._encrypted.post(doc)
const encryptedDoc = await this.db._encrypted.get(encryptedId)
const json = await this.db._crypt.decrypt(encryptedDoc.payload)
const plainDoc = JSON.parse(json)
assert.strictEqual(plainDoc._deleted, true)
let caught = false
// now test that it was deleted in the decrypted copy
try {
await this.db.get(doc._id)
} catch (error) {
assert.strictEqual(error.name, 'not_found')
caught = true
}
assert(caught, 'Document was not deleted!')
})
describe('initialization', function () {
beforeEach(async function () {
this.db2 = new PouchDB(this.name + '2')
})
afterEach(async function () {
await this.db2.destroy()
})
it('should fail without a password', async function () {
try {
await this.db2.setPassword()
throw new Error('fail')
} catch (err) {
assert.equal(err.message, 'You must provide a password.')
}
})
it('should fail if password is not a string', async function () {
try {
await this.db2.setPassword({ password: 'hello' })
throw new Error('fail')
} catch (err) {
assert.equal(err.message, 'Password must be a string.')
}
})
it('should initialize with an imported key', async function () {
await this.db.post({ hello: 'world' })
const key = await this.db.exportComDB()
await this.db2.importComDB(this.password, key)
await this.db2.importComDB(this.password, key) // can do repeatedly, ok
await this.db2.replicate.from(this.db)
const { rows: [{ doc }] } = await this.db2.allDocs({ include_docs: true, limit: 1 })
assert.equal(doc.hello, 'world')
})
it('should fail to import without required params', async function () {
const key = await this.db.exportComDB()
try {
await this.db2.importComDB()
} catch (err) {
assert.equal(err.message, 'You must provide a password.')
}
try {
await this.db2.importComDB({ password: this.password })
} catch (err) {
assert.equal(err.message, 'Password must be a string.')
}
try {
await this.db2.importComDB(this.password)
} catch (err) {
assert.equal(err.message, 'You must provide an export string.')
}
try {
await this.db2.importComDB(this.password, { key })
} catch (err) {
assert.equal(err.message, 'Your export string must be a string.')
}
})
})
describe('offline recovery', function () {
beforeEach(function () {
this.offline = {
decrypted: 'offline-decrypted',
encrypted: 'offline-encrypted'
}
this.crypt = new Crypt(this.password)
this.dbs = {
decrypted: new PouchDB(this.offline.decrypted),
encrypted: new PouchDB(this.offline.encrypted)
}
})
afterEach(async function () {
await this.dbs.decrypted.destroy({ unencrypted_only: true })
await this.dbs.encrypted.destroy()
})
it('should process encrypted writes that happened offline', async function () {
// 1. write to encrypted db
await this.dbs.encrypted.post({ _id: 'hello', hello: 'world' })
// 2. provide encrypted db with necessary auth
await this.dbs.encrypted.put({
_id: '_local/comdb',
exportString: await this.crypt.export()
})
await this.dbs.decrypted.setPassword(this.password, { name: this.offline.encrypted })
// 3. load docs from encrypted db
await this.dbs.decrypted.loadEncrypted()
// check for doc in db
const doc = await this.dbs.decrypted.get('hello')
assert.equal(doc.hello, 'world')
})
it('should process encrypted writes w/o revs', async function () {
// 1. write to decrypted db
const { id } = await this.dbs.decrypted.post({ hello: 'world' })
const { _rev: rev, ...postDoc } = await this.dbs.decrypted.get(id)
// 2. write encrypted doc to de
const payload = await this.crypt.encrypt(JSON.stringify(postDoc))
await this.dbs.encrypted.post({ payload, isEncrypted: true })
// 3. provide encrypted db with necessary auth
await this.dbs.encrypted.put({
_id: '_local/comdb',
exportString: await this.crypt.export()
})
await this.dbs.decrypted.setPassword(this.password, { name: this.offline.encrypted })
// 4. load docs from encrypted db
await this.dbs.decrypted.loadEncrypted()
// check for doc in db
const doc = await this.dbs.decrypted.get(id)
assert.equal(doc.hello, 'world')
})
it('should process decrypted writes that happened offline', async function () {
await this.dbs.decrypted.post({ hello: 'world' })
await this.dbs.decrypted.setPassword(this.password, { name: this.offline.encrypted })
let result = await this.dbs.encrypted.allDocs()
assert.equal(result.rows.length, 0)
await this.dbs.decrypted.loadDecrypted()
result = await this.dbs.encrypted.allDocs()
assert.equal(result.rows.length, 1)
})
it('should process decrypted writes repeatedly', async function () {
await this.dbs.decrypted.post({ hello: 'world' })
await this.dbs.decrypted.setPassword(this.password, { name: this.offline.encrypted })
let result = await this.dbs.encrypted.allDocs()
assert.equal(result.rows.length, 0)
await this.dbs.decrypted.loadDecrypted()
await this.dbs.decrypted.loadDecrypted()
await this.dbs.decrypted.loadDecrypted()
result = await this.dbs.encrypted.allDocs()
assert.equal(result.rows.length, 1)
})
it('should allow persistent loading of decrypted writes', async function () {
await this.dbs.decrypted.setPassword(this.password, { name: this.offline.encrypted })
const changes = this.dbs.decrypted.loadDecrypted({ live: true })
let ok = false
changes.on('change', () => { ok = true })
await this.dbs.decrypted.post({ hello: 'world' })
await new Promise((resolve) => setTimeout(resolve, 20))
assert(ok)
changes.cancel()
})
})
describe('destroy', function () {
beforeEach(async function () {
this.db_destroyable_1 = new PouchDB('test-destroy-1')
this.db_destroyable_2 = new PouchDB('test-destroy-2')
await this.db_destroyable_1.setPassword('goodpassword')
await this.db_destroyable_2.setPassword('goodpassword')
})
afterEach(async function () {
await this.db_destroyable_1.destroy()
await this.db_destroyable_2.destroy()
})
it('should optionally not destroy the encrypted copy', async function () {
await this.db_destroyable_1.destroy({ unencrypted_only: true })
assert.equal(this.db_destroyable_1._encrypted._destroyed, undefined)
assert.equal(this.db_destroyable_1._destroyed, true)
})
it('should optionally not destroy the unencrypted copy', async function () {
await this.db_destroyable_2.destroy({ encrypted_only: true })
assert.equal(this.db_destroyable_2._encrypted._destroyed, true)
assert.equal(this.db_destroyable_2._destroyed, undefined)
})
})
describe('replication', function () {
beforeEach(async function () {
this.db2 = new PouchDB(`${this.name}-replication`)
const exportString = await this.db.exportComDB()
await this.db2.importComDB(this.password, exportString)
await this.db.post({ hello: 'sol' })
})
afterEach(async function () {
await this.db2.destroy()
})
it('should restore data from an encrypted backup', async function () {
await this.db.replicate.to(this.db2)
const opts = { include_docs: true }
const results1 = await this.db.allDocs(opts)
const results2 = await this.db2.allDocs(opts)
assert.strictEqual(results1.total_rows, results2.total_rows)
const doc1 = results1.rows[0].doc
const doc2 = results2.rows[0].doc
assert(isEqual(doc1, doc2))
})
it('should perform normal replication ok', async function () {
await this.db.replicate.to(this.db2, { comdb: false })
const opts = { include_docs: true }
const results1 = await this.db.allDocs(opts)
const results2 = await this.db2.allDocs(opts)
assert.strictEqual(results1.total_rows, results2.total_rows)
const doc1 = results1.rows[0].doc
const doc2 = results2.rows[0].doc
assert(isEqual(doc1, doc2))
})
})
describe('replication w/o setup', async function () {
beforeEach(async function () {
this.db2 = new PouchDB(this.name + '2')
this.db3 = new PouchDB(this.name + '3')
})
afterEach(async function () {
await this.db2.destroy()
await this.db3.destroy()
})
it('should replicate ok w/o setting a password', async function () {
await this.db2.post({ hello: 'world' })
await this.db2.replicate.to(this.db3)
const { rows } = await this.db3.allDocs()
assert.equal(rows.length, 1)
})
})
describe('concurrency', function () {
beforeEach(async function () {
this.db1 = new PouchDB('.test-concurrency')
this.db2 = new PouchDB('.test-concurrency')
})
afterEach(async function () {
await this.db1.destroy()
})
it('should setup ok, concurrently', async function () {
await Promise.all([
this.db1.setPassword(this.password),
this.db2.setPassword(this.password)
])
const doc1 = { _id: 'hello', hello: 'world' }
await this.db1.put(doc1)
const doc2 = await this.db2.get(doc1._id)
assert.equal(doc1.hello, doc2.hello)
})
})
describe('in memory', function () {
beforeEach(async function () {
this.tempDb = new PouchDB('in-memory-test', { adapter: 'memory' })
this.tempEncryptedDbName = 'in-memory-test-backup'
await this.tempDb.setPassword(this.password, { name: this.tempEncryptedDbName })
})
afterEach(async function () {
try {
await this.tempDb.destroy()
} catch (err) {} // FIXME catch? maybe?
})
it('can create and recover', async function () {
const { id } = await this.tempDb.post({ hello: 'world' })
await this.tempDb.destroy({ unencrypted_only: true })
const otherDb = new PouchDB('in-memory-test-2', { adapter: 'memory' })
await otherDb.setPassword(this.password, { name: this.tempEncryptedDbName })
await otherDb.loadEncrypted()
const doc = await otherDb.get(id)
assert.equal(doc.hello, 'world')
})
})
describe('issues', function () {
it('should handle destruction without a set password', async function () {
const db3 = new PouchDB('.test-destruction')
await db3.destroy()
assert(true)
})
it('should handle calls to bulkDocs', async function () {
const docs = []
const k = 10
for (let i = 0; i < k; i++) {
docs.push({
_id: String(Math.floor(Math.random() * Date.now())),
a: Math.random(),
b: Math.random() * Date.now()
})
}
const result = await this.db.bulkDocs({ docs })
assert.equal(result.length, k)
})
})
describe('loadDecrypted queue', function () {
const NUM_DOCS = 150
beforeEach(async function () {
this.db2 = new PouchDB('.test-load-decrypted')
// add docs to DB
const docs = []
for (let i = 0; i < NUM_DOCS; i++) {
docs.push({ hello: 'world', i })
}
await this.db2.bulkDocs({ docs })
await this.db2.setPassword(this.password)
})
afterEach(async function () {
await this.db2.destroy()
})
it('should load decrypted docs efficiently', async function () {
const bulkDocs = this.db2._encrypted.bulkDocs
let callCount = 0
this.db2._encrypted.bulkDocs = async function () {
callCount++
return bulkDocs.apply(this, arguments)
}
await this.db2.loadDecrypted()
assert.equal(callCount, 2)
})
})
})