2018-09-14 23:54:14 +00:00
|
|
|
const { Provider } = require('nconf');
|
|
|
|
const nconf = new Provider();
|
2018-09-10 17:03:02 +00:00
|
|
|
const { assert } = require('chai');
|
2018-09-09 06:59:38 +00:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const { TorPool } = require('../');
|
|
|
|
const { WAIT_FOR_CREATE } = require('./constants');
|
|
|
|
|
2018-09-09 19:42:34 +00:00
|
|
|
nconf.use('memory');
|
2018-09-09 06:59:38 +00:00
|
|
|
require(`${__dirname}/../src/nconf_load_env.js`)(nconf);
|
|
|
|
nconf.defaults(require(`${__dirname}/../src/default_config.js`));
|
|
|
|
|
|
|
|
describe('TorPool', function () {
|
2018-09-09 19:42:34 +00:00
|
|
|
const torPoolFactory = () => new TorPool(nconf.get('torPath'), {}, nconf.get('parentDataDirectory'), 'round_robin', null);
|
2018-09-09 06:59:38 +00:00
|
|
|
|
2018-09-11 02:36:21 +00:00
|
|
|
describe('#default_tor_config', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let cfg = { "TestSocks": 1, "Log": "notice stdout", "NewCircuitPeriod": "10" };
|
|
|
|
|
|
|
|
before('create tor config based on nconf', function () {
|
|
|
|
nconf.set('torConfig', cfg);
|
|
|
|
tor_pool = new TorPool(nconf.get('torPath'), (() => nconf.get('torConfig')), nconf.get('parentDataDirectory'), 'round_robin', null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the tor config should have the same value as set in nconf', function () {
|
|
|
|
assert.deepEqual(nconf.get('torConfig'), tor_pool.default_tor_config);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
|
|
|
|
before('create tor config based on nconf', function () {
|
|
|
|
tor_pool = new TorPool(nconf.get('torPath'), cfg, nconf.get('parentDataDirectory'), 'round_robin', null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the tor config should have the same value as set', function () {
|
|
|
|
assert.deepEqual(cfg, tor_pool.default_tor_config);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
describe('#create_instance(instance_defintion)', function () {
|
2018-09-15 01:09:50 +00:00
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
let instance_defintion = {
|
|
|
|
Name: 'instance-1',
|
|
|
|
Config: {
|
|
|
|
ProtocolWarnings: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-09 19:42:34 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', () => { torPool = torPoolFactory(); })
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should create one tor instance based on the provided definition', async function () {
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create_instance(instance_defintion);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('one instance should exist in the instances collection', function () {
|
|
|
|
assert.equal(1, torPool.instances.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the created instance should have the defintion properties as the input definition', function () {
|
|
|
|
assert.deepEqual(instance_defintion, torPool.instances[0].definition);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the created instance should have the same config properties specified in the definiton', async function () {
|
|
|
|
let value = await torPool.instances[0].get_config('ProtocolWarnings');
|
|
|
|
assert.equal(value, instance_defintion.Config.ProtocolWarnings);
|
|
|
|
});
|
|
|
|
|
2018-09-11 02:36:21 +00:00
|
|
|
it('should not be able to create an instance with an existing name', async function () {
|
2018-09-15 01:09:50 +00:00
|
|
|
this.timeout(WAIT_FOR_CREATE * 2);
|
2018-09-11 02:36:21 +00:00
|
|
|
let fn = () => {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await torPool.create_instance({ Name: 'foo' });
|
|
|
|
await torPool.create_instance({ Name: 'foo' });
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
fn = () => { throw error };
|
|
|
|
} finally {
|
|
|
|
assert.throws(fn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
after('shutdown tor pool', async function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
await torPool.exit();
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#add(instance_defintions)', function () {
|
2018-09-11 17:39:49 +00:00
|
|
|
let instance_defintions = [
|
2018-09-11 22:17:35 +00:00
|
|
|
{ Name: 'instance-1', Group: [], Config: { ProtocolWarnings: 1} },
|
|
|
|
{ Name: 'instance-2', Group: [], Config: { ProtocolWarnings: 1 } }
|
2018-09-09 06:59:38 +00:00
|
|
|
];
|
|
|
|
|
2018-09-09 19:42:34 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', () => { torPool = torPoolFactory(); })
|
|
|
|
|
2018-09-14 02:35:40 +00:00
|
|
|
it('should throw if the "instance_defintions" field is falsy', async function () {
|
|
|
|
let fn = () => {};
|
|
|
|
try {
|
|
|
|
await torPool.add();
|
|
|
|
} catch (error) {
|
|
|
|
fn = () => { throw error };
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
assert.throws(fn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should create instances from several instance definitions', async function () {
|
|
|
|
this.timeout(WAIT_FOR_CREATE*2);
|
2018-09-09 19:42:34 +00:00
|
|
|
await torPool.add(_.cloneDeep(instance_defintions))
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('2 instances should exist in the pool', function () {
|
|
|
|
assert.equal(2, torPool.instances.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the created instances should have the same defintion properties as the input definitions', function () {
|
2018-09-09 19:42:34 +00:00
|
|
|
let live_instance_definitions = torPool.instances.map((instance) => {
|
2018-09-09 06:59:38 +00:00
|
|
|
let def_clone = _.cloneDeep(instance.definition);
|
|
|
|
delete def_clone.Config.DataDirectory;
|
|
|
|
return def_clone;
|
2018-09-09 19:42:34 +00:00
|
|
|
}).sort((a,b) => (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0));
|
|
|
|
|
|
|
|
assert.deepEqual(instance_defintions, live_instance_definitions);
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
2018-09-09 19:42:34 +00:00
|
|
|
it('the created instances should have the same config properties specified in the definiton', async function () {
|
2018-09-09 06:59:38 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
|
|
let values = await Promise.all(torPool.instances.map((instance) => instance.get_config('ProtocolWarnings')));
|
2018-09-09 19:42:34 +00:00
|
|
|
values = _.flatten(values);
|
|
|
|
assert.isTrue( values.every((value) => value === "1") );
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () {
|
|
|
|
await torPool.exit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-14 02:35:40 +00:00
|
|
|
describe('#create(instances)', function () {
|
2018-09-09 19:42:34 +00:00
|
|
|
let torPool;
|
|
|
|
|
|
|
|
before('create tor pool', () => {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
torPool.default_tor_config = { TestSocks: 1 };
|
2018-09-14 02:35:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if the "number_of_instances" field is falsy', async function () {
|
|
|
|
let fn = () => {};
|
|
|
|
try {
|
|
|
|
await torPool.create();
|
|
|
|
} catch (error) {
|
|
|
|
fn = () => { throw error; }
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
assert.throws(fn);
|
|
|
|
}
|
|
|
|
});
|
2018-09-09 19:42:34 +00:00
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should create 2 instances with the default config', async function () {
|
|
|
|
this.timeout(WAIT_FOR_CREATE*2);
|
|
|
|
await torPool.create(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('2 instances should exist in the pool', function () {
|
|
|
|
assert.equal(2, torPool.instances.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the created instances should have the same config properties specified in the default config', async function () {
|
|
|
|
this.timeout(10000);
|
|
|
|
|
2018-09-09 19:42:34 +00:00
|
|
|
let values = await Promise.all(torPool.instances.map((instance) => instance.get_config('TestSocks')));
|
|
|
|
values = _.flatten(values);
|
|
|
|
assert.isTrue( values.every((value) => value === "1") );
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () {
|
|
|
|
torPool.default_tor_config = {};
|
2018-09-09 19:42:34 +00:00
|
|
|
await torPool.exit();
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
describe('#instances_by_group(group)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instances;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
instances = (await tor_pool.add([ { Name: 'instance-1', Group: [ "bar", "foo" ] }, { Name: 'instance-2', Group: ["foo", "bar"] } ]));
|
|
|
|
});
|
|
|
|
|
2018-09-14 02:35:40 +00:00
|
|
|
it('should throw if the provided group does not exist', function () {
|
|
|
|
assert.throws(() => {
|
|
|
|
tor_pool.instances_by_group('baz');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
it('should return both instances', function () {
|
|
|
|
assert.deepEqual(tor_pool.instances_by_group('bar'), instances);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
describe('#next()', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
2018-09-09 19:42:34 +00:00
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
this.timeout(WAIT_FOR_CREATE * 3);
|
|
|
|
await torPool.add([
|
|
|
|
{
|
|
|
|
Name: 'instance-1',
|
2018-09-11 05:06:53 +00:00
|
|
|
Weight: 50,
|
|
|
|
Group: 'foo'
|
2018-09-09 06:59:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: 'instance-2',
|
2018-09-11 05:06:53 +00:00
|
|
|
Weight: 25,
|
|
|
|
Group: 'bar'
|
2018-09-09 06:59:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: 'instance-3',
|
2018-09-11 05:06:53 +00:00
|
|
|
Weight: 2,
|
|
|
|
Group: 'bar'
|
2018-09-09 06:59:38 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('result of next should be different if run twice', function () {
|
2018-09-09 19:42:34 +00:00
|
|
|
let t1 = torPool.next().instance_name;
|
|
|
|
let t2 = torPool.next().instance_name;
|
2018-09-09 06:59:38 +00:00
|
|
|
assert.notEqual(t1, t2);
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
|
|
|
|
describe('#next_by_group(group)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = new TorPool(nconf.get('torPath'), (() => nconf.get('torConfig')), nconf.get('parentDataDirectory'), 'round_robin', null);
|
|
|
|
this.timeout(WAIT_FOR_CREATE * 3);
|
|
|
|
await tor_pool.add([
|
|
|
|
{ Group: 'foo' },
|
|
|
|
{ Group: 'bar' },
|
|
|
|
{ Group: 'bar' }
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('after rotating the first instance in group should be different', function () {
|
|
|
|
let first_instance_name_before = tor_pool.groups['bar'][0].instance_name;
|
|
|
|
|
|
|
|
tor_pool.next_by_group('bar');
|
|
|
|
|
|
|
|
let first_instance_name_after = tor_pool.groups['bar'][0].instance_name;
|
|
|
|
|
|
|
|
assert.notEqual(first_instance_name_after, first_instance_name_before);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
describe('#instance_by_name(instance_name)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add([
|
|
|
|
{
|
|
|
|
Name: 'instance-1'
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should retrieve instance by name', function () {
|
|
|
|
assert.ok(torPool.instance_by_name('instance-1'));
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#remove_by_name(instance_name)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add([
|
|
|
|
{
|
|
|
|
Name: 'instance-3'
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should remove instance by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.remove_by_name('instance-3');
|
2018-09-11 05:06:53 +00:00
|
|
|
|
2018-09-11 06:48:56 +00:00
|
|
|
assert.notInclude(torPool.instance_names, "instance-3");
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#instance_at(index)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create(1);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should retrieve an instance by id', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
assert.ok(torPool.instance_at(0));
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#remove_at(index)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE );
|
|
|
|
await torPool.create(1);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should remove an instance by id', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.remove_at(0);
|
|
|
|
|
|
|
|
assert.notOk(torPool.instances[0]);
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
describe('#new_identites()', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE * 2);
|
|
|
|
await torPool.create(2);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should signal to retrieve a new identity to all instances', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.new_identites();
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#new_identites_by_group(group)', function () {
|
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE * 3);
|
|
|
|
await torPool.add([
|
|
|
|
{ Name: 'instance-1', Group: 'bar' },
|
|
|
|
{ Name: 'instance-2', Group: 'foo' }
|
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should signal to retrieve a new identity to all instances', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.new_identites_by_group('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#new_identity_at(index)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create(1);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should signal to retrieve a new identity identified by index', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.new_identity_at(0);
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#new_identity_by_name(instance_name)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add({ Name: 'instance-1' });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should signal to retrieve a new identity identified by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.new_identity_by_name('instance-1');
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('#set_config_all(keyword, value)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE * 2);
|
|
|
|
await torPool.create(2);
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should set configuration on all active instances', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.set_config_all('TestSocks', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('all instances should contain the same changed configuration', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
|
|
|
|
let values = await Promise.all(torPool.instances.map((instance) => instance.get_config('TestSocks')));
|
2018-09-09 19:42:34 +00:00
|
|
|
values = _.flatten(values);
|
|
|
|
assert.isTrue( values.every((value) => value === "1") );
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#set_config_by_group(group, keyword, value)', function () {
|
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add([ { Name: 'instance-1', Group: 'foo' }, { Name: 'instance-2', Group: 'bar' } ]);
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
it('should set configuration on all active instances', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.set_config_by_group('bar', 'TestSocks', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('all instances should contain the same changed configuration', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
|
|
|
|
let values_from_bar = await Promise.all(torPool.instances_by_group('bar').map((instance) => instance.get_config('TestSocks')));
|
|
|
|
values_from_bar = _.flatten(values_from_bar);
|
|
|
|
assert.isTrue( values_from_bar.every((value) => value === "1") );
|
|
|
|
|
|
|
|
let values_from_foo = await Promise.all(torPool.instances_by_group('foo').map((instance) => instance.get_config('TestSocks')));
|
|
|
|
values_from_foo = _.flatten(values_from_foo);
|
|
|
|
assert.isTrue( values_from_foo.every((value) => value !== "1") );
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#set_config_by_name(name, keyword, value)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add({ Name: 'instance-1' });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should set a configuration property of an instance identified by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.set_config_by_name('instance-1', 'ProtocolWarnings', 1);
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
it('should have the set value', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
let value = _.flatten(await torPool.get_config_by_name('instance-1', 'ProtocolWarnings'))[0];
|
|
|
|
assert.equal(value, '1');
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#get_config_by_name(name, keyword)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add({ Name: 'instance-1', Config: { ProtocolWarnings: 1 } });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should get retrieve the configuration of an instance identified by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
let value = _.flatten(await torPool.get_config_by_name('instance-1', 'ProtocolWarnings'))[0];
|
|
|
|
assert.equal(value, '1');
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#set_config_at(index, keyword, value)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create(1);
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should set a configuration property of an instance identified by index', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.set_config_at(0, 'ProtocolWarnings', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have the set value', async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
let value = _.flatten(await torPool.get_config_at(0, 'ProtocolWarnings'))[0];
|
|
|
|
assert.equal(value, '1');
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#get_config_at(index, keyword)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.add({ Config: { ProtocolWarnings: 1 } });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should get retrieve the configuration of an instance identified by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
let value = _.flatten(await torPool.get_config_at(0, 'ProtocolWarnings'))[0];
|
2018-09-09 06:59:38 +00:00
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
assert.equal(value, '1');
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#signal_all(signal)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create(1);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should send a signal to all instances', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.signal_all('DEBUG');
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#signal_by_name(name, signal)', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create({ Name: 'instance-1' });
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should send a signal to an instance identified by name', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.signal_by_name('instance-1', 'DEBUG');
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#signal_by_group(group, name, signal)', async function () {
|
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-14 23:54:14 +00:00
|
|
|
await torPool.create([ { "Name": "instance-1", "Group": ["foo"] }, { "Name": "instance-2", "Group": ["foo"] } ]);
|
2018-09-11 05:06:53 +00:00
|
|
|
});
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
it('should send a signal to a group of instances', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
await torPool.signal_by_group('foo', 'DEBUG');
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#signal_at(index, signal)', function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
let torPool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
torPool = torPoolFactory();
|
|
|
|
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
|
|
|
await torPool.create(1);
|
|
|
|
});
|
|
|
|
|
2018-09-09 06:59:38 +00:00
|
|
|
it('should send a signal to an instance identified by index', async function () {
|
2018-09-11 05:06:53 +00:00
|
|
|
this.timeout(5000);
|
2018-09-09 06:59:38 +00:00
|
|
|
await torPool.signal_at(0, 'DEBUG');
|
|
|
|
});
|
|
|
|
|
2018-09-11 05:06:53 +00:00
|
|
|
after('shutdown tor pool', async function () { await torPool.exit(); });
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|
2018-09-11 02:36:21 +00:00
|
|
|
|
|
|
|
describe('#group_names', function () {
|
|
|
|
let tor_pool;
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE * 2);
|
|
|
|
await tor_pool.add([
|
|
|
|
{ Name: 'instance-1', Group: [ "foo", "bar" ] },
|
|
|
|
{ Name: 'instance-2', Group: "baz" }
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the pool should contain three groups, bar, baz and foo', function () {
|
|
|
|
assert.deepEqual(tor_pool.group_names, (new Set([ "bar", "baz", "foo" ])));
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
2018-09-11 05:06:53 +00:00
|
|
|
|
2018-09-11 02:36:21 +00:00
|
|
|
describe('#add_instance_to_group(group, instance)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.create(1))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add the instance to the group successfully', function () {
|
|
|
|
tor_pool.add_instance_to_group("foo", instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should be added to the group', function () {
|
|
|
|
assert.deepEqual(instance.instance_group, ["foo"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#add_instance_to_group_by_name(group, instance_name)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.add({ Name: 'instance-1' }))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add the instance to the group successfully', function () {
|
|
|
|
tor_pool.add_instance_to_group_by_name("foo", instance.definition.Name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should be added to the group', function () {
|
|
|
|
assert.deepEqual(instance.instance_group, ["foo"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#add_instance_to_group_at(group, instance_index)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.create(1))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add the instance to the group successfully', function () {
|
|
|
|
tor_pool.add_instance_to_group_at("foo", 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should be added to the group', function () {
|
|
|
|
assert.deepEqual(instance.instance_group, ["foo"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#remove_instance_from_group(group, instance)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.add({ Group: "foo" }))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the instance from the group successfully', function () {
|
|
|
|
tor_pool.remove_instance_from_group("foo", instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should be no longer be in the group', function () {
|
2018-09-11 06:48:56 +00:00
|
|
|
assert.notInclude(instance.instance_group, "foo");
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#remove_instance_from_group_by_name(group, instance_name)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.add({ Name: 'instance-1', Group: "foo" }))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the instance from the group successfully', function () {
|
|
|
|
tor_pool.remove_instance_from_group_by_name("foo", instance.definition.Name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should no longer be in the group', function () {
|
2018-09-11 06:48:56 +00:00
|
|
|
assert.notInclude(instance.instance_group, "foo");
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#remove_instance_from_group_at(group, instance_index)', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
|
|
|
this.timeout(WAIT_FOR_CREATE);
|
2018-09-11 05:06:53 +00:00
|
|
|
instance = (await tor_pool.add({ Group: "foo" }))[0];
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the instance from the group successfully', function () {
|
|
|
|
tor_pool.remove_instance_from_group_at("foo", 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the instance should no longer be in the group', function () {
|
2018-09-11 06:48:56 +00:00
|
|
|
assert.notInclude(instance.instance_group, "foo");
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#groups', function () {
|
|
|
|
let tor_pool;
|
|
|
|
let instances;
|
|
|
|
|
|
|
|
let get_instance_names = (group_name) => {
|
|
|
|
let instances = [];
|
|
|
|
let group = tor_pool.groups[group_name];
|
|
|
|
for (let i = 0; i < group.length; i++)
|
|
|
|
instances.push(group[i]);
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
return instances.map((i) => i.instance_name).sort();
|
2018-09-11 02:36:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
before('create tor pool', async function () {
|
|
|
|
tor_pool = torPoolFactory();
|
2018-09-11 17:39:49 +00:00
|
|
|
this.timeout(WAIT_FOR_CREATE * 3);
|
2018-09-11 02:36:21 +00:00
|
|
|
instances = (await tor_pool.add([
|
2018-09-11 17:39:49 +00:00
|
|
|
{ Group: ["foo", "flob"], Name: 'instance-1' },
|
|
|
|
{ Group: ["bar", "baz"], Name: 'instance-2' },
|
|
|
|
{ Group: ["flob"], Name: 'instance-3' }
|
2018-09-11 02:36:21 +00:00
|
|
|
]));
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
|
2018-09-11 02:36:21 +00:00
|
|
|
it('should contain three groups, bar, baz and foo', function () {
|
2018-09-11 17:39:49 +00:00
|
|
|
assert.deepEqual(Array.from(tor_pool.group_names).sort(), [ "bar", "baz", "flob", "foo" ]);
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('#[Number] - the 1st element should be "instance-1"', function () {
|
|
|
|
assert.equal(tor_pool.groups["foo"][0], instances[0]);
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#length() - group "foo" should contain 1 instance', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
assert.equal(tor_pool.groups["foo"].length, 1);
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#add() - adding "instance-1" to "baz" should result in "baz" having "instance-1" and "instance-2"', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
tor_pool.groups["baz"].add(instances[0]);
|
|
|
|
|
|
|
|
assert.deepEqual(get_instance_names("baz"), [ "instance-1", "instance-2" ] );
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#remove() - removing "instance-1" firom "baz" should result in "baz" having just "instance-2"', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
tor_pool.groups["baz"].remove(instances[0]);
|
|
|
|
|
|
|
|
assert.deepEqual(get_instance_names("baz"), [ "instance-2" ] );
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#add_by_name() - adding "instance-1" to "baz" should result in "baz" having "instance-1" and "instance-2"', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
tor_pool.groups["baz"].add_by_name('instance-1');
|
|
|
|
|
|
|
|
assert.deepEqual(get_instance_names("baz"), [ "instance-1", "instance-2" ] );
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#remove_by_name() - removing "instance-1" from "baz" should result in "baz" having just "instance-2"', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
tor_pool.groups["baz"].remove_by_name('instance-1');
|
|
|
|
|
|
|
|
assert.deepEqual(get_instance_names("baz"), [ "instance-2" ] );
|
|
|
|
});
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
it('#remove_at() - removing "instance-1" from "baz" should result in "baz" having just "instance-2"', function () {
|
2018-09-11 02:36:21 +00:00
|
|
|
tor_pool.groups["baz"].add_by_name('instance-1');
|
|
|
|
tor_pool.groups["baz"].remove_at(0);
|
|
|
|
|
2018-09-11 17:39:49 +00:00
|
|
|
assert.deepEqual(get_instance_names("baz"), [ "instance-1" ] );
|
|
|
|
});
|
|
|
|
|
|
|
|
it('#rotate() - the name of the first instance should change', function () {
|
|
|
|
let first_instance_name_before = tor_pool.groups["flob"][0].instance_name;
|
|
|
|
tor_pool.groups["flob"].rotate();
|
|
|
|
let first_instance_name_after = tor_pool.groups["flob"][0].instance_name;
|
|
|
|
assert.notEqual(first_instance_name_after, first_instance_name_before);
|
2018-09-11 02:36:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after('shutdown tor pool', async function () { await tor_pool.exit(); });
|
|
|
|
});
|
2018-09-09 06:59:38 +00:00
|
|
|
});
|