11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst h2 = require('http2');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst preamble = Buffer.from([
121cb0ef41Sopenharmony_ci  0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f,
131cb0ef41Sopenharmony_ci  0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a,
141cb0ef41Sopenharmony_ci  0x0d, 0x0a, 0x00, 0x00, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
151cb0ef41Sopenharmony_ci  0x00, 0x03, 0x00, 0x00, 0x00, 0x64, 0x00, 0x04, 0x00, 0x00, 0xff,
161cb0ef41Sopenharmony_ci  0xff, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
171cb0ef41Sopenharmony_ci  0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
181cb0ef41Sopenharmony_ci  0x00, 0xc8, 0x00, 0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05,
191cb0ef41Sopenharmony_ci  0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x05, 0x02, 0x00, 0x00,
201cb0ef41Sopenharmony_ci  0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
211cb0ef41Sopenharmony_ci  0x02, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x07, 0x00,
221cb0ef41Sopenharmony_ci  0x00, 0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
231cb0ef41Sopenharmony_ci  0x00, 0x03, 0x00, 0x00, 0x00, 0x2c, 0x01, 0x24, 0x00, 0x00, 0x00,
241cb0ef41Sopenharmony_ci  0x0d, 0x00, 0x00, 0x00, 0x0b, 0x0f, 0x83, 0x84, 0x86, 0x41, 0x8a,
251cb0ef41Sopenharmony_ci  0xa0, 0xe4, 0x1d, 0x13, 0x9d, 0x09, 0xb8, 0xf0, 0x1e, 0x07, 0x53,
261cb0ef41Sopenharmony_ci  0x03, 0x2a, 0x2f, 0x2a, 0x90, 0x7a, 0x8a, 0xaa, 0x69, 0xd2, 0x9a,
271cb0ef41Sopenharmony_ci  0xc4, 0xc0, 0x57, 0x0b, 0xcb, 0x87, 0x0f, 0x0d, 0x83, 0x08, 0x00,
281cb0ef41Sopenharmony_ci  0x0f,
291cb0ef41Sopenharmony_ci]);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst data = Buffer.from([
321cb0ef41Sopenharmony_ci  0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d,
331cb0ef41Sopenharmony_ci  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x68, 0x65, 0x6c,
341cb0ef41Sopenharmony_ci  0x6c, 0x6f, 0x0a, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a,
351cb0ef41Sopenharmony_ci]);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// This is testing the case of a misbehaving client that is not paying
381cb0ef41Sopenharmony_ci// attention to flow control. The initial window size is set to data
391cb0ef41Sopenharmony_ci// payload * 2, which in this case is 36, the stream is paused so
401cb0ef41Sopenharmony_ci// WINDOW_UPDATE frames are not being sent, which means the window
411cb0ef41Sopenharmony_ci// size is not being updated. A well behaved client is supposed to
421cb0ef41Sopenharmony_ci// stop sending until the window size is expanded again.
431cb0ef41Sopenharmony_ci//
441cb0ef41Sopenharmony_ci// However, our malicious client keeps sending data beyond the flow
451cb0ef41Sopenharmony_ci// control window!
461cb0ef41Sopenharmony_ci//
471cb0ef41Sopenharmony_ci// Bad client! Bad!
481cb0ef41Sopenharmony_ci//
491cb0ef41Sopenharmony_ci// Fortunately, nghttp2 handles this situation for us by keeping track
501cb0ef41Sopenharmony_ci// of the flow control window and responding with a FLOW_CONTROL_ERROR
511cb0ef41Sopenharmony_ci// causing the stream to get shut down...
521cb0ef41Sopenharmony_ci//
531cb0ef41Sopenharmony_ci// At least, that's what is supposed to happen.
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cilet client;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciconst server = h2.createServer({ settings: { initialWindowSize: 36 } });
581cb0ef41Sopenharmony_ciserver.on('stream', (stream) => {
591cb0ef41Sopenharmony_ci  // Set the high water mark to zero, since otherwise we still accept
601cb0ef41Sopenharmony_ci  // reads from the source stream (if we can consume them).
611cb0ef41Sopenharmony_ci  stream._readableState.highWaterMark = 0;
621cb0ef41Sopenharmony_ci  stream.pause();
631cb0ef41Sopenharmony_ci  stream.on('error', common.expectsError({
641cb0ef41Sopenharmony_ci    code: 'ERR_HTTP2_STREAM_ERROR',
651cb0ef41Sopenharmony_ci    name: 'Error',
661cb0ef41Sopenharmony_ci    message: 'Stream closed with error code NGHTTP2_FLOW_CONTROL_ERROR'
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall(() => {
691cb0ef41Sopenharmony_ci    server.close();
701cb0ef41Sopenharmony_ci    client.destroy();
711cb0ef41Sopenharmony_ci  }));
721cb0ef41Sopenharmony_ci  stream.on('end', common.mustNotCall());
731cb0ef41Sopenharmony_ci});
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciserver.listen(0, () => {
761cb0ef41Sopenharmony_ci  client = net.connect(server.address().port, () => {
771cb0ef41Sopenharmony_ci    client.write(preamble);
781cb0ef41Sopenharmony_ci    client.write(data);
791cb0ef41Sopenharmony_ci    client.write(data);
801cb0ef41Sopenharmony_ci    client.write(data);
811cb0ef41Sopenharmony_ci  });
821cb0ef41Sopenharmony_ci});
83