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. 12
      src/node_crypto.cc
  2. 181
      test/simple/test-http-tls.js

12
src/node_crypto.cc

@ -27,7 +27,6 @@ static int x509_verify_error;
static inline const char *errno_string(int errorno) {
#define ERRNO_CASE(e) case e: return #e;
switch (errorno) {
#ifdef EACCES
ERRNO_CASE(EACCES);
#endif
@ -945,16 +944,19 @@ 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_hexdigest = (char *) malloc(*md_hex_len + 1);
for(int i = 0; i < md_len; i++) {
for (int i = 0; i < md_len; i++) {
sprintf((char *)(*md_hexdigest + (i*2)), "%02x", md_value[i]);
}
}
#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) : ((c) - 'a' + 10))
void hex_decode(unsigned char *input, int length, char** buf64, int* buf64_len) {
#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \
: ((c) - 'a' + 10))
void hex_decode(unsigned char *input, int length, char** buf64,
int* buf64_len) {
*buf64_len = (length/2);
*buf64 = (char*) malloc(length/2 + 1);
char *b = *buf64;

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

@ -1,110 +1,135 @@
require("../common");
var http = require("http");
var url = require("url");
net = require("net");
http = require("http");
url = require("url");
qs = require("querystring");
var fs = require('fs');
var sys = require('sys');
HOST = "localhost";
var have_tls;
var have_openssl;
try {
var dummy_server = http.createServer();
var crypto = require('crypto');
var dummy_server = http.createServer(function(){});
dummy_server.setSecure();
have_tls=true;
have_openssl=true;
} catch (e) {
have_tls=false;
puts("Not compiled with TLS support.");
have_openssl=false;
puts("Not compiled with OPENSSL support.");
process.exit();
}
var responses_sent = 0;
var responses_recvd = 0;
var body0 = "";
var body1 = "";
var request_number = 0;
var requests_sent = 0;
var server_response = "";
var client_got_eof = false;
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem");
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem");
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem");
var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem});
var https_server = http.createServer(function (req, res) {
res.id = request_number;
req.id = request_number++;
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"}');
var http_server=http.createServer(function (req, res) {
var verified = req.connection.verifyPeer();
var peerDN = req.connection.getPeerCertificate("DNstring");
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) {
if (req.id == 0) {
assert.equal("GET", req.method);
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);
assert.equal(true, "accept" in req.headers);
assert.equal("*/*", req.headers["accept"]);
if (req.id == 1) {
assert.equal("POST", req.method);
assert.equal("/quit", url.parse(req.url).pathname);
}
assert.equal(true, "foo" in req.headers);
assert.equal("bar", req.headers["foo"]);
if (req.id == 2) {
assert.equal("foo", req.headers['x-x']);
}
if (responses_sent == 1) {
assert.equal("POST", req.method);
assert.equal("/world", url.parse(req.url).pathname);
if (req.id == 3) {
assert.equal("bar", req.headers['x-x']);
this.close();
//puts("server closed");
}
req.addListener('end', function () {
setTimeout(function () {
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();
responses_sent += 1;
});
}, 1);
//assert.equal("127.0.0.1", res.connection.remoteAddress);
});
http_server.setSecure("X509_PEM", caPem, 0, keyPem, certPem);
http_server.listen(PORT);
var client = http.createClient(PORT, HOST);
client.setSecure("x509_PEM", caPem, 0, keyPem, certPem);
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"});
req.addListener('response', function (res) {
var verified = res.connection.verifyPeer();
var peerDN = res.connection.getPeerCertificate("DNstring");
assert.equal(verified, 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);
responses_recvd += 1;
res.setBodyEncoding("ascii");
res.addListener('data', function (chunk) { body0 += chunk; });
debug("Got /hello response");
https_server.setSecure(credentials);
https_server.listen(PORT);
var c = net.createConnection(PORT);
c.setEncoding("utf8");
c.addListener("connect", function () {
c.setSecure(credentials);
});
req.end();
setTimeout(function () {
req = client.request("POST", "/world");
req.addListener('response', function (res) {
var verified = res.connection.verifyPeer();
var peerDN = res.connection.getPeerCertificate("DNstring");
assert.equal(verified, 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);
responses_recvd += 1;
res.setBodyEncoding("utf8");
res.addListener('data', function (chunk) { body1 += chunk; });
debug("Got /world response");
});
req.end();
}, 1);
process.addListener("exit", function () {
debug("responses_recvd: " + responses_recvd);
assert.equal(2, responses_recvd);
c.addListener("secure", function () {
var verified = c.verifyPeer();
var peerDN = JSON.stringify(c.getPeerCertificate());
//assert.equal(verified, 1);
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"}');
c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
requests_sent += 1;
});
debug("responses_sent: " + responses_sent);
assert.equal(2, responses_sent);
c.addListener("data", function (chunk) {
server_response += chunk;
if (requests_sent == 1) {
c.write("POST /quit HTTP/1.1\r\n\r\n");
requests_sent += 1;
}
if (requests_sent == 2) {
c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n"
+"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n");
c.end();
assert.equal(c.readyState, "readOnly");
requests_sent += 2;
}
assert.equal("The path was /hello", body0);
assert.equal("The path was /world", body1);
});
c.addListener("end", function () {
client_got_eof = true;
});
c.addListener("close", function () {
assert.equal(c.readyState, "closed");
});
process.addListener("exit", function () {
assert.equal(4, request_number);
assert.equal(4, requests_sent);
var hello = new RegExp("/hello");
assert.equal(true, hello.exec(server_response) != null);
var quit = new RegExp("/quit");
assert.equal(true, quit.exec(server_response) != null);
assert.equal(true, client_got_eof);
});

Loading…
Cancel
Save