add test to crypto

This commit is contained in:
Matias Alejo Garcia 2014-12-02 10:48:57 -03:00
parent b3b0d7903e
commit b0cb2da3ec
4 changed files with 36 additions and 3 deletions

View File

@ -5,6 +5,7 @@ var preconditions = require('preconditions').singleton();
var inherits = require('inherits');
var events = require('events');
var async = require('async');
var request = require('request');
var bitcore = require('bitcore');
var BIP21 = bitcore.BIP21;

View File

@ -1,3 +1,5 @@
var preconditions = require('preconditions').singleton();
module.exports = {
request: function(options, callback) {
preconditions.checkArgument(_.isObject(options));
@ -25,9 +27,9 @@ module.exports = {
var req = options;
req.headers = req.headers || {};
req.body = req.body || req.data || {};
req.body = req.body || req.data || '';
var xhr = new XMLHttpRequest();
var xhr = options.xhr || new XMLHttpRequest();
xhr.open(method, url, true);
Object.keys(req.headers).forEach(function(key) {
@ -51,6 +53,7 @@ module.exports = {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};

View File

@ -31,7 +31,7 @@
},
"scripts": {
"start": "node server.js",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter spec test",
"coverage": "./node_modules/.bin/istanbul cover -x lib/sjcl.js ./node_modules/.bin/_mocha -- --reporter spec test",
"test": "sh test/run.sh",
"dist": "node shell/scripts/dist.js",
"shell": "node shell/scripts/launch.js",

View File

@ -14,6 +14,19 @@ describe('crypto utils', function() {
decrypted.should.equal(message);
});
it('should decrypt what it encrypts (JSON)', function() {
var key = 'My secret key';
var message = {'hola': 'picho'};
var encrypted = cryptoUtils.encrypt(key, message);
var decrypted = cryptoUtils.decrypt(key, encrypted);
JSON.parse(decrypted).should.deep.equal(message);
});
it('should return null if the provided key cant decrypt', function() {
var key = 'My secret key';
var message = 'My secret message';
@ -22,6 +35,17 @@ describe('crypto utils', function() {
assert(decrypted === null);
});
it('should sign a message', function() {
var key = 'My secret key';
var message = 'My secret message';
var signature = cryptoUtils.hmac(key, message);
signature.should.be.equal('6tpegxYl/Eig9k1Lla8b8G8OcdtOxyNbDsdyic1Yzh4=');
});
var tests = [
{
@ -55,6 +79,11 @@ describe('crypto utils', function() {
phrase.should.equal(expected);
});
});
it('should generate a passphrase using default salt/iter', function() {
var phrase = cryptoUtils.kdf('Pwd123!@#$%^&*(){}[]\|/?.>,<=+-_`~åéþ䲤þçæ¶');
var expected = 'ml+mMtjgcvL2pdfDwQqW2qONRNjZ3YD8KnGeV3aFjyOoM0ByOmoREw9zBvowC/ZXsfrezbRXX/W/XIzKOqdrXA==';
phrase.should.equal(expected);
});
});