From 303cfe7abc4b159fd71baaac7244d61906db77ec Mon Sep 17 00:00:00 2001 From: Matej Lubej Date: Tue, 30 Jul 2024 08:13:08 +0200 Subject: [PATCH] tests: Add parseCBORUint tests --- contracts/contracts/tests/SubcallTests.sol | 8 +++ contracts/test/subcall.ts | 84 +++++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/contracts/contracts/tests/SubcallTests.sol b/contracts/contracts/tests/SubcallTests.sol index 983771429..e6f2d6ae0 100644 --- a/contracts/contracts/tests/SubcallTests.sol +++ b/contracts/contracts/tests/SubcallTests.sol @@ -108,4 +108,12 @@ contract SubcallTests { function testConsensusWithdraw(StakingAddress to, uint128 value) external { Subcall.consensusWithdraw(to, value); } + + function testParseCBORUint(bytes memory result, uint256 offset) + pure + external + returns (uint256, uint256) + { + return Subcall._parseCBORUint(result, offset); + } } diff --git a/contracts/test/subcall.ts b/contracts/test/subcall.ts index 3d75dfee0..cb64a9734 100644 --- a/contracts/test/subcall.ts +++ b/contracts/test/subcall.ts @@ -106,7 +106,11 @@ describe('Subcall', () => { let provider: Provider; before(async () => { - const factory = await ethers.getContractFactory('SubcallTests'); + const subcallFactory = await ethers.getContractFactory("Subcall"); + const subcallLib = await subcallFactory.deploy(); + await subcallLib.waitForDeployment(); + + const factory = await ethers.getContractFactory('SubcallTests', { libraries: { Subcall: await subcallLib.getAddress() } }); contract = (await factory.deploy({ value: parseEther('1.0'), })) as unknown as SubcallTests; @@ -363,4 +367,82 @@ describe('Subcall', () => { expect(shares).eq(toBigInt(num)); } }); + + describe('Should successfully parse CBOR uint/s', () => { + it('Should successfully parse CBOR uint8', async () => { + const MAX_SAFE_UINT8 = 255n + + // bytes = 0x18FF + const bytes = cborg.encode(MAX_SAFE_UINT8) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT8); + expect(newOffset).eq(1 + 1); + }) + + it('Should successfully parse CBOR uint16', async () => { + const MAX_SAFE_UINT16 = 65535n + + // bytes = 0x19FFFF + const bytes = cborg.encode(MAX_SAFE_UINT16) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT16); + expect(newOffset).eq(2 + 1); + }) + + it('Should successfully parse CBOR uint32', async () => { + const MAX_SAFE_UINT32 = 4294967295n + + // bytes = 0x1AFFFFFFFF + const bytes = cborg.encode(MAX_SAFE_UINT32) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT32); + expect(newOffset).eq(4 + 1); + }) + + it('Should successfully parse CBOR uint64', async () => { + const MAX_SAFE_UINT64 = 18446744073709551615n + + // bytes = 0x1BFFFFFFFFFFFFFFFF + const bytes = cborg.encode(MAX_SAFE_UINT64) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT64); + expect(newOffset).eq(8 + 1); + }) + + it('Should successfully parse CBOR uint128', async () => { + const MAX_SAFE_UINT128 = 340282366920938463463374607431768211455n + + const hex = '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' + const uint128bytes = Uint8Array.from(Buffer.from(hex.replace('0x', ''), 'hex')) + + const bytes = cborg.encode(uint128bytes) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT128); + expect(newOffset).eq(16 + 1); + }) + + it('Should successfully parse CBOR uint256', async () => { + const MAX_SAFE_UINT256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935n + + const hex = '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' + const uint256bytes = Uint8Array.from(Buffer.from(hex.replace('0x', ''), 'hex')) + + const bytes = cborg.encode(uint256bytes) + + const [newOffset, parsedCborUint] = await contract.testParseCBORUint(bytes, 0) + + expect(parsedCborUint).eq(MAX_SAFE_UINT256); + expect(newOffset).eq(33 + 1); + }) + }) });