js-peer-id/test/index.spec.js

171 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-03-22 17:01:46 +01:00
/* eslint-env mocha */
2016-02-12 14:55:05 -08:00
'use strict'
const expect = require('chai').expect
const crypto = require('libp2p-crypto')
const mh = require('multihashes')
2016-02-12 14:55:05 -08:00
const PeerId = require('../src')
const testId = require('./fixtures/sample-id')
const testIdHex = testId.id
const testIdBytes = mh.fromHexString(testId.id)
const testIdB58String = mh.toB58String(testIdBytes)
2016-02-12 14:55:05 -08:00
const goId = require('./fixtures/go-private-key')
2016-02-12 14:55:05 -08:00
describe('PeerId', () => {
it('create an id without \'new\'', () => {
2016-04-11 14:43:50 -07:00
expect(PeerId).to.throw(Error)
})
it('create a new id', () => {
const id = PeerId.create()
2016-02-12 19:55:44 -08:00
expect(id.toB58String().length).to.equal(46)
2016-02-12 14:55:05 -08:00
})
2016-02-14 07:32:09 +00:00
it('recreate an Id from Hex string', () => {
const id = PeerId.createFromHexString(testIdHex)
2016-02-12 16:13:09 -08:00
expect(testIdBytes).to.deep.equal(id.id)
2016-02-12 14:55:05 -08:00
})
2016-02-14 07:32:09 +00:00
it('Recreate an Id from a Buffer', () => {
const id = PeerId.createFromBytes(testIdBytes)
2016-02-12 16:13:09 -08:00
expect(testId.id).to.equal(id.toHexString())
2016-02-12 14:55:05 -08:00
})
2016-02-14 07:32:09 +00:00
it('Recreate a B58 String', () => {
const id = PeerId.createFromB58String(testIdB58String)
2016-02-12 16:13:09 -08:00
expect(testIdB58String).to.equal(id.toB58String())
2016-02-12 14:55:05 -08:00
})
2016-02-14 07:32:09 +00:00
it('Recreate from a Public Key', () => {
const id = PeerId.createFromPubKey(testId.pubKey)
2016-02-12 16:13:09 -08:00
expect(testIdB58String).to.equal(id.toB58String())
2016-02-12 14:55:05 -08:00
})
2016-02-14 07:32:09 +00:00
it('Recreate from a Private Key', () => {
const id = PeerId.createFromPrivKey(testId.privKey)
2016-02-12 16:13:09 -08:00
expect(testIdB58String).to.equal(id.toB58String())
const id2 = PeerId.createFromPrivKey(new Buffer(testId.privKey, 'base64'))
expect(testIdB58String).to.equal(id2.toB58String())
2016-02-12 14:55:05 -08:00
})
2016-03-10 11:25:59 -08:00
it('Compare generated ID with one created from PubKey', () => {
2016-03-10 11:25:59 -08:00
const id1 = PeerId.create()
const id2 = PeerId.createFromPubKey(id1.marshalPubKey())
expect(id1.id).to.be.eql(id2.id)
2016-03-10 11:25:59 -08:00
})
2016-03-15 12:15:44 -07:00
it('Non-default # of bits', () => {
2016-03-15 12:15:44 -07:00
const shortId = PeerId.create({ bits: 128 })
const longId = PeerId.create({ bits: 256 })
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
2016-03-15 12:15:44 -07:00
})
2016-04-11 14:43:50 -07:00
it('Pretty printing', () => {
2016-04-11 14:43:50 -07:00
const id = PeerId.createFromPrivKey(testId.privKey)
const out = id.toPrint()
expect(out.id).to.equal(testIdB58String)
expect(out.privKey).to.equal(testId.privKey)
expect(out.pubKey).to.equal(testId.pubKey)
2016-04-11 14:43:50 -07:00
})
it('toBytes', () => {
2016-04-11 14:43:50 -07:00
const id = PeerId.createFromHexString(testIdHex)
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
})
describe('toJSON', () => {
it('full node', () => {
const id = PeerId.create({bits: 64})
expect(
id.toB58String()
).to.equal(
PeerId.createFromJSON(id.toJSON()).toB58String()
)
expect(
id.privKey.bytes
).to.deep.equal(
PeerId.createFromJSON(id.toJSON()).privKey.bytes
)
expect(
id.pubKey.bytes
).to.deep.equal(
PeerId.createFromJSON(id.toJSON()).pubKey.bytes
)
})
it('only id', () => {
const key = crypto.generateKeyPair('RSA', 64)
const id = PeerId.createFromBytes(key.public.hash())
expect(
id.toB58String()
).to.equal(
PeerId.createFromJSON(id.toJSON()).toB58String()
)
expect(id.privKey).to.not.exist
expect(id.pubKey).to.not.exist
})
it('go interop', () => {
const id = PeerId.createFromJSON(goId)
expect(
mh.toB58String(id.privKey.public.hash())
).to.be.eql(
goId.id
)
})
})
describe('throws on inconsistent data', () => {
const k1 = crypto.generateKeyPair('RSA', 64)
const k2 = crypto.generateKeyPair('RSA', 64)
const k3 = crypto.generateKeyPair('RSA', 64)
it('missmatch id - private key', () => {
expect(
() => new PeerId(k1.public.hash(), k2)
).to.throw(
/inconsistent arguments/
)
})
it('missmatch id - public key', () => {
expect(
() => new PeerId(k1.public.hash(), null, k2.public)
).to.throw(
/inconsistent arguments/
)
})
it('missmatch private - public key', () => {
expect(
() => new PeerId(k1.public.hash(), k1, k2.public)
).to.throw(
/inconsistent arguments/
)
})
it('missmatch id - private - public key', () => {
expect(
() => new PeerId(k1.public.hash(), k1, k3.public)
).to.throw(
/inconsistent arguments/
)
})
it('invalid id', () => {
expect(
() => new PeerId(k1.public.hash().toString())
).to.throw(
/invalid id/
)
})
})
2016-02-12 14:55:05 -08:00
})