From d26590dfb6e2168f156c97d8aed3f579aa6aff96 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 7 Jan 2018 11:06:12 +0000 Subject: [PATCH] test: make tests go faster, way faster --- README.md | 12 +++-- test/fixtures/peer-a.json | 5 ++ test/fixtures/peer-b.json | 5 ++ test/fixtures/peer-c.json | 5 ++ test/secio.spec.js | 104 +++++++++++++++++++------------------- 5 files changed, 74 insertions(+), 57 deletions(-) create mode 100644 test/fixtures/peer-a.json create mode 100644 test/fixtures/peer-b.json create mode 100644 test/fixtures/peer-c.json diff --git a/README.md b/README.md index ef11590..5a70cb7 100644 --- a/README.md +++ b/README.md @@ -39,18 +39,20 @@ const secio = require('libp2p-secio') ## API -### `tag` +### `.tag` The current `secio` tag, usable in `multistream`. -### `i.encrypt(id, key, insecure[, callback])` +### `const encryptedConnection = secio.encrypt(id, key, plainTextConnection [, callback])` -- `id: PeerId` - The id of the node. -- `key: RSAPrivateKey` - The private key of the node. +- `id: PeerId` - The id of the node we are doing the SECIO handshake with. +- `key: RSAPrivateKey` - The private key of our node node. - `insecure: PullStream` - The insecure connection. - `callback: Function` - Called if an error happens during the initialization. -Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream. +Returns an encrypted [Connection object](https://github.com/libp2p/interface-connection) that is the upgraded `plainTextConnection` with now having every byte encripted. + +Both plainTextConnection and encryptedConnection are at their base, PullStreams. ### This module uses `pull-streams` diff --git a/test/fixtures/peer-a.json b/test/fixtures/peer-a.json new file mode 100644 index 0000000..107c894 --- /dev/null +++ b/test/fixtures/peer-a.json @@ -0,0 +1,5 @@ +{ + "id": "QmeS1ou3mrjCFGoFtRx3MwrGDzqKD6xbuYJU1CKtMrtFFu", + "privKey": "CAASqAkwggSkAgEAAoIBAQChwzYwCNIyUkzEK3sILqq9ChAKZ9eU+ribY+B/xwAwDKPfvuqHq0hjauJBMcjiQyKAWz9xEBR3WupOM7h9M8oU+/e0xJUTt/CDOrtJ0PCgUXeBLkqsApbBoXW3yomHEDHx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pScQ2atAoGBAKMu3rb6XyUiRpe05MsHVuYX1vi5Dt1dfVKQv1W3JJbLvAZDbsMeuh4BjRFRoMMflQPwBEg+zpn3+WpVtFG9dL5J5gHgF0zWeLDSnFX8BS2TdELlhccKaBcEC8hbdFtxqIFO/vaeN2902hv/m8e0b1zpGNmWDyKG/a7GYpV1a3/xAoGBAJtgGANDVk6qqcWGEVk56FH1ZksvgF3SPXWaXpzbZ5KLCcV5ooRyhowylKUZBBPowMeZ46tem2xwJbraB5kDg6WiSjBsXcbN95ivb8AuoRa6gDqAszjokQUSdpY7FTgMaL046AuihrKsQSly1jrQqbQu8JBgmnnBzus3s77inL/j", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQChwzYwCNIyUkzEK3sILqq9ChAKZ9eU+ribY+B/xwAwDKPfvuqHq0hjauJBMcjiQyKAWz9xEBR3WupOM7h9M8oU+/e0xJUTt/CDOrtJ0PCgUXeBLkqsApbBoXW3yomHEDHxYttKzrtoTimiP1bhrxurcpVNC4CUYD+q8gw3sRZlsrqpeYYAfU04kS0BM75W/sUT90znnHvOxFXrEdMMdenEFhZOsDyEK9ENzwhkKgOGb18MBY4kN5DoW4bVd4ItfZnNwdkQtpP/X99tMWJxO4yqpngbywZGnkfirLeuRwt/xRGFVbLOigjBpTVpcbBqe1t2Flhuf/bfWYX4FbyElA5FAgMBAAE=" +} diff --git a/test/fixtures/peer-b.json b/test/fixtures/peer-b.json new file mode 100644 index 0000000..4c52c6e --- /dev/null +++ b/test/fixtures/peer-b.json @@ -0,0 +1,5 @@ +{ + "id": "QmYWHGZ9y1Bzx59bBzn85JsJxwmpBy5bpXDWDfwMfsHsxz", + "privKey": "CAASqQkwggSlAgEAAoIBAQDLVaPqWFA8WgK6ixuPvhTHeQfBblmEFLEmraLlIDSWbMUPva6aJ1V/hi2I5QLXNeeiig5sco+nF+RKhGnzQ9NpgHRVZ7Ze+LWq3Q4YxONdzFeNUjTvJrDSKgkub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Ky5Z4lV27pcCgYAWU3mqIELxnVFk5K0LYtwuRkC1Jqg9FVNHXnGnL7l3JjsRnXh4I6lNII1JfEvIr86b6LmybzvtWi1zHI5Rw4B68XfcJmpiOpnzJxyf0r+lLci1Tlqpka0nQlCbzYim5r6l9YLeIeBT5Zv7z7xoq4OUm6V4dX9lCNv3tM6mvcVwGQKBgQC9hhjD64/VKXL8wYKZyTAOVO5xYCcqylrpI39qdzl+sS8oqmLUbXnKsGY4If9U61XdULld41BJCRlv6CsKreynm6ZN41j9YRuWWLu8STJcniV9Ef9uVl1M1zo8kfnCHMCym9LkTfJY+Ow/kYhqPukJJL6ve1CVmIuA4rnZlshjbg==", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLVaPqWFA8WgK6ixuPvhTHeQfBblmEFLEmraLlIDSWbMUPva6aJ1V/hi2I5QLXNeeiig5sco+nF+RKhGnzQ9NpgHRVZ7Ze+LWq3Q4YxONdzFeNUjTvJrDSKgkubA5EKC/LI6pU33WZbjyKkomGo+Gzuqvlj4Rx1dLVXRIOjxUYcIQw3vpLQgwPpiz52eWCeoCpzn06DcsF6aNPjhlp9uJRZCRxZ4yeiwh/A0xxiQtnB4fdZuUPmia1r62+oaxrDl4hUwR7kzHYl0YGfXxAW9GT17KGtjES2yO4kAUgquelNh0hgBKZRvny9imwsObG7ntw5ZG7H62sP7UySIUJqoNRAgMBAAE=" +} diff --git a/test/fixtures/peer-c.json b/test/fixtures/peer-c.json new file mode 100644 index 0000000..ba43f60 --- /dev/null +++ b/test/fixtures/peer-c.json @@ -0,0 +1,5 @@ +{ + "id": "QmYsRYcrQAftGS5U6XRsHdxiVDdGowAfCCYZLgxgL5JGpu", + "privKey": "CAASqAkwggSkAgEAAoIBAQCoYkBQ0acKleSMT58n3IgxuCAoY4iGC8WMR5L5ZOeDGF1L8j31OwOXVP2AzxUMtBbhuqbaH6n0zXUDdCvXy+LSAE6IT+iDBMN9MhWv9XY8NB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6ySpyM0PzXn1HNJhUBcWBRsP/rrhqhhgEjXr1iYUElxQKBgDvgsDZqqVs3HBGxcOEZALjX5d7YAhhWsa3Ty3i/nAoJaPMJOkwzGfiVxJeqG7wJpJAtn9sdV6scVnGUnfK8Uo5lES/csTDOyA+0OP1FBpSsziucOMHtYKoqHyjmQUBTo8ca9sqWLgGhZdV0V3iql5z7f7jfzPkOXTICvKDnCp6HAoGBAJ+xBhd/dFvH12MmPsW1m2bPDXY3Arc3xW/qUAAh7zivK5dFVHYIcVjRSiG0agyWuzz1ig1evwk9qhs+mvGmTPj4Z3mkh+lCdH1SMzi3IygX6kYnvZZcGd892vQFjNVzAxUUb8MRm5aoznzRvwzpSgdKT5lO5dXrPIsH8eK3RIY9", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCoYkBQ0acKleSMT58n3IgxuCAoY4iGC8WMR5L5ZOeDGF1L8j31OwOXVP2AzxUMtBbhuqbaH6n0zXUDdCvXy+LSAE6IT+iDBMN9MhWv9XY8NBLmNJjoL6a5fWBbTmz3oFjnTLvSrmjFa7GuTFF9vFdYAf7ArFTAbwJxgofnfg+A2Vc572sh6ZFedeYWHC6lTe8f+RRAtlRnQsbvDuTRwj9s9EWT5WaKp+jGEoGHR2X8tekfHC6eDMju+Hb8325HZox1K1Tn7IAIZ0V0PfbqjdeyDtPaTWvBdTyhCrIwYIokMG3o8fn2PtyzBisStObXDUDH9ZBrypQ5wB5qJUsTZOEjAgMBAAE=" +} diff --git a/test/secio.spec.js b/test/secio.spec.js index 11730b0..34f598b 100644 --- a/test/secio.spec.js +++ b/test/secio.spec.js @@ -19,53 +19,51 @@ const Dialer = ms.Dialer const secio = require('../src') -function createSession (insecure, callback) { - crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { - expect(err).to.not.exist() +describe('secio', () => { + let peerA + let peerB + let peerC - key.public.hash((err, digest) => { + before((done) => { + parallel([ + (cb) => PeerId.createFromJSON(require('./fixtures/peer-a'), cb), + (cb) => PeerId.createFromJSON(require('./fixtures/peer-b'), cb), + (cb) => PeerId.createFromJSON(require('./fixtures/peer-c'), cb) + ], (err, peers) => { expect(err).to.not.exist() - - callback(null, secio.encrypt(new PeerId(digest, key), key, insecure)) + peerA = peers[0] + peerB = peers[1] + peerC = peers[2] + done() }) }) -} -describe('secio', () => { it('exports a secio multicodec', () => { expect(secio.tag).to.equal('/secio/1.0.0') }) - it('upgrades a connection', function (done) { - this.timeout(20 * 1000) - + it('upgrades a connection', (done) => { const p = pair() - createSession(p[0], (err, local) => { - expect(err).to.not.exist() - createSession(p[1], (err, remote) => { + const aToB = secio.encrypt(peerB, peerA.privKey, p[0], (err) => expect(err).to.not.exist()) + const bToA = secio.encrypt(peerA, peerB.privKey, p[1], (err) => expect(err).to.not.exist()) + + pull( + pull.values([Buffer.from('hello world')]), + aToB + ) + + pull( + bToA, + pull.collect((err, chunks) => { expect(err).to.not.exist() - - pull( - pull.values([Buffer.from('hello world')]), - local - ) - - pull( - remote, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.eql([Buffer.from('hello world')]) - done() - }) - ) + expect(chunks).to.eql([Buffer.from('hello world')]) + done() }) - }) + ) }) - it('works over multistream-select', function (done) { - this.timeout(20 * 1000) - + it('works over multistream-select', (done) => { const p = pair() const listener = new Listener() @@ -78,32 +76,34 @@ describe('secio', () => { ], cb), (cb) => { listener.addHandler('/banana/1.0.0', (protocol, conn) => { - createSession(conn, (err, local) => { - expect(err).to.not.exist() - pull( - local, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.eql([Buffer.from('hello world')]) - done() - }) - ) - }) + const bToA = secio.encrypt(peerA, peerB.privKey, conn, (err) => expect(err).to.not.exist()) + + pull( + bToA, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(chunks).to.eql([Buffer.from('hello world')]) + done() + }) + ) }) + cb() }, (cb) => dialer.select('/banana/1.0.0', (err, conn) => { expect(err).to.not.exist() - createSession(conn, (err, remote) => { - expect(err).to.not.exist() - pull( - pull.values([Buffer.from('hello world')]), - remote - ) - cb() - }) + const aToB = secio.encrypt(peerB, peerA.privKey, conn, (err) => expect(err).to.not.exist()) + + pull( + pull.values([Buffer.from('hello world')]), + aToB + ) + cb() }) - ], (err) => expect(err).to.not.exist()) + ]) + }) + + it.skip('fails if we dialed to the wrong peer', (done) => { }) })