fix test 7, removed a test that made no sense

This commit is contained in:
Manuel Araoz 2014-08-19 13:57:34 -04:00
parent df3aafe1a9
commit ed7916f1be
2 changed files with 61 additions and 78 deletions

View File

@ -119,8 +119,8 @@ Network.prototype._addConnectedCopayer = function(copayerId) {
};
Network.prototype.getKey = function() {
preconditions.checkState(this.privkey || this.key);
if (!this.key) {
preconditions.checkState(this.privkey);
var key = new bitcore.Key();
key.private = new Buffer(this.privkey, 'hex');
key.regenerateSync();
@ -286,42 +286,40 @@ Network.prototype.peerFromCopayer = function(hex) {
};
Network.prototype.start = function(opts, openCallback) {
opts = opts || {};
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.privkey);
preconditions.checkArgument(opts.copayerId);
preconditions.checkArgument(opts.lastTimestamp);
preconditions.checkState(this.connectedPeers && this.connectedPeers.length === 0);
if (this.started) return openCallback();
if (!this.privkey)
this.privkey = opts.privkey;
this.privkey = opts.privkey;
var pubkey = this.getKey().public.toString('hex');
this.setCopayerId(opts.copayerId);
this.maxPeers = opts.maxPeers || this.maxPeers;
if (opts.token)
this.opts.token = opts.token;
if (!this.copayerId)
this.setCopayerId(opts.copayerId);
if (this.connectedPeers.length > 0) {
// already connected
return;
}
if (this.socket) {
this.socket.destroy();
this.socket.removeAllListeners();
}
var hostPort = this.host + ':' + this.port;
this.socket = io.connect(hostPort, {
reconnection: true,
});
this.socket = this.createSocket(this.host, this.port);
this._setupConnectionHandlers(openCallback);
var pubkey = this.getKey().public.toString('hex');
this.socket.emit('subscribe', pubkey);
this.socket.emit('sync', opts.lastTimestamp);
this.started = true;
};
Network.prototype.createSocket = function(host, port) {
var hostPort = host + ':' + port;
return io.connect(hostPort, {
reconnection: true,
});
};
Network.prototype.getOnlinePeerIDs = function() {
return this.connectedPeers;
};

View File

@ -6,40 +6,46 @@ var expect = chai.expect;
var sinon = sinon || require('sinon');
var bitcore = bitcore || require('bitcore');
var Async = require('../js/models/network/Async');
var EventEmitter = require('events').EventEmitter;
describe('Network / Async', function() {
it('should create an instance', function() {
var createN = function() {
var n = new Async();
var fakeSocket = new EventEmitter();
n.createSocket = function() {
return fakeSocket;
};
var opts = {
copayerId: '03b51d01d798522cf61211b4dfcdd6db219ee33cf166e1cb7f43d836ab00ccddee',
privkey: '31701118abde096d166607115ed00ce74a2231f68f43144406c863f5ebf06c32',
lastTimestamp: 1,
};
n.start(opts);
return n;
};
it('should create an instance', function() {
var n = createN();
should.exist(n);
});
describe('#Async constructor', function() {
it('should call cleanUp', function() {
var save = Async.prototype.cleanUp;
Async.prototype.cleanUp = sinon.spy();
var n = new Async();
n.cleanUp.calledOnce.should.equal(true);
Async.prototype.cleanUp = save;
});
});
describe('#cleanUp', function() {
it('should not set netKey', function() {
var n = new Async();
var n = createN();
(n.netKey === undefined).should.equal(true);
});
it('should set privkey to null', function() {
var n = new Async();
var n = createN();
n.cleanUp();
expect(n.privkey).to.equal(null);
});
it('should remove handlers', function() {
var n = new Async();
var n = createN();
var save = Async.prototype.removeAllListeners;
var spy = Async.prototype.removeAllListeners = sinon.spy();
n.cleanUp();
@ -51,41 +57,27 @@ describe('Network / Async', function() {
describe('#send', function() {
it('should call _sendToOne for a copayer', function(done) {
var n = new Async();
var n = createN();
n.privkey = bitcore.util.sha256('test');
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, cb) {
var copayerId = '03b51d01d798522cf61211b4dfcdd6db219ee33cf166e1cb7f43d836abc5c18b23';
n._sendToOne = function(a, b, cb) {
cb();
};
var opts = {};
n.send(copayerId, data, function() {
done();
});
n.send(copayerId, data, done);
});
it('should call _sendToOne with encrypted data for a copayer', function(done) {
var n = new Async();
var n = createN();
n.privkey = bitcore.util.sha256('test');
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');
var copayerId = '03b51d01d798522cf61211b4dfcdd6db219ee33cf166e1cb7f43d836abc5c18b23';
n._sendToOne = function(a1, enc, cb) {
var encPayload = JSON.parse(enc.toString());
encPayload.sig.length.should.be.greaterThan(0);
@ -99,18 +91,11 @@ describe('Network / Async', function() {
});
it('should call _sendToOne for a list of copayers', function(done) {
var n = new Async();
var n = createN();
n.privkey = bitcore.util.sha256('test');
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')];
var copayerIds = ['03b51d01d798522cf61211b4dfcdd6db219ee33cf166e1cb7f43d836abc5c18b23'];
n._sendToOne = function(a1, a2, cb) {
cb();
};
@ -140,7 +125,7 @@ describe('Network / Async', function() {
key3.regenerateSync();
it('should not reject data sent from a peer with hijacked pubkey', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
var message = {
@ -164,7 +149,7 @@ describe('Network / Async', function() {
});
it('should reject data sent from a peer with hijacked pubkey', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
var message = {
@ -190,7 +175,7 @@ describe('Network / Async', function() {
});
it('should not reject data sent from a peer with no previously set nonce but who is setting one now', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
//n.networkNonces = {};
//n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('0000000000000001', 'hex'); //previously used nonce
@ -220,7 +205,7 @@ describe('Network / Async', function() {
});
it('should not reject data sent from a peer with a really big new nonce', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
n.networkNonces = {};
n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000001', 'hex'); //previously used nonce
@ -249,7 +234,7 @@ describe('Network / Async', function() {
});
it('should not reject data sent from a peer with a really big new nonce', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
n.networkNonces = {};
n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000001', 'hex'); //previously used nonce
@ -278,7 +263,7 @@ describe('Network / Async', function() {
});
it('should reject data sent from a peer with an outdated nonce', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
n.networkNonces = {};
n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('0000000000000002', 'hex'); //previously used nonce
@ -307,7 +292,7 @@ describe('Network / Async', function() {
});
it('should reject data sent from a peer with a really big outdated nonce', function() {
var n = new Async();
var n = createN();
n.privkey = key2.private.toString('hex');
n.networkNonces = {};
n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000002', 'hex'); //previously used nonce
@ -341,7 +326,7 @@ describe('Network / Async', function() {
it('should set a nonce from a hex value', function() {
var hex = '0000000000000000';
var n = new Async();
var n = createN();
n.setHexNonce(hex);
n.getHexNonce().should.equal(hex);
n.networkNonce.toString('hex').should.equal(hex);
@ -353,7 +338,7 @@ describe('Network / Async', function() {
it('should set a nonce from a hex value', function() {
var hex = '0000000000000000';
var n = new Async();
var n = createN();
n.setHexNonces({
fakeid: hex
});
@ -366,7 +351,7 @@ describe('Network / Async', function() {
it('should get a nonce hex value', function() {
var hex = '0000000000000000';
var n = new Async();
var n = createN();
n.setHexNonce(hex);
n.getHexNonce().should.equal(hex);
});
@ -377,7 +362,7 @@ describe('Network / Async', function() {
it('should get a nonce from a hex value', function() {
var hex = '0000000000000000';
var n = new Async();
var n = createN();
n.setHexNonces({
fakeid: hex
});
@ -389,14 +374,14 @@ describe('Network / Async', function() {
describe('#iterateNonce', function() {
it('should set a nonce not already set', function() {
var n = new Async();
var n = createN();
n.iterateNonce();
n.networkNonce.slice(4, 8).toString('hex').should.equal('00000001');
n.networkNonce.slice(0, 4).toString('hex').should.not.equal('00000000');
});
it('called twice should increment', function() {
var n = new Async();
var n = createN();
n.iterateNonce();
n.networkNonce.slice(4, 8).toString('hex').should.equal('00000001');
n.iterateNonce();
@ -404,7 +389,7 @@ describe('Network / Async', function() {
});
it('should set the first byte to the most significant "now" digit', function() {
var n = new Async();
var n = createN();
n.iterateNonce();
var buf = new Buffer(4);
buf.writeUInt32BE(Math.floor(Date.now() / 1000), 0);