Skip to content

Commit

Permalink
[tests] unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 18, 2024
1 parent 416de8c commit f886aa7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -73,6 +73,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
addToOutput.forEach((line) => {
this.ui.stdout.write(line);
});

return s;
}

/**
Expand Down Expand Up @@ -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}`);
Expand All @@ -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.
*
Expand Down
59 changes: 42 additions & 17 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
});
});

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit f886aa7

Please sign in to comment.