summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-truncate.js
diff options
context:
space:
mode:
authorSakthipriyan Vairamani <thechargingvolcano@gmail.com>2015-08-23 01:33:43 +0530
committerSakthipriyan Vairamani <thechargingvolcano@gmail.com>2016-07-22 03:58:33 +0530
commitc86c1eeab56df8a627d3d8da27008221ee295d33 (patch)
treee0a89469bd155e1ef853d2bd945f2ace2fd3bf38 /test/parallel/test-fs-truncate.js
parent9359de9dd2eae06ed5afcb75dad9bd4c466eb69d (diff)
downloadnode-new-c86c1eeab56df8a627d3d8da27008221ee295d33.tar.gz
fs: validate args of truncate functions in js
This patch 1. moves the basic validation of arguments to `truncate` family of functions to the JavaScript layer from the C++ layer. 2. makes sure that the File Descriptors are validated strictly. PR-URL: https://github.com/nodejs/node/pull/2498 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'test/parallel/test-fs-truncate.js')
-rw-r--r--test/parallel/test-fs-truncate.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js
index ab0148a246..be986937b3 100644
--- a/test/parallel/test-fs-truncate.js
+++ b/test/parallel/test-fs-truncate.js
@@ -24,6 +24,55 @@ fs.truncateSync(filename);
stat = fs.statSync(filename);
assert.equal(stat.size, 0);
+// path must be a string
+assert.throws(function() {
+ fs.truncateSync({});
+}, /path must be a string/);
+
+assert.throws(function() {
+ fs.truncateSync([]);
+}, /path must be a string/);
+
+// Even invalid file descriptors are not allowed
+assert.throws(function() {
+ fs.truncateSync(-1);
+}, /path must be a string/);
+
+assert.throws(function() {
+ fs.truncateSync(NaN);
+}, /path must be a string/);
+
+assert.throws(function() {
+ fs.truncateSync(Infinity);
+}, /path must be a string/);
+
+// Invalid lengths will also fail
+assert.throws(function() {
+ fs.truncateSync('', '');
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.truncateSync('', -1);
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.truncateSync('', NaN);
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.truncateSync('', Infinity);
+}, /length must be a positive integer/);
+
+// null is a special case which will also be treated as zero length
+fs.writeFileSync(filename, data);
+
+stat = fs.statSync(filename);
+assert.equal(stat.size, 1024 * 16);
+
+fs.truncateSync(filename, null);
+stat = fs.statSync(filename);
+assert.equal(stat.size, 0);
+
// ftruncateSync
fs.writeFileSync(filename, data);
var fd = fs.openSync(filename, 'r+');
@@ -41,6 +90,51 @@ assert.equal(stat.size, 0);
fs.closeSync(fd);
+// file descriptor must be a unsigned 32-bit integer
+assert.throws(function() {
+ fs.ftruncateSync({});
+}, /file descriptor must be a unsigned 32-bit integer/);
+
+// Even invalid file descriptors are not allowed
+assert.throws(function() {
+ fs.ftruncateSync(-1);
+}, /file descriptor must be a unsigned 32-bit integer/);
+
+assert.throws(function() {
+ fs.ftruncateSync(NaN);
+}, /file descriptor must be a unsigned 32-bit integer/);
+
+assert.throws(function() {
+ fs.ftruncateSync(Infinity);
+}, /file descriptor must be a unsigned 32-bit integer/);
+
+// Invalid lengths will also fail
+assert.throws(function() {
+ fs.ftruncateSync(1, '');
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.ftruncateSync(1, -1);
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.ftruncateSync(1, NaN);
+}, /length must be a positive integer/);
+
+assert.throws(function() {
+ fs.ftruncateSync(1, Infinity);
+}, /length must be a positive integer/);
+
+// null is a special case which will also be treated as zero length
+fs.writeFileSync(filename, data);
+fd = fs.openSync(filename, 'r+');
+
+fs.ftruncateSync(fd, null);
+stat = fs.statSync(filename);
+assert.equal(stat.size, 0);
+
+fs.closeSync(fd);
+
// async tests
testTruncate(common.mustCall(function(er) {
if (er) throw er;