Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 28, 2024
1 parent 45c117a commit a4f19a5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 27 deletions.
26 changes: 5 additions & 21 deletions src/cmd/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,21 @@ const INVALID_SUFFIX_SIZE = 65535;
const DEFAULT_PRODUCT_ID = 65535;
const DEFAULT_PRODUCT_VERSION = 65535;

const PROTECTED_MINIMUM_VERSION = '6.0.0';
const PROTECTED_MINIMUM_BOOTLOADER_VERSION = 3000;

class BinaryCommand {
async inspectApplicationBinary(file) {
async inspectBinary(file) {
await this._checkFile(file);
const extractedFiles = await this._extractApplicationFiles(file);
const parsedAppInfo = await this._parseApplicationBinary(extractedFiles.application);
const assets = extractedFiles.assets;
await this._verifyBundle(parsedAppInfo, assets);
}

async inspectBinary(file) {
await this._checkFile(file);
const extractedFiles = await this._extractFile(file);
const parsedInfo = await this._parseBinary(extractedFiles.application);
return parsedInfo;
}

async createProtectedBinary({ saveTo, file, verbose }) {
await this._checkFile(file);
const binaryModule = this.inspectBinary(file);
const extractedFile = await this._extractFile(file);
const binaryModule = await this._parseBinary(extractedFile);
this._validateProtectedBinary(binaryModule);
let resBinaryName;

Expand All @@ -83,20 +76,11 @@ class BinaryCommand {

_validateProtectedBinary(module) {
const { moduleFunction, moduleVersion, moduleIndex } = module.prefixInfo;
if (moduleFunction !== ModuleInfo.FunctionType.BOOTLOADER) {
throw new Error('Device Protection can only be enabled on bootloaders. The file provided is not a bootloader.');
}

if (moduleIndex !== 0) {
throw new Error('Device Protection can only be enabled on bootloaders with module index 0. Please use the correct bootloader file.');
}

if (moduleVersion < PROTECTED_MINIMUM_BOOTLOADER_VERSION) {
throw new Error(`Device Protection can only be enabled on bootloader for device-os version ${PROTECTED_MINIMUM_VERSION} and later. The provided file is an older version.`);
if (moduleFunction !== ModuleInfo.FunctionType.BOOTLOADER || moduleIndex !== 0 || moduleVersion < PROTECTED_MINIMUM_BOOTLOADER_VERSION) {
throw new Error('Device protection feature is not supported for this binary.');
}
}


async _checkFile(file) {
try {
await fs.access(file);
Expand Down
90 changes: 84 additions & 6 deletions src/cmd/binary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ describe('Binary Inspect', () => {
});
});

describe('_extractFiles', () => {
describe('_extractApplicationFiles', () => {
it('errors if file is not .zip or .bin', async () => {
let error;

try {
await binaryCommand._extractFiles('not-a-zip-or-bin-file');
await binaryCommand._extractApplicationFiles('not-a-zip-or-bin-file');
} catch (_error) {
error = _error;
}
Expand All @@ -73,7 +73,7 @@ describe('Binary Inspect', () => {
it('extracts a .zip file', async () => {
const zipPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'bundle.zip');

const binaryInfo = await binaryCommand._extractFiles(zipPath);
const binaryInfo = await binaryCommand._extractApplicationFiles(zipPath);

expect(binaryInfo).to.have.property('application').with.property('name', 'app.bin');
expect(binaryInfo).to.have.property('assets').with.lengthOf(3);
Expand All @@ -87,7 +87,7 @@ describe('Binary Inspect', () => {
it('extracts a .bin file', async () => {
const binPath = path.join(PATH_FIXTURES_BINARIES_DIR, 'argon_stroby.bin');

const binaryInfo = await binaryCommand._extractFiles(binPath);
const binaryInfo = await binaryCommand._extractApplicationFiles(binPath);

expect(binaryInfo).to.have.property('application').with.property('name', 'argon_stroby.bin');
expect(binaryInfo).to.have.property('assets').with.lengthOf(0);
Expand All @@ -96,7 +96,7 @@ describe('Binary Inspect', () => {
it('handles if zip file does not have a binary or assets', async () => {
const zipPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'invalid-bundle.zip');

const binaryInfo = await binaryCommand._extractFiles(zipPath);
const binaryInfo = await binaryCommand._extractApplicationFiles(zipPath);

expect(binaryInfo).to.have.property('application').with.property('name', 'app.txt');
expect(binaryInfo).to.have.property('assets').with.lengthOf(0);
Expand Down Expand Up @@ -136,13 +136,91 @@ describe('Binary Inspect', () => {
describe('_verifyBundle', () => {
it('verifies bundle with asset info', async () => {
const zipPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'bundle.zip');
const res = await binaryCommand._extractFiles(zipPath);
const res = await binaryCommand._extractApplicationFiles(zipPath);
const parsedBinaryInfo = await binaryCommand._parseApplicationBinary(res.application);

const verify = await binaryCommand._verifyBundle(parsedBinaryInfo, res.assets);

expect(verify).to.equal(true);
});
});

describe('_validateProtectedBinary', () => {
it('validates a protected binary', async () => {
const module = {
prefixInfo: {
moduleIndex: 0,
moduleFunction: 2,
moduleVersion: 3000
}
};

let error;
try {
binaryCommand._validateProtectedBinary(module);
} catch (e) {
error = e;
}

expect(error).to.equal(undefined);
});

it('errors if binary is of the wrong module index', async () => {
const module = {
prefixInfo: {
moduleIndex: 1,
moduleFunction: 2,
moduleVersion: 3000
}
};

let error;
try {
binaryCommand._validateProtectedBinary(module);
} catch (e) {
error = e;
}

expect(error.message).to.equal('Device protection feature is not supported for this binary.');
});

it('errors if binary is not a bootloader', async () => {
const module = {
prefixInfo: {
moduleIndex: 1,
moduleFunction: 0,
moduleVersion: 3000
}
};

let error;
try {
binaryCommand._validateProtectedBinary(module);
} catch (e) {
error = e;
}

expect(error.message).to.equal('Device protection feature is not supported for this binary.');
});

it('errors if binary is of an older bootloader version', async () => {
const module = {
prefixInfo: {
moduleIndex: 1,
moduleFunction: 0,
moduleVersion: 2000
}
};

let error;
try {
binaryCommand._validateProtectedBinary(module);
} catch (e) {
error = e;
}

expect(error.message).to.equal('Device protection feature is not supported for this binary.');
});
});
});

0 comments on commit a4f19a5

Please sign in to comment.