Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier to set temp dir, and all temp files are unlinked after use #11

Merged
merged 1 commit into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var spawn = require("child_process").spawn,
pathlib = require("path"),
fs = require("fs"),
crypto = require("crypto"),
tempDir = (os.tmpdir || os.tmpDir) && (os.tmpdir || os.tmpDir)() || "/tmp";
tempDir = process.env.PEMJS_TMPDIR || (os.tmpdir || os.tmpDir) && (os.tmpdir || os.tmpDir)() || "/tmp";

module.exports.createPrivateKey = createPrivateKey;
module.exports.createCSR =createCSR;
Expand Down Expand Up @@ -478,6 +478,7 @@ function spawnOpenSSL(params, callback) {

function spawnWrapper(params, tmpfiles, callback){
var files = [];
var toUnlink = [];

if(tmpfiles){
tmpfiles = [].concat(tmpfiles || []);
Expand All @@ -502,6 +503,7 @@ function spawnWrapper(params, tmpfiles, callback){
}

fs.writeFile(file.path, file.contents, function(err, bytes){
toUnlink.push(file.path);
processFiles();
});
}
Expand All @@ -510,8 +512,8 @@ function spawnWrapper(params, tmpfiles, callback){
spawnOpenSSL(params, function(err, code, stdout, stderr) {
var start, end;

files.forEach(function(file){
fs.unlink(file.path);
toUnlink.forEach(function(filePath){
fs.unlink(filePath);
})

callback(err, code, stdout, stderr);
Expand Down
34 changes: 30 additions & 4 deletions test/pem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var pem = require(".."),
testCase = require('nodeunit').testCase;
process.env.PEMJS_TMPDIR = "./tmp";
var fs = require("fs");
try {
fs.mkdirSync("./tmp");
} catch (e) {}

exports["General Tests"] = {

Expand All @@ -11,6 +16,7 @@ exports["General Tests"] = {
test.ok(key.match(/^\n*\-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\n/));
test.ok(key.match(/\n\-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\n*$/));
test.ok(key.trim().length > 850 && key.trim().length < 900);
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
},
Expand All @@ -23,6 +29,7 @@ exports["General Tests"] = {
test.ok(key.match(/^\n*\-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\n/));
test.ok(key.match(/\n\-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\n*$/));
test.ok(key.trim().length > 1650 && key.trim().length < 1700);
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
},
Expand All @@ -36,7 +43,7 @@ exports["General Tests"] = {
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));

test.ok(data && data.clientKey);

test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
},
Expand All @@ -55,7 +62,7 @@ exports["General Tests"] = {
test.equal(data && data.clientKey, key);

test.ok(data && data.clientKey);

test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});

Expand All @@ -75,7 +82,7 @@ exports["General Tests"] = {
test.ok(data && data.clientKey);
test.ok(data && data.serviceKey);
test.ok(data && data.csr);

test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
},
Expand All @@ -93,7 +100,7 @@ exports["General Tests"] = {
test.ok(data && data.clientKey);
test.ok(data && data.serviceKey);
test.ok(data && data.csr);

test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
},
Expand All @@ -102,6 +109,7 @@ exports["General Tests"] = {
pem.createCSR(function(error, data){
var csr = (data && data.csr || "").toString();
test.ifError(error);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.readCertificateInfo(csr, function(error, data){
test.ifError(error);
Expand All @@ -113,6 +121,7 @@ exports["General Tests"] = {
organizationUnit: '',
commonName: 'localhost',
emailAddress: '' });
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
});
Expand All @@ -129,10 +138,12 @@ exports["General Tests"] = {
pem.createCSR(Object.create(certInfo), function(error, data){
var csr = (data && data.csr || "").toString();
test.ifError(error);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.readCertificateInfo(csr, function(error, data){
test.ifError(error);
test.deepEqual(data, certInfo);
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
});
Expand All @@ -142,6 +153,7 @@ exports["General Tests"] = {
pem.createCertificate(function(error, data){
var certificate = (data && data.certificate || "").toString();
test.ifError(error);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.readCertificateInfo(certificate, function(error, data){
test.ifError(error);
Expand All @@ -155,6 +167,7 @@ exports["General Tests"] = {
organizationUnit: '',
commonName: 'localhost',
emailAddress: '' });
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
});
Expand All @@ -171,12 +184,14 @@ exports["General Tests"] = {
pem.createCertificate(Object.create(certInfo), function(error, data){
var certificate = (data && data.certificate || "").toString();
test.ifError(error);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.readCertificateInfo(certificate, function(error, data){
test.ifError(error);
if(data.validity)
delete data.validity;
test.deepEqual(data, certInfo);
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});
});
Expand All @@ -187,6 +202,7 @@ exports["General Tests"] = {
var key = (data && data.key || "").toString();
test.ifError(error);
test.ok(key);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.getPublicKey(key, function(error, data){
var pubkey = (data && data.publicKey || "").toString();
Expand All @@ -195,6 +211,7 @@ exports["General Tests"] = {

test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
test.ok(fs.readdirSync("./tmp").length == 0);

test.done();
});
Expand All @@ -207,6 +224,7 @@ exports["General Tests"] = {
var key = (data && data.clientKey || "").toString();
test.ifError(error);
test.ok(key);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.getPublicKey(key, function(error, data){
var pubkey = (data && data.publicKey || "").toString();
Expand All @@ -215,6 +233,7 @@ exports["General Tests"] = {

test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
test.ok(fs.readdirSync("./tmp").length == 0);

test.done();
});
Expand All @@ -227,6 +246,7 @@ exports["General Tests"] = {
var key = (data && data.clientKey || "").toString();
test.ifError(error);
test.ok(key);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.getPublicKey(key, function(error, data){
var pubkey = (data && data.publicKey || "").toString();
Expand All @@ -235,6 +255,7 @@ exports["General Tests"] = {

test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
test.ok(fs.readdirSync("./tmp").length == 0);

test.done();
});
Expand All @@ -247,12 +268,14 @@ exports["General Tests"] = {
var certificate = (data && data.certificate || "").toString();
test.ifError(error);
test.ok(certificate);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.getFingerprint(certificate, function(error, data){
var fingerprint = (data && data.fingerprint || "").toString();
test.ifError(error);
test.ok(fingerprint);
test.ok(fingerprint.match(/^[0-9A-F]{2}(:[0-9A-F]{2}){19}$/));
test.ok(fs.readdirSync("./tmp").length == 0);

test.done();
});
Expand All @@ -266,18 +289,21 @@ exports["General Tests"] = {
var key = (data && data.clientKey || "").toString();
test.ifError(error);
test.ok(certificate);
test.ok(fs.readdirSync("./tmp").length == 0);

pem.getModulus(certificate, function(error, data){
var certmodulus = (data && data.modulus || "").toString();
test.ifError(error);
test.ok(certmodulus);
test.ok(certmodulus.match(/^[0-9A-F]*$/));
test.ok(fs.readdirSync("./tmp").length == 0);
pem.getModulus(certificate, function(error, data){
var keymodulus = (data && data.modulus || "").toString();
test.ifError(error);
test.ok(keymodulus);
test.ok(keymodulus.match(/^[0-9A-F]*$/));
test.ok(keymodulus == certmodulus);
test.ok(fs.readdirSync("./tmp").length == 0);
test.done();
});

Expand Down