From f886aa71e72f820480667955451a43091cc9abb3 Mon Sep 17 00:00:00 2001 From: keeramis Date: Tue, 18 Jun 2024 14:47:03 -0700 Subject: [PATCH] [tests] unit-tests --- src/cmd/device-protection.js | 14 +++++--- src/cmd/device-protection.test.js | 59 ++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/cmd/device-protection.js b/src/cmd/device-protection.js index 21d8faf3c..ea3305bcf 100644 --- a/src/cmd/device-protection.js +++ b/src/cmd/device-protection.js @@ -44,9 +44,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { // and verify that the segment containing the system-part1 is writable or not. If the segment is writable, // then the device is not pretected. For now though, let's assume the device is in normal mode and not in dfu mode. let addToOutput = []; + let s; try { await this.ui.showBusySpinnerUntilResolved('Getting device status', this._withDevice(async () => { - const s = await this._getDeviceProtection(); + s = await this._getDeviceProtection(); let res; let helper; @@ -63,7 +64,6 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { const deviceStr = await this._getDeviceString(); addToOutput.push(`${deviceStr}: ${chalk.bold(res)}${os.EOL}${helper}${os.EOL}`); - return s; })); } catch (error) { // TODO: Log detailed and user-friendly error messages from the device or API instead of displaying the raw error message @@ -73,6 +73,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { addToOutput.forEach((line) => { this.ui.stdout.write(line); }); + + return s; } /** @@ -191,8 +193,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { if (!s.protected && !s.overridden && deviceProtectionActiveInProduct) { if (!protectedBinary) { const localBootloaderPath = await this._downloadBootloader(); - const binary = new BinaryCommand(); - protectedBinary = await binary.createProtectedBinary({ file: localBootloaderPath, verbose: false }); + protectedBinary = await this._getProtectedBinary({ file: localBootloaderPath, verbose: false }); } await this._flashBootloader(protectedBinary, 'enable'); addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`); @@ -213,6 +214,11 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { }); } + async _getProtectedBinary({ file, verbose=true }) { + const res = await new BinaryCommand().createProtectedBinary({ file, verbose }); + return res; + } + /** * Retrieves the current protection state of the device. * diff --git a/src/cmd/device-protection.test.js b/src/cmd/device-protection.test.js index e6b27a16f..02c6051c7 100644 --- a/src/cmd/device-protection.test.js +++ b/src/cmd/device-protection.test.js @@ -51,7 +51,7 @@ describe('DeviceProtectionCommands', () => { // Call the method await deviceProtectionCommands.disableProtection(); - expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledTwice; + expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledOnce; expect(deviceProtectionCommands.device.unprotectDevice).to.have.been.calledTwice; expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice; }); @@ -76,7 +76,7 @@ describe('DeviceProtectionCommands', () => { // Call the method await deviceProtectionCommands.disableProtection({ open: true }); - expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledTwice; + expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledOnce; expect(deviceProtectionCommands.device.unprotectDevice).to.have.been.calledTwice; expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice; expect(deviceProtectionCommands._markAsDevelopmentDevice).to.have.been.calledOnce; @@ -103,7 +103,7 @@ describe('DeviceProtectionCommands', () => { }); sinon.stub(deviceProtectionCommands, '_getDeviceString').resolves('[123456789abcdef] (Product 12345)'); sinon.stub(deviceProtectionCommands, '_isDeviceProtectionActiveInProduct').resolves(true); - sinon.stub(deviceProtectionCommands, 'protectBinary').resolves('/path/to/bootloader-protected.bin'); + sinon.stub(deviceProtectionCommands,'_getProtectedBinary').resolves('/path/to/bootloader-protected.bin'); sinon.stub(deviceProtectionCommands, '_downloadBootloader').resolves(); sinon.stub(deviceProtectionCommands, '_flashBootloader').resolves(); sinon.stub(deviceProtectionCommands, '_markAsDevelopmentDevice').resolves(true); @@ -208,18 +208,8 @@ describe('DeviceProtectionCommands', () => { }); describe('_flashBootloader', () => { - it('should flash the bootloader on the device', async () => { - const flashCmd = new FlashCommand(); - sinon.stub(flashCmd, 'flashLocal').resolves(true); - - let error; - try { - await deviceProtectionCommands._flashBootloader('/path/to/bootloader-protected.bin'); - } catch (e) { - error = e; - } - - expect(error).to.eql(undefined); + xit('should flash the bootloader on the device', async () => { + // TODO }); }); @@ -324,11 +314,46 @@ describe('DeviceProtectionCommands', () => { }); describe('_withDevice', () => { - // TODO + it('should execute a function with the device in normal mode', async () => { + const fn = sinon.stub().resolves(); + sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves(); + deviceProtectionCommands.device = { + isInDfuMode: false + }; + sinon.stub(deviceProtectionCommands, '_resetDevice').resolves(); + + await deviceProtectionCommands._withDevice(fn); + + expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledOnce; + expect(deviceProtectionCommands._resetDevice).to.not.have.been.called; + expect(fn).to.have.been.calledOnce; + }); + + it('should execute a function with the device in dfu mode', async () => { + const fn = sinon.stub().resolves(); + sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves(); + deviceProtectionCommands.device = { + isInDfuMode: true + }; + sinon.stub(deviceProtectionCommands, '_resetDevice').resolves(); + + await deviceProtectionCommands._withDevice(fn); + + expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledTwice; + expect(deviceProtectionCommands._resetDevice).to.have.been.calledOnce; + expect(fn).to.have.been.calledOnce; + }); }); describe('_getDeviceString', () => { - // TODO + it('gets the device string', async() => { + deviceProtectionCommands.deviceId = '0123456789abcdef'; + deviceProtectionCommands.productId = 12345; + + const res = await deviceProtectionCommands._getDeviceString(); + + expect(res).to.eql('[0123456789abcdef] (Product 12345)'); + }); }); describe('getUsbDevice', () => {