Browse Source

Added test and started tidy-up of node_crypto.cc

v0.7.4-release
Rhys Jones 15 years ago
parent
commit
e4a6b72ae3
  1. 10
      src/node_crypto.cc
  2. 173
      test/simple/test-http-tls.js

10
src/node_crypto.cc

@ -27,7 +27,6 @@ static int x509_verify_error;
static inline const char *errno_string(int errorno) { static inline const char *errno_string(int errorno) {
#define ERRNO_CASE(e) case e: return #e; #define ERRNO_CASE(e) case e: return #e;
switch (errorno) { switch (errorno) {
#ifdef EACCES #ifdef EACCES
ERRNO_CASE(EACCES); ERRNO_CASE(EACCES);
#endif #endif
@ -945,7 +944,8 @@ Handle<Value> SecureStream::Close(const Arguments& args) {
} }
void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest, int* md_hex_len) { void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest,
int* md_hex_len) {
*md_hex_len = (2*(md_len)); *md_hex_len = (2*(md_len));
*md_hexdigest = (char *) malloc(*md_hex_len + 1); *md_hexdigest = (char *) malloc(*md_hex_len + 1);
for (int i = 0; i < md_len; i++) { for (int i = 0; i < md_len; i++) {
@ -953,8 +953,10 @@ void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest, int* m
} }
} }
#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) : ((c) - 'a' + 10)) #define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \
void hex_decode(unsigned char *input, int length, char** buf64, int* buf64_len) { : ((c) - 'a' + 10))
void hex_decode(unsigned char *input, int length, char** buf64,
int* buf64_len) {
*buf64_len = (length/2); *buf64_len = (length/2);
*buf64 = (char*) malloc(length/2 + 1); *buf64 = (char*) malloc(length/2 + 1);
char *b = *buf64; char *b = *buf64;

173
test/simple/test-http-tls.js

@ -1,110 +1,135 @@
require("../common"); require("../common");
var http = require("http"); net = require("net");
var url = require("url"); http = require("http");
url = require("url");
qs = require("querystring");
var fs = require('fs'); var fs = require('fs');
var sys = require('sys');
HOST = "localhost"; var have_openssl;
var have_tls;
try { try {
var dummy_server = http.createServer(); var crypto = require('crypto');
var dummy_server = http.createServer(function(){});
dummy_server.setSecure(); dummy_server.setSecure();
have_tls=true; have_openssl=true;
} catch (e) { } catch (e) {
have_tls=false; have_openssl=false;
puts("Not compiled with TLS support."); puts("Not compiled with OPENSSL support.");
process.exit(); process.exit();
} }
var request_number = 0;
var responses_sent = 0; var requests_sent = 0;
var responses_recvd = 0; var server_response = "";
var body0 = ""; var client_got_eof = false;
var body1 = "";
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem"); var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem");
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem"); var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem");
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem"); var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem");
var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem});
var http_server=http.createServer(function (req, res) { var https_server = http.createServer(function (req, res) {
var verified = req.connection.verifyPeer(); res.id = request_number;
var peerDN = req.connection.getPeerCertificate("DNstring"); req.id = request_number++;
assert.equal(verified, 1);
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,"
+ "OU=Test TLS Certificate,CN=localhost");
if (responses_sent == 0) { var verified = c.verifyPeer();
var peerDN = JSON.stringify(c.getPeerCertificate());
assert.equal(verified, true);
assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones'
+ '/O=node.js/OU=Test TLS Certificate/CN=localhost",'
+ '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js'
+ '/OU=Test TLS Certificate/CN=localhost","valid_from":'
+ '"Nov 11 09:52:22 2009 GMT","valid_to":'
+ '"Nov 6 09:52:22 2029 GMT"}');
if (req.id == 0) {
assert.equal("GET", req.method); assert.equal("GET", req.method);
assert.equal("/hello", url.parse(req.url).pathname); assert.equal("/hello", url.parse(req.url).pathname);
assert.equal("world", qs.parse(url.parse(req.url).query).hello);
assert.equal("b==ar", qs.parse(url.parse(req.url).query).foo);
}
p(req.headers); if (req.id == 1) {
assert.equal(true, "accept" in req.headers); assert.equal("POST", req.method);
assert.equal("*/*", req.headers["accept"]); assert.equal("/quit", url.parse(req.url).pathname);
}
assert.equal(true, "foo" in req.headers); if (req.id == 2) {
assert.equal("bar", req.headers["foo"]); assert.equal("foo", req.headers['x-x']);
} }
if (responses_sent == 1) { if (req.id == 3) {
assert.equal("POST", req.method); assert.equal("bar", req.headers['x-x']);
assert.equal("/world", url.parse(req.url).pathname);
this.close(); this.close();
//puts("server closed");
} }
setTimeout(function () {
req.addListener('end', function () {
res.writeHead(200, {"Content-Type": "text/plain"}); res.writeHead(200, {"Content-Type": "text/plain"});
res.write("The path was " + url.parse(req.url).pathname); res.write(url.parse(req.url).pathname);
res.end(); res.end();
responses_sent += 1; }, 1);
}); });
https_server.setSecure(credentials);
https_server.listen(PORT);
//assert.equal("127.0.0.1", res.connection.remoteAddress); var c = net.createConnection(PORT);
c.setEncoding("utf8");
c.addListener("connect", function () {
c.setSecure(credentials);
}); });
http_server.setSecure("X509_PEM", caPem, 0, keyPem, certPem);
http_server.listen(PORT); c.addListener("secure", function () {
var verified = c.verifyPeer();
var client = http.createClient(PORT, HOST); var peerDN = JSON.stringify(c.getPeerCertificate());
client.setSecure("x509_PEM", caPem, 0, keyPem, certPem); //assert.equal(verified, 1);
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones'
req.addListener('response', function (res) { + '/O=node.js/OU=Test TLS Certificate/CN=localhost",'
var verified = res.connection.verifyPeer(); + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js'
var peerDN = res.connection.getPeerCertificate("DNstring"); + '/OU=Test TLS Certificate/CN=localhost","valid_from":'
assert.equal(verified, 1); + '"Nov 11 09:52:22 2009 GMT","valid_to":'
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js," + '"Nov 6 09:52:22 2029 GMT"}');
+ "OU=Test TLS Certificate,CN=localhost"); c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
assert.equal(200, res.statusCode); requests_sent += 1;
responses_recvd += 1;
res.setBodyEncoding("ascii");
res.addListener('data', function (chunk) { body0 += chunk; });
debug("Got /hello response");
}); });
req.end();
setTimeout(function () { c.addListener("data", function (chunk) {
req = client.request("POST", "/world"); server_response += chunk;
req.addListener('response', function (res) {
var verified = res.connection.verifyPeer(); if (requests_sent == 1) {
var peerDN = res.connection.getPeerCertificate("DNstring"); c.write("POST /quit HTTP/1.1\r\n\r\n");
assert.equal(verified, 1); requests_sent += 1;
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js," }
+ "OU=Test TLS Certificate,CN=localhost");
assert.equal(200, res.statusCode); if (requests_sent == 2) {
responses_recvd += 1; c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n"
res.setBodyEncoding("utf8"); +"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n");
res.addListener('data', function (chunk) { body1 += chunk; }); c.end();
debug("Got /world response"); assert.equal(c.readyState, "readOnly");
requests_sent += 2;
}
});
c.addListener("end", function () {
client_got_eof = true;
});
c.addListener("close", function () {
assert.equal(c.readyState, "closed");
}); });
req.end();
}, 1);
process.addListener("exit", function () { process.addListener("exit", function () {
debug("responses_recvd: " + responses_recvd); assert.equal(4, request_number);
assert.equal(2, responses_recvd); assert.equal(4, requests_sent);
debug("responses_sent: " + responses_sent); var hello = new RegExp("/hello");
assert.equal(2, responses_sent); assert.equal(true, hello.exec(server_response) != null);
assert.equal("The path was /hello", body0); var quit = new RegExp("/quit");
assert.equal("The path was /world", body1); assert.equal(true, quit.exec(server_response) != null);
});
assert.equal(true, client_got_eof);
});

Loading…
Cancel
Save