diff --git a/test/blobs.js b/test/blobs.js index 305d91d..dfee05b 100644 --- a/test/blobs.js +++ b/test/blobs.js @@ -1,4 +1,5 @@ const test = require('brittle') +const b4a = require('b4a') const RAM = require('random-access-memory') const Corestore = require('corestore') const { testBlobServer, request, testHyperblobs } = require('./helpers') @@ -19,6 +20,26 @@ test('can get blob from hypercore', async function (t) { t.is(res.data, 'Hello World') }) +test('can get encrypted blob from hypercore', async function (t) { + const store = new Corestore(RAM) + + const blobs = testHyperblobs(t, store) + + const id = await blobs.put(Buffer.from('Hello World')) + + const server = testBlobServer(t, store, { + resolve: function (key) { + return { key, encryptionKey: b4a.alloc(32).fill('a') } + } + }) + await server.listen() + + const res = await request(server, blobs.core.key, { blob: id }) + + t.is(res.status, 200) + t.absent(res.data.includes('Hello Wolrd')) +}) + test('can get blob from hypercore - multiple blocks', async function (t) { const store = new Corestore(RAM) diff --git a/test/drives.js b/test/drives.js index 6d7e5e1..97e503c 100644 --- a/test/drives.js +++ b/test/drives.js @@ -1,4 +1,5 @@ const test = require('brittle') +const b4a = require('b4a') const RAM = require('random-access-memory') const Corestore = require('corestore') const { testHyperdrive, testBlobServer, request, get } = require('./helpers') @@ -99,3 +100,21 @@ test('sending request after resume', async function (t) { t.is(res.status, 200) t.is(res.data, 'Here') }) + +test('can get encrypted blob from hyperdrive', async function (t) { + const store = new Corestore(RAM) + + const drive = testHyperdrive(t, store, { encryptionKey: b4a.alloc(32) }) + await drive.put('/file.txt', 'Here') + + const server = testBlobServer(t, store, { + resolve: function (key) { + return { key, encryptionKey: b4a.alloc(32) } + } + }) + await server.listen() + + const res = await request(server, drive.key, { filename: '/file.txt' }) + t.is(res.status, 200) + t.is(res.data, 'Here') +}) diff --git a/test/helpers/index.js b/test/helpers/index.js index 7ef0e25..a51ca6a 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -61,8 +61,8 @@ function testHyperblobs (t, store) { return blobs } -function testHyperdrive (t, store) { - const drive = new Hyperdrive(store) +function testHyperdrive (t, store, opts) { + const drive = new Hyperdrive(store, opts) t.teardown(() => drive.close()) return drive }