Skip to content

Commit

Permalink
fix: retrieve an appropriate unix socket path (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwakizaka authored Jan 4, 2023
1 parent 537f779 commit de11bfc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 27 deletions.
48 changes: 21 additions & 27 deletions lib/simulator-xcode-9.3.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import SimulatorXcode9 from './simulator-xcode-9';
import { exec } from 'teen_process';
import log from './logger';


// https://regex101.com/r/MEL55t/1
const WEBINSPECTOR_SOCKET_REGEXP = /\s+(\S+com\.apple\.webinspectord_sim\.socket)/;

class SimulatorXcode93 extends SimulatorXcode9 {
constructor (udid, xcodeVersion) {
super(udid, xcodeVersion);
Expand All @@ -20,32 +18,28 @@ class SimulatorXcode93 extends SimulatorXcode9 {
return this.webInspectorSocket;
}

// lsof -aUc launchd_sim
// gives a set of records like:
// launchd_s 69760 isaac 3u unix 0x57aa4fceea3937f3 0t0 /private/tmp/com.apple.CoreSimulator.SimDevice.D7082A5C-34B5-475C-994E-A21534423B9E/syslogsock
// launchd_s 69760 isaac 5u unix 0x57aa4fceea395f03 0t0 /private/tmp/com.apple.launchd.2B2u8CkN8S/Listeners
// launchd_s 69760 isaac 6u unix 0x57aa4fceea39372b 0t0 ->0x57aa4fceea3937f3
// launchd_s 69760 isaac 8u unix 0x57aa4fceea39598b 0t0 /private/tmp/com.apple.launchd.2j5k1TMh6i/com.apple.webinspectord_sim.socket
// launchd_s 69760 isaac 9u unix 0x57aa4fceea394c43 0t0 /private/tmp/com.apple.launchd.4zm9JO9KEs/com.apple.testmanagerd.unix-domain.socket
// launchd_s 69760 isaac 10u unix 0x57aa4fceea395f03 0t0 /private/tmp/com.apple.launchd.2B2u8CkN8S/Listeners
// launchd_s 69760 isaac 11u unix 0x57aa4fceea39598b 0t0 /private/tmp/com.apple.launchd.2j5k1TMh6i/com.apple.webinspectord_sim.socket
// launchd_s 69760 isaac 12u unix 0x57aa4fceea394c43 0t0 /private/tmp/com.apple.launchd.4zm9JO9KEs/com.apple.testmanagerd.unix-domain.socket
// these _appear_ to always be grouped together (so, the records for the particular sim are all in a group, before the next sim, etc.)
// so starting from the correct UDID, we ought to be able to pull the next record with `com.apple.webinspectord_sim.socket` to get the correct socket
let {stdout} = await exec('lsof', ['-aUc', 'launchd_sim']);
for (let record of stdout.split('com.apple.CoreSimulator.SimDevice.')) {
if (!record.includes(this.udid)) {
continue;
}
const match = WEBINSPECTOR_SOCKET_REGEXP.exec(record);
if (!match) {
return null;
}
this.webInspectorSocket = match[1];
return this.webInspectorSocket;
// lsof -aUc launchd_sim gives a set of records like
// https://github.com/appium/appium-ios-simulator/commit/c00901a9ddea178c5581a7a57d96d8cee3f17c59#diff-2be09dd2ea01cfd6bbbd73e10bc468da782a297365eec706999fc3709c01478dR102
// these _appear_ to always be grouped together by PID for each simulator.
// Therefore, by obtaining simulator PID with an expected simulator UDID,
// we can get the correct `com.apple.webinspectord_sim.socket`
// without depending on the order of `lsof -aUc launchd_sim` result.
const {stdout} = await exec('lsof', ['-aUc', 'launchd_sim']);
const udidPattern = `([0-9]{1,5}).+${this.udid}`;
const udidMatch = stdout.match(new RegExp(udidPattern));
if (!udidMatch) {
log.debug(`Failed to get Web Inspector socket. lsof result: ${stdout}`);
return null;
}

return null;
const pidPattern = `${udidMatch[1]}.+\\s+(\\S+com\\.apple\\.webinspectord_sim\\.socket)`;
const pidMatch = stdout.match(new RegExp(pidPattern));
if (!pidMatch) {
log.debug(`Failed to get Web Inspector socket. lsof result: ${stdout}`);
return null;
}
this.webInspectorSocket = pidMatch[1];
return this.webInspectorSocket;
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/unit/simulator-specs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// transpile:mocha

import { getSimulator } from '../../lib/simulator';
import * as teenProcess from 'teen_process';
import Simctl from 'node-simctl';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
Expand Down Expand Up @@ -96,4 +97,55 @@ describe('simulator', function () {
stats[1].name.should.equal('Resizable iPad');
});
});

describe('getWebInspectorSocket', function () {
const stdout = `COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
launchd_s 81243 mwakizaka 3u unix 0x9461828ef425ac31 0t0 /private/tmp/com.apple.launchd.ULf9wKNtd5/com.apple.webinspectord_sim.socket
launchd_s 81243 mwakizaka 4u unix 0x9461828ef425bc99 0t0 /tmp/com.apple.CoreSimulator.SimDevice.0829568F-7479-4ADE-9E51-B208DC99C107/syslogsock
launchd_s 81243 mwakizaka 6u unix 0x9461828ef27d4c39 0t0 ->0x9461828ef27d4b71
launchd_s 81243 mwakizaka 7u unix 0x9461828ef425dd69 0t0 ->0x9461828ef27d5021
launchd_s 81243 mwakizaka 8u unix 0x9461828ef425b4c9 0t0 /private/tmp/com.apple.launchd.88z8qTMoJA/Listeners
launchd_s 81243 mwakizaka 9u unix 0x9461828ef425be29 0t0 /private/tmp/com.apple.launchd.rbqFyGyXrT/com.apple.testmanagerd.unix-domain.socket
launchd_s 81243 mwakizaka 10u unix 0x9461828ef425b4c9 0t0 /private/tmp/com.apple.launchd.88z8qTMoJA/Listeners
launchd_s 81243 mwakizaka 11u unix 0x9461828ef425c081 0t0 /private/tmp/com.apple.launchd.zHidszZQUZ/com.apple.testmanagerd.remote-automation.unix-domain.socket
launchd_s 81243 mwakizaka 12u unix 0x9461828ef425def9 0t0 ->0x9461828ef425de31
launchd_s 35621 mwakizaka 4u unix 0x7b7dbedd6d63253f 0t0 /tmp/com.apple.CoreSimulator.SimDevice.B5048708-566E-45D5-9885-C878EF7D6D13/syslogsock
launchd_s 35621 mwakizaka 5u unix 0x7b7dbedd6d62f727 0t0 /private/tmp/com.apple.launchd.zuM1XDJcwr/com.apple.webinspectord_sim.socket
launchd_s 35621 mwakizaka 9u unix 0x7b7dbedd6d632607 0t0 /private/tmp/com.apple.launchd.KbYwOrA36E/Listeners
launchd_s 35621 mwakizaka 10u unix 0x7b7dbedd6d62f727 0t0 /private/tmp/com.apple.launchd.zuM1XDJcwr/com.apple.webinspectord_sim.socket
launchd_s 35621 mwakizaka 11u unix 0x7b7dbedd6d62e6bf 0t0 /private/tmp/com.apple.launchd.7wTVfXC9QX/com.apple.testmanagerd.unix-domain.socket
launchd_s 35621 mwakizaka 12u unix 0x7b7dbedd6d632607 0t0 /private/tmp/com.apple.launchd.KbYwOrA36E/Listeners
launchd_s 35621 mwakizaka 13u unix 0x7b7dbedd6d62e84f 0t0 /private/tmp/com.apple.launchd.g7KQlSsvXT/com.apple.testmanagerd.remote-automation.unix-domain.socket
launchd_s 35621 mwakizaka 15u unix 0x7b7dbedd6d62e6bf 0t0 /private/tmp/com.apple.launchd.7wTVfXC9QX/com.apple.testmanagerd.unix-domain.socket
launchd_s 35621 mwakizaka 16u unix 0x7b7dbedd6d62e84f 0t0 /private/tmp/com.apple.launchd.g7KQlSsvXT/com.apple.testmanagerd.remote-automation.unix-domain.socket`;

beforeEach(function () {
sinon.stub(teenProcess, 'exec').callsFake(() => ({ stdout }));
const xcodeVersion = {major: 9, versionString: '9.3.0'};
xcodeMock.expects('getVersion').atLeast(1).returns(B.resolve(xcodeVersion));
});
afterEach(function () {
teenProcess.exec.restore();
});

const testParams = [
{udid: '0829568F-7479-4ADE-9E51-B208DC99C107', line: 'first', expected: '/private/tmp/com.apple.launchd.ULf9wKNtd5/com.apple.webinspectord_sim.socket'},
{udid: 'B5048708-566E-45D5-9885-C878EF7D6D13', line: 'second', expected: '/private/tmp/com.apple.launchd.zuM1XDJcwr/com.apple.webinspectord_sim.socket'},
];

testParams.forEach(({udid, line, expected}) => {
it(`should find a Web Inspector socket when it appears at the ${line} line of grouped records`, async function () {
const sim = await getSimulator(udid);
const webInspectorSocket = await sim.getWebInspectorSocket();
webInspectorSocket.should.equal(expected);
});
});

it(`should assign webInspectorSocket value only once`, async function () {
const sim = await getSimulator(testParams[0].udid);
await sim.getWebInspectorSocket();
await sim.getWebInspectorSocket();
teenProcess.exec.callCount.should.equal(1);
});
});
});

0 comments on commit de11bfc

Please sign in to comment.