Skip to content

Commit d2179bd

Browse files
thefourtheyeFishrock123
authored andcommitted
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: #7648 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d565183 commit d2179bd

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/parallel/test-fs-truncate.js

+40
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,43 @@ function testFtruncate(cb) {
106106
});
107107
});
108108
}
109+
110+
111+
// Make sure if the size of the file is smaller than the length then it is
112+
// filled with zeroes.
113+
114+
{
115+
const file1 = path.resolve(tmp, 'truncate-file-1.txt');
116+
fs.writeFileSync(file1, 'Hi');
117+
fs.truncateSync(file1, 4);
118+
assert(fs.readFileSync(file1).equals(Buffer.from('Hi\u0000\u0000')));
119+
}
120+
121+
{
122+
const file2 = path.resolve(tmp, 'truncate-file-2.txt');
123+
fs.writeFileSync(file2, 'Hi');
124+
const fd = fs.openSync(file2, 'r+');
125+
process.on('exit', () => fs.closeSync(fd));
126+
fs.ftruncateSync(fd, 4);
127+
assert(fs.readFileSync(file2).equals(Buffer.from('Hi\u0000\u0000')));
128+
}
129+
130+
{
131+
const file3 = path.resolve(tmp, 'truncate-file-3.txt');
132+
fs.writeFileSync(file3, 'Hi');
133+
fs.truncate(file3, 4, common.mustCall(function(err) {
134+
assert.ifError(err);
135+
assert(fs.readFileSync(file3).equals(Buffer.from('Hi\u0000\u0000')));
136+
}));
137+
}
138+
139+
{
140+
const file4 = path.resolve(tmp, 'truncate-file-4.txt');
141+
fs.writeFileSync(file4, 'Hi');
142+
const fd = fs.openSync(file4, 'r+');
143+
process.on('exit', () => fs.closeSync(fd));
144+
fs.ftruncate(fd, 4, common.mustCall(function(err) {
145+
assert.ifError(err);
146+
assert(fs.readFileSync(file4).equals(Buffer.from('Hi\u0000\u0000')));
147+
}));
148+
}

0 commit comments

Comments
 (0)