Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Release 21.0.x validate symbol #667

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ export class SerialBuffer { // tslint:disable-line max-classes-per-file

/** Append a `symbol` */
public pushSymbol({ name, precision }: { name: string, precision: number }) {
if (!/^[A-Z]{1,7}$/.test(name)) {
throw new Error('Expected symbol to be A-Z and between one and seven characters');
}
const a = [precision & 0xff];
a.push(...this.textEncoder.encode(name));
while (a.length < 8) {
Expand Down
74 changes: 74 additions & 0 deletions src/tests/eosjs-serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ec } from 'elliptic';

import { createInitialTypes, Type, SerialBuffer } from '../eosjs-serialize';
import { TextEncoder, TextDecoder } from 'text-encoding';

describe('Serialize', () => {
let types: Map<string, Type>;
Expand All @@ -13,6 +14,79 @@ describe('Serialize', () => {
expect(types).toBeTruthy();
});

describe('pushAsset', () => {
let serialBuffer: SerialBuffer;
const genericValidSymbolCharacter = 'A';
const invalidSymbolErrorMessage = 'Expected symbol to be A-Z and between one and seven characters';

beforeEach(() => {
serialBuffer = new SerialBuffer({
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder()
});
});

const expectSuccessForICharactersSymbol = (i: number) => {
const symbol = genericValidSymbolCharacter.repeat(i);
const asset = `10.000 ${symbol}`;

serialBuffer.pushAsset(asset);

expect(serialBuffer.length).not.toBe(0);
};

const expectExceptionThrown = (asset: string) => {
let exceptionCaught = false;

try {
serialBuffer.pushAsset(asset);
} catch (e) {
expect(e.message).toBe(invalidSymbolErrorMessage);
exceptionCaught = true;
}

expect(exceptionCaught).toBeTruthy();
};

for (let i = 1; i <= 7; i++) {
it(`should be able to push asset with valid symbol of ${i} character(s)`, () => {
expectSuccessForICharactersSymbol(i);
});
}

it('should be able to push asset with valid EOS symbol "10.000 EOS"', () => {
const asset = '10.000 EOS';

serialBuffer.pushAsset(asset);

expect(serialBuffer.length).not.toBe(0);
});

it('should not be able to push no symbol "10.000 "', () => {
const asset = '10.000 ';

expectExceptionThrown(asset);
});

it('should not be able to push symbol with 8 or more characters "10.000 AAAAAAAA"', () => {
const asset = '10.000 AAAAAAAA';

expectExceptionThrown(asset);
});

it('should not be able to push invalid lowercase symbol "10.000 eos"', () => {
const asset = '10.000 eos';

expectExceptionThrown(asset);
});

it('should not be able to push two symbols "10.000 EOS blah"', () => {
const asset = '10.000 EOS blah';

expectExceptionThrown(asset);
});
});

describe('bool', () => {
let boolType: Type;
let mockedBuffer: SerialBuffer;
Expand Down