netmask.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* It is important to test our Javascript output as well as our coffeescript,
  2. * since code that is transpiled may be slightly different in effect from the
  3. * original.
  4. *
  5. * Run these tests (against lib/netmask.js, not lib/netmask.coffee directly)
  6. * using mocha, after re-generating lib/netmask.js including your changes:
  7. *
  8. * mocha tests/netmask.js
  9. */
  10. const assert = require('assert');
  11. const Netmask = require('../').Netmask;
  12. describe('Netmask', () => {
  13. describe('can build a block', () => {
  14. let block = new Netmask('10.1.2.0/24');
  15. it('should contain a sub-block', () => {
  16. let block1 = new Netmask('10.1.2.10/29');
  17. assert(block.contains(block1));
  18. });
  19. it('should contain another sub-block', () => {
  20. let block2 = new Netmask('10.1.2.10/31');
  21. assert(block.contains(block2));
  22. });
  23. it('should contain a third sub-block', () => {
  24. let block3 = new Netmask('10.1.2.20/32');
  25. assert(block.contains(block3));
  26. });
  27. });
  28. describe('when presented with an octet which is not a number', () => {
  29. let block = new Netmask('192.168.0.0/29')
  30. it('should throw', () => {
  31. assert.throws(() => block.contains('192.168.~.4'), Error);
  32. });
  33. });
  34. describe('can handle hexadecimal, octal, & decimal octets in input IP', () => {
  35. let block1 = new Netmask('31.0.0.0/19');
  36. let block2 = new Netmask('127.0.0.0/8');
  37. let block3 = new Netmask('255.0.0.1/12');
  38. let block4 = new Netmask('10.0.0.1/8');
  39. let block5 = new Netmask('1.0.0.1/4');
  40. describe('octal', () => {
  41. it('block 31.0.0.0/19 does not contain 031.0.5.5', () => {
  42. assert(!block1.contains('031.0.5.5'));
  43. });
  44. it('block 127.0.0.0/8 contains 0177.0.0.2 (127.0.0.2)', () => {
  45. assert(block2.contains('0177.0.0.2'));
  46. });
  47. it('block 255.0.0.1/12 does not contain 0255.0.0.2 (173.0.0.2)', () => {
  48. assert(!block3.contains('0255.0.0.2'));
  49. });
  50. it('block 10.0.0.1/8 contains 012.0.0.255 (10.0.0.255)', () => {
  51. assert(block4.contains('012.0.0.255'));
  52. });
  53. it('block 1.0.0.1/4 contains 01.02.03.04', () => {
  54. assert(block5.contains('01.02.03.04'));
  55. });
  56. });
  57. describe('hexadecimal', () => {
  58. it('block 31.0.0.0/19 does not contain 0x31.0.5.5', () => {
  59. assert(!block1.contains('0x31.0.5.5'));
  60. });
  61. it('block 127.0.0.0/8 contains 0x7f.0.0.0x2 (127.0.0.2)', () => {
  62. assert(block2.contains('0x7f.0.0.0x2'));
  63. });
  64. it('block 255.0.0.1/12 contains 0xff.0.0.2', () => {
  65. assert(block3.contains('0xff.0.0.2'));
  66. });
  67. it('block 10.0.0.1/8 does not contain 0x10.0.0.255', () => {
  68. assert(!block4.contains('0x10.0.0.255'));
  69. });
  70. it('block 1.0.0.1/4 contains 0x1.0x2.0x3.0x4', () => {
  71. assert(block5.contains('0x1.0x2.0x3.0x4'));
  72. });
  73. });
  74. describe('decimal', () => {
  75. it('block 31.0.0.0/19 contains 31.0.5.5', () => {
  76. assert(block1.contains('31.0.5.5'));
  77. });
  78. it('block 127.0.0.0/8 does not contain 128.0.0.2', () =>{
  79. assert(!block2.contains('128.0.0.2'));
  80. });
  81. it('block 255.0.0.1/12 contains 255.0.0.2', () => {
  82. assert(block3.contains('255.0.0.2'));
  83. });
  84. it('block 10.0.0.1/8 contains 10.0.0.255', () => {
  85. assert(block4.contains('10.0.0.255'));
  86. });
  87. it('block 1.0.0.1/4 contains 1.2.3.4', () => {
  88. assert(block5.contains('1.2.3.4'));
  89. });
  90. });
  91. });
  92. });