From 2a1445a6080d0035d8c8ce874bf5e5fb7c47f7ef Mon Sep 17 00:00:00 2001 From: Jason Papakostas Date: Mon, 19 Jun 2017 18:08:46 -0500 Subject: [PATCH] test: add test for ipfs.pubsub.publish with a binary (non-utf8) buffer --- src/pubsub.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/pubsub.js b/src/pubsub.js index cab12d53..8d9d0f91 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -382,6 +382,46 @@ module.exports = (common) => { }) }) + it('round trips a non-utf8 binary buffer correctly', (done) => { + const check = makeCheck(3, done) + const expectedHex = 'a36161636179656162830103056164a16466666666f4' + const buffer = Buffer.from(expectedHex, 'hex') + + const sub1 = (msg) => { + try { + expect(msg.data.toString('hex')).to.be.eql(expectedHex) + expect(msg.from).to.be.eql(ipfs2.peerId.id) + check() + } catch (e) { + check(e) + } finally { + ipfs1.pubsub.unsubscribe(topic, sub1) + } + } + + const sub2 = (msg) => { + try { + expect(msg.data.toString('hex')).to.be.eql(expectedHex) + expect(msg.from).to.be.eql(ipfs2.peerId.id) + check() + } catch (e) { + check(e) + } finally { + ipfs2.pubsub.unsubscribe(topic, sub2) + } + } + + series([ + (cb) => ipfs1.pubsub.subscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.subscribe(topic, sub2, cb), + (cb) => waitForPeers(ipfs2, topic, [ipfs1.peerId.id], cb), + ], (err) => { + expect(err).to.not.exist() + + ipfs2.pubsub.publish(topic, buffer, check) + }) + }) + it('receive multiple messages', (done) => { const inbox1 = [] const inbox2 = []