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_ciconst expected = Buffer.from('hello');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
301cb0ef41Sopenharmony_citmpdir.refresh();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// fs.write with all parameters provided:
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write1.txt');
351cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
361cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
371cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
381cb0ef41Sopenharmony_ci      fs.closeSync(fd);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
411cb0ef41Sopenharmony_ci      assert.strictEqual(found, expected.toString());
421cb0ef41Sopenharmony_ci    });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    fs.write(fd, expected, 0, expected.length, null, cb);
451cb0ef41Sopenharmony_ci  }));
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// fs.write with a buffer, without the length parameter:
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write2.txt');
511cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
521cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
531cb0ef41Sopenharmony_ci      assert.strictEqual(written, 2);
541cb0ef41Sopenharmony_ci      fs.closeSync(fd);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
571cb0ef41Sopenharmony_ci      assert.strictEqual(found, 'lo');
581cb0ef41Sopenharmony_ci    });
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    fs.write(fd, Buffer.from('hello'), 3, cb);
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// fs.write with a buffer, without the offset and length parameters:
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write3.txt');
671cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
681cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
691cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
701cb0ef41Sopenharmony_ci      fs.closeSync(fd);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
731cb0ef41Sopenharmony_ci      assert.deepStrictEqual(expected.toString(), found);
741cb0ef41Sopenharmony_ci    });
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    fs.write(fd, expected, cb);
771cb0ef41Sopenharmony_ci  }));
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// fs.write with the offset passed as undefined followed by the callback:
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write4.txt');
831cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
841cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
851cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
861cb0ef41Sopenharmony_ci      fs.closeSync(fd);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
891cb0ef41Sopenharmony_ci      assert.deepStrictEqual(expected.toString(), found);
901cb0ef41Sopenharmony_ci    });
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    fs.write(fd, expected, undefined, cb);
931cb0ef41Sopenharmony_ci  }));
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci// fs.write with offset and length passed as undefined followed by the callback:
971cb0ef41Sopenharmony_ci{
981cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write5.txt');
991cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
1001cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
1011cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
1021cb0ef41Sopenharmony_ci      fs.closeSync(fd);
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
1051cb0ef41Sopenharmony_ci      assert.strictEqual(found, expected.toString());
1061cb0ef41Sopenharmony_ci    });
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    fs.write(fd, expected, undefined, undefined, cb);
1091cb0ef41Sopenharmony_ci  }));
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci// fs.write with a Uint8Array, without the offset and length parameters:
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write6.txt');
1151cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
1161cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
1171cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
1181cb0ef41Sopenharmony_ci      fs.closeSync(fd);
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
1211cb0ef41Sopenharmony_ci      assert.strictEqual(found, expected.toString());
1221cb0ef41Sopenharmony_ci    });
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci    fs.write(fd, Uint8Array.from(expected), cb);
1251cb0ef41Sopenharmony_ci  }));
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci// fs.write with invalid offset type
1291cb0ef41Sopenharmony_ci{
1301cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write7.txt');
1311cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
1321cb0ef41Sopenharmony_ci    assert.throws(() => {
1331cb0ef41Sopenharmony_ci      fs.write(fd,
1341cb0ef41Sopenharmony_ci               Buffer.from('abcd'),
1351cb0ef41Sopenharmony_ci               NaN,
1361cb0ef41Sopenharmony_ci               expected.length,
1371cb0ef41Sopenharmony_ci               0,
1381cb0ef41Sopenharmony_ci               common.mustNotCall());
1391cb0ef41Sopenharmony_ci    }, {
1401cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
1411cb0ef41Sopenharmony_ci      name: 'RangeError',
1421cb0ef41Sopenharmony_ci      message: 'The value of "offset" is out of range. ' +
1431cb0ef41Sopenharmony_ci               'It must be an integer. Received NaN'
1441cb0ef41Sopenharmony_ci    });
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci    fs.closeSync(fd);
1471cb0ef41Sopenharmony_ci  }));
1481cb0ef41Sopenharmony_ci}
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci// fs.write with a DataView, without the offset and length parameters:
1511cb0ef41Sopenharmony_ci{
1521cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'write8.txt');
1531cb0ef41Sopenharmony_ci  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
1541cb0ef41Sopenharmony_ci    const cb = common.mustSucceed((written) => {
1551cb0ef41Sopenharmony_ci      assert.strictEqual(written, expected.length);
1561cb0ef41Sopenharmony_ci      fs.closeSync(fd);
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci      const found = fs.readFileSync(filename, 'utf8');
1591cb0ef41Sopenharmony_ci      assert.strictEqual(found, expected.toString());
1601cb0ef41Sopenharmony_ci    });
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci    const uint8 = Uint8Array.from(expected);
1631cb0ef41Sopenharmony_ci    fs.write(fd, new DataView(uint8.buffer), cb);
1641cb0ef41Sopenharmony_ci  }));
1651cb0ef41Sopenharmony_ci}
166