11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --expose-internals
41cb0ef41Sopenharmony_ci// This verifies that
51cb0ef41Sopenharmony_ci// 1. We do not leak internal modules unless the --require-internals option
61cb0ef41Sopenharmony_ci//    is on.
71cb0ef41Sopenharmony_ci// 2. We do not accidentally leak any modules to the public global scope.
81cb0ef41Sopenharmony_ci// 3. Deprecated modules are properly deprecated.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst common = require('../common');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (!common.isMainThread) {
131cb0ef41Sopenharmony_ci  common.skip('Cannot test the existence of --expose-internals from worker');
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst assert = require('assert');
171cb0ef41Sopenharmony_ciconst fork = require('child_process').fork;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst expectedPublicModules = new Set([
201cb0ef41Sopenharmony_ci  '_http_agent',
211cb0ef41Sopenharmony_ci  '_http_client',
221cb0ef41Sopenharmony_ci  '_http_common',
231cb0ef41Sopenharmony_ci  '_http_incoming',
241cb0ef41Sopenharmony_ci  '_http_outgoing',
251cb0ef41Sopenharmony_ci  '_http_server',
261cb0ef41Sopenharmony_ci  '_stream_duplex',
271cb0ef41Sopenharmony_ci  '_stream_passthrough',
281cb0ef41Sopenharmony_ci  '_stream_readable',
291cb0ef41Sopenharmony_ci  '_stream_transform',
301cb0ef41Sopenharmony_ci  '_stream_wrap',
311cb0ef41Sopenharmony_ci  '_stream_writable',
321cb0ef41Sopenharmony_ci  '_tls_common',
331cb0ef41Sopenharmony_ci  '_tls_wrap',
341cb0ef41Sopenharmony_ci  'assert',
351cb0ef41Sopenharmony_ci  'async_hooks',
361cb0ef41Sopenharmony_ci  'buffer',
371cb0ef41Sopenharmony_ci  'child_process',
381cb0ef41Sopenharmony_ci  'cluster',
391cb0ef41Sopenharmony_ci  'console',
401cb0ef41Sopenharmony_ci  'constants',
411cb0ef41Sopenharmony_ci  'crypto',
421cb0ef41Sopenharmony_ci  'dgram',
431cb0ef41Sopenharmony_ci  'dns',
441cb0ef41Sopenharmony_ci  'domain',
451cb0ef41Sopenharmony_ci  'events',
461cb0ef41Sopenharmony_ci  'fs',
471cb0ef41Sopenharmony_ci  'http',
481cb0ef41Sopenharmony_ci  'http2',
491cb0ef41Sopenharmony_ci  'https',
501cb0ef41Sopenharmony_ci  'inspector',
511cb0ef41Sopenharmony_ci  'module',
521cb0ef41Sopenharmony_ci  'net',
531cb0ef41Sopenharmony_ci  'os',
541cb0ef41Sopenharmony_ci  'path',
551cb0ef41Sopenharmony_ci  'perf_hooks',
561cb0ef41Sopenharmony_ci  'process',
571cb0ef41Sopenharmony_ci  'punycode',
581cb0ef41Sopenharmony_ci  'querystring',
591cb0ef41Sopenharmony_ci  'readline',
601cb0ef41Sopenharmony_ci  'repl',
611cb0ef41Sopenharmony_ci  'stream',
621cb0ef41Sopenharmony_ci  'string_decoder',
631cb0ef41Sopenharmony_ci  'sys',
641cb0ef41Sopenharmony_ci  'timers',
651cb0ef41Sopenharmony_ci  'tls',
661cb0ef41Sopenharmony_ci  'trace_events',
671cb0ef41Sopenharmony_ci  'tty',
681cb0ef41Sopenharmony_ci  'url',
691cb0ef41Sopenharmony_ci  'util',
701cb0ef41Sopenharmony_ci  'v8',
711cb0ef41Sopenharmony_ci  'vm',
721cb0ef41Sopenharmony_ci  'worker_threads',
731cb0ef41Sopenharmony_ci  'zlib',
741cb0ef41Sopenharmony_ci]);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
771cb0ef41Sopenharmony_ci  assert(!process.execArgv.includes('--expose-internals'));
781cb0ef41Sopenharmony_ci  process.once('message', ({ allBuiltins }) => {
791cb0ef41Sopenharmony_ci    const publicModules = new Set();
801cb0ef41Sopenharmony_ci    for (const id of allBuiltins) {
811cb0ef41Sopenharmony_ci      if (id.startsWith('internal/')) {
821cb0ef41Sopenharmony_ci        assert.throws(() => {
831cb0ef41Sopenharmony_ci          require(id);
841cb0ef41Sopenharmony_ci        }, {
851cb0ef41Sopenharmony_ci          code: 'MODULE_NOT_FOUND',
861cb0ef41Sopenharmony_ci          message: `Cannot find module '${id}'`
871cb0ef41Sopenharmony_ci        });
881cb0ef41Sopenharmony_ci      } else {
891cb0ef41Sopenharmony_ci        require(id);
901cb0ef41Sopenharmony_ci        publicModules.add(id);
911cb0ef41Sopenharmony_ci      }
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci    assert(allBuiltins.length > publicModules.size);
941cb0ef41Sopenharmony_ci    // Make sure all the public modules are available through
951cb0ef41Sopenharmony_ci    // require('module').builtinModules
961cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
971cb0ef41Sopenharmony_ci      publicModules,
981cb0ef41Sopenharmony_ci      new Set(require('module').builtinModules)
991cb0ef41Sopenharmony_ci    );
1001cb0ef41Sopenharmony_ci    assert.deepStrictEqual(publicModules, expectedPublicModules);
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci} else {
1031cb0ef41Sopenharmony_ci  assert(process.execArgv.includes('--expose-internals'));
1041cb0ef41Sopenharmony_ci  const child = fork(__filename, ['child'], {
1051cb0ef41Sopenharmony_ci    execArgv: []
1061cb0ef41Sopenharmony_ci  });
1071cb0ef41Sopenharmony_ci  const { builtinModules } = require('module');
1081cb0ef41Sopenharmony_ci  // When --expose-internals is on, require('module').builtinModules
1091cb0ef41Sopenharmony_ci  // contains internal modules.
1101cb0ef41Sopenharmony_ci  const message = { allBuiltins: builtinModules };
1111cb0ef41Sopenharmony_ci  child.send(message);
1121cb0ef41Sopenharmony_ci}
113