chore: refactor and better docs

This commit is contained in:
Vasco Santos
2020-06-24 15:10:08 +02:00
committed by Jacob Heun
parent 02a5095b9c
commit 71daac24b1
12 changed files with 174 additions and 226 deletions

View File

@ -0,0 +1,88 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-bytes'))
const { expect } = chai
const multicodec = require('multicodec')
const Envelope = require('../../src/record/envelope')
const Record = require('libp2p-interfaces/src/record')
const peerUtils = require('../utils/creators/peer')
const domain = '/test-domain'
class TestRecord extends Record {
constructor (data) {
super(domain, multicodec.LIBP2P_PEER_RECORD)
this.data = data
}
marshal () {
return Buffer.from(this.data)
}
isEqual (other) {
return Buffer.compare(this.data, other.data)
}
}
describe('Envelope', () => {
const payloadType = Buffer.from(`${multicodec.print[multicodec.LIBP2P_PEER_RECORD]}${domain}`)
let peerId
let testRecord
before(async () => {
[peerId] = await peerUtils.createPeerId()
testRecord = new TestRecord('test-data')
})
it('creates an envelope with a random key', () => {
const payload = testRecord.marshal()
const signature = Buffer.from(Math.random().toString(36).substring(7))
const envelope = new Envelope({
peerId,
payloadType,
payload,
signature
})
expect(envelope).to.exist()
expect(envelope.peerId.equals(peerId)).to.eql(true)
expect(envelope.payloadType).to.equalBytes(payloadType)
expect(envelope.payload).to.equalBytes(payload)
expect(envelope.signature).to.equalBytes(signature)
})
it('can seal a record', async () => {
const envelope = await Envelope.seal(testRecord, peerId)
expect(envelope).to.exist()
expect(envelope.peerId.equals(peerId)).to.eql(true)
expect(envelope.payloadType).to.equalBytes(payloadType)
expect(envelope.payload).to.exist()
expect(envelope.signature).to.exist()
})
it('can open and verify a sealed record', async () => {
const envelope = await Envelope.seal(testRecord, peerId)
const rawEnvelope = envelope.marshal()
const unmarshalledEnvelope = await Envelope.openAndCertify(rawEnvelope, testRecord.domain)
expect(unmarshalledEnvelope).to.exist()
const isEqual = envelope.isEqual(unmarshalledEnvelope)
expect(isEqual).to.eql(true)
})
it.skip('throw on open and verify when a different domain is used', async () => {
const envelope = await Envelope.seal(testRecord, peerId)
const rawEnvelope = envelope.marshal()
await expect(Envelope.openAndCertify(rawEnvelope, '/fake-domain'))
.to.eventually.rejected()
})
})

View File

@ -0,0 +1,117 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const multiaddr = require('multiaddr')
const tests = require('libp2p-interfaces/src/record/tests')
const PeerRecord = require('../../src/record/peer-record')
const peerUtils = require('../utils/creators/peer')
describe('interface-record compliance', () => {
tests({
async setup () {
const [peerId] = await peerUtils.createPeerId()
return new PeerRecord({ peerId })
},
async teardown () {
// cleanup resources created by setup()
}
})
})
describe('PeerRecord', () => {
let peerId
before(async () => {
[peerId] = await peerUtils.createPeerId()
})
it('creates a peer record with peerId', () => {
const peerRecord = new PeerRecord({ peerId })
expect(peerRecord).to.exist()
expect(peerRecord.peerId).to.exist()
expect(peerRecord.multiaddrs).to.exist()
expect(peerRecord.multiaddrs).to.have.lengthOf(0)
expect(peerRecord.seqNumber).to.exist()
})
it('creates a peer record with provided data', () => {
const multiaddrs = [
multiaddr('/ip4/127.0.0.1/tcp/2000')
]
const seqNumber = Date.now()
const peerRecord = new PeerRecord({ peerId, multiaddrs, seqNumber })
expect(peerRecord).to.exist()
expect(peerRecord.peerId).to.exist()
expect(peerRecord.multiaddrs).to.exist()
expect(peerRecord.multiaddrs).to.eql(multiaddrs)
expect(peerRecord.seqNumber).to.exist()
expect(peerRecord.seqNumber).to.eql(seqNumber)
})
it('marshals and unmarshals a peer record', () => {
const multiaddrs = [
multiaddr('/ip4/127.0.0.1/tcp/2000')
]
const seqNumber = Date.now()
const peerRecord = new PeerRecord({ peerId, multiaddrs, seqNumber })
// Marshal
const rawData = peerRecord.marshal()
expect(rawData).to.exist()
// Unmarshal
const unmarshalPeerRecord = PeerRecord.createFromProtobuf(rawData)
expect(unmarshalPeerRecord).to.exist()
const isEqual = peerRecord.isEqual(unmarshalPeerRecord)
expect(isEqual).to.eql(true)
})
it('isEqual returns false if the peer record has a different peerId', async () => {
const peerRecord0 = new PeerRecord({ peerId })
const [peerId1] = await peerUtils.createPeerId({ fixture: false })
const peerRecord1 = new PeerRecord({ peerId: peerId1 })
const isEqual = peerRecord0.isEqual(peerRecord1)
expect(isEqual).to.eql(false)
})
it('isEqual returns false if the peer record has a different seqNumber', () => {
const ts0 = Date.now()
const peerRecord0 = new PeerRecord({ peerId, seqNumber: ts0 })
const ts1 = ts0 + 20
const peerRecord1 = new PeerRecord({ peerId, seqNumber: ts1 })
const isEqual = peerRecord0.isEqual(peerRecord1)
expect(isEqual).to.eql(false)
})
it('isEqual returns false if the peer record has a different multiaddrs', () => {
const multiaddrs = [
multiaddr('/ip4/127.0.0.1/tcp/2000')
]
const peerRecord0 = new PeerRecord({ peerId, multiaddrs })
const multiaddrs1 = [
multiaddr('/ip4/127.0.0.1/tcp/2001')
]
const peerRecord1 = new PeerRecord({ peerId, multiaddrs: multiaddrs1 })
const isEqual = peerRecord0.isEqual(peerRecord1)
expect(isEqual).to.eql(false)
})
})
describe('PeerRecord inside Envelope', () => {
// TODO
})