mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
test: add URL tests to fs-read in pm
PR-URL: https://github.com/nodejs/node/pull/51213 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
1 parent
a71210ba60
commit
335ecda433
1 file changed
+81
-6
Vendored
+81
-6
@@ -7,6 +7,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const blockedFile = process.env.BLOCKEDFILE;
|
||||
const blockedFileURL = new URL('file://' + process.env.BLOCKEDFILE);
|
||||
const blockedFolder = process.env.BLOCKEDFOLDER;
|
||||
const allowedFolder = process.env.ALLOWEDFOLDER;
|
||||
const regularFile = __filename;
|
||||
@@ -21,15 +22,12 @@ const regularFile = __filename;
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.readFile(path.join(blockedFolder, 'anyfile'), () => {});
|
||||
fs.readFile(blockedFileURL, () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')),
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
|
||||
// doesNotThrow
|
||||
fs.readFile(regularFile, () => {});
|
||||
}
|
||||
|
||||
// fs.createReadStream
|
||||
@@ -44,6 +42,16 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
})).then(common.mustCall());
|
||||
assert.rejects(() => {
|
||||
return new Promise((_resolve, reject) => {
|
||||
const stream = fs.createReadStream(blockedFileURL);
|
||||
stream.on('error', reject);
|
||||
});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
})).then(common.mustCall());
|
||||
|
||||
assert.rejects(() => {
|
||||
return new Promise((_resolve, reject) => {
|
||||
@@ -66,6 +74,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.stat(blockedFileURL, () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.stat(path.join(blockedFolder, 'anyfile'), () => {});
|
||||
}, common.expectsError({
|
||||
@@ -89,6 +104,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.access(blockedFileURL, fs.constants.R_OK, () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.access(path.join(blockedFolder, 'anyfile'), fs.constants.R_OK, () => {});
|
||||
}, common.expectsError({
|
||||
@@ -112,6 +134,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.copyFile(blockedFileURL, path.join(blockedFolder, 'any-other-file'), () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.copyFile(blockedFile, path.join(__dirname, 'any-other-file'), () => {});
|
||||
}, common.expectsError({
|
||||
@@ -131,6 +160,14 @@ const regularFile = __filename;
|
||||
// cpSync calls statSync before reading blockedFile
|
||||
resource: path.toNamespacedPath(blockedFolder),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.cpSync(blockedFileURL, path.join(blockedFolder, 'any-other-file'));
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
// cpSync calls statSync before reading blockedFile
|
||||
resource: path.toNamespacedPath(blockedFolder),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.cpSync(blockedFile, path.join(__dirname, 'any-other-file'));
|
||||
}, common.expectsError({
|
||||
@@ -149,6 +186,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.open(blockedFileURL, 'r', () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.open(path.join(blockedFolder, 'anyfile'), 'r', () => {});
|
||||
}, common.expectsError({
|
||||
@@ -237,6 +281,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.watchFile(blockedFileURL, common.mustNotCall());
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
}
|
||||
|
||||
// fs.rename
|
||||
@@ -248,6 +299,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.rename(blockedFileURL, 'newfile', () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
}
|
||||
|
||||
// fs.openAsBlob
|
||||
@@ -259,6 +317,13 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
assert.throws(() => {
|
||||
fs.openAsBlob(blockedFileURL);
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
}
|
||||
|
||||
// fs.exists
|
||||
@@ -267,6 +332,9 @@ const regularFile = __filename;
|
||||
fs.exists(blockedFile, (exists) => {
|
||||
assert.equal(exists, false);
|
||||
});
|
||||
fs.exists(blockedFileURL, (exists) => {
|
||||
assert.equal(exists, false);
|
||||
});
|
||||
}
|
||||
|
||||
// fs.statfs
|
||||
@@ -278,4 +346,11 @@ const regularFile = __filename;
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
}
|
||||
assert.throws(() => {
|
||||
fs.statfs(blockedFileURL, () => {});
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ACCESS_DENIED',
|
||||
permission: 'FileSystemRead',
|
||||
resource: path.toNamespacedPath(blockedFile),
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user