Merge pull request #175 from BeaudanBrown/p2p-tests

P2p tests
pull/183/head
sachaaaaa 6 years ago committed by GitHub
commit 31e5013207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -219,6 +219,13 @@
return; return;
} }
first = false; first = false;
window.lokiP2pAPI = new window.LokiP2pAPI(
textsecure.storage.user.getNumber()
);
window.lokiP2pAPI.on('pingContact', pubKey => {
const forceP2p = true;
window.libloki.api.sendOnlineBroadcastMessage(pubKey, forceP2p);
});
// These make key operations available to IPC handlers created in preload.js // These make key operations available to IPC handlers created in preload.js
window.Events = { window.Events = {

@ -1,16 +1,23 @@
/* global setTimeout, clearTimeout, window */ /* global setTimeout, clearTimeout */
const EventEmitter = require('events'); const EventEmitter = require('events');
class LokiP2pAPI extends EventEmitter { class LokiP2pAPI extends EventEmitter {
constructor() { constructor(ourKey) {
super(); super();
this.contactP2pDetails = {}; this.contactP2pDetails = {};
this.ourKey = ourKey;
} }
updateContactP2pDetails(pubKey, address, port, fromP2p = false) { reset() {
Object.keys(this.contactP2pDetails).forEach(key => {
clearTimeout(this.contactP2pDetails[key].pingTimer);
delete this.contactP2pDetails[key];
});
}
updateContactP2pDetails(pubKey, address, port, isOnline = false) {
// Stagger the timers so the friends don't ping each other at the same time // Stagger the timers so the friends don't ping each other at the same time
this.ourKey = this.ourKey || window.textsecure.storage.user.getNumber();
const timerDuration = const timerDuration =
pubKey < this.ourKey pubKey < this.ourKey
? 60 * 1000 // 1 minute ? 60 * 1000 // 1 minute
@ -28,7 +35,7 @@ class LokiP2pAPI extends EventEmitter {
pingTimer: null, pingTimer: null,
}; };
if (fromP2p) { if (isOnline) {
this.setContactOnline(pubKey); this.setContactOnline(pubKey);
return; return;
} }
@ -73,7 +80,7 @@ class LokiP2pAPI extends EventEmitter {
if (!this.contactP2pDetails[pubKey]) { if (!this.contactP2pDetails[pubKey]) {
return; return;
} }
window.libloki.api.sendOnlineBroadcastMessage(pubKey, true); this.emit('pingContact', pubKey);
} }
} }

@ -0,0 +1,92 @@
const { assert } = require('chai');
const LokiP2pAPI = require('../../../js/modules/loki_p2p_api');
describe('LocalLokiServer', () => {
const usedKey = 'aPubKey';
const usedAddress = 'anAddress';
const usedPort = 'aPort';
beforeEach(() => {
this.lokiP2pAPI = new LokiP2pAPI();
});
afterEach(() => {
this.lokiP2pAPI.reset();
});
it("Should not emit a pingContact event if that contact doesn't exits", () => {
this.lokiP2pAPI.on('pingContact', () => {
assert.fail();
});
this.lokiP2pAPI.pingContact('not stored');
});
it('Should emit an online event if the contact is online', done => {
this.lokiP2pAPI.on('online', pubKey => {
assert.strictEqual(pubKey, usedKey);
done();
});
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
true
);
}).timeout(1000);
it("Should send a pingContact event if the contact isn't online", done => {
this.lokiP2pAPI.on('pingContact', pubKey => {
assert.strictEqual(pubKey, usedKey);
done();
});
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
false
);
}).timeout(1000);
it('Should store a contacts p2p details', () => {
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
true
);
const p2pDetails = this.lokiP2pAPI.getContactP2pDetails(usedKey);
assert.strictEqual(usedAddress, p2pDetails.address);
assert.strictEqual(usedPort, p2pDetails.port);
});
it('Should say if a contact is online', () => {
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
true
);
assert.isTrue(this.lokiP2pAPI.isOnline(usedKey));
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
false
);
assert.isFalse(this.lokiP2pAPI.isOnline(usedKey));
});
it('Should set a contact as offline', () => {
this.lokiP2pAPI.updateContactP2pDetails(
usedKey,
usedAddress,
usedPort,
true
);
let p2pDetails = this.lokiP2pAPI.getContactP2pDetails(usedKey);
assert.isTrue(p2pDetails.isOnline);
p2pDetails = this.lokiP2pAPI.getContactP2pDetails(usedKey);
this.lokiP2pAPI.setContactOffline(usedKey);
assert.isFalse(p2pDetails.isOnline);
});
});

@ -33,6 +33,7 @@
"test-electron": "yarn grunt test", "test-electron": "yarn grunt test",
"test-node": "mocha --recursive test/app test/modules ts/test libloki/test/node", "test-node": "mocha --recursive test/app test/modules ts/test libloki/test/node",
"test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test libloki/test/node", "test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test libloki/test/node",
"test-node-coverage-html": "nyc --reporter=lcov --reporter=html mocha --recursive test/app test/modules ts/test libloki/test/node",
"eslint": "eslint .", "eslint": "eslint .",
"lint": "yarn format --list-different && yarn lint-windows", "lint": "yarn format --list-different && yarn lint-windows",
"lint-windows": "yarn eslint && yarn tslint", "lint-windows": "yarn eslint && yarn tslint",

@ -276,9 +276,7 @@ window.LokiSnodeAPI = new LokiSnodeAPI({
swarmServerPort: config.swarmServerPort, swarmServerPort: config.swarmServerPort,
}); });
const LokiP2pAPI = require('./js/modules/loki_p2p_api'); window.LokiP2pAPI = require('./js/modules/loki_p2p_api');
window.lokiP2pAPI = new LokiP2pAPI();
const LokiMessageAPI = require('./js/modules/loki_message_api'); const LokiMessageAPI = require('./js/modules/loki_message_api');

@ -79,3 +79,5 @@ before(async () => {
window.clearDatabase = async () => { window.clearDatabase = async () => {
await window.Signal.Data.removeAll(); await window.Signal.Data.removeAll();
}; };
window.lokiP2pAPI = new window.LokiP2pAPI('ourKey');

Loading…
Cancel
Save