From d38ae70ab21bd3cbea807c433237add5a08f6a9a Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 22 Jan 2021 11:58:28 -0600 Subject: [PATCH] proper error handling for bad checksum algorithm --- src/sagas/flash-firmware.test.ts | 62 ++++++++++++++++++++++++++++++++ src/sagas/flash-firmware.ts | 9 ++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/sagas/flash-firmware.test.ts b/src/sagas/flash-firmware.test.ts index dfc5eace..89959dfe 100644 --- a/src/sagas/flash-firmware.test.ts +++ b/src/sagas/flash-firmware.test.ts @@ -521,6 +521,68 @@ describe('flashFirmware', () => { await saga.end(); }); + + test('bad checksum algorithm', async () => { + const metadata: FirmwareMetadata = { + 'metadata-version': '1.0.0', + 'device-id': HubType.MoveHub, + // @ts-expect-error: testing bad value + 'checksum-type': 'bad', + 'firmware-version': '1.2.3', + 'max-firmware-size': 1024, + 'mpy-abi-version': 5, + 'mpy-cross-options': ['-mno-unicode'], + 'user-mpy-offset': 100, + }; + + const zip = new JSZip(); + zip.file('firmware-base.bin', new Uint8Array(64)); + zip.file('firmware.metadata.json', JSON.stringify(metadata)); + zip.file('main.py', 'print("test")'); + zip.file('ReadMe_OSS.txt', 'test'); + + const saga = new AsyncSaga(flashFirmware, { + nextMessageId: createCountFunc(), + }); + + saga.setState({ settings: { flashCurrentProgram: false } }); + + // saga is triggered by this action + + saga.put( + flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })), + ); + + // the first step is to compile main.py to .mpy + + let action = await saga.take(); + expect(action).toMatchInlineSnapshot(` + Object { + "options": Array [ + "-mno-unicode", + ], + "script": "print(\\"test\\")", + "type": "mpy.action.compile", + } + `); + + const mpySize = 20; + const mpyBinaryData = new Uint8Array(mpySize); + saga.put(didCompile(mpyBinaryData)); + + // should fail due to bad checksum algorithm + + action = await saga.take(); + expect(action).toEqual( + didFailToStart( + FailToStartReasonType.BadMetadata, + 'checksum-type', + MetadataProblem.NotSupported, + ), + ); + + await saga.end(); + }); }); test('user supplied main.py', async () => { diff --git a/src/sagas/flash-firmware.ts b/src/sagas/flash-firmware.ts index 7b25a65e..a81c6f0b 100644 --- a/src/sagas/flash-firmware.ts +++ b/src/sagas/flash-firmware.ts @@ -183,7 +183,14 @@ function* loadFirmware( firmware.set(mpy.data, metadata['user-mpy-offset'] + 4); if (metadata['checksum-type'] !== 'sum') { - throw Error(`Unknown checksum type "${metadata['checksum-type']}"`); + yield* put( + didFailToStart( + FailToStartReasonType.BadMetadata, + 'checksum-type', + MetadataProblem.NotSupported, + ), + ); + yield* cancel(); } firmwareView.setUint32(