2017-12-04 16:11:14 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const { expect } = require('chai');
|
2017-12-21 10:26:01 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const rimraf = require('rimraf');
|
2017-12-04 16:11:14 +00:00
|
|
|
const Web3 = require('web3');
|
|
|
|
|
|
|
|
describe('Whisper MailServer', () => {
|
|
|
|
const topic = `0x${crypto.randomBytes(4).toString('hex')}`;
|
|
|
|
const sharedSymKey = '0x6c32583c0bc13ef90a10b36ed6f66baaa0e537d0677619993bfd72c819cba6f3';
|
2017-12-21 10:26:01 +00:00
|
|
|
const mailServerEnode = 'enode://b7e65e1bedc2499ee6cbd806945af5e7df0e59e4070c96821570bd581473eade24a489f5ec95d060c0db118c879403ab88d827d3766978f28708989d35474f87@127.0.0.1:8549';
|
|
|
|
const messageTTL = 5;
|
|
|
|
|
|
|
|
describe('Check prerequisites', () => {
|
|
|
|
console.log('Expecting MailServer running.')
|
2018-01-17 21:40:14 +00:00
|
|
|
console.log('./build/bin/statusd -les=false -shh -shh.mailserver -passwordfile=./static/keys/wnodepassword -http -httpport 8540 -listenaddr=127.0.0.1:8549 -identity=./static/keys/wnodekey')
|
2017-12-21 10:26:01 +00:00
|
|
|
|
|
|
|
it('MailServer should be running', () => {
|
|
|
|
const mailServer = new Web3(new Web3.providers.HttpProvider('http://localhost:8540'));
|
|
|
|
const version = mailServer.shh.version();
|
|
|
|
expect(version).to.equal("5.0");
|
|
|
|
});
|
|
|
|
});
|
2017-12-04 16:11:14 +00:00
|
|
|
|
|
|
|
describe('NodeA', () => {
|
|
|
|
let nodeA;
|
|
|
|
let nodeAProcess;
|
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
before((done) => {
|
2017-12-04 16:11:14 +00:00
|
|
|
nodeAProcess = spawn(
|
2018-01-17 21:40:14 +00:00
|
|
|
'./build/bin/statusd',
|
|
|
|
['-shh', '-les=false', '-datadir', 'wnode-data-1', '-http', '-httpport', '8590']
|
2017-12-04 16:11:14 +00:00
|
|
|
);
|
|
|
|
nodeA = new Web3(new Web3.providers.HttpProvider('http://localhost:8590'));
|
2017-12-21 10:26:01 +00:00
|
|
|
|
|
|
|
// need to wait a bit until the node is up and running
|
|
|
|
setTimeout(done, 500);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after((done) => {
|
|
|
|
nodeAProcess.kill('SIGTERM');
|
|
|
|
nodeAProcess.on('exit', (code, signal) => {
|
|
|
|
expect(code).to.be.null;
|
|
|
|
expect(signal).to.equal('SIGTERM');
|
2017-12-21 10:26:01 +00:00
|
|
|
rimraf('wnode-data-1', done);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
it('Should add MailServer as a peer', (done) => {
|
|
|
|
// add MailServer as a peer
|
|
|
|
axios.post(nodeA.currentProvider.host, {
|
|
|
|
method: 'admin_addPeer',
|
|
|
|
params: [mailServerEnode],
|
|
|
|
id: 1
|
|
|
|
}).then((resp) => {
|
|
|
|
expect(resp.data.id).to.equal(1);
|
|
|
|
expect(resp.data.result).to.equal(true);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
})
|
2017-12-04 16:11:14 +00:00
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
it('Should send a message', (done) => {
|
2017-12-04 16:11:14 +00:00
|
|
|
const symKeyId = nodeA.shh.addSymKey(sharedSymKey);
|
|
|
|
const result = nodeA.shh.post({
|
|
|
|
symKeyID: symKeyId,
|
|
|
|
topic: topic,
|
|
|
|
payload: nodeA.toHex('hello!'),
|
2017-12-21 10:26:01 +00:00
|
|
|
ttl: messageTTL,
|
2017-12-04 16:11:14 +00:00
|
|
|
powTime: 10,
|
|
|
|
powTarget: 2.5
|
|
|
|
});
|
|
|
|
expect(result).to.be.true;
|
2017-12-21 10:26:01 +00:00
|
|
|
|
|
|
|
// give it some time to propagate before the node is shut down
|
|
|
|
setTimeout(done, 500);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('NodeB', () => {
|
|
|
|
let nodeBProcess;
|
|
|
|
let nodeB;
|
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
before((done) => {
|
2017-12-04 16:11:14 +00:00
|
|
|
nodeBProcess = spawn(
|
2018-01-17 21:40:14 +00:00
|
|
|
'./build/bin/statusd',
|
|
|
|
['-shh', '-les=false', '-datadir', 'wnode-data-2', '-http', '-httpport', '8591', '-log', 'INFO', '-logfile', 'wnode-data-2/wnode.log']
|
2017-12-04 16:11:14 +00:00
|
|
|
);
|
|
|
|
nodeB = new Web3(new Web3.providers.HttpProvider('http://localhost:8591'));
|
2017-12-21 10:26:01 +00:00
|
|
|
|
|
|
|
// need to wait a bit until the node is up and running
|
|
|
|
setTimeout(done, 500);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after((done) => {
|
|
|
|
nodeBProcess.kill('SIGTERM');
|
|
|
|
nodeBProcess.on('exit', (code, signal) => {
|
|
|
|
expect(code).to.be.null;
|
|
|
|
expect(signal).to.equal('SIGTERM');
|
2017-12-21 10:26:01 +00:00
|
|
|
rimraf('wnode-data-2', done);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
it('Should add MailServer as a peer', (done) => {
|
|
|
|
// add MailServer as a peer
|
|
|
|
axios.post(nodeB.currentProvider.host, {
|
|
|
|
method: 'admin_addPeer',
|
|
|
|
params: [mailServerEnode],
|
|
|
|
id: 1
|
|
|
|
}).then((resp) => {
|
|
|
|
expect(resp.data.id).to.equal(1);
|
|
|
|
expect(resp.data.result).to.equal(true);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
})
|
2017-12-04 16:11:14 +00:00
|
|
|
|
|
|
|
it('Should request and receive old messages', (done) => {
|
2017-12-21 10:26:01 +00:00
|
|
|
const mailServerSymKeyID = nodeB.shh.generateSymKeyFromPassword('status-offline-inbox');
|
2017-12-04 16:11:14 +00:00
|
|
|
const symKeyId = nodeB.shh.addSymKey(sharedSymKey);
|
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
let requestedForMessages = false;
|
|
|
|
|
|
|
|
// wait until the message expires before setting up a filter
|
|
|
|
setTimeout(() => {
|
|
|
|
let counter = 0;
|
|
|
|
nodeB.shh.newMessageFilter({
|
|
|
|
topics: [topic],
|
|
|
|
symKeyID: symKeyId,
|
|
|
|
allowP2P: true
|
|
|
|
}, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(nodeB.toAscii(data.payload)).to.equal('hello!');
|
|
|
|
|
|
|
|
if (requestedForMessages) {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done('should not receive the message before requesting it');
|
|
|
|
}
|
|
|
|
}, done);
|
|
|
|
}, (messageTTL + 1) * 1000);
|
|
|
|
|
|
|
|
// request messages after the filter is set up and give it some addotional time
|
|
|
|
// so we are sure that the message was received after requesting it
|
|
|
|
setTimeout(() => {
|
|
|
|
// send a request for old messages
|
|
|
|
axios.post(nodeB.currentProvider.host, {
|
|
|
|
method: 'shh_requestMessages',
|
|
|
|
params: [{
|
|
|
|
mailServerPeer: mailServerEnode,
|
|
|
|
topic: topic,
|
|
|
|
symKeyID: mailServerSymKeyID
|
|
|
|
}],
|
|
|
|
id: 2
|
|
|
|
}).then((resp) => {
|
|
|
|
requestedForMessages = true;
|
|
|
|
|
|
|
|
expect(resp.data.id).to.equal(2);
|
|
|
|
expect(resp.data.result).to.equal(true);
|
|
|
|
}).catch(done);
|
|
|
|
}, (messageTTL + 5) * 1000);
|
2017-12-04 16:11:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|