11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (common.isWindows) {
71cb0ef41Sopenharmony_ci  assert.strictEqual(process.geteuid, undefined);
81cb0ef41Sopenharmony_ci  assert.strictEqual(process.getegid, undefined);
91cb0ef41Sopenharmony_ci  assert.strictEqual(process.seteuid, undefined);
101cb0ef41Sopenharmony_ci  assert.strictEqual(process.setegid, undefined);
111cb0ef41Sopenharmony_ci  return;
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciif (!common.isMainThread)
151cb0ef41Sopenharmony_ci  return;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciassert.throws(() => {
181cb0ef41Sopenharmony_ci  process.seteuid({});
191cb0ef41Sopenharmony_ci}, {
201cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
211cb0ef41Sopenharmony_ci  message: 'The "id" argument must be one of type number or string. ' +
221cb0ef41Sopenharmony_ci    'Received an instance of Object'
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciassert.throws(() => {
261cb0ef41Sopenharmony_ci  process.seteuid('fhqwhgadshgnsdhjsdbkhsdabkfabkveyb');
271cb0ef41Sopenharmony_ci}, {
281cb0ef41Sopenharmony_ci  code: 'ERR_UNKNOWN_CREDENTIAL',
291cb0ef41Sopenharmony_ci  message: 'User identifier does not exist: fhqwhgadshgnsdhjsdbkhsdabkfabkveyb'
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// IBMi does not support below operations.
331cb0ef41Sopenharmony_ciif (common.isIBMi)
341cb0ef41Sopenharmony_ci  return;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// If we're not running as super user...
371cb0ef41Sopenharmony_ciif (process.getuid() !== 0) {
381cb0ef41Sopenharmony_ci  // Should not throw.
391cb0ef41Sopenharmony_ci  process.getegid();
401cb0ef41Sopenharmony_ci  process.geteuid();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  assert.throws(() => {
431cb0ef41Sopenharmony_ci    process.setegid('nobody');
441cb0ef41Sopenharmony_ci  }, /(?:EPERM, .+|Group identifier does not exist: nobody)$/);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  assert.throws(() => {
471cb0ef41Sopenharmony_ci    process.seteuid('nobody');
481cb0ef41Sopenharmony_ci  }, /^Error: (?:EPERM, .+|User identifier does not exist: nobody)$/);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  return;
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// If we are running as super user...
541cb0ef41Sopenharmony_ciconst oldgid = process.getegid();
551cb0ef41Sopenharmony_citry {
561cb0ef41Sopenharmony_ci  process.setegid('nobody');
571cb0ef41Sopenharmony_ci} catch (err) {
581cb0ef41Sopenharmony_ci  if (err.message !== 'Group identifier does not exist: nobody') {
591cb0ef41Sopenharmony_ci    throw err;
601cb0ef41Sopenharmony_ci  } else {
611cb0ef41Sopenharmony_ci    process.setegid('nogroup');
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ciconst newgid = process.getegid();
651cb0ef41Sopenharmony_ciassert.notStrictEqual(newgid, oldgid);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciconst olduid = process.geteuid();
681cb0ef41Sopenharmony_ciprocess.seteuid('nobody');
691cb0ef41Sopenharmony_ciconst newuid = process.geteuid();
701cb0ef41Sopenharmony_ciassert.notStrictEqual(newuid, olduid);
71