Fix test-securepair-server

This commit is contained in:
Ryan Dahl 2010-11-23 18:30:52 -08:00
parent a2b3c865c9
commit a6f6532dfb
4 changed files with 42 additions and 27 deletions

View File

@ -145,17 +145,18 @@ function setImplmentationMethods (self) {
};
function onReadable (readable, writeable) {
function onReadable (readable, writable) {
assert(this.socket);
var socket = this.socket;
socket._onReadable();
}
function onWritable (readable, writeable) {
function onWritable (readable, writable) {
assert(this.socket);
var socket = this.socket;
if (socket._connecting) {
assert(socket.writable);
socket._onConnect();
} else {
socket._onWritable();
@ -472,6 +473,7 @@ Stream.prototype._onConnect = function () {
// connection established
this._connecting = false;
this.resume();
assert(this.writable);
this.readable = this.writable = true;
try {
this.emit('connect');
@ -690,9 +692,10 @@ Stream.prototype._shutdown = function () {
if (!this.writable) {
throw new Error('The connection is not writable');
} else {
// readable and writable
this.writable = false;
if (this.readable) {
// readable and writable
this.writable = false;
try {
this._shutdownImpl();

View File

@ -66,11 +66,15 @@ function SecurePair(credentials, isServer) {
/* Acts as a r/w stream to the cleartext side of the stream. */
this.cleartext = new stream.Stream();
this.cleartext.readable = true;
this.cleartext.writable = true;
/* Acts as a r/w stream to the encrypted side of the stream. */
this.encrypted = new stream.Stream();
this.encrypted.readable = true;
this.encrypted.writable = true;
this.cleartext.write = function (data) {
if (typeof data == 'string') data = Buffer(data);
debug('clearIn data');
self._clearInPending.push(data);
self._cycle();
@ -104,6 +108,7 @@ function SecurePair(credentials, isServer) {
};
this.encrypted.pause = function () {
if (typeof data == 'string') data = Buffer(data);
debug('pause encrypted');
self._encryptedWriteState = false;
};

View File

@ -11,7 +11,9 @@ Stream.prototype.pipe = function (dest, options) {
var source = this;
function ondata (chunk) {
if (false === dest.write(chunk)) source.pause();
if (dest.writable) {
if (false === dest.write(chunk)) source.pause();
}
}
source.on("data", ondata);
@ -36,9 +38,9 @@ Stream.prototype.pipe = function (dest, options) {
}
dest.on('close', function () {
dest.removeListener('data', ondata);
source.removeListener('data', ondata);
dest.removeListener('drain', ondrain);
dest.removeListener('end', onend);
source.removeListener('end', onend);
});

View File

@ -12,50 +12,51 @@ var key = fs.readFileSync(join(common.fixturesDir, "agent.key")).toString();
var cert = fs.readFileSync(join(common.fixturesDir, "agent.crt")).toString();
function log (a) {
console.log('***server*** ' + a);
console.error('***server*** ' + a);
}
var server = net.createServer(function (socket) {
connections++;
log('connection');
log('connection fd=' + socket.fd);
var sslcontext = crypto.createCredentials({key: key, cert: cert});
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
var spair = crypto.createPair(sslcontext, true);
var pair = crypto.createPair(sslcontext, true);
spair.encrypted.pipe(socket);
socket.pipe(spair.encrypted);
assert.ok(pair.encrypted.writable);
assert.ok(pair.cleartext.writable);
var clear = spair.cleartext;
pair.encrypted.pipe(socket);
socket.pipe(pair.encrypted);
log('i set it secure');
spair.on('secure', function () {
pair.on('secure', function () {
log('connected+secure!');
clear.write(new Buffer('hello\r\n'));
log(spair.getPeerCertificate());
log(spair.getCipher());
pair.cleartext.write('hello\r\n');
log(pair.getPeerCertificate());
log(pair.getCipher());
});
clear.on('data', function (data) {
log('read %d bytes', data.length);
clear.write(data);
pair.cleartext.on('data', function (data) {
log('read bytes ' + data.length);
pair.cleartext.write(data);
});
socket.on('end', function (err) {
log('all done: '+ err);
clear.write(new Buffer('goodbye\r\n'));
clear.end();
socket.on('end', function () {
log('socket end');
pair.cleartext.write('goodbye\r\n');
pair.cleartext.end();
});
clear.on('error', function(err) {
pair.cleartext.on('error', function(err) {
log('got error: ');
log(err);
log(err.stack);
socket.destroy();
});
spair.encrypted.on('error', function(err) {
pair.encrypted.on('error', function(err) {
log('encrypted error: ');
log(err);
log(err.stack);
@ -69,7 +70,11 @@ var server = net.createServer(function (socket) {
socket.destroy();
});
spair.on('error', function(err) {
socket.on('close', function(err) {
log('socket closed');
});
pair.on('error', function(err) {
log('secure error: ');
log(err);
log(err.stack);