11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst http2 = require('http2');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst http2util = require('../common/http2');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Test that ping flooding causes the session to be torn down
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst kSettings = new http2util.SettingsFrame();
161cb0ef41Sopenharmony_ciconst kPing = new http2util.PingFrame();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst server = http2.createServer();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cilet interval;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciserver.on('stream', common.mustNotCall());
231cb0ef41Sopenharmony_ciserver.on('session', common.mustCall((session) => {
241cb0ef41Sopenharmony_ci  session.on('error', (e) => {
251cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ERR_HTTP2_ERROR');
261cb0ef41Sopenharmony_ci    assert(e.message.includes('Flooding was detected'));
271cb0ef41Sopenharmony_ci    clearInterval(interval);
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  session.on('close', common.mustCall(() => {
301cb0ef41Sopenharmony_ci    server.close();
311cb0ef41Sopenharmony_ci  }));
321cb0ef41Sopenharmony_ci}));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
351cb0ef41Sopenharmony_ci  const client = net.connect(server.address().port);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // nghttp2 uses a limit of 10000 items in it's outbound queue.
381cb0ef41Sopenharmony_ci  // If this number is exceeded, a flooding error is raised.
391cb0ef41Sopenharmony_ci  // TODO(jasnell): Unfortunately, this test is inherently flaky because
401cb0ef41Sopenharmony_ci  // it is entirely dependent on how quickly the server is able to handle
411cb0ef41Sopenharmony_ci  // the inbound frames and whether those just happen to overflow nghttp2's
421cb0ef41Sopenharmony_ci  // outbound queue. The threshold at which the flood error occurs can vary
431cb0ef41Sopenharmony_ci  // from one system to another, and from one test run to another.
441cb0ef41Sopenharmony_ci  client.on('connect', common.mustCall(() => {
451cb0ef41Sopenharmony_ci    client.write(http2util.kClientMagic, () => {
461cb0ef41Sopenharmony_ci      client.write(kSettings.data, () => {
471cb0ef41Sopenharmony_ci        interval = setInterval(() => {
481cb0ef41Sopenharmony_ci          for (let n = 0; n < 10000; n++)
491cb0ef41Sopenharmony_ci            client.write(kPing.data);
501cb0ef41Sopenharmony_ci        }, 1);
511cb0ef41Sopenharmony_ci      });
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // An error event may or may not be emitted, depending on operating system
561cb0ef41Sopenharmony_ci  // and timing. We do not really care if one is emitted here or not, as the
571cb0ef41Sopenharmony_ci  // error on the server side is what we are testing for. Do not make this
581cb0ef41Sopenharmony_ci  // a common.mustCall() and there's no need to check the error details.
591cb0ef41Sopenharmony_ci  client.on('error', () => {});
601cb0ef41Sopenharmony_ci}));
61