From fbdc7e19a891b9d6140bee79725a7972ca3bb02e Mon Sep 17 00:00:00 2001 From: "markin.io" Date: Tue, 30 Jan 2024 11:32:35 +0000 Subject: [PATCH] test: validation --- lib/transaction/payload/assetunlockpayload.js | 8 +- .../transaction/payload/assetunlockpayload.js | 161 +++++++++++++++++- 2 files changed, 163 insertions(+), 6 deletions(-) diff --git a/lib/transaction/payload/assetunlockpayload.js b/lib/transaction/payload/assetunlockpayload.js index 236cafbd6..5c6249f7b 100644 --- a/lib/transaction/payload/assetunlockpayload.js +++ b/lib/transaction/payload/assetunlockpayload.js @@ -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( @@ -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; diff --git a/test/transaction/payload/assetunlockpayload.js b/test/transaction/payload/assetunlockpayload.js index a2a709f95..61d58c543 100644 --- a/test/transaction/payload/assetunlockpayload.js +++ b/test/transaction/payload/assetunlockpayload.js @@ -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(); @@ -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 () {