2016-05-20 21:29:53 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const expect = require('chai').expect
|
2016-05-22 13:19:17 +02:00
|
|
|
const through = require('through2')
|
|
|
|
const bl = require('bl')
|
|
|
|
const PeerId = require('peer-id')
|
2016-05-22 22:39:36 +02:00
|
|
|
const crypto = require('libp2p-crypto')
|
|
|
|
const streamPair = require('stream-pair')
|
2016-05-20 21:29:53 +02:00
|
|
|
|
2016-05-22 22:39:36 +02:00
|
|
|
const SecureSession = require('../src').SecureSession
|
2016-05-20 21:29:53 +02:00
|
|
|
|
|
|
|
describe('libp2p-secio', () => {
|
2016-05-22 13:19:17 +02:00
|
|
|
describe('insecure length prefixed stream', () => {
|
|
|
|
it('encodes', (done) => {
|
|
|
|
const id = PeerId.create({bits: 64})
|
|
|
|
const key = {}
|
2016-05-22 22:39:36 +02:00
|
|
|
const insecure = through()
|
|
|
|
const s = new SecureSession(id, key, insecure)
|
2016-05-22 13:19:17 +02:00
|
|
|
|
|
|
|
// encoded on raw
|
|
|
|
s.insecure.pipe(bl((err, res) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(res.toString()).to.be.eql('\u0005hello\u0005world')
|
|
|
|
done()
|
|
|
|
}))
|
|
|
|
|
|
|
|
s.insecureLp.write('hello')
|
|
|
|
s.insecureLp.write('world')
|
|
|
|
insecure.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('decodes', (done) => {
|
|
|
|
const id = PeerId.create({bits: 64})
|
|
|
|
const key = {}
|
2016-05-22 22:39:36 +02:00
|
|
|
const insecure = through()
|
|
|
|
const s = new SecureSession(id, key, insecure)
|
2016-05-22 13:19:17 +02:00
|
|
|
|
|
|
|
// encoded on raw
|
|
|
|
s.insecureLp.pipe(bl((err, res) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(res.toString()).to.be.eql('helloworld')
|
|
|
|
done()
|
|
|
|
}))
|
|
|
|
|
|
|
|
s.insecure.write('\u0005hello')
|
|
|
|
s.insecure.write('\u0005world')
|
|
|
|
s.insecureLp.end()
|
|
|
|
})
|
2016-05-22 22:39:36 +02:00
|
|
|
|
|
|
|
it('all together now', (done) => {
|
|
|
|
const pair = streamPair.create()
|
|
|
|
|
|
|
|
createSession(pair, (err, local) => {
|
|
|
|
if (err) throw err
|
|
|
|
createSession(pair.other, (err, remote) => {
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
remote.session.insecureLp.pipe(bl((err, res) => {
|
|
|
|
if (err) throw err
|
|
|
|
expect(res.toString()).to.be.eql('hello world')
|
|
|
|
done()
|
|
|
|
}))
|
|
|
|
|
|
|
|
local.session.insecureLp.write('hello ')
|
|
|
|
local.session.insecureLp.write('world')
|
|
|
|
pair.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('upgrades a connection', (done) => {
|
|
|
|
const pair = streamPair.create()
|
|
|
|
|
|
|
|
createSession(pair, (err, local) => {
|
|
|
|
if (err) throw err
|
|
|
|
createSession(pair.other, (err, remote) => {
|
|
|
|
if (err) throw err
|
|
|
|
|
2016-05-23 17:33:16 +02:00
|
|
|
const localSecure = local.session.secureStream()
|
|
|
|
localSecure.write('hello world')
|
2016-05-22 22:39:36 +02:00
|
|
|
|
2016-05-23 17:33:16 +02:00
|
|
|
const remoteSecure = remote.session.secureStream()
|
|
|
|
remoteSecure.once('data', (chunk) => {
|
|
|
|
expect(chunk.toString()).to.be.eql('hello world')
|
|
|
|
done()
|
2016-05-22 22:39:36 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-22 13:19:17 +02:00
|
|
|
})
|
2016-05-20 21:29:53 +02:00
|
|
|
})
|
2016-05-22 22:39:36 +02:00
|
|
|
|
|
|
|
function createSession (insecure, cb) {
|
|
|
|
crypto.generateKeyPair('RSA', 2048, (err, key) => {
|
|
|
|
if (err) return cb(err)
|
|
|
|
const id = PeerId.createFromPrivKey(key.bytes)
|
|
|
|
cb(null, {
|
|
|
|
id,
|
|
|
|
key,
|
|
|
|
insecure,
|
|
|
|
session: new SecureSession(id, key, insecure)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|