Skip to content

Commit

Permalink
Update debugger version to accommodate changes in IoT.js (#28)
Browse files Browse the repository at this point in the history
IoT.js-VSCode-DCO-1.0-Signed-off-by: Tibor Dusnoki [email protected]
  • Loading branch information
tdusnoki authored and robertsipka committed Oct 10, 2018
1 parent 3bf5f07 commit 8b1f6e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/JerryProtocolConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

// Expected JerryScript debugger protocol version.
export const JERRY_DEBUGGER_VERSION = 5;
export const JERRY_DEBUGGER_VERSION = 6;

// Packages sent from the server to the client.
export enum SERVER {
Expand Down
10 changes: 5 additions & 5 deletions src/JerryProtocolHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ export class JerryDebugProtocolHandler {

public onConfiguration(data: Uint8Array): void {
this.logPacket('Configuration');
if (data.length < 5) {
if (data.length < 8) {
this.abort('configuration message wrong size');
return;
}

this.maxMessageSize = data[1];
this.byteConfig.cpointerSize = data[2];
this.byteConfig.littleEndian = Boolean(data[3]);
this.version = data[4];
this.maxMessageSize = data[6];
this.byteConfig.cpointerSize = data[7];
this.byteConfig.littleEndian = Boolean(data[1]);
this.version = this.decodeMessage('I', data, 2)[0];

if (this.byteConfig.cpointerSize !== 2 && this.byteConfig.cpointerSize !== 4) {
this.abort('compressed pointer must be 2 or 4 bytes long');
Expand Down
34 changes: 22 additions & 12 deletions src/test/JerryProtocolHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Breakpoint } from '../JerryBreakpoints';
import { JerryDebugProtocolHandler } from '../JerryProtocolHandler';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { stringToCesu8 } from '../JerryUtils';
import { stringToCesu8, setUint32 } from '../JerryUtils';

// utility function
function encodeArray(byte: number, str: string) {
Expand All @@ -15,6 +15,16 @@ function encodeArray(byte: number, str: string) {
return array;
}

// Extends configArray with JERRY_DEBUGGER_VERSION.
//
// configArray data: [messageType, byteOrder, maxMessageSize, cpointerSize]
function createConfiguration(configArray) {
let version = new Uint8Array(4);
setUint32(Boolean(configArray[1]), version, 0, SP.JERRY_DEBUGGER_VERSION);
configArray.splice(2, 0, ...version);
return Uint8Array.from(configArray);
}

function setupHaltedProtocolHandler(isThereBreakpointHit: boolean = false) {
const debugClient = {
send: sinon.spy(),
Expand Down Expand Up @@ -47,35 +57,35 @@ suite('JerryProtocolHandler', () => {

test('aborts when message too short', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([1, 2, 3, 4]);
const array = Uint8Array.from([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 2, 3, 4]);
handler.onConfiguration(array);
assert(delegate.onError.calledOnce);
});

test('allows otherwise valid message to be too long', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([0, 200, 4, 1, SP.JERRY_DEBUGGER_VERSION, 0]);
const array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 200, 4, 0]);
handler.onConfiguration(array);
assert(delegate.onError.notCalled);
});

test('aborts when compressed pointer wrong size', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([0, 200, 6, 1, SP.JERRY_DEBUGGER_VERSION]);
const array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 200, 6]);
handler.onConfiguration(array);
assert(delegate.onError.calledOnce);
});

test('aborts when version unexpected', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([0, 200, 4, 1, 0]);
const array = Uint8Array.from([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 0, 0, 0, 0, 200, 4]);
handler.onConfiguration(array);
assert(delegate.onError.calledOnce);
});

test('succeeds when everything is normal', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([0, 200, 4, 1, SP.JERRY_DEBUGGER_VERSION]);
const array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 200, 4]);
handler.onConfiguration(array);
assert(delegate.onError.notCalled);
});
Expand Down Expand Up @@ -223,7 +233,7 @@ suite('JerryProtocolHandler', () => {
};
const handler = new JerryDebugProtocolHandler(delegate);

let array = Uint8Array.from([0, 128, 2, 1, SP.JERRY_DEBUGGER_VERSION]);
let array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 128, 2]);
handler.onConfiguration(array);
array = encodeArray(SP.SERVER.JERRY_DEBUGGER_SOURCE_CODE_END, 'code');
handler.onSourceCode(array);
Expand All @@ -250,7 +260,7 @@ suite('JerryProtocolHandler', () => {

test('calls delegate function immediately on END event', () => {
delegate.onBacktrace.resetHistory();
let array = Uint8Array.from([0, 128, 2, 1, SP.JERRY_DEBUGGER_VERSION]);
let array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 128, 2]);
handler.onConfiguration(array);
array = encodeArray(SP.SERVER.JERRY_DEBUGGER_SOURCE_CODE_END, 'code');
handler.onSourceCode(array);
Expand All @@ -275,7 +285,7 @@ suite('JerryProtocolHandler', () => {

test('calls delegate function only on END event', () => {
delegate.onBacktrace.resetHistory();
let array = Uint8Array.from([0, 128, 2, 1, SP.JERRY_DEBUGGER_VERSION]);
let array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 128, 2]);
handler.onConfiguration(array);
array = encodeArray(SP.SERVER.JERRY_DEBUGGER_SOURCE_CODE_END, 'code');
handler.onSourceCode(array);
Expand Down Expand Up @@ -361,7 +371,7 @@ suite('JerryProtocolHandler', () => {

test('aborts when unhandled message sent', () => {
delegate.onError.resetHistory();
const array = Uint8Array.from([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 200, 4, 1, 5]);
const array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 200, 4]);
handler.onMessage(array);
assert(delegate.onError.notCalled);
array[0] = 255;
Expand Down Expand Up @@ -495,7 +505,7 @@ suite('JerryProtocolHandler', () => {

test('throws on line w/o breakpoint, succeeds on line w/ breakpoint', () => {
const handler = new JerryDebugProtocolHandler({});
let array = Uint8Array.from([0, 128, 2, 1, 1]);
let array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 128, 2]);
handler.onConfiguration(array);

array = encodeArray(SP.SERVER.JERRY_DEBUGGER_SOURCE_CODE_END, 'code');
Expand Down Expand Up @@ -754,7 +764,7 @@ suite('JerryProtocolHandler', () => {
const handler = new JerryDebugProtocolHandler({});
handler.debuggerClient = debugClient as any;

let array = Uint8Array.from([0, 128, 2, 1, 1]);
let array = createConfiguration([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 1, 128, 2]);
handler.onConfiguration(array);
array = encodeArray(SP.SERVER.JERRY_DEBUGGER_SOURCE_CODE_END, 'code');
handler.onSourceCode(array);
Expand Down

0 comments on commit 8b1f6e6

Please sign in to comment.