11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst http2 = require('http2');
81cb0ef41Sopenharmony_ciconst { PADDING_STRATEGY_ALIGNED, PADDING_STRATEGY_CALLBACK } = http2.constants;
91cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const testData = '<h1>Hello World.</h1>';
131cb0ef41Sopenharmony_ci  const server = http2.createServer({
141cb0ef41Sopenharmony_ci    paddingStrategy: PADDING_STRATEGY_ALIGNED
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall((stream, headers) => {
171cb0ef41Sopenharmony_ci    stream.respond({
181cb0ef41Sopenharmony_ci      'content-type': 'text/html',
191cb0ef41Sopenharmony_ci      ':status': 200
201cb0ef41Sopenharmony_ci    });
211cb0ef41Sopenharmony_ci    stream.end(testData);
221cb0ef41Sopenharmony_ci  }));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = makeDuplexPair();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // The lengths of the expected writes... note that this is highly
271cb0ef41Sopenharmony_ci  // sensitive to how the internals are implemented.
281cb0ef41Sopenharmony_ci  const serverLengths = [24, 9, 9, 32];
291cb0ef41Sopenharmony_ci  const clientLengths = [9, 9, 48, 9, 1, 21, 1];
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Adjust for the 24-byte preamble and two 9-byte settings frames, and
321cb0ef41Sopenharmony_ci  // the result must be equally divisible by 8
331cb0ef41Sopenharmony_ci  assert.strictEqual(
341cb0ef41Sopenharmony_ci    (serverLengths.reduce((i, n) => i + n) - 24 - 9 - 9) % 8, 0);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Adjust for two 9-byte settings frames, and the result must be equally
371cb0ef41Sopenharmony_ci  // divisible by 8
381cb0ef41Sopenharmony_ci  assert.strictEqual(
391cb0ef41Sopenharmony_ci    (clientLengths.reduce((i, n) => i + n) - 9 - 9) % 8, 0);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  serverSide.on('data', common.mustCall((chunk) => {
421cb0ef41Sopenharmony_ci    assert.strictEqual(chunk.length, serverLengths.shift());
431cb0ef41Sopenharmony_ci  }, serverLengths.length));
441cb0ef41Sopenharmony_ci  clientSide.on('data', common.mustCall((chunk) => {
451cb0ef41Sopenharmony_ci    assert.strictEqual(chunk.length, clientLengths.shift());
461cb0ef41Sopenharmony_ci  }, clientLengths.length));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  server.emit('connection', serverSide);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const client = http2.connect('http://localhost:80', {
511cb0ef41Sopenharmony_ci    paddingStrategy: PADDING_STRATEGY_ALIGNED,
521cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide)
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const req = client.request({ ':path': '/a' });
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  req.on('response', common.mustCall());
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  req.setEncoding('utf8');
601cb0ef41Sopenharmony_ci  req.on('data', common.mustCall((data) => {
611cb0ef41Sopenharmony_ci    assert.strictEqual(data, testData);
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => {
641cb0ef41Sopenharmony_ci    clientSide.destroy();
651cb0ef41Sopenharmony_ci    clientSide.end();
661cb0ef41Sopenharmony_ci  }));
671cb0ef41Sopenharmony_ci  req.end();
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci// PADDING_STRATEGY_CALLBACK has been aliased to mean aligned padding.
711cb0ef41Sopenharmony_ciassert.strictEqual(PADDING_STRATEGY_ALIGNED, PADDING_STRATEGY_CALLBACK);
72