11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.isWindows) {
61cb0ef41Sopenharmony_ci  // TODO: Similar checks on *nix-like systems (e.g using chmod or the like)
71cb0ef41Sopenharmony_ci  common.skip('test only runs on Windows');
81cb0ef41Sopenharmony_ci}
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ciconst path = require('path');
131cb0ef41Sopenharmony_ciconst cp = require('child_process');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
161cb0ef41Sopenharmony_citmpdir.refresh();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Create readOnlyMod.js and set to read only
191cb0ef41Sopenharmony_ciconst readOnlyMod = path.join(tmpdir.path, 'readOnlyMod');
201cb0ef41Sopenharmony_ciconst readOnlyModRelative = path.relative(__dirname, readOnlyMod);
211cb0ef41Sopenharmony_ciconst readOnlyModFullPath = `${readOnlyMod}.js`;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cifs.writeFileSync(readOnlyModFullPath, 'module.exports = 42;');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Removed any inherited ACEs, and any explicitly granted ACEs for the
261cb0ef41Sopenharmony_ci// current user
271cb0ef41Sopenharmony_cicp.execSync(
281cb0ef41Sopenharmony_ci  `icacls.exe "${readOnlyModFullPath}" /inheritance:r /remove "%USERNAME%"`);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// Grant the current user read & execute only
311cb0ef41Sopenharmony_cicp.execSync(`icacls.exe "${readOnlyModFullPath}" /grant "%USERNAME%":RX`);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cilet except = null;
341cb0ef41Sopenharmony_citry {
351cb0ef41Sopenharmony_ci  // Attempt to load the module. Will fail if write access is required
361cb0ef41Sopenharmony_ci  require(readOnlyModRelative);
371cb0ef41Sopenharmony_ci} catch (err) {
381cb0ef41Sopenharmony_ci  except = err;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci// Remove the explicitly granted rights, and re-enable inheritance
421cb0ef41Sopenharmony_cicp.execSync(
431cb0ef41Sopenharmony_ci  `icacls.exe "${readOnlyModFullPath}" /remove "%USERNAME%" /inheritance:e`);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// Delete the test module (note: tmpdir should get cleaned anyway)
461cb0ef41Sopenharmony_cifs.unlinkSync(readOnlyModFullPath);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciassert.ifError(except);
49