mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.3 KiB
124 lines
3.3 KiB
15 years ago
|
common = require("../common");
|
||
|
assert = common.assert
|
||
14 years ago
|
net = require("net");
|
||
15 years ago
|
fs=require("fs");
|
||
15 years ago
|
|
||
|
var tests_run = 0;
|
||
|
|
||
|
function tlsTest (port, host, caPem, keyPem, certPem) {
|
||
|
var N = 50;
|
||
|
var count = 0;
|
||
|
var sent_final_ping = false;
|
||
|
|
||
14 years ago
|
var server = net.createServer(function (socket) {
|
||
15 years ago
|
assert.equal(true, socket.remoteAddress !== null);
|
||
|
assert.equal(true, socket.remoteAddress !== undefined);
|
||
15 years ago
|
if (host === "127.0.0.1")
|
||
15 years ago
|
assert.equal(socket.remoteAddress, "127.0.0.1");
|
||
15 years ago
|
else if (host == null)
|
||
15 years ago
|
assert.equal(socket.remoteAddress, "127.0.0.1");
|
||
15 years ago
|
|
||
|
socket.setEncoding("utf8");
|
||
|
socket.setNoDelay();
|
||
|
socket.timeout = 0;
|
||
|
|
||
15 years ago
|
socket.addListener("data", function (data) {
|
||
15 years ago
|
var verified = socket.verifyPeer();
|
||
|
var peerDN = socket.getPeerCertificate("DNstring");
|
||
15 years ago
|
assert.equal(verified, 1);
|
||
|
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,"
|
||
15 years ago
|
+ "OU=Test TLS Certificate,CN=localhost");
|
||
15 years ago
|
console.log("server got: " + JSON.stringify(data));
|
||
15 years ago
|
assert.equal("open", socket.readyState);
|
||
|
assert.equal(true, count <= N);
|
||
15 years ago
|
if (/PING/.exec(data)) {
|
||
15 years ago
|
socket.write("PONG");
|
||
15 years ago
|
}
|
||
|
});
|
||
|
|
||
15 years ago
|
socket.addListener("end", function () {
|
||
15 years ago
|
assert.equal("writeOnly", socket.readyState);
|
||
15 years ago
|
socket.end();
|
||
15 years ago
|
});
|
||
|
|
||
|
socket.addListener("close", function (had_error) {
|
||
15 years ago
|
assert.equal(false, had_error);
|
||
|
assert.equal("closed", socket.readyState);
|
||
15 years ago
|
socket.server.close();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
server.setSecure('X509_PEM', caPem, 0, keyPem, certPem);
|
||
|
server.listen(port, host);
|
||
|
|
||
14 years ago
|
var client = net.createConnection(port, host);
|
||
15 years ago
|
|
||
|
client.setEncoding("utf8");
|
||
|
client.setSecure('X509_PEM', caPem, 0, keyPem, caPem);
|
||
|
|
||
|
client.addListener("connect", function () {
|
||
15 years ago
|
assert.equal("open", client.readyState);
|
||
15 years ago
|
var verified = client.verifyPeer();
|
||
|
var peerDN = client.getPeerCertificate("DNstring");
|
||
15 years ago
|
assert.equal(verified, 1);
|
||
|
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,"
|
||
15 years ago
|
+ "OU=Test TLS Certificate,CN=localhost");
|
||
15 years ago
|
client.write("PING");
|
||
15 years ago
|
});
|
||
|
|
||
15 years ago
|
client.addListener("data", function (data) {
|
||
15 years ago
|
assert.equal("PONG", data);
|
||
15 years ago
|
count += 1;
|
||
|
|
||
15 years ago
|
console.log("client got PONG");
|
||
15 years ago
|
|
||
|
if (sent_final_ping) {
|
||
15 years ago
|
assert.equal("readOnly", client.readyState);
|
||
15 years ago
|
return;
|
||
|
} else {
|
||
15 years ago
|
assert.equal("open", client.readyState);
|
||
15 years ago
|
}
|
||
|
|
||
|
if (count < N) {
|
||
15 years ago
|
client.write("PING");
|
||
15 years ago
|
} else {
|
||
|
sent_final_ping = true;
|
||
15 years ago
|
client.write("PING");
|
||
15 years ago
|
client.end();
|
||
15 years ago
|
}
|
||
|
});
|
||
|
|
||
|
client.addListener("close", function () {
|
||
15 years ago
|
assert.equal(N+1, count);
|
||
|
assert.equal(true, sent_final_ping);
|
||
15 years ago
|
tests_run += 1;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
var have_tls;
|
||
|
try {
|
||
14 years ago
|
var dummy_server = net.createServer();
|
||
15 years ago
|
dummy_server.setSecure();
|
||
|
have_tls=true;
|
||
|
} catch (e) {
|
||
|
have_tls=false;
|
||
15 years ago
|
}
|
||
15 years ago
|
|
||
|
if (have_tls) {
|
||
15 years ago
|
var caPem = fs.readFileSync(common.fixturesDir+"/test_ca.pem");
|
||
|
var certPem = fs.readFileSync(common.fixturesDir+"/test_cert.pem");
|
||
|
var keyPem = fs.readFileSync(common.fixturesDir+"/test_key.pem");
|
||
15 years ago
|
|
||
|
/* All are run at once, so run on different ports */
|
||
15 years ago
|
tlsTest(common.PORT, "localhost", caPem, keyPem, certPem);
|
||
|
tlsTest(common.PORT+1, null, caPem, keyPem, certPem);
|
||
15 years ago
|
|
||
|
process.addListener("exit", function () {
|
||
15 years ago
|
assert.equal(2, tests_run);
|
||
15 years ago
|
});
|
||
|
} else {
|
||
15 years ago
|
console.log("Not compiled with TLS support -- skipping test");
|
||
15 years ago
|
process.exit(0);
|
||
15 years ago
|
}
|