11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci// Test fs.readFile using a file descriptor. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ciconst fs = require('fs'); 91cb0ef41Sopenharmony_ciconst fn = fixtures.path('empty.txt'); 101cb0ef41Sopenharmony_ciconst join = require('path').join; 111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 121cb0ef41Sopenharmony_citmpdir.refresh(); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_citempFd(function(fd, close) { 151cb0ef41Sopenharmony_ci fs.readFile(fd, function(err, data) { 161cb0ef41Sopenharmony_ci assert.ok(data); 171cb0ef41Sopenharmony_ci close(); 181cb0ef41Sopenharmony_ci }); 191cb0ef41Sopenharmony_ci}); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_citempFd(function(fd, close) { 221cb0ef41Sopenharmony_ci fs.readFile(fd, 'utf8', function(err, data) { 231cb0ef41Sopenharmony_ci assert.strictEqual(data, ''); 241cb0ef41Sopenharmony_ci close(); 251cb0ef41Sopenharmony_ci }); 261cb0ef41Sopenharmony_ci}); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_citempFdSync(function(fd) { 291cb0ef41Sopenharmony_ci assert.ok(fs.readFileSync(fd)); 301cb0ef41Sopenharmony_ci}); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_citempFdSync(function(fd) { 331cb0ef41Sopenharmony_ci assert.strictEqual(fs.readFileSync(fd, 'utf8'), ''); 341cb0ef41Sopenharmony_ci}); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cifunction tempFd(callback) { 371cb0ef41Sopenharmony_ci fs.open(fn, 'r', function(err, fd) { 381cb0ef41Sopenharmony_ci assert.ifError(err); 391cb0ef41Sopenharmony_ci callback(fd, function() { 401cb0ef41Sopenharmony_ci fs.close(fd, function(err) { 411cb0ef41Sopenharmony_ci assert.ifError(err); 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cifunction tempFdSync(callback) { 481cb0ef41Sopenharmony_ci const fd = fs.openSync(fn, 'r'); 491cb0ef41Sopenharmony_ci callback(fd); 501cb0ef41Sopenharmony_ci fs.closeSync(fd); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci{ 541cb0ef41Sopenharmony_ci // This test makes sure that `readFile()` always reads from the current 551cb0ef41Sopenharmony_ci // position of the file, instead of reading from the beginning of the file, 561cb0ef41Sopenharmony_ci // when used with file descriptors. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci const filename = join(tmpdir.path, 'test.txt'); 591cb0ef41Sopenharmony_ci fs.writeFileSync(filename, 'Hello World'); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci { 621cb0ef41Sopenharmony_ci // Tests the fs.readFileSync(). 631cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'r'); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // Read only five bytes, so that the position moves to five. 661cb0ef41Sopenharmony_ci const buf = Buffer.alloc(5); 671cb0ef41Sopenharmony_ci assert.strictEqual(fs.readSync(fd, buf, 0, 5), 5); 681cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString(), 'Hello'); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci // readFileSync() should read from position five, instead of zero. 711cb0ef41Sopenharmony_ci assert.strictEqual(fs.readFileSync(fd).toString(), ' World'); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci fs.closeSync(fd); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci { 771cb0ef41Sopenharmony_ci // Tests the fs.readFile(). 781cb0ef41Sopenharmony_ci fs.open(filename, 'r', common.mustSucceed((fd) => { 791cb0ef41Sopenharmony_ci const buf = Buffer.alloc(5); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci // Read only five bytes, so that the position moves to five. 821cb0ef41Sopenharmony_ci fs.read(fd, buf, 0, 5, null, common.mustSucceed((bytes) => { 831cb0ef41Sopenharmony_ci assert.strictEqual(bytes, 5); 841cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString(), 'Hello'); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci fs.readFile(fd, common.mustSucceed((data) => { 871cb0ef41Sopenharmony_ci // readFile() should read from position five, instead of zero. 881cb0ef41Sopenharmony_ci assert.strictEqual(data.toString(), ' World'); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci fs.closeSync(fd); 911cb0ef41Sopenharmony_ci })); 921cb0ef41Sopenharmony_ci })); 931cb0ef41Sopenharmony_ci })); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci} 96