Skip to content

Commit

Permalink
feat: StringCoder to throw for length value mismatch (#988)
Browse files Browse the repository at this point in the history
* test: add individual unit test files for coders

* test: implemented abi coder test utils

* Revert "test: add individual unit test files for coders"

This reverts commit 9d77f9d.

* test: refactor `NumberCoder` tests (#949)

* test: refactor and increase verbosity of number coder tests

* chore: changeset

* chore: force rebuild

---------

Co-authored-by: danielbate <--global>

* test: refactor `B256Coder` tests (#946)

* test: refactor and increase verbosity of b256 coder tests

* chore: changeset

* test: add test for missing throw condition

---------

Co-authored-by: danielbate <--global>

* test: refactor `B512Coder` tests (#945)

* test: refactor and increase verbosity of b512 coder tests

* chore: changeset

* chore: linting

* test: add test case for missing throw condition in b512 coder

* test: correct imported class name in b512 coder test

Co-authored-by: Sérgio Torres <[email protected]>

---------

Co-authored-by: danielbate <--global>
Co-authored-by: Sérgio Torres <[email protected]>

* test: refactor `BooleanCoder` tests (#944)

* test: refactor and increase verbosity of boolean coder tests

* chore: changeset

* test: add missing test coverage to boolean coder throw conditions

---------

Co-authored-by: danielbate <--global>

* test: refactor `ByteCoder` tests (#943)

* test: refactored byte coder tests

* chore: changeset

---------

Co-authored-by: danielbate <--global>

* test: refactor `TupleCoder` tests (#939)

* test: refactor and increase verbosity of tuple coder tests

* chore: changeset

---------

Co-authored-by: danielbate <--global>

* test: refactor `StructCoder` tests (#940)

* test: refactor and increase verbosity of struct test

* test: fix test number inputs in struct coder test

* chore: changeset

* test: use template literals for struct name in struct coder test

Co-authored-by: Anderson Arboleya <[email protected]>

---------

Co-authored-by: danielbate <--global>
Co-authored-by: Anderson Arboleya <[email protected]>

* test: refactor `ArrayCoder` tests (#937)

* test: refactor and increase verbosity of array coder tests

* chore: changeset

* test: remove redundant describe block from array coder test

* test: add missing throw condition testcase to array coder

---------

Co-authored-by: danielbate <--global>

* test: refactor `U64Coder` tests (#941)

* test: refactor and increase verbosity of u64 coder tests

* test: remove redundant imports and comments

* chore: changeset

* chore: force rebuild

---------

Co-authored-by: danielbate <--global>

* test: refactor `VecCoder` tests (#936)

* test: refactor and increase verbosity of vec test

* chore: changeset

* test: add missing test cases for encoding non array inputs in vec coder

---------

Co-authored-by: danielbate <--global>

* test: refactor `StringCoder` tests (#972)

* test: refactor and increase verbosity of string coder tests

* test: remove redundant import

* chore: changeset

* chore: force rebuild

* test: remove commented out tests from string coder

---------

Co-authored-by: danielbate <--global>

* feat: add throw to string coder when encoding value with length mismatch

* test: add unit test for string cdoer encoder throw conditon

* chore: changeset

* chore: force rebuild

---------

Co-authored-by: danielbate <--global>
Co-authored-by: Sérgio Torres <[email protected]>
Co-authored-by: Anderson Arboleya <[email protected]>
  • Loading branch information
3 people authored May 18, 2023
1 parent ce74fa4 commit 7d9017d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-birds-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": minor
---

ABI coder will throw when encoding a string with a value legnth mismatch
17 changes: 9 additions & 8 deletions packages/abi-coder/src/coders/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ describe('StringCoder', () => {
expect(actualLength).toBe(expectedLength);
});

it('should not completely encode a string that is too big for the coder', () => {
const coderLength = 0;
const coder = new StringCoder(coderLength);
it('should throw when encoding a string that is too big', () => {
const coder = new StringCoder(0);
const invalidInput = STRING_MAX_DECODED;
const actual = coder.encode(invalidInput);

expect(actual).not.toStrictEqual(STRING_MAX_ENCODED);
expect(() => {
coder.encode(invalidInput);
}).toThrow();
});

it('should not completely encode a string that is too small for the coder', () => {
it('should throw when encoding a string that is too small', () => {
const coder = new StringCoder(1);
const invalidInput = STRING_MIN_DECODED;
const actual = coder.encode(invalidInput);

expect(actual).not.toStrictEqual(STRING_MIN_ENCODED);
expect(() => {
coder.encode(invalidInput);
}).toThrow();
});

it('should not completely decode a string that is too big for the coder', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/abi-coder/src/coders/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export default class StringCoder<TLength extends number = number> extends Coder<
}

encode(value: string): Uint8Array {
const encoded = toUtf8Bytes(value.slice(0, this.length));
if (this.length !== value.length) {
this.throwError('Value length mismatch during encode', value);
}

const encoded = toUtf8Bytes(value);
const padding = new Uint8Array(this.#paddingLength);
return concat([encoded, padding]);
}
Expand Down

0 comments on commit 7d9017d

Please sign in to comment.