summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-truncate.js
diff options
context:
space:
mode:
authorSakthipriyan Vairamani <thechargingvolcano@gmail.com>2016-08-27 15:51:52 +0530
committerSakthipriyan Vairamani <thechargingvolcano@gmail.com>2016-08-27 19:51:16 +0530
commit82c7a9c40c89086e6e8e8d1cac2d502cd7b1d8fd (patch)
tree5e153d1b267825b68f118459a54e1dad44ec6c65 /test/parallel/test-fs-truncate.js
parentdf4880de557fabafb625745c6ea75d3b755595d2 (diff)
downloadandroid-node-v8-82c7a9c40c89086e6e8e8d1cac2d502cd7b1d8fd.tar.gz
android-node-v8-82c7a9c40c89086e6e8e8d1cac2d502cd7b1d8fd.tar.bz2
android-node-v8-82c7a9c40c89086e6e8e8d1cac2d502cd7b1d8fd.zip
test: make sure over truncation of file zero fills
If the file is over truncated, then the rest of the file should be filled with null bytes. These tests ensure the same. PR-URL: https://github.com/nodejs/node/pull/7648 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-fs-truncate.js')
-rw-r--r--test/parallel/test-fs-truncate.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js
index ab0148a246..442038eeb5 100644
--- a/test/parallel/test-fs-truncate.js
+++ b/test/parallel/test-fs-truncate.js
@@ -106,3 +106,43 @@ function testFtruncate(cb) {
});
});
}
+
+
+// Make sure if the size of the file is smaller than the length then it is
+// filled with zeroes.
+
+{
+ const file1 = path.resolve(tmp, 'truncate-file-1.txt');
+ fs.writeFileSync(file1, 'Hi');
+ fs.truncateSync(file1, 4);
+ assert(fs.readFileSync(file1).equals(Buffer.from('Hi\u0000\u0000')));
+}
+
+{
+ const file2 = path.resolve(tmp, 'truncate-file-2.txt');
+ fs.writeFileSync(file2, 'Hi');
+ const fd = fs.openSync(file2, 'r+');
+ process.on('exit', () => fs.closeSync(fd));
+ fs.ftruncateSync(fd, 4);
+ assert(fs.readFileSync(file2).equals(Buffer.from('Hi\u0000\u0000')));
+}
+
+{
+ const file3 = path.resolve(tmp, 'truncate-file-3.txt');
+ fs.writeFileSync(file3, 'Hi');
+ fs.truncate(file3, 4, common.mustCall(function(err) {
+ assert.ifError(err);
+ assert(fs.readFileSync(file3).equals(Buffer.from('Hi\u0000\u0000')));
+ }));
+}
+
+{
+ const file4 = path.resolve(tmp, 'truncate-file-4.txt');
+ fs.writeFileSync(file4, 'Hi');
+ const fd = fs.openSync(file4, 'r+');
+ process.on('exit', () => fs.closeSync(fd));
+ fs.ftruncate(fd, 4, common.mustCall(function(err) {
+ assert.ifError(err);
+ assert(fs.readFileSync(file4).equals(Buffer.from('Hi\u0000\u0000')));
+ }));
+}