test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import path from 'path';
  2. import fs from 'fs';
  3. import net from 'net';
  4. import test from 'ava';
  5. import portScanner from './lib/portscanner.js'
  6. test.before('Set #1 test server', t => {
  7. const server = net.createServer();
  8. server.listen(3000, '127.0.0.1', () => t.end());
  9. });
  10. test.before('Set #2 test server', t => {
  11. const server2 = net.createServer();
  12. server2.listen(2999, '127.0.0.1', () => t.end());
  13. });
  14. /* checkPortStatus */
  15. test('checkPortStatus - taken', t => {
  16. t.plan(2);
  17. portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => {
  18. t.is(error, null);
  19. t.is(port, 'open');
  20. });
  21. });
  22. test('checkPortStatus - free', t => {
  23. t.plan(2);
  24. portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => {
  25. t.is(error, null);
  26. t.is(port, 'closed');
  27. });
  28. });
  29. /* findPortInUse */
  30. test('findPortInUse - taken port in range', t => {
  31. t.plan(2);
  32. portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => {
  33. t.is(error, null);
  34. t.is(port, 3000);
  35. });
  36. });
  37. test('findPortInUse - all ports in range free', t => {
  38. t.plan(2);
  39. portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => {
  40. t.is(error, null);
  41. t.false(port);
  42. });
  43. });
  44. /* findPortNotInUse */
  45. test('findAPortNotInUse - start from free port', t => {
  46. t.plan(2);
  47. portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => {
  48. t.is(error, null);
  49. t.true(port >= 3001 && port <= 3010);
  50. });
  51. });
  52. test('findAPortNotInUse - start from taken port', t => {
  53. t.plan(2);
  54. portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => {
  55. t.is(error, null);
  56. t.true(port >= 3001 && port <= 3010);
  57. });
  58. });
  59. test('findAPortNotInUse - all ports in range taken', t => {
  60. t.plan(2);
  61. portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => {
  62. t.is(error, null);
  63. t.false(port);
  64. });
  65. });
  66. test('checkPortStatus - taken (port as a string)', t => {
  67. t.plan(2)
  68. portScanner.checkPortStatus('3000', '127.0.0.1', (error, port) => {
  69. t.is(error, null)
  70. t.is(port, 'open')
  71. })
  72. })
  73. test('checkPortStatus - free (port as a string)', t => {
  74. t.plan(2)
  75. portScanner.checkPortStatus('3001', '127.0.0.1', (error, port) => {
  76. t.is(error, null)
  77. t.is(port, 'closed')
  78. })
  79. })
  80. test('findPortInUse - taken port in range (startPort as a string)', t => {
  81. t.plan(2)
  82. portScanner.findAPortInUse('3000', 3010, '127.0.0.1', (error, port) => {
  83. t.is(error, null)
  84. t.is(port, '3000')
  85. })
  86. })
  87. test('findPortInUse - taken port in range (endPort as a string)', t => {
  88. t.plan(2)
  89. portScanner.findAPortInUse(3000, '3010', '127.0.0.1', (error, port) => {
  90. t.is(error, null)
  91. t.is(port, 3000)
  92. })
  93. })
  94. test('findPortInUse - taken port in range (startPort and endPort as strings)', t => {
  95. t.plan(2)
  96. portScanner.findAPortInUse('3000', '3010', '127.0.0.1', (error, port) => {
  97. t.is(error, null)
  98. t.is(port, '3000')
  99. })
  100. })
  101. // test('findAPortNotInUse - start from free port (startPort as a string)', t => {
  102. // t.plan(2);
  103. // portScanner.findAPortNotInUse('3001', 3010, '127.0.0.1', (error, port) => {
  104. // t.is(error, null);
  105. // t.true(port !== '3001');
  106. // });
  107. // });