• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const cp = require('child_process');
5const fs = require('fs');
6const path = require('path');
7const util = require('util');
8
9const tests = new Array();
10
11let gid = 1;
12let uid = 1;
13
14if (!common.isWindows) {
15  gid = process.getgid();
16  uid = process.getuid();
17}
18
19tests['fs.sync.access'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
20                          'fs.accessSync("fs.txt");' +
21                          'fs.unlinkSync("fs.txt")';
22tests['fs.sync.chmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
23                         'fs.chmodSync("fs.txt",100);' +
24                         'fs.unlinkSync("fs.txt")';
25tests['fs.sync.chown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
26                         `fs.chownSync("fs.txt", ${uid}, ${gid});` +
27                         'fs.unlinkSync("fs.txt")';
28tests['fs.sync.close'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
29                         'fs.unlinkSync("fs.txt")';
30tests['fs.sync.copyfile'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
31                            'fs.copyFileSync("fs.txt","a.txt");' +
32                            'fs.unlinkSync("fs.txt")';
33tests['fs.sync.fchmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
34                          'const fd = fs.openSync("fs.txt", "r+");' +
35                          'fs.fchmodSync(fd,100);' +
36                          'fs.unlinkSync("fs.txt")';
37tests['fs.sync.fchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
38                          'const fd = fs.openSync("fs.txt", "r+");' +
39                          `fs.fchownSync(fd, ${uid}, ${gid});` +
40                          'fs.unlinkSync("fs.txt")';
41tests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
42                             'const fd = fs.openSync("fs.txt", "r+");' +
43                             'fs.fdatasyncSync(fd);' +
44                             'fs.unlinkSync("fs.txt")';
45tests['fs.sync.fstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
46                         'fs.readFileSync("fs.txt");' +
47                         'fs.unlinkSync("fs.txt")';
48tests['fs.sync.fsync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
49                         'const fd = fs.openSync("fs.txt", "r+");' +
50                         'fs.fsyncSync(fd);' +
51                         'fs.unlinkSync("fs.txt")';
52tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
53                             'const fd = fs.openSync("fs.txt", "r+");' +
54                             'fs.ftruncateSync(fd, 1);' +
55                             'fs.unlinkSync("fs.txt")';
56tests['fs.sync.futimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
57                           'const fd = fs.openSync("fs.txt", "r+");' +
58                           'fs.futimesSync(fd,1,1);' +
59                           'fs.unlinkSync("fs.txt")';
60tests['fs.sync.lchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
61                          `fs.lchownSync("fs.txt", ${uid}, ${gid});` +
62                          'fs.unlinkSync("fs.txt")';
63tests['fs.sync.link'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
64                        'fs.linkSync("fs.txt", "linkx");' +
65                        'fs.unlinkSync("linkx");' +
66                        'fs.unlinkSync("fs.txt")';
67tests['fs.sync.lstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
68                         'fs.lstatSync("fs.txt");' +
69                         'fs.unlinkSync("fs.txt")';
70tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp");' +
71                         'fs.rmdirSync("fstemp")';
72tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstest");' +
73                           'fs.rmdirSync(fp)';
74tests['fs.sync.open'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
75                        'fs.unlinkSync("fs.txt")';
76tests['fs.sync.read'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
77                        'fs.readFileSync("fs.txt");' +
78                        'fs.unlinkSync("fs.txt")';
79tests['fs.sync.readdir'] = 'fs.readdirSync("./")';
80tests['fs.sync.realpath'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
81                            'fs.linkSync("fs.txt", "linkx");' +
82                            'fs.realpathSync.native("linkx");' +
83                            'fs.unlinkSync("linkx");' +
84                            'fs.unlinkSync("fs.txt")';
85tests['fs.sync.rename'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
86                          'fs.renameSync("fs.txt","xyz.txt"); ' +
87                          'fs.unlinkSync("xyz.txt")';
88tests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp");' +
89                         'fs.rmdirSync("fstemp")';
90tests['fs.sync.stat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
91                        'fs.statSync("fs.txt");' +
92                        'fs.unlinkSync("fs.txt")';
93tests['fs.sync.unlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
94                          'fs.linkSync("fs.txt", "linkx");' +
95                          'fs.unlinkSync("linkx");' +
96                          'fs.unlinkSync("fs.txt")';
97tests['fs.sync.utimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
98                          'fs.utimesSync("fs.txt",1,1);' +
99                          'fs.unlinkSync("fs.txt")';
100tests['fs.sync.write'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
101                         'fs.unlinkSync("fs.txt")';
102
103// On windows, we need permissions to test symlink and readlink.
104// We'll only try to run these tests if we have enough privileges.
105if (common.canCreateSymLink()) {
106  tests['fs.sync.symlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
107                             'fs.symlinkSync("fs.txt", "linkx");' +
108                             'fs.unlinkSync("linkx");' +
109                             'fs.unlinkSync("fs.txt")';
110  tests['fs.sync.readlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
111                              'fs.symlinkSync("fs.txt", "linkx");' +
112                              'fs.readlinkSync("linkx");' +
113                              'fs.unlinkSync("linkx");' +
114                              'fs.unlinkSync("fs.txt")';
115}
116
117const tmpdir = require('../common/tmpdir');
118tmpdir.refresh();
119const traceFile = path.join(tmpdir.path, 'node_trace.1.log');
120
121for (const tr in tests) {
122  const proc = cp.spawnSync(process.execPath,
123                            [ '--trace-events-enabled',
124                              '--trace-event-categories', 'node.fs.sync',
125                              '-e', tests[tr] ],
126                            { cwd: tmpdir.path, encoding: 'utf8' });
127  // Some AIX versions don't support futimes or utimes, so skip.
128  if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.futimes') {
129    continue;
130  }
131  if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.utimes') {
132    continue;
133  }
134
135  // Make sure the operation is successful.
136  // Don't use assert with a custom message here. Otherwise the
137  // inspection in the message is done eagerly and wastes a lot of CPU
138  // time.
139  if (proc.status !== 0) {
140    throw new Error(`${tr}:\n${util.inspect(proc)}`);
141  }
142
143  // Confirm that trace log file is created.
144  assert(fs.existsSync(traceFile));
145  const data = fs.readFileSync(traceFile);
146  const traces = JSON.parse(data.toString()).traceEvents;
147  assert(traces.length > 0);
148
149  // C++ fs sync trace events should be generated.
150  assert(traces.some((trace) => {
151    if (trace.pid !== proc.pid)
152      return false;
153    if (trace.cat !== 'node,node.fs,node.fs.sync')
154      return false;
155    if (trace.name !== tr)
156      return false;
157    return true;
158  }));
159}
160