Skip to content

Commit

Permalink
some more tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Oct 30, 2018
1 parent 383d9df commit e3211e0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.1",
"class-is": "^1.1.0",
"libp2p-crypto": "libp2p/js-libp2p-crypto#371bd05",
"lodash": "^4.17.11",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ class PeerId {
/*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid (callback) {
async isValid () {
// TODO Needs better checking
if (this.privKey &&
this.privKey.public &&
this.privKey.public.bytes &&
Buffer.isBuffer(this.pubKey.bytes) &&
this.privKey.public.bytes.equals(this.pubKey.bytes)) {
callback()
return true
} else {
callback(new Error('Keys not match'))
throw new Error('Keys not match')
}
}
}
Expand Down
60 changes: 24 additions & 36 deletions test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,51 +139,38 @@ describe('PeerId', () => {
const other = await PeerId.createFromJSON(await id.toJSON())
expect(id.toB58String()).to.equal(other.toB58String())
})

it('go interop', async () => {
const id = await PeerId.createFromJSON(goId)
const digest = await id.privKey.public.hash()
expect(mh.toB58String(digest)).to.eql(goId.id)
})
})

/*
it('set privKey (valid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
peerId.privKey = peerId._privKey
peerId.isValid(done)
})
it('set privKey (valid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.privKey = peerId._privKey
await peerId.isValid()
})

it('set pubKey (valid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
peerId.pubKey = peerId._pubKey
peerId.isValid(done)
})
it('set pubKey (valid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.pubKey = peerId._pubKey
await peerId.isValid()
})

it('set privKey (invalid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
peerId.privKey = Buffer.from('bufff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
})
})
it('set privKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.privKey = Buffer.from('bufff')
await expect(peerId.isValid()).to.be.rejected
})

it('set pubKey (invalid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
peerId.pubKey = Buffer.from('buffff')
peerId.isValid((err) => {
/*
it('set pubKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.pubKey = Buffer.from('buffff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
})
Expand Down Expand Up @@ -213,7 +200,7 @@ describe('PeerId', () => {
let k2
let k3
before((done) => {
before(async () => {
parallel([
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
Expand All @@ -228,7 +215,7 @@ describe('PeerId', () => {
})
})
it('missmatch private - public key', (done) => {
it('missmatch private - public key', async () => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist()
expect(() => new PeerId(digest, k1, k2.public))
Expand All @@ -237,7 +224,7 @@ describe('PeerId', () => {
})
})
it('missmatch id - private - public key', (done) => {
it('missmatch id - private - public key', async () => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist()
expect(() => new PeerId(digest, k1, k3.public))
Expand All @@ -249,5 +236,6 @@ describe('PeerId', () => {
it('invalid id', () => {
expect(() => new PeerId('hello world')).to.throw(/invalid id/)
})
}) */
})
*/
})

0 comments on commit e3211e0

Please sign in to comment.