Skip to content

Commit

Permalink
test: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
markin-io committed Jan 30, 2024
1 parent bc214cf commit fbdc7e1
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/transaction/payload/assetunlockpayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ AssetUnlockPayload.prototype.validate = function () {

Preconditions.checkArgument(
isUnsignedInteger(this.index),
`Invalid asset unlock index ${this.index}`
`Expect index to be an unsigned integer`
);

Preconditions.checkArgument(
isUnsignedInteger(this.fee),
`Invalid asset unlock fee ${this.fee}`
`Expect fee to be an unsigned integer`
);

Preconditions.checkArgument(
isUnsignedInteger(this.requestHeight),
`Invalid asset unlock request height ${this.requestHeight}`
`Expect requestHeight to be an unsigned integer`
);

Preconditions.checkArgument(
Expand All @@ -120,7 +120,7 @@ AssetUnlockPayload.prototype.validate = function () {

Preconditions.checkArgument(
utils.isHexaString(this.quorumSig),
'Expect quorumHash to be a hex string'
'Expect quorumSig to be a hex string'
);

return true;
Expand Down
161 changes: 159 additions & 2 deletions test/transaction/payload/assetunlockpayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ describe('AssetUnlockPayload', function () {
});
});

// TODO: write more tests
describe.skip('#validate', function () {
describe('#validate', function () {
it('Should allow only unsigned integer as version', function () {
var payload = validAssetUnlockPayload.copy();

Expand Down Expand Up @@ -155,6 +154,164 @@ describe('AssetUnlockPayload', function () {
payload.validate();
}).not.to.throw;
});

it('Should allow only unsigned integer as index', function () {
var payload = validAssetUnlockPayload.copy();

payload.index = -1;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect index to be an unsigned integer');

payload.index = 1.5;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect index to be an unsigned integer');

payload.index = '12';

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect index to be an unsigned integer');

payload.index = Buffer.from('0a0f', 'hex');

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect index to be an unsigned integer');

payload.index = 123;

expect(function () {
payload.validate();
}).not.to.throw;
});

it('Should allow only unsigned integer as fee', function () {
var payload = validAssetUnlockPayload.copy();

payload.fee = -1;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect fee to be an unsigned integer');

payload.fee = 1.5;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect fee to be an unsigned integer');

payload.fee = '12';

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect fee to be an unsigned integer');

payload.fee = Buffer.from('0a0f', 'hex');

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect fee to be an unsigned integer');

payload.fee = 123;

expect(function () {
payload.validate();
}).not.to.throw;
});

it('Should allow only unsigned integer as requestHeight', function () {
var payload = validAssetUnlockPayload.copy();

payload.requestHeight = -1;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect requestHeight to be an unsigned integer');

payload.requestHeight = 1.5;

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect requestHeight to be an unsigned integer');

payload.requestHeight = '12';

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect requestHeight to be an unsigned integer');

payload.requestHeight = Buffer.from('0a0f', 'hex');

expect(function () {
payload.validate();
}).to.throw('Invalid Argument: Expect requestHeight to be an unsigned integer');

payload.requestHeight = 123;

expect(function () {
payload.validate();
}).not.to.throw;
});

it('Should allow only hex string as quorumHash', function () {
var payload = validAssetUnlockPayload.copy();

payload.quorumHash = -1;

expect(function () {
payload.validate();
}).to.throw('Expect quorumHash to be a hex string');

payload.quorumHash = 1.5;

expect(function () {
payload.validate();
}).to.throw('Expect quorumHash to be a hex string');

payload.quorumHash = Buffer.from('0a0f', 'hex');

expect(function () {
payload.validate();
}).to.throw('Expect quorumHash to be a hex string');

payload.quorumHash = 123;

expect(function () {
payload.validate();
}).not.to.throw;
});

it('Should allow only hex string as quorumSig', function () {
var payload = validAssetUnlockPayload.copy();

payload.quorumSig = -1;

expect(function () {
payload.validate();
}).to.throw('Expect quorumSig to be a hex string');

payload.quorumSig = 1.5;

expect(function () {
payload.validate();
}).to.throw('Expect quorumSig to be a hex string');

payload.quorumSig = Buffer.from('0a0f', 'hex');

expect(function () {
payload.validate();
}).to.throw('Expect quorumSig to be a hex string');

payload.quorumSig = 123;

expect(function () {
payload.validate();
}).not.to.throw;
});
});

describe('#toJSON', function () {
Expand Down

0 comments on commit fbdc7e1

Please sign in to comment.