microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/test/debugger/ios/deviceRunner.test.ts

182lines · modeblame

bdad2966Joshua Skelton10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4/* tslint:disable:no-var-keyword */
5/* tslint:disable:no-var-requires */
6/* tslint:disable:no-unused-variable */
7// var require needed for should module to work correctly
8// Note not import: We don't want to refer to shouldModule, but we need the require to occur since it modifies the prototype of Object.
9var shouldModule: any = require("should");
10/* tslint:enable:no-unused-variable */
11/* tslint:enable:no-var-requires */
12/* tslint:enable:no-var-keyword */
13
14import {Log} from "../../../common/log/log";
15import net = require ("net");
16import Q = require ("q");
17import {DeviceRunner} from "../../../debugger/ios/deviceRunner";
18
19interface IMockDebuggerProxy extends net.Server {
20protocolState?: number;
21};
22
23suite("deviceRunner", function() {
24suite("commonContext", function() {
25test("should complete the startup sequence when the debugger is well behaved", function(done: MochaDone) {
26// Check that when the debugger behaves nicely, we do as well
27let runner: DeviceRunner = new DeviceRunner(".");
28let port: number = 12345;
29let appPath: string = "/private/var/mobile/Applications/042F57CA-9717-4655-8349-532093FFCF44/BlankCordovaApp1.app";
30let encodedAppPath: string = "2F707269766174652F7661722F6D6F62696C652F4170706C69636174696F6E732F30343246353743412D393731372D343635352D383334392D3533323039334646434634342F426C616E6B436F72646F7661417070312E617070";
31encodedAppPath.should.equal(runner.encodePath(appPath));
32
33let mockDebuggerProxy: IMockDebuggerProxy = net.createServer(function (client: net.Socket): void {
34mockDebuggerProxy.close();
35client.on("data", function (data: Buffer): void {
36let dataString: string = data.toString();
37if (mockDebuggerProxy.protocolState % 2 === 1) {
38// Every second message should be an acknowledgement of a send of ours
39dataString[0].should.equal("+");
40mockDebuggerProxy.protocolState++;
41dataString = dataString.substring(1);
42if (dataString === "") {
43return;
44}
45}
46
47dataString[0].should.equal("$");
48let expectedResponse: string = "";
49switch (mockDebuggerProxy.protocolState) {
50case 0:
51expectedResponse = "A" + encodedAppPath.length + ",0," + encodedAppPath;
52let checksum: number = 0;
53for (let i: number = 0; i < expectedResponse.length; ++i) {
54checksum += expectedResponse.charCodeAt(i);
55};
56/* tslint:disable:no-bitwise */
57// Some bitwise operations needed to calculate the checksum here
58checksum = checksum & 0xFF;
59/* tslint:enable:no-bitwise */
60let checkstring: string = checksum.toString(16).toUpperCase();
61if (checkstring.length === 1) {
62checkstring = "0" + checkstring;
63}
64
65expectedResponse = "$" + expectedResponse + "#" + checkstring;
66dataString.should.equal(expectedResponse);
67mockDebuggerProxy.protocolState++;
68client.write("+");
69client.write("$OK#9A");
70break;
71case 2:
72expectedResponse = "$Hc0#DB";
73dataString.should.equal(expectedResponse);
74mockDebuggerProxy.protocolState++;
75client.write("+");
76client.write("$OK#9A");
77break;
78case 4:
79expectedResponse = "$c#63";
80dataString.should.equal(expectedResponse);
81mockDebuggerProxy.protocolState++;
82client.write("+");
83// Respond with empty output
84client.write("$O#4F");
85client.end();
7b22c5e3Daniel Lebu10 years ago86break;
bdad2966Joshua Skelton10 years ago87default:
88break;
89}
90});
91});
92mockDebuggerProxy.protocolState = 0;
93mockDebuggerProxy.on("error", done);
94
95mockDebuggerProxy.listen(port, function (): void {
96Log.logMessage("MockDebuggerProxy listening");
97});
98
99Q.timeout(runner.startAppViaDebugger(port, appPath, 5000), 1000).done(() => done(), done);
100});
101test("should report an error if the debugger fails for some reason", function(done: MochaDone) {
102let runner: DeviceRunner = new DeviceRunner(".");
103let port: number = 12345;
104let appPath: string = "/private/var/mobile/Applications/042F57CA-9717-4655-8349-532093FFCF44/BlankCordovaApp1.app";
105
106let encodedAppPath: string = "2F707269766174652F7661722F6D6F62696C652F4170706C69636174696F6E732F30343246353743412D393731372D343635352D383334392D3533323039334646434634342F426C616E6B436F72646F7661417070312E617070";
107encodedAppPath.should.equal(runner.encodePath(appPath));
108
109let mockDebuggerProxy: IMockDebuggerProxy = net.createServer(function (client: net.Socket): void {
110mockDebuggerProxy.close();
111client.on("data", function (data: Buffer): void {
112let dataString: string = data.toString();
113if (mockDebuggerProxy.protocolState % 2 === 1) {
114// Every second message should be an acknowledgement of a send of ours
115dataString[0].should.equal("+");
116mockDebuggerProxy.protocolState++;
117dataString = dataString.substring(1);
118if (dataString === "") {
119return;
120}
121}
122
123dataString[0].should.equal("$");
124
125let expectedResponse: string = "";
126switch (mockDebuggerProxy.protocolState) {
127case 0:
128expectedResponse = "A" + encodedAppPath.length + ",0," + encodedAppPath;
129let checksum: number = 0;
130for (let i: number = 0; i < expectedResponse.length; ++i) {
131checksum += expectedResponse.charCodeAt(i);
132};
133/* tslint:disable:no-bitwise */
134// Some bit operations needed to calculate checksum
135checksum = checksum & 0xFF;
136/* tslint:enable:no-bitwise */
137let checkstring: string = checksum.toString(16).toUpperCase();
138if (checkstring.length === 1) {
139checkstring = "0" + checkstring;
140}
141
142expectedResponse = "$" + expectedResponse + "#" + checkstring;
143dataString.should.equal(expectedResponse);
144mockDebuggerProxy.protocolState++;
145client.write("+");
146client.write("$OK#9A");
147break;
148case 2:
149expectedResponse = "$Hc0#DB";
150dataString.should.equal(expectedResponse);
151mockDebuggerProxy.protocolState++;
152client.write("+");
153client.write("$OK#9A");
154break;
155case 4:
156expectedResponse = "$c#63";
157dataString.should.equal(expectedResponse);
158mockDebuggerProxy.protocolState++;
159client.write("+");
160client.write("$E23#AA"); // Report an error
161client.end();
7b22c5e3Daniel Lebu10 years ago162break;
bdad2966Joshua Skelton10 years ago163default:
164break;
165}
166});
167});
168mockDebuggerProxy.protocolState = 0;
169mockDebuggerProxy.on("error", done);
170
171mockDebuggerProxy.listen(port, function (): void {
172Log.logMessage("MockDebuggerProxy listening");
173});
174
175Q.timeout(runner.startAppViaDebugger(port, appPath, 5000), 1000).then(function (): void {
176throw new Error("Starting the app should have failed!");
177}, function (err: any): void {
178err.message.should.equal("Unable to launch application.");
179}).done(() => done(), done);
180});
181});
182});