You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netmasks.coffee 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.82.0', '255.255.255.0', 24]
  13. ['140.174', null, '140.174.0.0', '255.255.0.0', 16]
  14. ['10', null, '10.0.0.0', '255.0.0.0', 8]
  15. ['10/8', null, '10.0.0.0', '255.0.0.0', 8]
  16. ['209.157.64/19', null, '209.157.64.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.157.0.0', '255.255.128.0', 17]
  19. ['0.0.0.0/0', null, '0.0.0.0', '0.0.0.0', 0]
  20. ]
  21. contexts = []
  22. fixtures.forEach (fixture) ->
  23. [addr, mask, base, newmask, bitmask] = fixture
  24. context = topic: -> new Netmask(addr, mask)
  25. context["base is `#{base}'"] = (block) -> assert.equal block.base, base
  26. context["mask is `#{newmask}'"] = (block) -> assert.equal block.mask, newmask
  27. context["bitmask is `#{bitmask}'"] = (block) -> assert.equal block.bitmask, bitmask
  28. context["toString is `#{base}/`#{bitmask}'"] = (block) -> assert.equal block.toString(), block.base + "/" + block.bitmask
  29. contexts["for #{addr}" + (if mask then " with #{mask}" else '')] = context
  30. vows.describe('Netmaks parsing').addBatch(contexts).export(module)
  31. vows.describe('Netmask contains IP')
  32. .addBatch
  33. 'block 192.168.1.0/24':
  34. topic: -> new Netmask('192.168.1.0/24')
  35. 'contains IP 192.168.1.0': (block) -> assert.ok block.contains('192.168.1.0')
  36. 'contains IP 192.168.1.255': (block) -> assert.ok block.contains('192.168.1.255')
  37. 'contains IP 192.168.1.63': (block) -> assert.ok block.contains('192.168.1.63')
  38. 'does not contain IP 192.168.0.255': (block) -> assert.ok not block.contains('192.168.0.255')
  39. 'does not contain IP 192.168.2.0': (block) -> assert.ok not block.contains('192.168.2.0')
  40. 'does not contain IP 10.168.2.0': (block) -> assert.ok not block.contains('10.168.2.0')
  41. 'does not contain IP 209.168.2.0': (block) -> assert.ok not block.contains('209.168.2.0')
  42. 'contains block 192.168.1.0/24': (block) -> assert.ok block.contains('192.168.1.0/24')
  43. 'contains block 192.168.1': (block) -> assert.ok block.contains('192.168.1')
  44. 'contains block 192.168.1.128/25': (block) -> assert.ok block.contains('192.168.1.128/25')
  45. 'does not contain block 192.168.1.0/23': (block) -> assert.ok not block.contains('192.168.1.0/23')
  46. 'does not contain block 192.168.2.0/24': (block) -> assert.ok not block.contains('192.168.2.0/24')
  47. 'toString equals 192.168.1.0/24': (block) -> assert.equal block.toString(), '192.168.1.0/24'
  48. 'block 192.168.0.0/24':
  49. topic: -> new Netmask('192.168.0.0/24')
  50. 'does not contain block 192.168': (block) -> assert.ok not block.contains('192.168')
  51. .export(module)
  52. vows.describe('Netmask forEach')
  53. .addBatch
  54. 'block 192.168.1.0/24':
  55. topic: -> new Netmask('192.168.1.0/24')
  56. 'should loop through all ip addresses': (block) ->
  57. called = 0
  58. block.forEach (ip, long, index) ->
  59. called = index
  60. assert.equal (called + 1), 254
  61. 'block 192.168.1.0/23':
  62. topic: -> new Netmask('192.168.1.0/23')
  63. 'should loop through all ip addresses': (block) ->
  64. called = 0
  65. block.forEach (ip, long, index) ->
  66. called = index
  67. assert.equal (called + 1), 510
  68. .export(module)