Skip to content

Commit

Permalink
add more thorough external tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Feb 4, 2020
1 parent ae4c300 commit e48be5e
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { toBuffer, baToJSON, stripZeros } from './bytes'
* @param data data to be validated against the definitions
* @deprecated
*/
export const defineProperties = function(self: any, fields: any, data: any) {
export const defineProperties = function(self: any, fields: any, data?: any) {
self.raw = []
self._fields = []

Expand Down
8 changes: 4 additions & 4 deletions test/account.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert = require('assert')
const {
import * as assert from 'assert'
import * as BN from 'bn.js'
import {
isValidPrivate,
isValidPublic,
importPublic,
Expand All @@ -13,8 +14,7 @@ const {
isValidChecksumAddress,
isValidAddress,
toChecksumAddress,
} = require('../src')
import BN = require('bn.js')
} from '../src'
const eip1014Testdata = require('./testdata/eip1014Examples.json')

describe('isValidPrivate', function() {
Expand Down
26 changes: 4 additions & 22 deletions test/bytes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as assert from 'assert'
const {
import * as BN from 'bn.js'
import {
zeros,
zeroAddress,
isZeroAddress,
Expand All @@ -8,16 +9,13 @@ const {
setLengthLeft,
setLengthRight,
bufferToHex,
intToHex,
intToBuffer,
bufferToInt,
fromSigned,
toUnsigned,
addHexPrefix,
toBuffer,
baToJSON,
} = require('../src')
import BN = require('bn.js')
} from '../src'

describe('zeros function', function() {
it('should produce lots of 0s', function() {
Expand Down Expand Up @@ -109,22 +107,6 @@ describe('bufferToHex', function() {
})
})

describe('intToHex', function() {
it('should convert a int to hex', function() {
const i = 6003400
const hex = intToHex(i)
assert.equal(hex, '0x5b9ac8')
})
})

describe('intToBuffer', function() {
it('should convert a int to a buffer', function() {
const i = 6003400
const buf = intToBuffer(i)
assert.equal(buf.toString('hex'), '5b9ac8')
})
})

describe('bufferToInt', function() {
it('should convert a int to hex', function() {
const buf = Buffer.from('5b9ac8', 'hex')
Expand Down Expand Up @@ -178,7 +160,7 @@ describe('hex prefix', function() {
assert.equal(addHexPrefix(string), '0x' + string)
})
it('should return on non-string input', function() {
assert.equal(addHexPrefix(1), 1)
assert.equal(addHexPrefix(1 as any), 1)
})
})

Expand Down
6 changes: 3 additions & 3 deletions test/constants.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert = require('assert')
const {
import * as assert from 'assert'
import {
MAX_INTEGER,
TWO_POW256,
KECCAK256_NULL_S,
Expand All @@ -8,7 +8,7 @@ const {
KECCAK256_RLP_ARRAY,
KECCAK256_RLP_S,
KECCAK256_RLP,
} = require('../src')
} from '../src'

describe('constants', function() {
it('should match constants', function() {
Expand Down
160 changes: 149 additions & 11 deletions test/externals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,167 @@
import assert = require('assert')
import * as assert from 'assert'

import * as BN_export from 'bn.js'
import * as rlp_export from 'rlp'
import * as secp256k1_export from 'secp256k1'

const ethjsUtil = require('ethjs-util')

import * as src from '../src'

describe('externals', function() {
it('should export `BN`', function() {
describe('External BN export', () => {
it('should export `BN`', () => {
assert.equal(src.BN, BN_export)
})

it('should export `rlp`', function() {
it('should use a BN function correctly', () => {
const a = new src.BN('dead', 16)
const b = new src.BN('101010', 2)
const result = a.add(b)
assert.equal(result.toString(10), 57047)
})

it('should throw on exceptions', () => {
// should not allow 0 input
assert.throws(() => {
new src.BN(1).egcd(new src.BN('0'))
}, /^Error: Assertion failed$/)
})

// should not accept an unsafe integer
const num = Math.pow(2, 53)
assert.throws(() => {
return new src.BN(num, 10)
}, /^Error: Assertion failed$/)

// should throw error with num eq 0x4000000
assert.throws(function() {
new src.BN(0).iaddn(0x4000000)
}, /^Error: Assertion failed$/)
})

describe('External rlp export', () => {
it('should export `rlp`', () => {
assert.equal(src.rlp, rlp_export)
})

it('should export `scep256k1`', function() {
it('should use a rlp function correctly', () => {
const nestedList = [[], [[]], [[], [[]]]]
var encoded = src.rlp.encode(nestedList)
var decoded = src.rlp.decode(encoded)
assert.deepEqual(nestedList, decoded)
})

it('should throw on exceptions', () => {
// bad values: wrong encoded a zero
const val = Buffer.from(
'f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3',
'hex',
)
let result
try {
result = src.rlp.decode(val)
} catch (e) {}
assert.equal(result, undefined)

// bad values: invalid length
const a = Buffer.from(
'f86081000182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3',
'hex',
)

let res
try {
result = src.rlp.decode(a)
} catch (e) {}
assert.equal(res, undefined)
})
})

describe('External secp256k1 export', () => {
it('should export `secp256k1`', () => {
assert.equal(src.secp256k1, secp256k1_export)
})

it('should have `ethjs-util` methods', function() {
for (const property in ethjsUtil) {
assert.ok(src.hasOwnProperty(property))
}
it('should use a secp256k1 function correctly', () => {
// generate message to sign
const msg = Buffer.from(
'983232e10f8d440b3bde2c0787084b1228a0a0bd6d18bf78165696bc76f3530e',
'hex',
)
// generate privKey
const privKey = Buffer.from(
'59812df42e7bbb8f60a0ae92c660dcb6700927f944c709eaa0b9447d9ebffaf7',
'hex',
)
// get the public key in a compressed format
const pubKey = src.secp256k1.publicKeyCreate(privKey)
// sign the message
const sigObj = src.secp256k1.sign(msg, privKey)
// verify the signature
assert.ok(src.secp256k1.verify(msg, sigObj.signature, pubKey))
})

it('should throw on exceptions', () => {
// publicKeyVerify should be a Buffer
assert.throws(() => {
src.secp256k1.publicKeyVerify(null as any)
})

// publicKeyCombine public keys should have length greater that zero
assert.throws(() => {
src.secp256k1.publicKeyCombine([])
})

// privateKeyImport invalid format
assert.throws(() => {
const buffer = Buffer.from([0x00])
src.secp256k1.privateKeyImport(buffer)
})
})
})

describe('External ethjsUtil export', () => {
it('should have all ethjsUtil methods', () => {
const expected = [
'arrayContainsArray',
'toBuffer',
'intToBuffer',
'getBinarySize',
'stripHexPrefix',
'isHexPrefixed',
'padToEven',
'intToHex',
'fromAscii',
'fromUtf8',
'toAscii',
'getKeys',
'isHexString',
'toUtf8',
]

expected.forEach(prop => {
assert.ok(src.hasOwnProperty(prop))
})
})

it('should use ethjsUtil functions correctly', () => {
// should convert intToHex
assert.equal((src as any).intToHex(new src.BN(0)), '0x0')

// should convert intToHex
const i = 6003400
const hex = (src as any).intToHex(i)
assert.equal(hex, '0x5b9ac8')

// should convert a int to a buffer
const j = 6003400
const buf = (src as any).intToBuffer(j)
assert.equal(buf.toString('hex'), '5b9ac8')
})

it('should handle exceptions and invalid inputs', () => {
// should throw when invalid abi
assert.throws(() => (src as any).getKeys([], 3289), Error)

// should detect invalid length hex string
assert.equal((src as any).isHexString('0x0', 2), false)
})
})
4 changes: 2 additions & 2 deletions test/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert = require('assert')
const { keccak, keccak256, sha256, ripemd160, rlphash } = require('../src')
import * as assert from 'assert'
import { keccak, keccak256, sha256, ripemd160, rlphash } from '../src'

describe('keccak', function() {
it('should produce a hash', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert')
const { zeros, defineProperties } = require('../src')
import * as assert from 'assert'
import { zeros, defineProperties } from '../src'

describe('define', function() {
const fields = [
Expand Down
8 changes: 4 additions & 4 deletions test/signature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import assert = require('assert')
const {
import * as assert from 'assert'
import * as BN from 'bn.js'
import {
ecsign,
ecrecover,
privateToPublic,
hashPersonalMessage,
isValidSignature,
fromRpcSig,
toRpcSig,
} = require('../src')
import BN = require('bn.js')
} from '../src'

const echash = Buffer.from(
'82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28',
Expand Down

0 comments on commit e48be5e

Please sign in to comment.