Skip to content

Commit a145237

Browse files
committed
test: Write unit tests for isSafeDynamicKey validation
1 parent a2a561a commit a145237

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

packages/address-book-controller/src/AddressBookController.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ describe('AddressBookController', () => {
294294
).toBe(true);
295295
});
296296

297+
it('should return false to indicate an address book entry has NOT been deleted due to unsafe input', () => {
298+
const controller = new AddressBookController();
299+
// @ts-expect-error Suppressing error to test runtime behavior
300+
expect(controller.delete('__proto__', '0x01')).toBe(false);
301+
expect(controller.delete(toHex(1), 'constructor')).toBe(false);
302+
});
303+
297304
it('should return false to indicate an address book entry has NOT been deleted', () => {
298305
const controller = new AddressBookController();
299306
controller.set('0x32Be343B94f860124dC4fEe278FDCBD38C102D88', '0x00');

packages/ens-controller/src/EnsController.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ describe('EnsController', () => {
359359
expect(controller.state.ensEntries['0x1']).toBeUndefined();
360360
});
361361

362+
it('should return false if an ENS entry was NOT deleted due to unsafe input', () => {
363+
const messenger = getMessenger();
364+
const controller = new EnsController({
365+
messenger,
366+
});
367+
// @ts-expect-error Suppressing error to test runtime behavior
368+
expect(controller.delete('__proto__', 'bar')).toBe(false);
369+
expect(controller.delete(toHex(2), 'constructor')).toBe(false);
370+
});
371+
362372
it('should return false if an ENS entry was NOT deleted', () => {
363373
const messenger = getMessenger();
364374
const controller = new EnsController({

packages/name-controller/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 99.15,
20+
branches: 100,
2121
functions: 100,
22-
lines: 99.72,
23-
statements: 99.72,
22+
lines: 100,
23+
statements: 100,
2424
},
2525
},
2626
});

packages/name-controller/src/NameController.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,64 @@ describe('NameController', () => {
458458
});
459459
});
460460

461+
it('does not update if passed unsafe input', () => {
462+
const provider1 = createMockProvider(1);
463+
464+
const controller = new NameController({
465+
...CONTROLLER_ARGS_MOCK,
466+
providers: [provider1],
467+
state: {
468+
names: {
469+
[NameType.ETHEREUM_ADDRESS]: {
470+
[VALUE_MOCK]: {
471+
[CHAIN_ID_MOCK]: {
472+
name: null,
473+
sourceId: null,
474+
origin: null,
475+
proposedNames: {
476+
[SOURCE_ID_MOCK]: {
477+
proposedNames: [PROPOSED_NAME_MOCK, PROPOSED_NAME_2_MOCK],
478+
lastRequestTime: null,
479+
updateDelay: null,
480+
},
481+
},
482+
},
483+
},
484+
},
485+
},
486+
},
487+
});
488+
489+
controller.setName({
490+
value: '__proto__',
491+
type: NameType.ETHEREUM_ADDRESS,
492+
name: NAME_MOCK,
493+
sourceId: `${SOURCE_ID_MOCK}1`,
494+
variation: CHAIN_ID_MOCK,
495+
});
496+
497+
expect(controller.state.names).toStrictEqual<
498+
NameControllerState['names']
499+
>({
500+
[NameType.ETHEREUM_ADDRESS]: {
501+
[VALUE_MOCK]: {
502+
[CHAIN_ID_MOCK]: {
503+
name: null,
504+
sourceId: null,
505+
origin: null,
506+
proposedNames: {
507+
[SOURCE_ID_MOCK]: {
508+
proposedNames: [PROPOSED_NAME_MOCK, PROPOSED_NAME_2_MOCK],
509+
lastRequestTime: null,
510+
updateDelay: null,
511+
},
512+
},
513+
},
514+
},
515+
},
516+
});
517+
});
518+
461519
it('does not throw if variation is fallback and type is Ethereum address', () => {
462520
const controller = new NameController(CONTROLLER_ARGS_MOCK);
463521

0 commit comments

Comments
 (0)