11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst path = require('path'); 261cb0ef41Sopenharmony_ciconst fs = require('fs'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst filepath = path.join(tmpdir.path, 'write_pos.txt'); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst cb_expected = 'write open close write open close write open close '; 351cb0ef41Sopenharmony_cilet cb_occurred = ''; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciconst fileDataInitial = 'abcdefghijklmnopqrstuvwxyz'; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciconst fileDataExpected_1 = 'abcdefghijklmnopqrstuvwxyz'; 401cb0ef41Sopenharmony_ciconst fileDataExpected_2 = 'abcdefghij123456qrstuvwxyz'; 411cb0ef41Sopenharmony_ciconst fileDataExpected_3 = 'abcdefghij\u2026\u2026qrstuvwxyz'; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciprocess.on('exit', function() { 451cb0ef41Sopenharmony_ci if (cb_occurred !== cb_expected) { 461cb0ef41Sopenharmony_ci console.log(' Test callback events missing or out of order:'); 471cb0ef41Sopenharmony_ci console.log(` expected: ${cb_expected}`); 481cb0ef41Sopenharmony_ci console.log(` occurred: ${cb_occurred}`); 491cb0ef41Sopenharmony_ci assert.strictEqual( 501cb0ef41Sopenharmony_ci cb_occurred, cb_expected, 511cb0ef41Sopenharmony_ci `events missing or out of order: "${cb_occurred}" !== "${cb_expected}"`); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci}); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_citmpdir.refresh(); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_cifunction run_test_1() { 601cb0ef41Sopenharmony_ci const options = {}; 611cb0ef41Sopenharmony_ci const file = fs.createWriteStream(filepath, options); 621cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 631cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci file.on('open', function(fd) { 661cb0ef41Sopenharmony_ci cb_occurred += 'open '; 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci file.on('close', function() { 701cb0ef41Sopenharmony_ci cb_occurred += 'close '; 711cb0ef41Sopenharmony_ci console.log(' (debug: bytesWritten ', file.bytesWritten); 721cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 731cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 741cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesWritten, buffer.length); 751cb0ef41Sopenharmony_ci const fileData = fs.readFileSync(filepath, 'utf8'); 761cb0ef41Sopenharmony_ci console.log(' (debug: file data ', fileData); 771cb0ef41Sopenharmony_ci console.log(' (debug: expected ', fileDataExpected_1); 781cb0ef41Sopenharmony_ci assert.strictEqual(fileData, fileDataExpected_1); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci run_test_2(); 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci file.on('error', function(err) { 841cb0ef41Sopenharmony_ci cb_occurred += 'error '; 851cb0ef41Sopenharmony_ci console.log(' (debug: err event ', err); 861cb0ef41Sopenharmony_ci throw err; 871cb0ef41Sopenharmony_ci }); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci const buffer = Buffer.from(fileDataInitial); 901cb0ef41Sopenharmony_ci file.write(buffer); 911cb0ef41Sopenharmony_ci cb_occurred += 'write '; 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci file.end(); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_cifunction run_test_2() { 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci const buffer = Buffer.from('123456'); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci const options = { start: 10, 1021cb0ef41Sopenharmony_ci flags: 'r+' }; 1031cb0ef41Sopenharmony_ci const file = fs.createWriteStream(filepath, options); 1041cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 1051cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci file.on('open', function(fd) { 1081cb0ef41Sopenharmony_ci cb_occurred += 'open '; 1091cb0ef41Sopenharmony_ci }); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci file.on('close', function() { 1121cb0ef41Sopenharmony_ci cb_occurred += 'close '; 1131cb0ef41Sopenharmony_ci console.log(' (debug: bytesWritten ', file.bytesWritten); 1141cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 1151cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 1161cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesWritten, buffer.length); 1171cb0ef41Sopenharmony_ci const fileData = fs.readFileSync(filepath, 'utf8'); 1181cb0ef41Sopenharmony_ci console.log(' (debug: file data ', fileData); 1191cb0ef41Sopenharmony_ci console.log(' (debug: expected ', fileDataExpected_2); 1201cb0ef41Sopenharmony_ci assert.strictEqual(fileData, fileDataExpected_2); 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci run_test_3(); 1231cb0ef41Sopenharmony_ci }); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci file.on('error', function(err) { 1261cb0ef41Sopenharmony_ci cb_occurred += 'error '; 1271cb0ef41Sopenharmony_ci console.log(' (debug: err event ', err); 1281cb0ef41Sopenharmony_ci throw err; 1291cb0ef41Sopenharmony_ci }); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci file.write(buffer); 1321cb0ef41Sopenharmony_ci cb_occurred += 'write '; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci file.end(); 1351cb0ef41Sopenharmony_ci} 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_cifunction run_test_3() { 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci const data = '\u2026\u2026'; // 3 bytes * 2 = 6 bytes in UTF-8 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci const options = { start: 10, 1431cb0ef41Sopenharmony_ci flags: 'r+' }; 1441cb0ef41Sopenharmony_ci const file = fs.createWriteStream(filepath, options); 1451cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 1461cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci file.on('open', function(fd) { 1491cb0ef41Sopenharmony_ci cb_occurred += 'open '; 1501cb0ef41Sopenharmony_ci }); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci file.on('close', function() { 1531cb0ef41Sopenharmony_ci cb_occurred += 'close '; 1541cb0ef41Sopenharmony_ci console.log(' (debug: bytesWritten ', file.bytesWritten); 1551cb0ef41Sopenharmony_ci console.log(' (debug: start ', file.start); 1561cb0ef41Sopenharmony_ci console.log(' (debug: pos ', file.pos); 1571cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesWritten, data.length * 3); 1581cb0ef41Sopenharmony_ci const fileData = fs.readFileSync(filepath, 'utf8'); 1591cb0ef41Sopenharmony_ci console.log(' (debug: file data ', fileData); 1601cb0ef41Sopenharmony_ci console.log(' (debug: expected ', fileDataExpected_3); 1611cb0ef41Sopenharmony_ci assert.strictEqual(fileData, fileDataExpected_3); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci run_test_4(); 1641cb0ef41Sopenharmony_ci run_test_5(); 1651cb0ef41Sopenharmony_ci }); 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci file.on('error', function(err) { 1681cb0ef41Sopenharmony_ci cb_occurred += 'error '; 1691cb0ef41Sopenharmony_ci console.log(' (debug: err event ', err); 1701cb0ef41Sopenharmony_ci throw err; 1711cb0ef41Sopenharmony_ci }); 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci file.write(data, 'utf8'); 1741cb0ef41Sopenharmony_ci cb_occurred += 'write '; 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci file.end(); 1771cb0ef41Sopenharmony_ci} 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ciconst run_test_4 = common.mustCall(function() { 1811cb0ef41Sopenharmony_ci // Error: start must be >= zero 1821cb0ef41Sopenharmony_ci const fn = () => { 1831cb0ef41Sopenharmony_ci fs.createWriteStream(filepath, { start: -5, flags: 'r+' }); 1841cb0ef41Sopenharmony_ci }; 1851cb0ef41Sopenharmony_ci // Verify the range of values using a common integer verifier. 1861cb0ef41Sopenharmony_ci // Limit Number.MAX_SAFE_INTEGER 1871cb0ef41Sopenharmony_ci const err = { 1881cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1891cb0ef41Sopenharmony_ci message: 'The value of "start" is out of range. ' + 1901cb0ef41Sopenharmony_ci `It must be >= 0 && <= ${Number.MAX_SAFE_INTEGER}. Received -5`, 1911cb0ef41Sopenharmony_ci name: 'RangeError' 1921cb0ef41Sopenharmony_ci }; 1931cb0ef41Sopenharmony_ci assert.throws(fn, err); 1941cb0ef41Sopenharmony_ci}); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ciconst run_test_5 = common.mustCall(function() { 1981cb0ef41Sopenharmony_ci // Error: start must be <= 2 ** 53 - 1 1991cb0ef41Sopenharmony_ci const fn = () => { 2001cb0ef41Sopenharmony_ci fs.createWriteStream(filepath, { start: 2 ** 53, flags: 'r+' }); 2011cb0ef41Sopenharmony_ci }; 2021cb0ef41Sopenharmony_ci // Verify the range of values using a common integer verifier. 2031cb0ef41Sopenharmony_ci // Limit Number.MAX_SAFE_INTEGER 2041cb0ef41Sopenharmony_ci const err = { 2051cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 2061cb0ef41Sopenharmony_ci message: 'The value of "start" is out of range. It must be ' + 2071cb0ef41Sopenharmony_ci `>= 0 && <= ${Number.MAX_SAFE_INTEGER}. ` + 2081cb0ef41Sopenharmony_ci 'Received 9_007_199_254_740_992', 2091cb0ef41Sopenharmony_ci name: 'RangeError' 2101cb0ef41Sopenharmony_ci }; 2111cb0ef41Sopenharmony_ci assert.throws(fn, err); 2121cb0ef41Sopenharmony_ci}); 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_cirun_test_1(); 215