js-libp2p/test/insecure/plaintext.spec.js
Hugo Dias 9b13fe321a
fix: remove node global (#587)
* fix: use new libp2p-crypto

* fix: remove node globals and reduce size

- adds buffer require
- adds globalThis where needed
- streaming-iterables was remove and new utils created, this will be consolidated in `ipfs-utils` and backported here to normalize all these iterator helper functions
- latency-monitor was copied inside the repo
- iso-random-stream is now used instead of node crypto
- the test `should ignore self on discovery` was moved to node only

Size: 172.97KB
47.03KB below the 220KB limit.

`aegir build --node false` and `aegir test -t browser --node false` now work 🎉

* fix: fix require path

* fix: feedback

* fix: update deps and bundle size

* chore: bump interfaces

* chore: update size
2020-04-24 17:10:40 +02:00

71 lines
2.0 KiB
JavaScript

'use strict'
/* eslint-env mocha */
const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const sinon = require('sinon')
const PeerId = require('peer-id')
const duplexPair = require('it-pair/duplex')
const peers = require('../fixtures/peers')
const plaintext = require('../../src/insecure/plaintext')
const {
InvalidCryptoExchangeError,
UnexpectedPeerError
} = require('libp2p-interfaces/src/crypto/errors')
describe('plaintext', () => {
let localPeer
let remotePeer
let wrongPeer
before(async () => {
[localPeer, remotePeer, wrongPeer] = await Promise.all([
PeerId.createFromJSON(peers[0]),
PeerId.createFromJSON(peers[1]),
PeerId.createFromJSON(peers[2])
])
})
afterEach(() => {
sinon.restore()
})
it('should verify the public key and id match', () => {
const [localConn, remoteConn] = duplexPair()
// When we attempt to get the remote peer key, return the wrong peers pub key
sinon.stub(remotePeer, 'marshalPubKey').callsFake(() => {
return wrongPeer.marshalPubKey()
})
return Promise.all([
plaintext.secureInbound(remotePeer, localConn),
plaintext.secureOutbound(localPeer, remoteConn, remotePeer)
]).then(() => expect.fail('should have failed'), (err) => {
expect(err).to.exist()
expect(err).to.have.property('code', UnexpectedPeerError.code)
})
})
it('should fail if the peer does not provide its public key', () => {
const [localConn, remoteConn] = duplexPair()
// When we attempt to get the remote peer key, return the wrong peers pub key
sinon.stub(remotePeer, 'marshalPubKey').callsFake(() => {
return Buffer.alloc(0)
})
return Promise.all([
plaintext.secureInbound(remotePeer, localConn),
plaintext.secureOutbound(localPeer, remoteConn, remotePeer)
]).then(() => expect.fail('should have failed'), (err) => {
expect(err).to.exist()
expect(err).to.have.property('code', InvalidCryptoExchangeError.code)
})
})
})