11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst cp = require('child_process'); 51cb0ef41Sopenharmony_ciconst fs = require('fs'); 61cb0ef41Sopenharmony_ciconst path = require('path'); 71cb0ef41Sopenharmony_ciconst util = require('util'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst tests = Object.create(null); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cilet gid = 1; 121cb0ef41Sopenharmony_cilet uid = 1; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciif (!common.isWindows) { 151cb0ef41Sopenharmony_ci gid = process.getgid(); 161cb0ef41Sopenharmony_ci uid = process.getuid(); 171cb0ef41Sopenharmony_ci} 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_citests['fs.sync.access'] = 'fs.writeFileSync("fs0.txt", "123", "utf8");' + 201cb0ef41Sopenharmony_ci 'fs.accessSync("fs0.txt");' + 211cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs0.txt")'; 221cb0ef41Sopenharmony_citests['fs.sync.chmod'] = 'fs.writeFileSync("fs1.txt", "123", "utf8");' + 231cb0ef41Sopenharmony_ci 'fs.chmodSync("fs1.txt",100);' + 241cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs1.txt")'; 251cb0ef41Sopenharmony_citests['fs.sync.chown'] = 'fs.writeFileSync("fs2.txt", "123", "utf8");' + 261cb0ef41Sopenharmony_ci `fs.chownSync("fs2.txt", ${uid}, ${gid});` + 271cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs2.txt")'; 281cb0ef41Sopenharmony_citests['fs.sync.close'] = 'fs.writeFileSync("fs3.txt", "123", "utf8");' + 291cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs3.txt")'; 301cb0ef41Sopenharmony_citests['fs.sync.copyfile'] = 'fs.writeFileSync("fs4.txt", "123", "utf8");' + 311cb0ef41Sopenharmony_ci 'fs.copyFileSync("fs4.txt","a.txt");' + 321cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs4.txt")'; 331cb0ef41Sopenharmony_citests['fs.sync.fchmod'] = 'fs.writeFileSync("fs5.txt", "123", "utf8");' + 341cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs5.txt", "r+");' + 351cb0ef41Sopenharmony_ci 'fs.fchmodSync(fd,100);' + 361cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs5.txt")'; 371cb0ef41Sopenharmony_citests['fs.sync.fchown'] = 'fs.writeFileSync("fs6.txt", "123", "utf8");' + 381cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs6.txt", "r+");' + 391cb0ef41Sopenharmony_ci `fs.fchownSync(fd, ${uid}, ${gid});` + 401cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs6.txt")'; 411cb0ef41Sopenharmony_citests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs7.txt", "123", "utf8");' + 421cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs7.txt", "r+");' + 431cb0ef41Sopenharmony_ci 'fs.fdatasyncSync(fd);' + 441cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs7.txt")'; 451cb0ef41Sopenharmony_citests['fs.sync.fstat'] = 'fs.writeFileSync("fs8.txt", "123", "utf8");' + 461cb0ef41Sopenharmony_ci 'fs.readFileSync("fs8.txt");' + 471cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs8.txt")'; 481cb0ef41Sopenharmony_citests['fs.sync.fsync'] = 'fs.writeFileSync("fs9.txt", "123", "utf8");' + 491cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs9.txt", "r+");' + 501cb0ef41Sopenharmony_ci 'fs.fsyncSync(fd);' + 511cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs9.txt")'; 521cb0ef41Sopenharmony_citests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs10.txt", "123", "utf8");' + 531cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs10.txt", "r+");' + 541cb0ef41Sopenharmony_ci 'fs.ftruncateSync(fd, 1);' + 551cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs10.txt")'; 561cb0ef41Sopenharmony_citests['fs.sync.futimes'] = 'fs.writeFileSync("fs11.txt", "123", "utf8");' + 571cb0ef41Sopenharmony_ci 'const fd = fs.openSync("fs11.txt", "r+");' + 581cb0ef41Sopenharmony_ci 'fs.futimesSync(fd,1,1);' + 591cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs11.txt")'; 601cb0ef41Sopenharmony_citests['fs.sync.lchown'] = 'fs.writeFileSync("fs12.txt", "123", "utf8");' + 611cb0ef41Sopenharmony_ci `fs.lchownSync("fs12.txt", ${uid}, ${gid});` + 621cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs12.txt")'; 631cb0ef41Sopenharmony_citests['fs.sync.link'] = 'fs.writeFileSync("fs13.txt", "123", "utf8");' + 641cb0ef41Sopenharmony_ci 'fs.linkSync("fs13.txt", "fs14.txt");' + 651cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs13.txt");' + 661cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs14.txt")'; 671cb0ef41Sopenharmony_citests['fs.sync.lstat'] = 'fs.writeFileSync("fs15.txt", "123", "utf8");' + 681cb0ef41Sopenharmony_ci 'fs.lstatSync("fs15.txt");' + 691cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs15.txt")'; 701cb0ef41Sopenharmony_citests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp0");' + 711cb0ef41Sopenharmony_ci 'fs.rmdirSync("fstemp0")'; 721cb0ef41Sopenharmony_citests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstemp1");' + 731cb0ef41Sopenharmony_ci 'fs.rmdirSync(fp)'; 741cb0ef41Sopenharmony_citests['fs.sync.open'] = 'fs.writeFileSync("fs16.txt", "123", "utf8");' + 751cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs16.txt")'; 761cb0ef41Sopenharmony_citests['fs.sync.read'] = 'fs.writeFileSync("fs17.txt", "123", "utf8");' + 771cb0ef41Sopenharmony_ci 'fs.readFileSync("fs17.txt");' + 781cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs17.txt")'; 791cb0ef41Sopenharmony_citests['fs.sync.readdir'] = 'fs.readdirSync("./")'; 801cb0ef41Sopenharmony_citests['fs.sync.realpath'] = 'fs.writeFileSync("fs18.txt", "123", "utf8");' + 811cb0ef41Sopenharmony_ci 'fs.linkSync("fs18.txt", "fs19.txt");' + 821cb0ef41Sopenharmony_ci 'fs.realpathSync.native("fs19.txt");' + 831cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs18.txt");' + 841cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs19.txt")'; 851cb0ef41Sopenharmony_citests['fs.sync.rename'] = 'fs.writeFileSync("fs20.txt", "123", "utf8");' + 861cb0ef41Sopenharmony_ci 'fs.renameSync("fs20.txt","fs21.txt"); ' + 871cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs21.txt")'; 881cb0ef41Sopenharmony_citests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp2");' + 891cb0ef41Sopenharmony_ci 'fs.rmdirSync("fstemp2")'; 901cb0ef41Sopenharmony_citests['fs.sync.stat'] = 'fs.writeFileSync("fs22.txt", "123", "utf8");' + 911cb0ef41Sopenharmony_ci 'fs.statSync("fs22.txt");' + 921cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs22.txt")'; 931cb0ef41Sopenharmony_citests['fs.sync.unlink'] = 'fs.writeFileSync("fs23.txt", "123", "utf8");' + 941cb0ef41Sopenharmony_ci 'fs.linkSync("fs23.txt", "fs24.txt");' + 951cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs23.txt");' + 961cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs24.txt")'; 971cb0ef41Sopenharmony_citests['fs.sync.utimes'] = 'fs.writeFileSync("fs25.txt", "123", "utf8");' + 981cb0ef41Sopenharmony_ci 'fs.utimesSync("fs25.txt",1,1);' + 991cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs25.txt")'; 1001cb0ef41Sopenharmony_citests['fs.sync.write'] = 'fs.writeFileSync("fs26.txt", "123", "utf8");' + 1011cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs26.txt")'; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci// On windows, we need permissions to test symlink and readlink. 1041cb0ef41Sopenharmony_ci// We'll only try to run these tests if we have enough privileges. 1051cb0ef41Sopenharmony_ciif (common.canCreateSymLink()) { 1061cb0ef41Sopenharmony_ci tests['fs.sync.symlink'] = 'fs.writeFileSync("fs27.txt", "123", "utf8");' + 1071cb0ef41Sopenharmony_ci 'fs.symlinkSync("fs27.txt", "fs28.txt");' + 1081cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs27.txt");' + 1091cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs28.txt")'; 1101cb0ef41Sopenharmony_ci tests['fs.sync.readlink'] = 'fs.writeFileSync("fs29.txt", "123", "utf8");' + 1111cb0ef41Sopenharmony_ci 'fs.symlinkSync("fs29.txt", "fs30.txt");' + 1121cb0ef41Sopenharmony_ci 'fs.readlinkSync("fs30.txt");' + 1131cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs29.txt");' + 1141cb0ef41Sopenharmony_ci 'fs.unlinkSync("fs30.txt")'; 1151cb0ef41Sopenharmony_ci} 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 1181cb0ef41Sopenharmony_citmpdir.refresh(); 1191cb0ef41Sopenharmony_ciconst traceFile = path.join(tmpdir.path, 'node_trace.1.log'); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_cifor (const tr in tests) { 1221cb0ef41Sopenharmony_ci const proc = cp.spawnSync(process.execPath, 1231cb0ef41Sopenharmony_ci [ '--trace-events-enabled', 1241cb0ef41Sopenharmony_ci '--trace-event-categories', 'node.fs.sync', 1251cb0ef41Sopenharmony_ci '-e', tests[tr] ], 1261cb0ef41Sopenharmony_ci { cwd: tmpdir.path, encoding: 'utf8' }); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // Make sure the operation is successful. 1291cb0ef41Sopenharmony_ci // Don't use assert with a custom message here. Otherwise the 1301cb0ef41Sopenharmony_ci // inspection in the message is done eagerly and wastes a lot of CPU 1311cb0ef41Sopenharmony_ci // time. 1321cb0ef41Sopenharmony_ci if (proc.status !== 0) { 1331cb0ef41Sopenharmony_ci throw new Error(`${tr}:\n${util.inspect(proc)}`); 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci // Confirm that trace log file is created. 1371cb0ef41Sopenharmony_ci assert(fs.existsSync(traceFile)); 1381cb0ef41Sopenharmony_ci const data = fs.readFileSync(traceFile); 1391cb0ef41Sopenharmony_ci const traces = JSON.parse(data.toString()).traceEvents; 1401cb0ef41Sopenharmony_ci assert(traces.length > 0); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci // C++ fs sync trace events should be generated. 1431cb0ef41Sopenharmony_ci assert(traces.some((trace) => { 1441cb0ef41Sopenharmony_ci if (trace.pid !== proc.pid) 1451cb0ef41Sopenharmony_ci return false; 1461cb0ef41Sopenharmony_ci if (trace.cat !== 'node,node.fs,node.fs.sync') 1471cb0ef41Sopenharmony_ci return false; 1481cb0ef41Sopenharmony_ci if (trace.name !== tr) 1491cb0ef41Sopenharmony_ci return false; 1501cb0ef41Sopenharmony_ci return true; 1511cb0ef41Sopenharmony_ci })); 1521cb0ef41Sopenharmony_ci} 153