mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-17 03:01:21 +00:00
refactor: crypto and pnet (#469)
* feat: add initial plaintext 2 module * refactor: initial refactor of pnet * chore: fix lint * fix: update plaintext api usage * test: use plaintext for test crypto * chore: update deps test: update dialer suite scope * feat: add connection protection to the upgrader * refactor: cleanup and lint fix * chore: remove unncessary transforms * chore: temporarily disable bundlesize * chore: add missing dep * fix: use it-handshake to prevent overreading * chore(fix): PR feedback updates * chore: apply suggestions from code review Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>
This commit is contained in:
94
test/pnet/index.spec.js
Normal file
94
test/pnet/index.spec.js
Normal file
@ -0,0 +1,94 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
chai.use(dirtyChai)
|
||||
const expect = chai.expect
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect } = require('streaming-iterables')
|
||||
|
||||
const Protector = require('../../src/pnet')
|
||||
const Errors = Protector.errors
|
||||
const generate = Protector.generate
|
||||
|
||||
const swarmKeyBuffer = Buffer.alloc(95)
|
||||
const wrongSwarmKeyBuffer = Buffer.alloc(95)
|
||||
|
||||
// Write new psk files to the buffers
|
||||
generate(swarmKeyBuffer)
|
||||
generate(wrongSwarmKeyBuffer)
|
||||
|
||||
describe('private network', () => {
|
||||
it('should accept a valid psk buffer', () => {
|
||||
const protector = new Protector(swarmKeyBuffer)
|
||||
|
||||
expect(protector.tag).to.equal('/key/swarm/psk/1.0.0/')
|
||||
expect(protector.psk.byteLength).to.equal(32)
|
||||
})
|
||||
|
||||
it('should protect a simple connection', async () => {
|
||||
const [inbound, outbound] = duplexPair()
|
||||
const protector = new Protector(swarmKeyBuffer)
|
||||
|
||||
const [aToB, bToA] = await Promise.all([
|
||||
protector.protect(inbound),
|
||||
protector.protect(outbound)
|
||||
])
|
||||
|
||||
pipe(
|
||||
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
||||
aToB
|
||||
)
|
||||
|
||||
const output = await pipe(
|
||||
bToA,
|
||||
source => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield chunk.slice()
|
||||
}
|
||||
})(),
|
||||
collect
|
||||
)
|
||||
|
||||
expect(output).to.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
||||
})
|
||||
|
||||
it('should not be able to share correct data with different keys', async () => {
|
||||
const [inbound, outbound] = duplexPair()
|
||||
const protector = new Protector(swarmKeyBuffer)
|
||||
const protectorB = new Protector(wrongSwarmKeyBuffer)
|
||||
|
||||
const [aToB, bToA] = await Promise.all([
|
||||
protector.protect(inbound),
|
||||
protectorB.protect(outbound)
|
||||
])
|
||||
|
||||
pipe(
|
||||
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
||||
aToB
|
||||
)
|
||||
|
||||
const output = await pipe(
|
||||
bToA,
|
||||
collect
|
||||
)
|
||||
|
||||
expect(output).to.not.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
||||
})
|
||||
|
||||
describe('invalid psks', () => {
|
||||
it('should not accept a bad psk', () => {
|
||||
expect(() => {
|
||||
return new Protector(Buffer.from('not-a-key'))
|
||||
}).to.throw(Errors.INVALID_PSK)
|
||||
})
|
||||
|
||||
it('should not accept a psk of incorrect length', () => {
|
||||
expect(() => {
|
||||
return new Protector(Buffer.from('/key/swarm/psk/1.0.0/\n/base16/\ndffb7e'))
|
||||
}).to.throw(Errors.INVALID_PSK)
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user