Skip to content

Commit

Permalink
Add BigInteger tests (aws-amplify#4520)
Browse files Browse the repository at this point in the history
* fix:(amazon-cognito-identity-js): include radix parameter

Without this fix, a negative bigint will always end up throwing `new Error('Only radix 2, 4, 8, 16, 32 are supported')` because b is undefined.  This fix is present in the original code from which this file was derived: http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js

* Include radix when calling bnToString() for negatives

The bnToString() function throws an exception when called without a radix value or given a radix outside of [2, 4, 8, 16, 32]. 

When bnToString() is called on a negative big int value, it's negated value is used to call bnToString() which then gets concatenated to a '-'. However, in the bnToString() call for the negated value, no radix value is passed in so it always throws an exception. 

This change will use the radix value argument from the initial bnToString() call on the negative big integer value when calling bnToString() for it's negated value so that it works as intended.

* Add BigInteger.test.js

* Update dependencies so that jest runs

* Correct negative test name

* Update .babelrc to mirror previous one
  • Loading branch information
ericclemmons authored Dec 18, 2019
1 parent 8674120 commit 36dfd8f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
37 changes: 22 additions & 15 deletions packages/amazon-cognito-identity-js/.babelrc
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
{
"presets": [
[
"es2015", {
"loose": true,
"modules": false
}
]
],
"env": {
"commonjs": {
"plugins": [
["transform-es2015-modules-commonjs", { "loose": true }]
]
}
}
"presets": [
[
"@babel/preset-env",
{
"loose": true,
"modules": false
}
]
],
"env": {
"commonjs": {
"presets": [
[
"@babel/preset-env",
{
"loose": true,
"modules": "commonjs"
}
]
]
}
}
}
11 changes: 4 additions & 7 deletions packages/amazon-cognito-identity-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@
"js-cookie": "^2.1.4"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.13.2",
"babel-loader": "^6.2.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.23.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"babel-loader": "^8.0.6",
"cross-env": "^3.1.4",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^5.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/amazon-cognito-identity-js/src/BigInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function bnpClamp() {

// (public) return string representation in given radix
function bnToString(b) {
if (this.s < 0) return '-' + this.negate().toString();
if (this.s < 0) return '-' + this.negate().toString(b);
var k;
if (b == 16) k = 4;
else if (b == 8) k = 3;
Expand Down
17 changes: 17 additions & 0 deletions packages/amazon-cognito-identity-js/src/BigInteger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import BigInteger from './BigInteger';

describe('BigInteger', () => {
describe('.toString(radix)', () => {
it('should support positive numbers', () => {
expect(new BigInteger('abcd1234', 16).toString(4)).toBe(
'2223303101020310'
);
});

it('should support negative numbers', () => {
expect(new BigInteger('-abcd1234', 16).toString(4)).toBe(
'-2223303101020310'
);
});
});
});

0 comments on commit 36dfd8f

Please sign in to comment.