11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
81cb0ef41Sopenharmony_ciconst os = require('os');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst ip = pickIPv4Address();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (!ip)
131cb0ef41Sopenharmony_ci  common.skip('No IP address found');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction checkIpAddress(ip, response) {
161cb0ef41Sopenharmony_ci  const res = response[0];
171cb0ef41Sopenharmony_ci  const wsUrl = res.webSocketDebuggerUrl;
181cb0ef41Sopenharmony_ci  assert.ok(wsUrl);
191cb0ef41Sopenharmony_ci  const match = wsUrl.match(/^ws:\/\/(.*):\d+\/(.*)/);
201cb0ef41Sopenharmony_ci  assert.strictEqual(ip, match[1]);
211cb0ef41Sopenharmony_ci  assert.strictEqual(res.id, match[2]);
221cb0ef41Sopenharmony_ci  assert.strictEqual(ip, res.devtoolsFrontendUrl.match(/.*ws=(.*):\d+/)[1]);
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction pickIPv4Address() {
261cb0ef41Sopenharmony_ci  for (const i of [].concat(...Object.values(os.networkInterfaces()))) {
271cb0ef41Sopenharmony_ci    if (i.family === 'IPv4' && i.address !== '127.0.0.1')
281cb0ef41Sopenharmony_ci      return i.address;
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciasync function test() {
331cb0ef41Sopenharmony_ci  const instance = new NodeInstance('--inspect-brk=0.0.0.0:0');
341cb0ef41Sopenharmony_ci  try {
351cb0ef41Sopenharmony_ci    checkIpAddress(ip, await instance.httpGet(ip, '/json/list'));
361cb0ef41Sopenharmony_ci  } catch (error) {
371cb0ef41Sopenharmony_ci    if (error.code === 'EHOSTUNREACH') {
381cb0ef41Sopenharmony_ci      common.printSkipMessage('Unable to connect to self');
391cb0ef41Sopenharmony_ci    } else {
401cb0ef41Sopenharmony_ci      throw error;
411cb0ef41Sopenharmony_ci    }
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  instance.kill();
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_citest().then(common.mustCall());
47