Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nikos/5430/validator unit test coverage #5525

Merged
merged 10 commits into from
Oct 21, 2022
Merged
4 changes: 3 additions & 1 deletion packages/web3-validator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export const transformJsonDataToAbiFormat = (
abiName = abi.name;
abiComponents = abi.components as FullValidationSchema;
// If its short form string value e.g. ['uint']
// todo abis is FullValidationSchema aka ReadOnlyArray<AbiParameter>, thus abi cannot be something else, so may next two ifs are reduntant
Copy link
Contributor

@jdevcs jdevcs Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next two if are checking internal members of abis that is not complete abi type, and also not abis param it self.

} else if (typeof abi === 'string') {
abiType = abi;

Expand Down Expand Up @@ -329,6 +330,7 @@ export const hexToNumber = (value: string): bigint | number => {
return negative ? -num : num;
}

// todo will never be here
if (num < Number.MIN_SAFE_INTEGER) {
return num;
}
Expand Down Expand Up @@ -366,7 +368,7 @@ export const padLeft = (value: ValidInputTypes, characterAmount: number, sign =
if (typeof value === 'string' && !isHexStrict(value)) {
return value.padStart(characterAmount, sign);
}

// typeof value === 'string' && isHexStrict(value) will never evaluate to false
const hex = typeof value === 'string' && isHexStrict(value) ? value : numberToHex(value);

const [prefix, hexValue] = hex.startsWith('-') ? ['-0x', hex.slice(3)] : ['0x', hex.slice(2)];
Expand Down
76 changes: 73 additions & 3 deletions packages/web3-validator/test/fixtures/abi_to_json_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { FullValidationSchema, JsonSchema, ShortValidationSchema } from '../../src/types';

export const abiToJsonSchemaCases: {
export type AbiToJsonSchemaCase = {
title: string;
abi: {
fullSchema: FullValidationSchema;
Expand All @@ -29,9 +29,10 @@ export const abiToJsonSchemaCases: {
shortSchema: JsonSchema;
data: Record<string, unknown> | Array<unknown>;
};
}[] = [
};
export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
{
title: 'single param',
title: 'single param uint',
abi: {
fullSchema: [{ name: 'a', type: 'uint' }],
shortSchema: ['uint'],
Expand All @@ -53,6 +54,75 @@ export const abiToJsonSchemaCases: {
data: [12],
},
},
{
title: 'single param address',
abi: {
fullSchema: [{ name: 'a', type: 'address' }],
shortSchema: ['address'],
data: ['0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b'],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', eth: 'address' }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', eth: 'address' }],
minItems: 1,
maxItems: 1,
},
data: ['0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b'],
},
},
{
title: 'single param bool',
abi: {
fullSchema: [{ name: 'a', type: 'bool' }],
shortSchema: ['bool'],
data: [true],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', eth: 'bool' }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', eth: 'bool' }],
minItems: 1,
maxItems: 1,
},
data: [true],
},
},
{
title: 'single param bytes',
abi: {
fullSchema: [{ name: 'a', type: 'bytes' }],
shortSchema: ['bytes'],
data: ['0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b'],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', eth: 'bytes' }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', eth: 'bytes' }],
minItems: 1,
maxItems: 1,
},
data: ['0xCB00CDE33a7a0Fba30C63745534F1f7Ae607076b'],
},
},

{
title: 'multiple params',
Expand Down
69 changes: 69 additions & 0 deletions packages/web3-validator/test/fixtures/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

export const fullErrors: any[] = [
{
instancePath: '',
schemaPath: '#/minItems',
keyword: 'minItems',
params: { limit: 1 },
message: 'must NOT have fewer than 1 items',
},
{
instancePath: '',
schemaPath: '#/minItems',
keyword: 'minItems',
params: { limit: 2 },
message: 'must NOT have fewer than 2 items',
},
{
instancePath: '',
schemaPath: '#/maxItems',
keyword: 'maxItems',
params: { limit: 1 },
message: 'must NOT have more than 1 items',
},
];

export const fullErrorsWithInstance: any[] = [
{
message: 'must pass "uint" validation',
keyword: 'eth',
params: { value: -1 },
instancePath: '/0',
schemaPath: '#/items/0/eth',
},
];

export const errorsWithInstanceNoParams: any[] = [
{
message: 'must pass "uint" validation',
keyword: 'eth',
instancePath: '/0',
schemaPath: '#/items/0/eth',
},
];

export const errorsWithInstanceNoParamsNoMessage: any[] = [
{
keyword: 'eth',
instancePath: '/0',
schemaPath: '#/items/0/eth',
},
];

export const unspecifiedErrors: any[] = [{}];
114 changes: 105 additions & 9 deletions packages/web3-validator/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Filter } from 'web3-types';
import { ValidInputTypes } from '../../src/types';

export const validUintData: any[] = [
'0x48',
Expand Down Expand Up @@ -109,16 +110,28 @@ export const invalidIntDataWithAbiType: [any, string][] = [
['-0x0dec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b', 'uint8'],
];

export const validHexStrictDataWithNumber: [string, number | bigint][] = [
['0x48', 72],
['0x123c', 4668],
[
'0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
BigInt('6297078121011128569053558207054331251192909352593326480842737114300118477835'),
],
[
'0xd115bffabbdd893a6f7cea402e7338643ced44a6',
BigInt('1193664110518272216229793131906554422260021413030'),
],
[
'0x2C941171bD2A7aEda7c2767c438DfF36EAaFdaFc',
BigInt('254497623817844434235817792799421766503337286396'),
],
['0x1', 1],
['0xcd', 205],
['-0xcd', -205],
];
export const validHexStrictData: any[] = [
'0x48',
'0x123c',
'0x0dec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
'0xd115bffabbdd893a6f7cea402e7338643ced44a6',
'0x2C941171bD2A7aEda7c2767c438DfF36EAaFdaFc',
'0x1',
'0xcd',
'-0xcd',
'-0x0dec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
...validHexStrictDataWithNumber.map(tuple => tuple[0]),
'-0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
];

export const invalidHexData: any[] = [
Expand Down Expand Up @@ -158,6 +171,31 @@ export const validHexData: any[] = [
BigInt(-255),
];

export const validStringNumbersWithHex: [string, string][] = [
['72', '0x48'],
['4668', '0x123c'],
[
'6297078121011128569053558207054331251192909352593326480842737114300118477835',
'0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
],
[
'1193664110518272216229793131906554422260021413030',
'0xd115bffabbdd893a6f7cea402e7338643ced44a6',
],
[
'254497623817844434235817792799421766503337286396',
'0x2C941171bD2A7aEda7c2767c438DfF36EAaFdaFc',
],
['1', '0x1'],
['205', '0xcd'],
['-205', '-0xcd'],
];

export const invalidStringNumbers: ValidInputTypes[] = [
new ArrayBuffer(23255),
Buffer.from('abce', 'hex'),
Buffer.from('hello', 'utf8'),
];
export const validCheckAddressCheckSumData: any[] = [
'0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d',
'0x52908400098527886E0F7030069857D2E4169EE7',
Expand Down Expand Up @@ -261,6 +299,12 @@ export const invalidInBloomData: any[] = [
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'0xhello',
],
[0, '0x6254B927ecC25DDd233aAECD5296D746B1C006B4'],
[
// mix a and A
'0xaA000000200000000010000080000000000002000000000000000000000000000000000000020200000000000000000000800001000000000000000000200000000000000000000000000008000000800000000000000000000000000000000000000000020000000000000000000800000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000080000000000000000000000100000000000000000000000002000000000001000080000000000000000000000000000000000020200010000000000000000000000000000000000000100000000000000000000000',
'0x98afe7a8d28bbc88dcf41f8e06d97c74958a47dc',
],
];

export const validUserEthereumAddressInBloomData: any[] = [
Expand All @@ -279,6 +323,11 @@ export const invalidUserEthereumAddressInBloomData: any[] = [
'0x00000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000002000000000000000000000000000000100000000000000082000000000080000000000000000000000000000000000000000000000002000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'0xH1',
],
[
// mix a and A
'0xaA000000200000000010000080000000000002000000000000000000000000000000000000020200000000000000000000800001000000000000000000200000000000000000000000000008000000800000000000000000000000000000000000000000020000000000000000000800000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000080000000000000000000000100000000000000000000000002000000000001000080000000000000000000000000000000000020200010000000000000000000000000000000000000100000000000000000000000',
'0x98afe7a8d28bbc88dcf41f8e06d97c74958a47dc',
],
];

export const validTopicData: any[] = [
Expand Down Expand Up @@ -545,3 +594,50 @@ export const invalidEthTypeData: string[] = [
'my-addresss',
'boolean',
];

export const validCodePoints: [number, number][] = [
[48, 0],
[51, 3],
[55, 7],
[57, 9],
[65, 10],
[70, 15],
[97, 10],
[100, 13],
[102, 15],
];

export const invalidCodePoints: number[] = [-100, -5, 0, 30, 58, 75, 90, 103, 200];

export const padLeftData: { padDigits: number; data: [ValidInputTypes, string][] } = {
padDigits: 64,
data: [
[
'dfd5293d8e347dfe59e90efd55b2956a1343963d',
'000000000000000000000000dfd5293d8e347dfe59e90efd55b2956a1343963d',
],
[
'-0xdfd5293d8e347dfe59e90efd55b2956a1343963d',
'-0x000000000000000000000000dfd5293d8e347dfe59e90efd55b2956a1343963d',
],
[2, '0x0000000000000000000000000000000000000000000000000000000000000002'],
],
};

export const validNotBaseTypeData: { dataType: string; data: any }[] = [
{ dataType: 'hex', data: '0x000000000000000000000000dfd5293d8e347dfe59e90efd55b2956a1343963d' },
{ dataType: 'number', data: 42 },
{ dataType: 'blockNumber', data: 42 },
{ dataType: 'blockNumberOrTag', data: 'latest' },
{
dataType: 'filter',
data: {
fromBlock: 'latest',
toBlock: 'latest',
},
},
{
dataType: 'bloom',
data: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
},
];
26 changes: 26 additions & 0 deletions packages/web3-validator/test/unit/default_validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { validator } from '../../src/default_validator';
import { Web3Validator } from '../../src/web3_validator';

describe('default web3-validator', () => {
it('should be instance', () => {
expect(validator).toBeDefined();
expect(validator).toBeInstanceOf(Web3Validator);
});
});
Loading