|
1 | 1 | const test = require('brittle')
|
2 | 2 | const b4a = require('b4a')
|
| 3 | +const crypto = require('hypercore-crypto') |
3 | 4 | const HypercoreEncryption = require('hypercore-encryption')
|
4 | 5 | const Hypercore = require('..')
|
5 | 6 | const { create, createStorage, replicate } = require('./helpers')
|
6 | 7 |
|
| 8 | +const fixturesRaw = require('./fixtures/encryption/v11.0.48.cjs') |
| 9 | + |
7 | 10 | const encryptionKey = b4a.alloc(32, 'hello world')
|
8 | 11 |
|
9 | 12 | test('encrypted append and get', async function (t) {
|
@@ -273,9 +276,86 @@ test('block encryption module', async function (t) {
|
273 | 276 | }
|
274 | 277 | })
|
275 | 278 |
|
| 279 | +test('encryption backwards compatibility', async function (t) { |
| 280 | + const encryptionKey = b4a.alloc(32).fill('encryption key') |
| 281 | + |
| 282 | + const compatKey = crypto.keyPair(b4a.alloc(32, 0)) |
| 283 | + const defaultKey = crypto.keyPair(b4a.alloc(32, 1)) |
| 284 | + const blockKey = crypto.keyPair(b4a.alloc(32, 2)) |
| 285 | + |
| 286 | + const fixtures = [ |
| 287 | + getFixture('compat'), |
| 288 | + getFixture('default'), |
| 289 | + getFixture('default'), |
| 290 | + getFixture('block') |
| 291 | + ] |
| 292 | + |
| 293 | + const compat = await create(t, null, { keyPair: compatKey, encryptionKey, compat: true }) |
| 294 | + const def = await create(t, null, { keyPair: defaultKey, encryptionKey, isBlockKey: false }) |
| 295 | + const notBlock = await create(t, null, { keyPair: defaultKey, encryptionKey, isBlockKey: false }) |
| 296 | + const block = await create(t, null, { keyPair: blockKey, encryptionKey, isBlockKey: true }) |
| 297 | + |
| 298 | + await compat.ready() |
| 299 | + await def.ready() |
| 300 | + await notBlock.ready() |
| 301 | + await block.ready() |
| 302 | + |
| 303 | + const largeBlock = Buffer.alloc(512) |
| 304 | + for (let i = 0; i < largeBlock.byteLength; i++) largeBlock[i] = i & 0xff |
| 305 | + |
| 306 | + for (let i = 0; i < 10; i++) { |
| 307 | + await compat.append('compat test: ' + i.toString()) |
| 308 | + await def.append('default test: ' + i.toString()) |
| 309 | + await notBlock.append('default test: ' + i.toString()) |
| 310 | + await block.append('block test: ' + i.toString()) |
| 311 | + } |
| 312 | + |
| 313 | + await compat.append(largeBlock.toString('hex')) |
| 314 | + await def.append(largeBlock.toString('hex')) |
| 315 | + await notBlock.append(largeBlock.toString('hex')) |
| 316 | + await block.append(largeBlock.toString('hex')) |
| 317 | + |
| 318 | + // compat |
| 319 | + t.comment('test compat mode') |
| 320 | + t.is(compat.length, fixtures[0].length) |
| 321 | + |
| 322 | + for (let i = 0; i < compat.length; i++) { |
| 323 | + t.alike(await compat.get(i, { raw: true }), fixtures[0][i]) |
| 324 | + } |
| 325 | + |
| 326 | + // default |
| 327 | + t.comment('test default mode') |
| 328 | + t.is(def.length, fixtures[1].length) |
| 329 | + |
| 330 | + for (let i = 0; i < def.length; i++) { |
| 331 | + t.alike(await def.get(i, { raw: true }), fixtures[1][i]) |
| 332 | + } |
| 333 | + |
| 334 | + // not block |
| 335 | + t.comment('test block false') |
| 336 | + t.is(notBlock.length, fixtures[2].length) |
| 337 | + |
| 338 | + for (let i = 0; i < notBlock.length; i++) { |
| 339 | + t.alike(await notBlock.get(i, { raw: true }), fixtures[2][i]) |
| 340 | + } |
| 341 | + |
| 342 | + // compat |
| 343 | + t.comment('test block mode') |
| 344 | + t.is(block.length, fixtures[3].length) |
| 345 | + |
| 346 | + for (let i = 0; i < block.length; i++) { |
| 347 | + t.alike(await block.get(i, { raw: true }), fixtures[3][i]) |
| 348 | + } |
| 349 | +}) |
| 350 | + |
276 | 351 | function getBlock (core, index) {
|
277 | 352 | const batch = core.core.storage.read()
|
278 | 353 | const b = batch.getBlock(index)
|
279 | 354 | batch.tryFlush()
|
280 | 355 | return b
|
281 | 356 | }
|
| 357 | + |
| 358 | +function getFixture (name) { |
| 359 | + const blocks = fixturesRaw[name] |
| 360 | + return blocks.map(b => b4a.from(b, 'base64')) |
| 361 | +} |
0 commit comments