1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6 7const assert = require('assert'); 8const http2 = require('http2'); 9const net = require('net'); 10 11const http2util = require('../common/http2'); 12 13// Test that ping flooding causes the session to be torn down 14 15const kSettings = new http2util.SettingsFrame(); 16const kPing = new http2util.PingFrame(); 17 18const server = http2.createServer(); 19 20let interval; 21 22server.on('stream', common.mustNotCall()); 23server.on('session', common.mustCall((session) => { 24 session.on('error', (e) => { 25 assert.strictEqual(e.code, 'ERR_HTTP2_ERROR'); 26 assert(e.message.includes('Flooding was detected')); 27 clearInterval(interval); 28 }); 29 session.on('close', common.mustCall(() => { 30 server.close(); 31 })); 32})); 33 34server.listen(0, common.mustCall(() => { 35 const client = net.connect(server.address().port); 36 37 // nghttp2 uses a limit of 10000 items in it's outbound queue. 38 // If this number is exceeded, a flooding error is raised. 39 // TODO(jasnell): Unfortunately, this test is inherently flaky because 40 // it is entirely dependent on how quickly the server is able to handle 41 // the inbound frames and whether those just happen to overflow nghttp2's 42 // outbound queue. The threshold at which the flood error occurs can vary 43 // from one system to another, and from one test run to another. 44 client.on('connect', common.mustCall(() => { 45 client.write(http2util.kClientMagic, () => { 46 client.write(kSettings.data, () => { 47 interval = setInterval(() => { 48 for (let n = 0; n < 10000; n++) 49 client.write(kPing.data); 50 }, 1); 51 }); 52 }); 53 })); 54 55 // An error event may or may not be emitted, depending on operating system 56 // and timing. We do not really care if one is emitted here or not, as the 57 // error on the server side is what we are testing for. Do not make this 58 // a common.mustCall() and there's no need to check the error details. 59 client.on('error', () => {}); 60})); 61