Created
June 8, 2011 22:14
-
-
Save brainpicture/1015568 to your computer and use it in GitHub Desktop.
test for https://github.com/joyent/node/issues/1046. For example you can run it from node-v0.4.8/test/pummel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// STARTTLS Body | |
// test is below | |
function starttls(socket, sslcontext, cb, isServer) { | |
var pair = require('tls').createSecurePair(sslcontext, isServer); | |
var cleartext = pipe(pair, socket); | |
pair.on('secure', function() { | |
var verifyError = pair.ssl.verifyError(); | |
if (verifyError) { | |
cleartext.authorized = false; | |
cleartext.authorizationError = verifyError; | |
} else { | |
cleartext.authorized = true; | |
} | |
if (cb) cb(); | |
}); | |
cleartext._controlReleased = true; | |
return cleartext; | |
}; | |
function forwardEvents(events, emitterSource, emitterDestination) { | |
var map = {}, name, handler; | |
for(var i = 0; i < events.length; i++) { | |
name = events[i]; | |
handler = (function generateForwardEvent(){ | |
return function forwardEvent(name) { | |
return emitterDestination.emit.apply(emitterDestination, arguments); | |
} | |
})(name); | |
map[name] = handler; | |
emitterSource.on(name, handler); | |
} | |
return map; | |
} | |
function removeEvents(map,emitterSource) { | |
for(var k in map) { | |
emitterSource.removeListener(k,map[k]) | |
} | |
} | |
function pipe(pair, socket) { | |
pair.encrypted.pipe(socket); | |
socket.pipe(pair.encrypted); | |
pair.fd = socket.fd; | |
var cleartext = pair.cleartext; | |
cleartext.socket = socket; | |
cleartext.encrypted = pair.encrypted; | |
cleartext.authorized = false; | |
function onerror(e) { | |
if (cleartext._controlReleased) { | |
cleartext.emit('error', e); | |
} | |
} | |
var map = forwardEvents(["timeout","end","close"], socket, cleartext); | |
function onclose() { | |
socket.removeListener('error', onerror); | |
socket.removeListener('close', onclose); | |
removeEvents(map,socket) | |
} | |
socket.on('error', onerror); | |
socket.on('close', onclose); | |
return cleartext; | |
} | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
var common = require('../common'); | |
var assert = require('assert'); | |
var net = require('net'); | |
var crypto = require('crypto'); | |
var options = { | |
key: require('fs').readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), | |
cert: require('fs').readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') | |
}; | |
var credentials = crypto.createCredentials(options); | |
var reqCount = 0; | |
var server = net.createServer(function (c) { | |
c.on('data', function(chunk) { | |
if (chunk != 'STARTTLS') return c.write(chunk); | |
c.write('secure'); | |
c.removeAllListeners('data'); | |
var pair = starttls(c, credentials, function() { | |
pair.on('data', function(chunk) { | |
pair.write(chunk); | |
}); | |
pair.on('error', function(err) { | |
// open ssl error here by bad client | |
}); | |
}, true); | |
}); | |
}); | |
server.listen(common.PORT, function() { | |
startGood(); | |
setTimeout(startBad, 300); | |
}); | |
function startGood() { | |
var s = net.createConnection(common.PORT, function() { | |
s.write('STARTTLS'); | |
}); | |
s.on('data', function(chunk) { | |
s.removeAllListeners('data'); | |
var pair = starttls(s, crypto.createCredentials(), function() { | |
pair.write('client msg'); | |
}); | |
pair.on('data', function(chunk) { | |
pair.write('client msg'); | |
reqCount += 1; | |
}); | |
pair.on('error', function(err) { | |
throw new Error("Bad client produce error at good client communication"); // sometimes this error also throws | |
}); | |
}); | |
} | |
function startBad() { | |
assert.notEqual(reqCount, 0, 'this error means that test is incorrect'); // everithing allright here | |
var badC = net.createConnection(common.PORT, function() { | |
badC.write('STARTTLS'); | |
badC.on('data', function(chunk) { | |
badC.write('lets_go_secure'); | |
badC.removeAllListeners('data'); | |
reqCount = 0; | |
endTestSoon(); | |
}); | |
badC.on('error', function() {}); | |
}); | |
} | |
function endTestSoon() { | |
setTimeout(function() { | |
server.close(); | |
process.exit(1); | |
}, 300); | |
} | |
process.on('exit', function () { | |
assert.notEqual(reqCount, 0, 'reqCount should not be 0, because goodClient should increase it'); // reqCount should be bigger | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment