Skip to content

Commit

Permalink
encryption test (#2)
Browse files Browse the repository at this point in the history
* encryption test

* fix encryption test

---------

Co-authored-by: Mathias Buus <[email protected]>
  • Loading branch information
natzcam and mafintosh authored Jan 4, 2025
1 parent e5c50ab commit 6fb278c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
21 changes: 21 additions & 0 deletions test/blobs.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions test/drives.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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')
})
4 changes: 2 additions & 2 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6fb278c

Please sign in to comment.