11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../../common');
31cb0ef41Sopenharmony_ciif (common.isWindows && (process.env.PROCESSOR_ARCHITEW6432 !== undefined))
41cb0ef41Sopenharmony_ci  common.skip('doesn\'t work on WOW64');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst fs = require('fs');
71cb0ef41Sopenharmony_ciconst path = require('path');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { fork } = require('child_process');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst tmpdir = require('../../common/tmpdir');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Make a path that is more than 260 chars long.
141cb0ef41Sopenharmony_ci// Any given folder cannot have a name longer than 260 characters,
151cb0ef41Sopenharmony_ci// so create 10 nested folders each with 30 character long names.
161cb0ef41Sopenharmony_cilet addonDestinationDir = path.resolve(tmpdir.path);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifor (let i = 0; i < 10; i++) {
191cb0ef41Sopenharmony_ci  addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst addonPath = path.join(__dirname,
231cb0ef41Sopenharmony_ci                            'build',
241cb0ef41Sopenharmony_ci                            common.buildType,
251cb0ef41Sopenharmony_ci                            'binding.node');
261cb0ef41Sopenharmony_ciconst addonDestinationPath = path.join(addonDestinationDir, 'binding.node');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// Loading an addon keeps the file open until the process terminates. Load
291cb0ef41Sopenharmony_ci// the addon in a child process so that when the parent terminates the file
301cb0ef41Sopenharmony_ci// is already closed and the tmpdir can be cleaned up.
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Child
331cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
341cb0ef41Sopenharmony_ci  // Attempt to load at long path destination
351cb0ef41Sopenharmony_ci  const addon = require(addonDestinationPath);
361cb0ef41Sopenharmony_ci  assert.notStrictEqual(addon, null);
371cb0ef41Sopenharmony_ci  assert.strictEqual(addon.hello(), 'world');
381cb0ef41Sopenharmony_ci  return;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci// Parent
421cb0ef41Sopenharmony_citmpdir.refresh();
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// Copy binary to long path destination
451cb0ef41Sopenharmony_cifs.mkdirSync(addonDestinationDir, { recursive: true });
461cb0ef41Sopenharmony_ciconst contents = fs.readFileSync(addonPath);
471cb0ef41Sopenharmony_cifs.writeFileSync(addonDestinationPath, contents);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// Run test
501cb0ef41Sopenharmony_ciconst child = fork(__filename, ['child'], { stdio: 'inherit' });
511cb0ef41Sopenharmony_cichild.on('exit', common.mustCall((code) => {
521cb0ef41Sopenharmony_ci  assert.strictEqual(code, 0);
531cb0ef41Sopenharmony_ci}));
54