badnets.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. vows = require 'vows'
  2. assert = require 'assert'
  3. Netmask = require('../lib/netmask').Netmask
  4. shouldFailWithError = (msg) ->
  5. context =
  6. topic: ->
  7. try
  8. return new Netmask(@context.name)
  9. catch e
  10. return e
  11. 'should fail': (e) ->
  12. assert.ok isError(e), "is an Error object #{e}"
  13. "with error `#{msg}'": (e) ->
  14. assert.ok e.message?.toLowerCase().indexOf(msg.toLowerCase()) > -1, "'#{e.message}' =~ #{msg}"
  15. return context
  16. isError = (e) ->
  17. return typeof e == 'object' and Object.prototype.toString.call(e) == '[object Error]'
  18. vows.describe('IPs with bytes greater than 255')
  19. .addBatch
  20. '209.256.68.22/255.255.224.0': shouldFailWithError 'Invalid net'
  21. '209.180.68.22/256.255.224.0': shouldFailWithError 'Invalid mask'
  22. '209.500.70.33/19': shouldFailWithError 'Invalid net'
  23. '140.999.82': shouldFailWithError 'Invalid net'
  24. '899.174': shouldFailWithError 'Invalid net'
  25. '209.157.65536/19': shouldFailWithError 'Invalid net'
  26. '209.300.64.0.10': shouldFailWithError 'Invalid net'
  27. 'garbage': shouldFailWithError 'Invalid net'
  28. .export(module)
  29. vows.describe('Invalid IP format')
  30. .addBatch
  31. ' 1.2.3.4': shouldFailWithError 'Invalid net'
  32. ' 1.2.3.4': shouldFailWithError 'Invalid net'
  33. '1. 2.3.4': shouldFailWithError 'Invalid net'
  34. '1.2. 3.4': shouldFailWithError 'Invalid net'
  35. '1.2.3. 4': shouldFailWithError 'Invalid net'
  36. '1.2.3.4 ': shouldFailWithError 'Invalid net'
  37. '1 .2.3.4': shouldFailWithError 'Invalid net'
  38. '018.0.0.0': shouldFailWithError 'Invalid net'
  39. '08.0.0.0': shouldFailWithError 'Invalid net'
  40. '0xfg.0.0.0': shouldFailWithError 'Invalid net'
  41. .export(module)
  42. vows.describe('Ranges that are a power-of-two big, but are not legal blocks')
  43. .addBatch
  44. '218.0.0.0/221.255.255.255': shouldFailWithError 'Invalid mask'
  45. '218.0.0.4/218.0.0.11': shouldFailWithError 'Invalid mask'
  46. .export(module)