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 { Readable: R } = require('stream');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciclass TestReader extends R {
281cb0ef41Sopenharmony_ci  constructor(n, opts) {
291cb0ef41Sopenharmony_ci    super(opts);
301cb0ef41Sopenharmony_ci    this.pos = 0;
311cb0ef41Sopenharmony_ci    this.len = n || 100;
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  _read(n) {
351cb0ef41Sopenharmony_ci    setTimeout(() => {
361cb0ef41Sopenharmony_ci      if (this.pos >= this.len) {
371cb0ef41Sopenharmony_ci        // Double push(null) to test eos handling
381cb0ef41Sopenharmony_ci        this.push(null);
391cb0ef41Sopenharmony_ci        return this.push(null);
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci      n = Math.min(n, this.len - this.pos);
431cb0ef41Sopenharmony_ci      if (n <= 0) {
441cb0ef41Sopenharmony_ci        // Double push(null) to test eos handling
451cb0ef41Sopenharmony_ci        this.push(null);
461cb0ef41Sopenharmony_ci        return this.push(null);
471cb0ef41Sopenharmony_ci      }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci      this.pos += n;
501cb0ef41Sopenharmony_ci      const ret = Buffer.alloc(n, 'a');
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci      return this.push(ret);
531cb0ef41Sopenharmony_ci    }, 1);
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  // Verify utf8 encoding
591cb0ef41Sopenharmony_ci  const tr = new TestReader(100);
601cb0ef41Sopenharmony_ci  tr.setEncoding('utf8');
611cb0ef41Sopenharmony_ci  const out = [];
621cb0ef41Sopenharmony_ci  const expect =
631cb0ef41Sopenharmony_ci    [ 'aaaaaaaaaa',
641cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
651cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
661cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
671cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
681cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
691cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
701cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
711cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
721cb0ef41Sopenharmony_ci      'aaaaaaaaaa' ];
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
751cb0ef41Sopenharmony_ci    let chunk;
761cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
771cb0ef41Sopenharmony_ci      out.push(chunk);
781cb0ef41Sopenharmony_ci  });
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
811cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
821cb0ef41Sopenharmony_ci  }));
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci{
871cb0ef41Sopenharmony_ci  // Verify hex encoding
881cb0ef41Sopenharmony_ci  const tr = new TestReader(100);
891cb0ef41Sopenharmony_ci  tr.setEncoding('hex');
901cb0ef41Sopenharmony_ci  const out = [];
911cb0ef41Sopenharmony_ci  const expect =
921cb0ef41Sopenharmony_ci    [ '6161616161',
931cb0ef41Sopenharmony_ci      '6161616161',
941cb0ef41Sopenharmony_ci      '6161616161',
951cb0ef41Sopenharmony_ci      '6161616161',
961cb0ef41Sopenharmony_ci      '6161616161',
971cb0ef41Sopenharmony_ci      '6161616161',
981cb0ef41Sopenharmony_ci      '6161616161',
991cb0ef41Sopenharmony_ci      '6161616161',
1001cb0ef41Sopenharmony_ci      '6161616161',
1011cb0ef41Sopenharmony_ci      '6161616161',
1021cb0ef41Sopenharmony_ci      '6161616161',
1031cb0ef41Sopenharmony_ci      '6161616161',
1041cb0ef41Sopenharmony_ci      '6161616161',
1051cb0ef41Sopenharmony_ci      '6161616161',
1061cb0ef41Sopenharmony_ci      '6161616161',
1071cb0ef41Sopenharmony_ci      '6161616161',
1081cb0ef41Sopenharmony_ci      '6161616161',
1091cb0ef41Sopenharmony_ci      '6161616161',
1101cb0ef41Sopenharmony_ci      '6161616161',
1111cb0ef41Sopenharmony_ci      '6161616161' ];
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
1141cb0ef41Sopenharmony_ci    let chunk;
1151cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
1161cb0ef41Sopenharmony_ci      out.push(chunk);
1171cb0ef41Sopenharmony_ci  });
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
1201cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
1211cb0ef41Sopenharmony_ci  }));
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci{
1251cb0ef41Sopenharmony_ci  // Verify hex encoding with read(13)
1261cb0ef41Sopenharmony_ci  const tr = new TestReader(100);
1271cb0ef41Sopenharmony_ci  tr.setEncoding('hex');
1281cb0ef41Sopenharmony_ci  const out = [];
1291cb0ef41Sopenharmony_ci  const expect =
1301cb0ef41Sopenharmony_ci    [ '6161616161616',
1311cb0ef41Sopenharmony_ci      '1616161616161',
1321cb0ef41Sopenharmony_ci      '6161616161616',
1331cb0ef41Sopenharmony_ci      '1616161616161',
1341cb0ef41Sopenharmony_ci      '6161616161616',
1351cb0ef41Sopenharmony_ci      '1616161616161',
1361cb0ef41Sopenharmony_ci      '6161616161616',
1371cb0ef41Sopenharmony_ci      '1616161616161',
1381cb0ef41Sopenharmony_ci      '6161616161616',
1391cb0ef41Sopenharmony_ci      '1616161616161',
1401cb0ef41Sopenharmony_ci      '6161616161616',
1411cb0ef41Sopenharmony_ci      '1616161616161',
1421cb0ef41Sopenharmony_ci      '6161616161616',
1431cb0ef41Sopenharmony_ci      '1616161616161',
1441cb0ef41Sopenharmony_ci      '6161616161616',
1451cb0ef41Sopenharmony_ci      '16161' ];
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
1481cb0ef41Sopenharmony_ci    let chunk;
1491cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(13)))
1501cb0ef41Sopenharmony_ci      out.push(chunk);
1511cb0ef41Sopenharmony_ci  });
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
1541cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
1551cb0ef41Sopenharmony_ci  }));
1561cb0ef41Sopenharmony_ci}
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci{
1591cb0ef41Sopenharmony_ci  // Verify base64 encoding
1601cb0ef41Sopenharmony_ci  const tr = new TestReader(100);
1611cb0ef41Sopenharmony_ci  tr.setEncoding('base64');
1621cb0ef41Sopenharmony_ci  const out = [];
1631cb0ef41Sopenharmony_ci  const expect =
1641cb0ef41Sopenharmony_ci    [ 'YWFhYWFhYW',
1651cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1661cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1671cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1681cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1691cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1701cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1711cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1721cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1731cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1741cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1751cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
1761cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
1771cb0ef41Sopenharmony_ci      'FhYQ==' ];
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
1801cb0ef41Sopenharmony_ci    let chunk;
1811cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
1821cb0ef41Sopenharmony_ci      out.push(chunk);
1831cb0ef41Sopenharmony_ci  });
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
1861cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
1871cb0ef41Sopenharmony_ci  }));
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci  // Verify utf8 encoding
1921cb0ef41Sopenharmony_ci  const tr = new TestReader(100, { encoding: 'utf8' });
1931cb0ef41Sopenharmony_ci  const out = [];
1941cb0ef41Sopenharmony_ci  const expect =
1951cb0ef41Sopenharmony_ci    [ 'aaaaaaaaaa',
1961cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
1971cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
1981cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
1991cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
2001cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
2011cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
2021cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
2031cb0ef41Sopenharmony_ci      'aaaaaaaaaa',
2041cb0ef41Sopenharmony_ci      'aaaaaaaaaa' ];
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
2071cb0ef41Sopenharmony_ci    let chunk;
2081cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
2091cb0ef41Sopenharmony_ci      out.push(chunk);
2101cb0ef41Sopenharmony_ci  });
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
2131cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
2141cb0ef41Sopenharmony_ci  }));
2151cb0ef41Sopenharmony_ci}
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci{
2191cb0ef41Sopenharmony_ci  // Verify hex encoding
2201cb0ef41Sopenharmony_ci  const tr = new TestReader(100, { encoding: 'hex' });
2211cb0ef41Sopenharmony_ci  const out = [];
2221cb0ef41Sopenharmony_ci  const expect =
2231cb0ef41Sopenharmony_ci    [ '6161616161',
2241cb0ef41Sopenharmony_ci      '6161616161',
2251cb0ef41Sopenharmony_ci      '6161616161',
2261cb0ef41Sopenharmony_ci      '6161616161',
2271cb0ef41Sopenharmony_ci      '6161616161',
2281cb0ef41Sopenharmony_ci      '6161616161',
2291cb0ef41Sopenharmony_ci      '6161616161',
2301cb0ef41Sopenharmony_ci      '6161616161',
2311cb0ef41Sopenharmony_ci      '6161616161',
2321cb0ef41Sopenharmony_ci      '6161616161',
2331cb0ef41Sopenharmony_ci      '6161616161',
2341cb0ef41Sopenharmony_ci      '6161616161',
2351cb0ef41Sopenharmony_ci      '6161616161',
2361cb0ef41Sopenharmony_ci      '6161616161',
2371cb0ef41Sopenharmony_ci      '6161616161',
2381cb0ef41Sopenharmony_ci      '6161616161',
2391cb0ef41Sopenharmony_ci      '6161616161',
2401cb0ef41Sopenharmony_ci      '6161616161',
2411cb0ef41Sopenharmony_ci      '6161616161',
2421cb0ef41Sopenharmony_ci      '6161616161' ];
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
2451cb0ef41Sopenharmony_ci    let chunk;
2461cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
2471cb0ef41Sopenharmony_ci      out.push(chunk);
2481cb0ef41Sopenharmony_ci  });
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
2511cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
2521cb0ef41Sopenharmony_ci  }));
2531cb0ef41Sopenharmony_ci}
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci{
2561cb0ef41Sopenharmony_ci  // Verify hex encoding with read(13)
2571cb0ef41Sopenharmony_ci  const tr = new TestReader(100, { encoding: 'hex' });
2581cb0ef41Sopenharmony_ci  const out = [];
2591cb0ef41Sopenharmony_ci  const expect =
2601cb0ef41Sopenharmony_ci    [ '6161616161616',
2611cb0ef41Sopenharmony_ci      '1616161616161',
2621cb0ef41Sopenharmony_ci      '6161616161616',
2631cb0ef41Sopenharmony_ci      '1616161616161',
2641cb0ef41Sopenharmony_ci      '6161616161616',
2651cb0ef41Sopenharmony_ci      '1616161616161',
2661cb0ef41Sopenharmony_ci      '6161616161616',
2671cb0ef41Sopenharmony_ci      '1616161616161',
2681cb0ef41Sopenharmony_ci      '6161616161616',
2691cb0ef41Sopenharmony_ci      '1616161616161',
2701cb0ef41Sopenharmony_ci      '6161616161616',
2711cb0ef41Sopenharmony_ci      '1616161616161',
2721cb0ef41Sopenharmony_ci      '6161616161616',
2731cb0ef41Sopenharmony_ci      '1616161616161',
2741cb0ef41Sopenharmony_ci      '6161616161616',
2751cb0ef41Sopenharmony_ci      '16161' ];
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
2781cb0ef41Sopenharmony_ci    let chunk;
2791cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(13)))
2801cb0ef41Sopenharmony_ci      out.push(chunk);
2811cb0ef41Sopenharmony_ci  });
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
2841cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
2851cb0ef41Sopenharmony_ci  }));
2861cb0ef41Sopenharmony_ci}
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci{
2891cb0ef41Sopenharmony_ci  // Verify base64 encoding
2901cb0ef41Sopenharmony_ci  const tr = new TestReader(100, { encoding: 'base64' });
2911cb0ef41Sopenharmony_ci  const out = [];
2921cb0ef41Sopenharmony_ci  const expect =
2931cb0ef41Sopenharmony_ci    [ 'YWFhYWFhYW',
2941cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
2951cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
2961cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
2971cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
2981cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
2991cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
3001cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
3011cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
3021cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
3031cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
3041cb0ef41Sopenharmony_ci      'FhYWFhYWFh',
3051cb0ef41Sopenharmony_ci      'YWFhYWFhYW',
3061cb0ef41Sopenharmony_ci      'FhYQ==' ];
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci  tr.on('readable', function flow() {
3091cb0ef41Sopenharmony_ci    let chunk;
3101cb0ef41Sopenharmony_ci    while (null !== (chunk = tr.read(10)))
3111cb0ef41Sopenharmony_ci      out.push(chunk);
3121cb0ef41Sopenharmony_ci  });
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci  tr.on('end', common.mustCall(function() {
3151cb0ef41Sopenharmony_ci    assert.deepStrictEqual(out, expect);
3161cb0ef41Sopenharmony_ci  }));
3171cb0ef41Sopenharmony_ci}
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ci{
3201cb0ef41Sopenharmony_ci  // Verify chaining behavior
3211cb0ef41Sopenharmony_ci  const tr = new TestReader(100);
3221cb0ef41Sopenharmony_ci  assert.deepStrictEqual(tr.setEncoding('utf8'), tr);
3231cb0ef41Sopenharmony_ci}
324