microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/debugger/ios/deviceRunner.test.ts
175lines · modecode
| 1 | // 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. |
| 9 | var shouldModule: any = require("should"); |
| 10 | /* tslint:enable:no-unused-variable */ |
| 11 | /* tslint:enable:no-var-requires */ |
| 12 | /* tslint:enable:no-var-keyword */ |
| 13 | |
| 14 | import {Log} from "../../../common/log/log"; |
| 15 | import net = require ("net"); |
| 16 | import Q = require ("q"); |
| 17 | import {DeviceRunner} from "../../../debugger/ios/deviceRunner"; |
| 18 | |
| 19 | interface IMockDebuggerProxy extends net.Server { |
| 20 | protocolState?: number; |
| 21 | }; |
| 22 | |
| 23 | suite("deviceRunner", function() { |
| 24 | suite("commonContext", function() { |
| 25 | test("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 |
| 27 | let runner: DeviceRunner = new DeviceRunner("."); |
| 28 | let port: number = 12345; |
| 29 | let appPath: string = "/private/var/mobile/Applications/042F57CA-9717-4655-8349-532093FFCF44/BlankCordovaApp1.app"; |
| 30 | let encodedAppPath: string = "2F707269766174652F7661722F6D6F62696C652F4170706C69636174696F6E732F30343246353743412D393731372D343635352D383334392D3533323039334646434634342F426C616E6B436F72646F7661417070312E617070"; |
| 31 | encodedAppPath.should.equal(runner.encodePath(appPath)); |
| 32 | |
| 33 | let mockDebuggerProxy: IMockDebuggerProxy = net.createServer(function (client: net.Socket): void { |
| 34 | mockDebuggerProxy.close(); |
| 35 | client.on("data", function (data: Buffer): void { |
| 36 | let dataString: string = data.toString(); |
| 37 | if (mockDebuggerProxy.protocolState % 2 === 1) { |
| 38 | // Every second message should be an acknowledgement of a send of ours |
| 39 | dataString[0].should.equal("+"); |
| 40 | mockDebuggerProxy.protocolState++; |
| 41 | dataString = dataString.substring(1); |
| 42 | if (dataString === "") { |
| 43 | return; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | dataString[0].should.equal("$"); |
| 48 | let expectedResponse: string = ""; |
| 49 | switch (mockDebuggerProxy.protocolState) { |
| 50 | case 0: |
| 51 | expectedResponse = "A" + encodedAppPath.length + ",0," + encodedAppPath; |
| 52 | let checksum: number = 0; |
| 53 | for (let i: number = 0; i < expectedResponse.length; ++i) { |
| 54 | checksum += expectedResponse.charCodeAt(i); |
| 55 | }; |
| 56 | /* tslint:disable:no-bitwise */ |
| 57 | // Some bitwise operations needed to calculate the checksum here |
| 58 | checksum = checksum & 0xFF; |
| 59 | /* tslint:enable:no-bitwise */ |
| 60 | let checkstring: string = checksum.toString(16).toUpperCase(); |
| 61 | if (checkstring.length === 1) { |
| 62 | checkstring = "0" + checkstring; |
| 63 | } |
| 64 | |
| 65 | expectedResponse = "$" + expectedResponse + "#" + checkstring; |
| 66 | dataString.should.equal(expectedResponse); |
| 67 | mockDebuggerProxy.protocolState++; |
| 68 | client.write("+"); |
| 69 | client.write("$OK#9A"); |
| 70 | break; |
| 71 | case 2: |
| 72 | expectedResponse = "$Hc0#DB"; |
| 73 | dataString.should.equal(expectedResponse); |
| 74 | mockDebuggerProxy.protocolState++; |
| 75 | client.write("+"); |
| 76 | client.write("$OK#9A"); |
| 77 | break; |
| 78 | case 4: |
| 79 | expectedResponse = "$c#63"; |
| 80 | dataString.should.equal(expectedResponse); |
| 81 | mockDebuggerProxy.protocolState++; |
| 82 | client.write("+"); |
| 83 | // Respond with empty output |
| 84 | client.write("$O#4F"); |
| 85 | client.end(); |
| 86 | break; |
| 87 | default: |
| 88 | break; |
| 89 | } |
| 90 | }); |
| 91 | }); |
| 92 | mockDebuggerProxy.protocolState = 0; |
| 93 | mockDebuggerProxy.on("error", done); |
| 94 | |
| 95 | mockDebuggerProxy.listen(port, function (): void { |
| 96 | Log.logMessage("MockDebuggerProxy listening"); |
| 97 | }); |
| 98 | |
| 99 | Q.timeout(runner.startAppViaDebugger(port, appPath, 5000), 1000).done(() => done(), done); |
| 100 | }); |
| 101 | test("should report an error if the debugger fails for some reason", function(done: MochaDone) { |
| 102 | let runner: DeviceRunner = new DeviceRunner("."); |
| 103 | let port: number = 12345; |
| 104 | let appPath: string = "/private/var/mobile/Applications/042F57CA-9717-4655-8349-532093FFCF44/BlankCordovaApp1.app"; |
| 105 | |
| 106 | let encodedAppPath: string = "2F707269766174652F7661722F6D6F62696C652F4170706C69636174696F6E732F30343246353743412D393731372D343635352D383334392D3533323039334646434634342F426C616E6B436F72646F7661417070312E617070"; |
| 107 | encodedAppPath.should.equal(runner.encodePath(appPath)); |
| 108 | |
| 109 | let mockDebuggerProxy: IMockDebuggerProxy = net.createServer(function (client: net.Socket): void { |
| 110 | mockDebuggerProxy.close(); |
| 111 | client.on("data", function (data: Buffer): void { |
| 112 | let dataString: string = data.toString(); |
| 113 | if (mockDebuggerProxy.protocolState % 2 === 1) { |
| 114 | // Every second message should be an acknowledgement of a send of ours |
| 115 | dataString[0].should.equal("+"); |
| 116 | mockDebuggerProxy.protocolState++; |
| 117 | dataString = dataString.substring(1); |
| 118 | if (dataString === "") { |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | dataString[0].should.equal("$"); |
| 124 | |
| 125 | let expectedResponse: string = ""; |
| 126 | switch (mockDebuggerProxy.protocolState) { |
| 127 | case 0: |
| 128 | expectedResponse = "A" + encodedAppPath.length + ",0," + encodedAppPath; |
| 129 | let checksum: number = 0; |
| 130 | for (let i: number = 0; i < expectedResponse.length; ++i) { |
| 131 | checksum += expectedResponse.charCodeAt(i); |
| 132 | }; |
| 133 | /* tslint:disable:no-bitwise */ |
| 134 | // Some bit operations needed to calculate checksum |
| 135 | checksum = checksum & 0xFF; |
| 136 | /* tslint:enable:no-bitwise */ |
| 137 | let checkstring: string = checksum.toString(16).toUpperCase(); |
| 138 | if (checkstring.length === 1) { |
| 139 | checkstring = "0" + checkstring; |
| 140 | } |
| 141 | |
| 142 | expectedResponse = "$" + expectedResponse + "#" + checkstring; |
| 143 | dataString.should.equal(expectedResponse); |
| 144 | mockDebuggerProxy.protocolState++; |
| 145 | client.write("+"); |
| 146 | client.write("$OK#9A"); |
| 147 | break; |
| 148 | case 2: |
| 149 | expectedResponse = "$Hc0#DB"; |
| 150 | dataString.should.equal(expectedResponse); |
| 151 | mockDebuggerProxy.protocolState++; |
| 152 | client.write("+"); |
| 153 | client.write("$E23#AA"); |
| 154 | client.end(); |
| 155 | break; |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | }); |
| 160 | }); |
| 161 | mockDebuggerProxy.protocolState = 0; |
| 162 | mockDebuggerProxy.on("error", done); |
| 163 | |
| 164 | mockDebuggerProxy.listen(port, function (): void { |
| 165 | Log.logMessage("MockDebuggerProxy listening"); |
| 166 | }); |
| 167 | |
| 168 | Q.timeout(runner.startAppViaDebugger(port, appPath, 5000), 1000).then(function (): void { |
| 169 | throw new Error("Starting the app should have failed!"); |
| 170 | }, function (err: any): void { |
| 171 | err.message.should.equal("Unable to launch application."); |
| 172 | }).done(() => done(), done); |
| 173 | }); |
| 174 | }); |
| 175 | }); |