|  | 
|  | 1 | +'use strict'; | 
|  | 2 | + | 
|  | 3 | +const common = require('../common'); | 
|  | 4 | + | 
|  | 5 | +if (!common.hasCrypto) { | 
|  | 6 | +  console.log('1..0 # Skipped: missing crypto'); | 
|  | 7 | +  return; | 
|  | 8 | +} | 
|  | 9 | + | 
|  | 10 | +const assert = require('assert'); | 
|  | 11 | +const https = require('https'); | 
|  | 12 | +const fs = require('fs'); | 
|  | 13 | +const constants = require('constants'); | 
|  | 14 | + | 
|  | 15 | +const options = { | 
|  | 16 | +  key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), | 
|  | 17 | +  cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'), | 
|  | 18 | +  secureOptions: constants.SSL_OP_NO_TICKET | 
|  | 19 | +}; | 
|  | 20 | + | 
|  | 21 | +// Create TLS1.2 server | 
|  | 22 | +https.createServer(options, function(req, res) { | 
|  | 23 | +  res.end('ohai'); | 
|  | 24 | +}).listen(common.PORT, function() { | 
|  | 25 | +  first(this); | 
|  | 26 | +}); | 
|  | 27 | + | 
|  | 28 | +// Do request and let agent cache the session | 
|  | 29 | +function first(server)  { | 
|  | 30 | +  const req = https.request({ | 
|  | 31 | +    port: common.PORT, | 
|  | 32 | +    rejectUnauthorized: false | 
|  | 33 | +  }, function(res) { | 
|  | 34 | +    res.resume(); | 
|  | 35 | + | 
|  | 36 | +    server.close(function() { | 
|  | 37 | +      faultyServer(); | 
|  | 38 | +    }); | 
|  | 39 | +  }); | 
|  | 40 | +  req.end(); | 
|  | 41 | +} | 
|  | 42 | + | 
|  | 43 | +// Create TLS1 server | 
|  | 44 | +function faultyServer() { | 
|  | 45 | +  options.secureProtocol = 'TLSv1_method'; | 
|  | 46 | +  https.createServer(options, function(req, res) { | 
|  | 47 | +    res.end('hello faulty'); | 
|  | 48 | +  }).listen(common.PORT, function() { | 
|  | 49 | +    second(this); | 
|  | 50 | +  }); | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +// Attempt to request using cached session | 
|  | 54 | +function second(server, session) { | 
|  | 55 | +  const req = https.request({ | 
|  | 56 | +    port: common.PORT, | 
|  | 57 | +    rejectUnauthorized: false | 
|  | 58 | +  }, function(res) { | 
|  | 59 | +    res.resume(); | 
|  | 60 | +  }); | 
|  | 61 | + | 
|  | 62 | +  // Let it fail | 
|  | 63 | +  req.on('error', common.mustCall(function(err) { | 
|  | 64 | +    assert(/wrong version number/.test(err.message)); | 
|  | 65 | + | 
|  | 66 | +    req.on('close', function() { | 
|  | 67 | +      third(server); | 
|  | 68 | +    }); | 
|  | 69 | +  })); | 
|  | 70 | +  req.end(); | 
|  | 71 | +} | 
|  | 72 | + | 
|  | 73 | +// Try on more time - session should be evicted! | 
|  | 74 | +function third(server) { | 
|  | 75 | +  const req = https.request({ | 
|  | 76 | +    port: common.PORT, | 
|  | 77 | +    rejectUnauthorized: false | 
|  | 78 | +  }, function(res) { | 
|  | 79 | +    res.resume(); | 
|  | 80 | +    assert(!req.socket.isSessionReused()); | 
|  | 81 | +    server.close(); | 
|  | 82 | +  }); | 
|  | 83 | +  req.on('error', function(err) { | 
|  | 84 | +    // never called | 
|  | 85 | +    assert(false); | 
|  | 86 | +  }); | 
|  | 87 | +  req.end(); | 
|  | 88 | +} | 
0 commit comments