test: upgrade libp2p-crypto-secp256k1; ensure secp256k1 is tested (#151)

This commit is contained in:
Arve Knudsen 2019-07-11 00:32:51 +02:00 committed by Jacob Heun
parent d675670ed9
commit 5500ac4a6e
2 changed files with 26 additions and 37 deletions

View File

@ -57,7 +57,8 @@
"bundlesize": "~0.18.0", "bundlesize": "~0.18.0",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-string": "^1.5.0", "chai-string": "^1.5.0",
"dirty-chai": "^2.0.1" "dirty-chai": "^2.0.1",
"sinon": "^7.3.2"
}, },
"engines": { "engines": {
"node": ">=10.0.0", "node": ">=10.0.0",

View File

@ -5,34 +5,19 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai') const dirtyChai = require('dirty-chai')
const expect = chai.expect const expect = chai.expect
chai.use(dirtyChai) chai.use(dirtyChai)
const sinon = require('sinon')
const fixtures = require('../fixtures/secp256k1') const fixtures = require('../fixtures/secp256k1')
const crypto = require('../../src') const crypto = require('../../src')
const mockPublicKey = {
bytes: fixtures.pbmPublicKey
}
const mockPrivateKey = {
bytes: fixtures.pbmPrivateKey,
public: mockPublicKey
}
const mockSecp256k1Module = {
generateKeyPair (bits) {
return mockPrivateKey
},
unmarshalSecp256k1PrivateKey (buf) {
return mockPrivateKey
},
unmarshalSecp256k1PublicKey (buf) {
return mockPublicKey
}
}
describe('without libp2p-crypto-secp256k1 module present', () => { describe('without libp2p-crypto-secp256k1 module present', () => {
crypto.keys.supportedKeys.secp256k1 = undefined before(() => {
sinon.replace(crypto.keys.supportedKeys, 'secp256k1', null)
})
after(() => {
sinon.restore()
})
it('fails to generate a secp256k1 key', async () => { it('fails to generate a secp256k1 key', async () => {
try { try {
@ -60,22 +45,15 @@ describe('without libp2p-crypto-secp256k1 module present', () => {
}) })
describe('with libp2p-crypto-secp256k1 module present', () => { describe('with libp2p-crypto-secp256k1 module present', () => {
let key it('generates a valid key', async () => {
const key = await crypto.keys.generateKeyPair('secp256k1', 256)
before(async () => {
crypto.keys.supportedKeys.secp256k1 = mockSecp256k1Module
key = await crypto.keys.generateKeyPair('secp256k1', 256)
})
after(() => {
delete crypto.keys.secp256k1
})
it('generates a valid key', () => {
expect(key).to.exist() expect(key).to.exist()
}) })
it('protobuf encoding', async () => { it('protobuf encoding', async () => {
const key = await crypto.keys.generateKeyPair('secp256k1', 256)
expect(key).to.exist()
const keyMarshal = crypto.keys.marshalPrivateKey(key) const keyMarshal = crypto.keys.marshalPrivateKey(key)
const key2 = await crypto.keys.unmarshalPrivateKey(keyMarshal) const key2 = await crypto.keys.unmarshalPrivateKey(keyMarshal)
const keyMarshal2 = crypto.keys.marshalPrivateKey(key2) const keyMarshal2 = crypto.keys.marshalPrivateKey(key2)
@ -89,4 +67,14 @@ describe('with libp2p-crypto-secp256k1 module present', () => {
expect(pkMarshal).to.eql(pkMarshal2) expect(pkMarshal).to.eql(pkMarshal2)
}) })
it('unmarshals a secp256k1 private key', async () => {
const key = await crypto.keys.unmarshalPrivateKey(fixtures.pbmPrivateKey)
expect(key).to.exist()
})
it('unmarshals a secp256k1 public key', () => {
const key = crypto.keys.unmarshalPublicKey(fixtures.pbmPublicKey)
expect(key).to.exist()
})
}) })