Skip to content

Commit

Permalink
[tests] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jul 26, 2024
1 parent dc1511c commit 851d12e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Dfu {
async getProtectionState() {
try {
const res = await this._getStringDescriptor(0xfe);
const state = res.split(';').find(kv => kv.startsWith('s='))?.split('=')[1];
const state = res.split(';').find(kv => kv.startsWith('sm='))?.split('=')[1];

switch (state) {
case 'o': return { protected: false, overridden: false };
Expand Down
30 changes: 30 additions & 0 deletions src/dfu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,35 @@ describe('dfu', () => {
});

describe('getProtectionState', () => {
it ('detects an Open Device from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves("sm=o");

Check failure on line 370 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

Strings must use singlequote

Check failure on line 370 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

Strings must use singlequote

Check failure on line 370 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

Strings must use singlequote

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(false);
expect(res.overridden).to.eql(false);
});

it ('detects a Protected Device from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves("sm=p");

Check failure on line 379 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

Strings must use singlequote

Check failure on line 379 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

Strings must use singlequote

Check failure on line 379 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

Strings must use singlequote

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(true);
});

it ('detects a Protected Device in Service Mode from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves("sm=s");

Check failure on line 387 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

Strings must use singlequote

Check failure on line 387 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

Strings must use singlequote

Check failure on line 387 in src/dfu.test.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

Strings must use singlequote

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(false);
expect(res.overridden).to.eql(true);
});

it('returns that all segments are protected', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').rejects('random error');
sinon.stub(dfu, 'setAltSetting').resolves();
const internalFlashDesc = {
'name': 'Internal Flash',
Expand Down Expand Up @@ -409,11 +436,13 @@ describe('dfu', () => {

const res = await dfu.getProtectionState();

expect(dfu.setAltSetting).to.have.been.calledOnce;
expect(res.protected).to.eql(true);
});

it('returns that all segments are not protected', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').rejects('random error');
sinon.stub(dfu, 'setAltSetting').resolves();
const internalFlashDesc = {
'name': 'Internal Flash',
Expand Down Expand Up @@ -456,6 +485,7 @@ describe('dfu', () => {

const res = await dfu.getProtectionState();

expect(dfu.setAltSetting).to.have.been.calledOnce;
expect(res.protected).to.eql(false);
});
});
Expand Down

0 comments on commit 851d12e

Please sign in to comment.