finish tests

This commit is contained in:
Friedel Ziegelmayer 2016-11-10 17:47:40 +01:00
parent 3bc842851b
commit d05593d111
4 changed files with 54 additions and 31 deletions

View File

@ -2,29 +2,36 @@
const gulp = require('gulp')
const multiaddr = require('multiaddr')
const secio = require('./src')
const pull = require('pull-stream')
const WS = require('libp2p-websockets')
const PeerId = require('peer-id')
const peerNodeJSON = require('./test/peer-node.json')
const secio = require('./src')
let listener
const PeerId = require('peer-id')
const peerNodeJSON = require('./test/peer-node.json')
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
gulp.task('test:browser:before', (done) => {
// echo on an encrypted channel
PeerId.createFromJSON(peerNodeJSON, (err, pid) => {
PeerId.createFromJSON(peerNodeJSON, (err, id) => {
if (err) {
return done(err)
throw err
}
const ws = new WS()
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
listener = ws.createListener((conn) => {
const encrypted = secio.encrypt(pid, pid._privKey, conn)
const encrypted = secio.encrypt(id, id._privKey, conn, (err) => {
if (err) {
throw err
}
})
pull(conn, encrypted, conn)
pull(
encrypted,
encrypted
)
})
listener.listen(ma, done)
})
})

View File

@ -46,8 +46,7 @@
"multistream-select": "^0.13.0",
"pre-commit": "^1.1.3",
"pull-goodbye": "0.0.1",
"pull-pair": "^1.1.0",
"pull-pushable": "^2.0.1"
"pull-pair": "^1.1.0"
},
"pre-commit": [
"lint",

View File

@ -10,6 +10,7 @@ const lpOpts = {
exports.createBoxStream = (cipher, mac) => {
return pull(
ensureBuffer(),
pull.asyncMap((chunk, cb) => {
cipher.encrypt(chunk, (err, data) => {
if (err) {
@ -31,6 +32,7 @@ exports.createBoxStream = (cipher, mac) => {
exports.createUnboxStream = (decipher, mac) => {
return pull(
ensureBuffer(),
lp.decode(lpOpts),
pull.asyncMap((chunk, cb) => {
const l = chunk.length
@ -65,3 +67,13 @@ exports.createUnboxStream = (decipher, mac) => {
})
)
}
function ensureBuffer () {
return pull.map((c) => {
if (typeof c === 'string') {
return new Buffer(c, 'utf-8')
}
return c
})
}

View File

@ -5,37 +5,35 @@ const expect = require('chai').expect
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const pullGoodbye = require('pull-goodbye')
const secio = require('../src')
const WS = require('libp2p-websockets')
const PeerId = require('peer-id')
const parallel = require('async/parallel')
const peerBrowserJSON = require('./peer-browser.json')
const secio = require('../src')
describe('secio browser <-> nodejs', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
let ws
let conn
let pid
let encryptedConn
before((done) => {
PeerId.createFromJSON(peerBrowserJSON, (err, _pid) => {
expect(err).to.not.exist
parallel([
(cb) => PeerId.createFromJSON(peerBrowserJSON, cb),
(cb) => {
const ws = new WS()
conn = ws.dial(ma, cb)
}
], (err, res) => {
if (err) {
return done(err)
}
pid = _pid
ws = new WS()
expect(ws).to.exist
conn = ws.dial(ma, (err) => {
expect(err).to.not.exist
done()
})
encryptedConn = secio.encrypt(res[0], res[0]._privKey, conn)
done()
})
})
it('encrypt', () => {
encryptedConn = secio.encrypt(pid, pid._privKey, conn)
})
it('echo', (done) => {
const message = 'Hello World!'
@ -46,8 +44,15 @@ describe('secio browser <-> nodejs', () => {
expect(results).to.be.eql([message])
done()
})
})
}, 'GoodBye')
pull(s, encryptedConn, s)
pull(
s,
encryptedConn,
// Need to convert to a string as goodbye only understands strings
pull.map((msg) => msg.toString()),
s
)
})
})