mirror of
https://github.com/fluencelabs/js-libp2p-secio
synced 2025-03-16 10:20:50 +00:00
finish tests
This commit is contained in:
parent
3bc842851b
commit
d05593d111
27
gulpfile.js
27
gulpfile.js
@ -2,29 +2,36 @@
|
|||||||
|
|
||||||
const gulp = require('gulp')
|
const gulp = require('gulp')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const secio = require('./src')
|
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
const WS = require('libp2p-websockets')
|
const WS = require('libp2p-websockets')
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
|
||||||
|
const peerNodeJSON = require('./test/peer-node.json')
|
||||||
|
const secio = require('./src')
|
||||||
|
|
||||||
let listener
|
let listener
|
||||||
const PeerId = require('peer-id')
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||||
const peerNodeJSON = require('./test/peer-node.json')
|
|
||||||
|
|
||||||
gulp.task('test:browser:before', (done) => {
|
gulp.task('test:browser:before', (done) => {
|
||||||
// echo on an encrypted channel
|
PeerId.createFromJSON(peerNodeJSON, (err, id) => {
|
||||||
PeerId.createFromJSON(peerNodeJSON, (err, pid) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err)
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
const ws = new WS()
|
const ws = new WS()
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
|
||||||
listener = ws.createListener((conn) => {
|
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)
|
listener.listen(ma, done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -46,8 +46,7 @@
|
|||||||
"multistream-select": "^0.13.0",
|
"multistream-select": "^0.13.0",
|
||||||
"pre-commit": "^1.1.3",
|
"pre-commit": "^1.1.3",
|
||||||
"pull-goodbye": "0.0.1",
|
"pull-goodbye": "0.0.1",
|
||||||
"pull-pair": "^1.1.0",
|
"pull-pair": "^1.1.0"
|
||||||
"pull-pushable": "^2.0.1"
|
|
||||||
},
|
},
|
||||||
"pre-commit": [
|
"pre-commit": [
|
||||||
"lint",
|
"lint",
|
||||||
|
12
src/etm.js
12
src/etm.js
@ -10,6 +10,7 @@ const lpOpts = {
|
|||||||
|
|
||||||
exports.createBoxStream = (cipher, mac) => {
|
exports.createBoxStream = (cipher, mac) => {
|
||||||
return pull(
|
return pull(
|
||||||
|
ensureBuffer(),
|
||||||
pull.asyncMap((chunk, cb) => {
|
pull.asyncMap((chunk, cb) => {
|
||||||
cipher.encrypt(chunk, (err, data) => {
|
cipher.encrypt(chunk, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -31,6 +32,7 @@ exports.createBoxStream = (cipher, mac) => {
|
|||||||
|
|
||||||
exports.createUnboxStream = (decipher, mac) => {
|
exports.createUnboxStream = (decipher, mac) => {
|
||||||
return pull(
|
return pull(
|
||||||
|
ensureBuffer(),
|
||||||
lp.decode(lpOpts),
|
lp.decode(lpOpts),
|
||||||
pull.asyncMap((chunk, cb) => {
|
pull.asyncMap((chunk, cb) => {
|
||||||
const l = chunk.length
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -5,37 +5,35 @@ const expect = require('chai').expect
|
|||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
const pullGoodbye = require('pull-goodbye')
|
const pullGoodbye = require('pull-goodbye')
|
||||||
|
|
||||||
const secio = require('../src')
|
|
||||||
const WS = require('libp2p-websockets')
|
const WS = require('libp2p-websockets')
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
|
const parallel = require('async/parallel')
|
||||||
|
|
||||||
const peerBrowserJSON = require('./peer-browser.json')
|
const peerBrowserJSON = require('./peer-browser.json')
|
||||||
|
const secio = require('../src')
|
||||||
|
|
||||||
describe('secio browser <-> nodejs', () => {
|
describe('secio browser <-> nodejs', () => {
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||||
let ws
|
|
||||||
let conn
|
let conn
|
||||||
let pid
|
|
||||||
let encryptedConn
|
let encryptedConn
|
||||||
|
|
||||||
before((done) => {
|
before((done) => {
|
||||||
PeerId.createFromJSON(peerBrowserJSON, (err, _pid) => {
|
parallel([
|
||||||
expect(err).to.not.exist
|
(cb) => PeerId.createFromJSON(peerBrowserJSON, cb),
|
||||||
|
(cb) => {
|
||||||
|
const ws = new WS()
|
||||||
|
conn = ws.dial(ma, cb)
|
||||||
|
}
|
||||||
|
], (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
pid = _pid
|
encryptedConn = secio.encrypt(res[0], res[0]._privKey, conn)
|
||||||
ws = new WS()
|
done()
|
||||||
expect(ws).to.exist
|
|
||||||
conn = ws.dial(ma, (err) => {
|
|
||||||
expect(err).to.not.exist
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('encrypt', () => {
|
|
||||||
encryptedConn = secio.encrypt(pid, pid._privKey, conn)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('echo', (done) => {
|
it('echo', (done) => {
|
||||||
const message = 'Hello World!'
|
const message = 'Hello World!'
|
||||||
|
|
||||||
@ -46,8 +44,15 @@ describe('secio browser <-> nodejs', () => {
|
|||||||
expect(results).to.be.eql([message])
|
expect(results).to.be.eql([message])
|
||||||
done()
|
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
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user