Skip to content

Commit

Permalink
contracts: improve CBOR unit tests for bytes encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Oct 16, 2024
1 parent bdaa5b6 commit 843fd37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions contracts/contracts/CBOR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error CBOR_Error_InvalidUintSize(uint8);
/// CBOR parsed value is out of expected range
error CBOR_Error_ValueOutOfRange();

error CBOR_Error_BytesTooLong();
error CBOR_Error_BytesTooLong(uint256 byteLength);

function encodeUint(uint256 value) pure returns (bytes memory) {
// NOTE: we don't follow bignum tagged encoding
Expand All @@ -38,7 +38,7 @@ function encodeUint(uint256 value) pure returns (bytes memory) {
} else if (value <= type(uint128).max) {
return abi.encodePacked(uint8(0x50), uint128(value));
}
return abi.encodePacked(uint8(0x58), uint(32), value);
return abi.encodePacked(uint8(0x58), uint256(32), value);
}

function encodeBytes(bytes memory in_bytes)
Expand All @@ -62,7 +62,7 @@ function encodeBytes(bytes memory in_bytes)
return abi.encodePacked(uint8(0x59), uint16(in_bytes.length), in_bytes);
}
// We assume Solidity won't be encoding anything larger than 64kb
revert CBOR_Error_BytesTooLong();
revert CBOR_Error_BytesTooLong(in_bytes.length);
}

function parseMapStart(bytes memory in_data, uint256 in_offset)
Expand Down
24 changes: 19 additions & 5 deletions contracts/test/cbor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe('CBOR', () => {
});

it('Bytes encoding', async () => {
for (let i = 0; i < 2048; i = i + (1 + i / 10)) {
//for (let i = 0; i < 0xFFFF; i = i + (1 + i / 10)) {
for (let i = 0; i <= 16; i += 1) {
const nBytes = 1 << i;
const input = randomBytes(i);
const result = await contract.testBytesEncoding(input);
const output = cborDecode(getBytes(result));
Expand All @@ -34,14 +36,26 @@ describe('CBOR', () => {
}
});

it('Bytes encoding (errors)', async () => {
const length = 0xffff + 1;
const bytes = randomBytes(length);
await expect(contract.testBytesEncoding(bytes)).revertedWithCustomError(
contract,
'CBOR_Error_BytesTooLong',
);
});

/// Verifies that bigints (byte encoded) can be parsed from 1 to 255 bits
it('Parse uint (bytes)', async () => {
for( let i = 0n; i < 256n; i += 1n ) {
const value = 1n<<i;
for (let i = 0n; i < 256n; i += 1n) {
const value = 1n << i;
const hex = value.toString(16);
const hexPadded = hex.padStart(hex.length + hex.length % 2, '0');
const hexPadded = hex.padStart(hex.length + (hex.length % 2), '0');
const encoded = cborEncode(getBytes(`0x${hexPadded}`));
const [newOffset,parsedCborUint] = await contract.testParseUint(encoded, 0);
const [newOffset, parsedCborUint] = await contract.testParseUint(
encoded,
0,
);
expect(await contract.testUintRoundtrip(i)).eq(true);
expect(newOffset).eq(encoded.length);
expect(toQuantity(parsedCborUint)).eq(`0x${hex}`);
Expand Down

0 comments on commit 843fd37

Please sign in to comment.