add some basic tests for WebRTC

This commit is contained in:
Ryan X. Charles 2014-06-16 15:35:54 -07:00
parent 2b2a807f7c
commit 98b864454f
2 changed files with 99 additions and 13 deletions

View File

@ -404,23 +404,22 @@ Network.prototype.send = function(copayerIds, payload, cb) {
payload.isBroadcast = 1;
}
if (typeof copayerIds === 'string')
copayerIds = [copayerIds];
var sig;
var payloadStr = JSON.stringify(payload);
var payloadBuf = new Buffer(payloadStr);
//var encPayload = this._encrypt(payloadStr);
if (Array.isArray(copayerIds)) {
var l = copayerIds.length;
var i = 0;
copayerIds.forEach(function(copayerId) {
var copayerIdBuf = new Buffer(copayerId, 'hex');
var encPayload = self._encrypt(copayerIdBuf, payloadBuf);
self._sendToOne(copayerId, encPayload, sig, function () {
if (++i === l && typeof cb === 'function') cb();
});
var l = copayerIds.length;
var i = 0;
copayerIds.forEach(function(copayerId) {
var copayerIdBuf = new Buffer(copayerId, 'hex');
var encPayload = self._encrypt(copayerIdBuf, payloadBuf);
self._sendToOne(copayerId, encPayload, sig, function () {
if (++i === l && typeof cb === 'function') cb();
});
}
else if (typeof copayerIds === 'string')
self._sendToOne(copayerIds, encPayload, sig, cb);
});
};

View File

@ -15,6 +15,7 @@ describe('Network / WebRTC', function() {
});
describe('#WebRTC constructor', function() {
it('should set reconnect attempts', function() {
var n = new WebRTC();
n.reconnectAttempts.should.equal(3);
@ -27,14 +28,100 @@ describe('Network / WebRTC', function() {
n.cleanUp.calledOnce.should.equal(true);
WebRTC.prototype.cleanUp = save;
});
});
describe('#cleanUp', function() {
it('should not set netKey', function() {
var n = new WebRTC();
(n.netKey === undefined).should.equal(true);
});
it('should set privkey to null', function() {
var n = new WebRTC();
n.cleanUp();
expect(n.privkey).to.equal(null);
});
});
describe('#_encrypt', function() {
it('should encrypt data successfully', function() {
var n = new WebRTC();
var data = new bitcore.Buffer('my data to encrypt');
var privkeystr = new bitcore.Buffer('test privkey');
var privkey = bitcore.util.sha256(privkeystr);
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var encrypted = n._encrypt(key.public, data);
encrypted.length.should.not.equal(0);
encrypted.length.should.equal(145);
});
});
describe('#_decrypt', function() {
it('should decrypt that which was encrypted', function() {
var n = new WebRTC();
var data = new bitcore.Buffer('my data to encrypt');
var privkeystr = new bitcore.Buffer('test privkey');
var privkey = bitcore.util.sha256(privkeystr);
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var encrypted = n._encrypt(key.public, data);
var decrypted = n._decrypt(key.private, encrypted);
encrypted.length.should.not.equal(0);
decrypted.toString().should.equal('my data to encrypt');
});
});
describe('#send', function() {
it('should call _sendToOne for a copayer', function(done) {
var n = new WebRTC();
var data = new bitcore.Buffer('my data to send');
var privkeystr = new bitcore.Buffer('test privkey');
var privkey = bitcore.util.sha256(privkeystr);
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var copayerId = key.public.toString('hex');
n._sendToOne = function(a1, a2, a3, cb) {cb();};
var sig = undefined;
n.send(copayerId, data, function() {
done();
});
});
it('should call _sendToOne for a list of copayers', function(done) {
var n = new WebRTC();
var data = new bitcore.Buffer('my data to send');
var privkeystr = new bitcore.Buffer('test privkey');
var privkey = bitcore.util.sha256(privkeystr);
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var copayerIds = [key.public.toString('hex')];
n._sendToOne = function(a1, a2, a3, cb) {cb();};
var sig = undefined;
n.send(copayerIds, data, function() {
done();
});
});
});
});