js-libp2p/test/insecure/plaintext.spec.js
Alex Potsides 1e869717ff fix: replace node buffers with uint8arrays (#730)
* fix: replace node buffers with uint8arrays

Upgrades all deps and replaces all use of node Buffers with Uint8Arrays

BREAKING CHANGES:

- All deps used by this module now use Uint8Arrays in place of node Buffers

* chore: browser fixes

* chore: remove .only

* chore: stringify uint8array before parsing

* chore: update interop suite

* chore: remove ts from build command

* chore: update deps

* fix: update records to use uint8array

* chore: fix lint

* chore: update deps

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
2020-08-27 15:38:01 +02:00

70 lines
2.0 KiB
JavaScript

'use strict'
/* eslint-env mocha */
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 new Uint8Array(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)
})
})
})