Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from ethereumjs/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
axic authored Jul 28, 2018
2 parents 3a96b6e + 18ded5b commit 688bd06
Showing 1 changed file with 60 additions and 19 deletions.
79 changes: 60 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ var Wallet = require('../')
var Thirdparty = require('../thirdparty.js')
var ethUtil = require('ethereumjs-util')

var fixturekey = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex')
var fixturewallet = Wallet.fromPrivateKey(fixturekey)
var fixturePrivateKey = 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378'
var fixturePrivateKeyStr = '0x' + fixturePrivateKey
var fixturePrivateKeyBuffer = Buffer.from(fixturePrivateKey, 'hex')

var fixturePublicKey = '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c'
var fixturePublicKeyStr = '0x' + fixturePublicKey
var fixturePublicKeyBuffer = Buffer.from(fixturePublicKey, 'hex')

var fixtureWallet = Wallet.fromPrivateKey(fixturePrivateKeyBuffer)

describe('.getPrivateKey()', function () {
it('should work', function () {
assert.equal(fixturewallet.getPrivateKey().toString('hex'), 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378')
assert.equal(fixtureWallet.getPrivateKey().toString('hex'), fixturePrivateKey)
})
it('should fail', function () {
assert.throws(function () {
Expand All @@ -20,45 +27,45 @@ describe('.getPrivateKey()', function () {

describe('.getPrivateKeyString()', function () {
it('should work', function () {
assert.equal(fixturewallet.getPrivateKeyString(), '0xefca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378')
assert.equal(fixtureWallet.getPrivateKeyString(), fixturePrivateKeyStr)
})
})

describe('.getPublicKey()', function () {
it('should work', function () {
assert.equal(fixturewallet.getPublicKey().toString('hex'), '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
assert.equal(fixtureWallet.getPublicKey().toString('hex'), fixturePublicKey)
})
})

describe('.getPublicKeyString()', function () {
it('should work', function () {
assert.equal(fixturewallet.getPublicKeyString(), '0x5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
assert.equal(fixtureWallet.getPublicKeyString(), fixturePublicKeyStr)
})
})

describe('.getAddress()', function () {
it('should work', function () {
assert.equal(fixturewallet.getAddress().toString('hex'), 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
assert.equal(fixtureWallet.getAddress().toString('hex'), 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
})
})

describe('.getAddressString()', function () {
it('should work', function () {
assert.equal(fixturewallet.getAddressString(), '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
assert.equal(fixtureWallet.getAddressString(), '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
})
})

describe('.getChecksumAddressString()', function () {
it('should work', function () {
assert.equal(fixturewallet.getChecksumAddressString(), '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c')
assert.equal(fixtureWallet.getChecksumAddressString(), '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c')
})
})

describe('public key only wallet', function () {
var pubKey = Buffer.from('5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c', 'hex')
var pubKey = Buffer.from(fixturePublicKey, 'hex')
it('.fromPublicKey() should work', function () {
assert.equal(Wallet.fromPublicKey(pubKey).getPublicKey().toString('hex'),
'5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
fixturePublicKey)
})
it('.fromPublicKey() should not accept compressed keys in strict mode', function () {
assert.throws(function () {
Expand Down Expand Up @@ -112,17 +119,25 @@ describe('.generate()', function () {
})

describe('.generateVanityAddress()', function () {
it('should generate an account with 000 prefix', function () {
it('should generate an account with 000 prefix (object)', function () {
this.timeout(180000) // 3minutes
var wallet = Wallet.generateVanityAddress(/^000/)
assert.equal(wallet.getPrivateKey().length, 32)
assert.equal(wallet.getAddress()[0], 0)
assert.equal(wallet.getAddress()[1] >>> 4, 0)
})
it('should generate an account with 000 prefix (string)', function () {
this.timeout(180000) // 3minutes
var wallet = Wallet.generateVanityAddress('^000')
assert.equal(wallet.getPrivateKey().length, 32)
assert.equal(wallet.getAddress()[0], 0)
assert.equal(wallet.getAddress()[1] >>> 4, 0)
})
})

describe('.getV3Filename()', function () {
it('should work', function () {
assert.equal(fixturewallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
assert.equal(fixtureWallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
})
})

Expand All @@ -132,19 +147,25 @@ describe('.toV3()', function () {
var uuid = Buffer.from('7e59dc028d42d09db29aa8a0f862cc81', 'hex')

it('should work with PBKDF2', function () {
var key = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex')
var wallet = Wallet.fromPrivateKey(key)
var w = '{"version":3,"id":"7e59dc02-8d42-409d-b29a-a8a0f862cc81","address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"ciphertext":"01ee7f1a3c8d187ea244c92eea9e332ab0bb2b4c902d89bdd71f80dc384da1be","cipherparams":{"iv":"cecacd85e9cb89788b5aab2f93361233"},"cipher":"aes-128-ctr","kdf":"pbkdf2","kdfparams":{"dklen":32,"salt":"dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6","c":262144,"prf":"hmac-sha256"},"mac":"0c02cd0badfebd5e783e0cf41448f84086a96365fc3456716c33641a86ebc7cc"}}'
// FIXME: just test for ciphertext and mac?
assert.equal(wallet.toV3String('testtest', { kdf: 'pbkdf2', uuid: uuid, salt: salt, iv: iv }), w)
assert.equal(fixtureWallet.toV3String('testtest', { kdf: 'pbkdf2', uuid: uuid, salt: salt, iv: iv }), w)
})
it('should work with Scrypt', function () {
var key = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex')
var wallet = Wallet.fromPrivateKey(key)
var w = '{"version":3,"id":"7e59dc02-8d42-409d-b29a-a8a0f862cc81","address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"ciphertext":"c52682025b1e5d5c06b816791921dbf439afe7a053abb9fac19f38a57499652c","cipherparams":{"iv":"cecacd85e9cb89788b5aab2f93361233"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6","n":262144,"r":8,"p":1},"mac":"27b98c8676dc6619d077453b38db645a4c7c17a3e686ee5adaf53c11ac1b890e"}}'
this.timeout(180000) // 3minutes
// FIXME: just test for ciphertext and mac?
assert.equal(wallet.toV3String('testtest', { kdf: 'scrypt', uuid: uuid, salt: salt, iv: iv }), w)
assert.equal(fixtureWallet.toV3String('testtest', { kdf: 'scrypt', uuid: uuid, salt: salt, iv: iv }), w)
})
it('should work without providing options', function () {
this.timeout(180000) // 3minutes
assert.equal(fixtureWallet.toV3('testtest')['version'], 3)
})
it('should fail for unsupported kdf', function () {
this.timeout(180000) // 3minutes
assert.throws(function () {
fixtureWallet.toV3('testtest', { kdf: 'superkey' })
}, /^Error: Unsupported kdf$/)
})
})

Expand Down Expand Up @@ -187,6 +208,18 @@ describe('.fromV3()', function () {
Wallet.fromV3(w, 'testpassword')
}) // FIXME: check for assert message(s)
})
it('should fail for wrong version', function () {
var w = '{"version":2}'
assert.throws(function () {
Wallet.fromV3(w, 'testpassword')
}, /^Error: Not a V3 wallet$/)
})
it('should fail for wrong kdf', function () {
var w = '{"Crypto":{"kdf":"superkey"},"version":3}'
assert.throws(function () {
Wallet.fromV3(w, 'testpassword', true)
}, /^Error: Unsupported key derivation scheme$/)
})
})

describe('.fromEthSale()', function () {
Expand Down Expand Up @@ -235,3 +268,11 @@ describe('.fromQuorumWallet()', function () {
assert.equal(wallet.getAddressString(), '0x1b86ccc22e8f137f204a41a23033541242a48815')
})
})

describe('raw new Wallet() init', function () {
it('should fail when both priv and pub key provided', function () {
assert.throws(function () {
new Wallet(fixturePrivateKeyBuffer, fixturePublicKeyBuffer) // eslint-disable-line
}, /^Error: Cannot supply both a private and a public key to the constructor$/)
})
})

0 comments on commit 688bd06

Please sign in to comment.