TorProcess.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const { Provider } = require('nconf');
  2. const nconf = new Provider();
  3. const { assert } = require('chai');
  4. const _ = require('lodash');
  5. const { TorProcess } = require('../');
  6. const { WAIT_FOR_CREATE } = require('./constants');
  7. nconf.use('memory');
  8. require(`${__dirname}/../src/nconf_load_env.js`)(nconf);
  9. nconf.defaults(require(`${__dirname}/../src/default_config.js`));
  10. describe('TorProcess', function () {
  11. const tor_factory = (d) => {
  12. let defaults = { Config: { DataDirectory: nconf.get('parentDataDirectory') } };
  13. d = _.extend(_.cloneDeep(defaults), (d || {}));
  14. return new TorProcess(nconf.get('torPath'), d, null);
  15. }
  16. describe('#instance_name', function () {
  17. let def = { Name: 'foo' };
  18. before('create tor process', function () {
  19. tor = tor_factory(def);
  20. });
  21. it('should have the same name as the definition', function () {
  22. assert.equal(tor.instance_name, def.Name);
  23. });
  24. });
  25. describe('#instance_group', function () {
  26. let def = { Group: ['foo'] };
  27. before('create tor process', function () {
  28. tor = tor_factory(def);
  29. });
  30. it('should have the same group as the definition', function () {
  31. assert.deepEqual(tor.instance_group, def.Group);
  32. });
  33. });
  34. describe('#definition', function () {
  35. let def = { Group: ['foo'], Config: {} };
  36. before('create tor process', function () {
  37. tor = tor_factory(def);
  38. });
  39. it('should be the same as the definition', function () {
  40. let tor_def = _.cloneDeep(tor.definition);
  41. assert.deepEqual(tor.definition, def);
  42. });
  43. });
  44. describe('#exit()', function () {
  45. let tor
  46. before('create tor process', async function () {
  47. tor = tor_factory();
  48. this.timeout(WAIT_FOR_CREATE);
  49. await tor.create();
  50. });
  51. it('should exit cleanly', async function () {
  52. await tor.exit();
  53. });
  54. it('the process should be dead', function () {
  55. assert.isTrue(tor.process.killed);
  56. });
  57. });
  58. describe('#create()', function () {
  59. let tor
  60. before('create tor process', function () {
  61. tor = tor_factory();
  62. });
  63. it('should create the child process', async function () {
  64. this.timeout(WAIT_FOR_CREATE);
  65. await tor.create();
  66. });
  67. it('should signal when it is listening on the control port', function (done) {
  68. if (tor.control_port_listening)
  69. return done();
  70. tor.once('control_listen', done);
  71. });
  72. it('should signal when connected to the control port', function (done) {
  73. if (tor.control_port_connected)
  74. return done();
  75. tor.once('controller_ready', done);
  76. });
  77. it('should signal when it is listening on the socks port', function (done) {
  78. if (tor.socks_port_listening)
  79. return done();
  80. tor.once('socks_listen', done);
  81. });
  82. it('should signal when it is listening on the dns port', function (done) {
  83. if (tor.dns_port_listening)
  84. return done();
  85. tor.once('dns_listen', done);
  86. });
  87. it('should signal when bootstrapped', function (done) {
  88. tor.once('error', done);
  89. if (tor.bootstrapped)
  90. return done();
  91. tor.once('ready', done);
  92. });
  93. after('exit tor', async function () {
  94. await tor.exit();
  95. });
  96. });
  97. describe('#set_config(keyword, value)', function () {
  98. let tor
  99. before('create tor process', function (done) {
  100. tor = tor_factory();
  101. this.timeout(WAIT_FOR_CREATE);
  102. tor.once('controller_ready', done);
  103. tor.create().catch(done);
  104. });
  105. it('should set sample configuration option via the control protocol', async function () {
  106. await tor.set_config('ProtocolWarnings', 1);
  107. });
  108. it('should have the value set', async function () {
  109. let value = (await tor.get_config('ProtocolWarnings'))[0];
  110. assert.equal(value, "1");
  111. });
  112. after('exit tor', async function () {
  113. await tor.exit();
  114. });
  115. });
  116. describe('#get_config(keyword, value)', function () {
  117. let tor
  118. before('create tor process', function (done) {
  119. tor = tor_factory({ Config: { ProtocolWarnings: 1 } });
  120. this.timeout(WAIT_FOR_CREATE);
  121. tor.once('controller_ready', done);
  122. tor.create().catch(done);
  123. });
  124. it('should retrieve sample configuration option via the control protocol', async function () {
  125. let value = await tor.get_config('ProtocolWarnings');
  126. assert.equal(value, 1);
  127. });
  128. after('exit tor', async function () {
  129. await tor.exit();
  130. });
  131. });
  132. describe('#new_identity()', function () {
  133. before('create tor process', function (done) {
  134. tor = tor_factory();
  135. this.timeout(WAIT_FOR_CREATE);
  136. tor.once('controller_ready', done);
  137. tor.create().catch(done);
  138. });
  139. it('should use a new identity', async function () {
  140. await tor.new_identity();
  141. });
  142. after('exit tor', async function () {
  143. await tor.exit();
  144. });
  145. });
  146. describe('#signal()', function () {
  147. let tor;
  148. before('create tor process', function (done) {
  149. tor = tor_factory();
  150. this.timeout(WAIT_FOR_CREATE);
  151. tor.once('controller_ready', done);
  152. tor.create().catch(done);
  153. });
  154. it('should send a signal via the control protocol', async function () {
  155. await tor.signal('DEBUG');
  156. });
  157. after('exit tor', async function () {
  158. await tor.exit();
  159. });
  160. });
  161. });