11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { mustNotMutateObjectDeep } = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst filepath = fixtures.path('x.txt');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst expected = Buffer.from('xyz\n');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction runTest(defaultBuffer, options) {
121cb0ef41Sopenharmony_ci  let fd;
131cb0ef41Sopenharmony_ci  try {
141cb0ef41Sopenharmony_ci    fd = fs.openSync(filepath, 'r');
151cb0ef41Sopenharmony_ci    const result = fs.readSync(fd, defaultBuffer, options);
161cb0ef41Sopenharmony_ci    assert.strictEqual(result, expected.length);
171cb0ef41Sopenharmony_ci    assert.deepStrictEqual(defaultBuffer, expected);
181cb0ef41Sopenharmony_ci  } finally {
191cb0ef41Sopenharmony_ci    if (fd != null) fs.closeSync(fd);
201cb0ef41Sopenharmony_ci  }
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cifor (const options of [
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Test options object
261cb0ef41Sopenharmony_ci  { offset: 0 },
271cb0ef41Sopenharmony_ci  { length: expected.length },
281cb0ef41Sopenharmony_ci  { position: 0 },
291cb0ef41Sopenharmony_ci  { offset: 0, length: expected.length },
301cb0ef41Sopenharmony_ci  { offset: 0, position: 0 },
311cb0ef41Sopenharmony_ci  { length: expected.length, position: 0 },
321cb0ef41Sopenharmony_ci  { offset: 0, length: expected.length, position: 0 },
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  { offset: null },
351cb0ef41Sopenharmony_ci  { position: null },
361cb0ef41Sopenharmony_ci  { position: -1 },
371cb0ef41Sopenharmony_ci  { position: 0n },
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Test default params
401cb0ef41Sopenharmony_ci  {},
411cb0ef41Sopenharmony_ci  null,
421cb0ef41Sopenharmony_ci  undefined,
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  // Test if bad params are interpreted as default (not mandatory)
451cb0ef41Sopenharmony_ci  false,
461cb0ef41Sopenharmony_ci  true,
471cb0ef41Sopenharmony_ci  Infinity,
481cb0ef41Sopenharmony_ci  42n,
491cb0ef41Sopenharmony_ci  Symbol(),
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  // Test even more malicious corner cases
521cb0ef41Sopenharmony_ci  '4'.repeat(expected.length),
531cb0ef41Sopenharmony_ci  new String('4444'),
541cb0ef41Sopenharmony_ci  [4, 4, 4, 4],
551cb0ef41Sopenharmony_ci]) {
561cb0ef41Sopenharmony_ci  runTest(Buffer.allocUnsafe(expected.length), mustNotMutateObjectDeep(options));
571cb0ef41Sopenharmony_ci}
58