This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic crypto interface test suite
- Loading branch information
Showing
7 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const duplexPair = require('it-pair/duplex') | ||
const pipe = require('it-pipe') | ||
const peers = require('../../utils/peers') | ||
const PeerId = require('peer-id') | ||
const { collect } = require('streaming-iterables') | ||
const chai = require('chai') | ||
const expect = chai.expect | ||
chai.use(require('dirty-chai')) | ||
|
||
module.exports = (common) => { | ||
describe('interface-crypto', () => { | ||
let crypto | ||
let localPeer | ||
let remotePeer | ||
|
||
before(async () => { | ||
[ | ||
crypto, | ||
localPeer, | ||
remotePeer | ||
] = await Promise.all([ | ||
common.setup(), | ||
PeerId.createFromJSON(peers[0]), | ||
PeerId.createFromJSON(peers[1]) | ||
]) | ||
}) | ||
|
||
after(() => common.teardown && common.teardown()) | ||
|
||
it('has a protocol string', () => { | ||
expect(crypto.protocol).to.exist() | ||
expect(crypto.protocol).to.be.a('string') | ||
}) | ||
|
||
it('it wraps the provided duplex connection', async () => { | ||
const [localConn, remoteConn] = duplexPair() | ||
|
||
const [ | ||
inboundResult, | ||
outboundResult | ||
] = await Promise.all([ | ||
crypto.secureInbound(remotePeer, localConn), | ||
crypto.secureOutbound(localPeer, remoteConn, remotePeer) | ||
]) | ||
|
||
// Echo server | ||
pipe(inboundResult.conn, inboundResult.conn) | ||
|
||
// Send some data and collect the result | ||
const input = Buffer.from('data to encrypt') | ||
const result = await pipe( | ||
[input], | ||
outboundResult.conn, | ||
// Convert BufferList to Buffer via slice | ||
(source) => (async function * toBuffer () { | ||
for await (const chunk of source) { | ||
yield chunk.slice() | ||
} | ||
})(), | ||
collect | ||
) | ||
|
||
expect(result).to.eql([input]) | ||
}) | ||
|
||
it('should return the remote peer id', async () => { | ||
const [localConn, remoteConn] = duplexPair() | ||
|
||
const [ | ||
inboundResult, | ||
outboundResult | ||
] = await Promise.all([ | ||
crypto.secureInbound(remotePeer, localConn), | ||
crypto.secureOutbound(localPeer, remoteConn, remotePeer) | ||
]) | ||
|
||
// Inbound should return the initiator (local) peer | ||
expect(inboundResult.remotePeer.id).to.eql(localPeer.id) | ||
// Outbound should return the receiver (remote) peer | ||
expect(outboundResult.remotePeer.id).to.eql(remotePeer.id) | ||
}) | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const tests = require('../../src/crypto/tests') | ||
const mockCrypto = require('./mock-crypto') | ||
|
||
describe('compliance tests', () => { | ||
tests({ | ||
setup () { | ||
return mockCrypto | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const handshake = require('it-handshake') | ||
const duplexPair = require('it-pair/duplex') | ||
const pipe = require('it-pipe') | ||
|
||
// A basic transform that does nothing to the data | ||
const transform = () => { | ||
return (source) => (async function * () { | ||
for await (const chunk of source) { | ||
yield chunk | ||
} | ||
})() | ||
} | ||
|
||
module.exports = { | ||
protocol: 'insecure', | ||
secureInbound: async (localPeer, duplex) => { | ||
// 1. Perform a basic handshake. | ||
const shake = handshake(duplex) | ||
shake.write(localPeer.id) | ||
const remoteId = await shake.read() | ||
shake.rest() | ||
|
||
// 2. Create your encryption box/unbox wrapper | ||
const wrapper = duplexPair() | ||
const encrypt = transform() // Use transform iterables to modify data | ||
const decrypt = transform() | ||
|
||
pipe( | ||
wrapper[0], // We write to wrapper | ||
encrypt, // The data is encrypted | ||
shake.stream, // It goes to the remote peer | ||
decrypt, // Decrypt the incoming data | ||
wrapper[0] // Pipe to the wrapper | ||
) | ||
|
||
return { | ||
conn: wrapper[1], | ||
remotePeer: new PeerId(remoteId.slice()) | ||
} | ||
}, | ||
secureOutbound: async (localPeer, duplex, remotePeer) => { | ||
// 1. Perform a basic handshake. | ||
const shake = handshake(duplex) | ||
shake.write(localPeer.id) | ||
const remoteId = await shake.read() | ||
shake.rest() | ||
|
||
// 2. Create your encryption box/unbox wrapper | ||
const wrapper = duplexPair() | ||
const encrypt = transform() | ||
const decrypt = transform() | ||
|
||
pipe( | ||
wrapper[0], // We write to wrapper | ||
encrypt, // The data is encrypted | ||
shake.stream, // It goes to the remote peer | ||
decrypt, // Decrypt the incoming data | ||
wrapper[0] // Pipe to the wrapper | ||
) | ||
|
||
return { | ||
conn: wrapper[1], | ||
remotePeer: new PeerId(remoteId.slice()) | ||
} | ||
} | ||
} |