mirror of https://github.com/BTCPrivate/copay.git
add some basic tests for WebRTC
This commit is contained in:
parent
2b2a807f7c
commit
98b864454f
|
@ -404,11 +404,13 @@ Network.prototype.send = function(copayerIds, payload, cb) {
|
||||||
payload.isBroadcast = 1;
|
payload.isBroadcast = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof copayerIds === 'string')
|
||||||
|
copayerIds = [copayerIds];
|
||||||
|
|
||||||
var sig;
|
var sig;
|
||||||
var payloadStr = JSON.stringify(payload);
|
var payloadStr = JSON.stringify(payload);
|
||||||
var payloadBuf = new Buffer(payloadStr);
|
var payloadBuf = new Buffer(payloadStr);
|
||||||
//var encPayload = this._encrypt(payloadStr);
|
|
||||||
if (Array.isArray(copayerIds)) {
|
|
||||||
var l = copayerIds.length;
|
var l = copayerIds.length;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
copayerIds.forEach(function(copayerId) {
|
copayerIds.forEach(function(copayerId) {
|
||||||
|
@ -418,9 +420,6 @@ Network.prototype.send = function(copayerIds, payload, cb) {
|
||||||
if (++i === l && typeof cb === 'function') cb();
|
if (++i === l && typeof cb === 'function') cb();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
else if (typeof copayerIds === 'string')
|
|
||||||
self._sendToOne(copayerIds, encPayload, sig, cb);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ describe('Network / WebRTC', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#WebRTC constructor', function() {
|
describe('#WebRTC constructor', function() {
|
||||||
|
|
||||||
it('should set reconnect attempts', function() {
|
it('should set reconnect attempts', function() {
|
||||||
var n = new WebRTC();
|
var n = new WebRTC();
|
||||||
n.reconnectAttempts.should.equal(3);
|
n.reconnectAttempts.should.equal(3);
|
||||||
|
@ -27,14 +28,100 @@ describe('Network / WebRTC', function() {
|
||||||
n.cleanUp.calledOnce.should.equal(true);
|
n.cleanUp.calledOnce.should.equal(true);
|
||||||
WebRTC.prototype.cleanUp = save;
|
WebRTC.prototype.cleanUp = save;
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#cleanUp', function() {
|
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() {
|
it('should set privkey to null', function() {
|
||||||
var n = new WebRTC();
|
var n = new WebRTC();
|
||||||
n.cleanUp();
|
n.cleanUp();
|
||||||
expect(n.privkey).to.equal(null);
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue