netmasks.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. vows = require 'vows'
  2. assert = require 'assert'
  3. util = require 'util'
  4. Netmask = require('../lib/netmask').Netmask
  5. fixtures =
  6. [
  7. # addr mask base newmask bitmask
  8. ['209.157.68.22/255.255.224.0', null, '209.157.64.0', '255.255.224.0', 19]
  9. ['209.157.68.22', '255.255.224.0', '209.157.64.0', '255.255.224.0', 19]
  10. ['209.157.70.33/19', null, '209.157.64.0', '255.255.224.0', 19]
  11. ['209.157.70.33', null, '209.157.70.33', '255.255.255.255', 32]
  12. ['140.174.82', null, '140.174.0.82', '255.255.255.255', 32]
  13. ['140.174', null, '140.0.0.174', '255.255.255.255', 32]
  14. ['10', null, '0.0.0.10', '255.255.255.255', 32]
  15. ['10/8', null, '0.0.0.0', '255.0.0.0', 8]
  16. ['209.157.64/19', null, '209.157.0.0', '255.255.224.0', 19]
  17. ['216.140.48.16/32', null, '216.140.48.16', '255.255.255.255', 32]
  18. ['209.157/17', null, '209.0.0.0', '255.255.128.0', 17]
  19. ['0.0.0.0/0', null, '0.0.0.0', '0.0.0.0', 0]
  20. ['0xffffffff', null, '255.255.255.255', '255.255.255.255', 32]
  21. ['1.1', null, '1.0.0.1', '255.255.255.255', 32]
  22. ['1.0xffffff', null, '1.255.255.255', '255.255.255.255', 32]
  23. ['1.2.3', null, '1.2.0.3', '255.255.255.255', 32]
  24. ['1.2.0xffff', null, '1.2.255.255', '255.255.255.255', 32]
  25. ]
  26. contexts = []
  27. fixtures.forEach (fixture) ->
  28. [addr, mask, base, newmask, bitmask] = fixture
  29. context = topic: -> new Netmask(addr, mask)
  30. context["base is `#{base}'"] = (block) -> assert.equal block.base, base
  31. context["mask is `#{newmask}'"] = (block) -> assert.equal block.mask, newmask
  32. context["bitmask is `#{bitmask}'"] = (block) -> assert.equal block.bitmask, bitmask
  33. context["toString is `#{base}/`#{bitmask}'"] = (block) -> assert.equal block.toString(), block.base + "/" + block.bitmask
  34. contexts["for #{addr}" + (if mask then " with #{mask}" else '')] = context
  35. vows.describe('Netmaks parsing').addBatch(contexts).export(module)
  36. vows.describe('Netmask contains IP')
  37. .addBatch
  38. 'block 192.168.1.0/24':
  39. topic: -> new Netmask('192.168.1.0/24')
  40. 'contains IP 192.168.1.0': (block) -> assert.ok block.contains('192.168.1.0')
  41. 'contains IP 192.168.1.255': (block) -> assert.ok block.contains('192.168.1.255')
  42. 'contains IP 192.168.1.63': (block) -> assert.ok block.contains('192.168.1.63')
  43. 'does not contain IP 192.168.0.255': (block) -> assert.ok not block.contains('192.168.0.255')
  44. 'does not contain IP 192.168.2.0': (block) -> assert.ok not block.contains('192.168.2.0')
  45. 'does not contain IP 10.168.2.0': (block) -> assert.ok not block.contains('10.168.2.0')
  46. 'does not contain IP 209.168.2.0': (block) -> assert.ok not block.contains('209.168.2.0')
  47. 'contains block 192.168.1.0/24': (block) -> assert.ok block.contains('192.168.1.0/24')
  48. 'contains block 192.168.1 (0.192.168.10)': (block) -> assert.ok not block.contains('192.168.1')
  49. 'does not contains block 192.168.1.128/25': (block) -> assert.ok block.contains('192.168.1.128/25')
  50. 'does not contain block 192.168.1.0/23': (block) -> assert.ok not block.contains('192.168.1.0/23')
  51. 'does not contain block 192.168.2.0/24': (block) -> assert.ok not block.contains('192.168.2.0/24')
  52. 'toString equals 192.168.1.0/24': (block) -> assert.equal block.toString(), '192.168.1.0/24'
  53. 'block 192.168.0.0/24':
  54. topic: -> new Netmask('192.168.0.0/24')
  55. 'does not contain block 192.168 (0.0.192.168)': (block) -> assert.ok not block.contains('192.168')
  56. 'does not contain block 192.168.0.0/16': (block) -> assert.ok not block.contains('192.168.0.0/16')
  57. 'block 31.0.0.0/8':
  58. topic: -> new Netmask('31.0.0.0/8')
  59. 'contains IP 31.5.5.5': (block) -> assert.ok block.contains('31.5.5.5')
  60. 'does not contain IP 031.5.5.5 (25.5.5.5)': (block) -> assert.ok not block.contains('031.5.5.5')
  61. 'does not contain IP 0x31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0x31.5.5.5')
  62. 'does not contain IP 0X31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0X31.5.5.5')
  63. 'block 127.0.0.0/8':
  64. topic: -> new Netmask('127.0.0.0/8')
  65. 'contains IP 127.0.0.2': (block) -> assert.ok block.contains('127.0.0.2')
  66. 'contains IP 0177.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0177.0.0.2')
  67. 'contains IP 0x7f.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0x7f.0.0.2')
  68. 'does not contains IP 127 (0.0.0.127)': (block) -> assert.ok not block.contains('127')
  69. 'does not contains IP 0177 (0.0.0.127)': (block) -> assert.ok not block.contains('0177')
  70. 'block 0.0.0.0/24':
  71. topic: -> new Netmask('0.0.0.0/0')
  72. 'contains IP 0.0.0.0': (block) -> assert.ok block.contains('0.0.0.0')
  73. 'contains IP 0': (block) -> assert.ok block.contains('0')
  74. 'contains IP 10 (0.0.0.10)': (block) -> assert.ok block.contains('10')
  75. 'contains IP 010 (0.0.0.8)': (block) -> assert.ok block.contains('010')
  76. 'contains IP 0x10 (0.0.0.16)': (block) -> assert.ok block.contains('0x10')
  77. .export(module)
  78. vows.describe('Netmask forEach')
  79. .addBatch
  80. 'block 192.168.1.0/24':
  81. topic: -> new Netmask('192.168.1.0/24')
  82. 'should loop through all ip addresses': (block) ->
  83. called = 0
  84. block.forEach (ip, long, index) ->
  85. called = index
  86. assert.equal (called + 1), 254
  87. 'block 192.168.1.0/23':
  88. topic: -> new Netmask('192.168.1.0/23')
  89. 'should loop through all ip addresses': (block) ->
  90. called = 0
  91. block.forEach (ip, long, index) ->
  92. called = index
  93. assert.equal (called + 1), 510
  94. .export(module)