ipaddr.test.coffee 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. ipaddr = require '../lib/ipaddr'
  2. module.exports =
  3. 'should define main classes': (test) ->
  4. test.ok(ipaddr.IPv4?, 'defines IPv4 class')
  5. test.ok(ipaddr.IPv6?, 'defines IPv6 class')
  6. test.done()
  7. 'can construct IPv4 from octets': (test) ->
  8. test.doesNotThrow ->
  9. new ipaddr.IPv4([192, 168, 1, 2])
  10. test.done()
  11. 'refuses to construct invalid IPv4': (test) ->
  12. test.throws ->
  13. new ipaddr.IPv4([300, 1, 2, 3])
  14. test.throws ->
  15. new ipaddr.IPv4([8, 8, 8])
  16. test.done()
  17. 'converts IPv4 to string correctly': (test) ->
  18. addr = new ipaddr.IPv4([192, 168, 1, 1])
  19. test.equal(addr.toString(), '192.168.1.1')
  20. test.done()
  21. 'returns correct kind for IPv4': (test) ->
  22. addr = new ipaddr.IPv4([1, 2, 3, 4])
  23. test.equal(addr.kind(), 'ipv4')
  24. test.done()
  25. 'allows to access IPv4 octets': (test) ->
  26. addr = new ipaddr.IPv4([42, 0, 0, 0])
  27. test.equal(addr.octets[0], 42)
  28. test.done()
  29. 'checks IPv4 address format': (test) ->
  30. test.equal(ipaddr.IPv4.isIPv4('192.168.007.0xa'), true)
  31. test.equal(ipaddr.IPv4.isIPv4('1024.0.0.1'), true)
  32. test.equal(ipaddr.IPv4.isIPv4('8.0xa.wtf.6'), false)
  33. test.done()
  34. 'validates IPv4 addresses': (test) ->
  35. test.equal(ipaddr.IPv4.isValid('192.168.007.0xa'), true)
  36. test.equal(ipaddr.IPv4.isValid('1024.0.0.1'), false)
  37. test.equal(ipaddr.IPv4.isValid('8.0xa.wtf.6'), false)
  38. test.done()
  39. 'parses IPv4 in several weird formats': (test) ->
  40. test.deepEqual(ipaddr.IPv4.parse('192.168.1.1').octets, [192, 168, 1, 1])
  41. test.deepEqual(ipaddr.IPv4.parse('0xc0.168.1.1').octets, [192, 168, 1, 1])
  42. test.deepEqual(ipaddr.IPv4.parse('192.0250.1.1').octets, [192, 168, 1, 1])
  43. test.deepEqual(ipaddr.IPv4.parse('0xc0a80101').octets, [192, 168, 1, 1])
  44. test.deepEqual(ipaddr.IPv4.parse('030052000401').octets, [192, 168, 1, 1])
  45. test.deepEqual(ipaddr.IPv4.parse('3232235777').octets, [192, 168, 1, 1])
  46. test.done()
  47. 'barfs at invalid IPv4': (test) ->
  48. test.throws ->
  49. ipaddr.IPv4.parse('10.0.0.wtf')
  50. test.done()
  51. 'matches IPv4 CIDR correctly': (test) ->
  52. addr = new ipaddr.IPv4([10, 5, 0, 1])
  53. test.equal(addr.match(ipaddr.IPv4.parse('0.0.0.0'), 0), true)
  54. test.equal(addr.match(ipaddr.IPv4.parse('11.0.0.0'), 8), false)
  55. test.equal(addr.match(ipaddr.IPv4.parse('10.0.0.0'), 8), true)
  56. test.equal(addr.match(ipaddr.IPv4.parse('10.0.0.1'), 8), true)
  57. test.equal(addr.match(ipaddr.IPv4.parse('10.0.0.10'), 8), true)
  58. test.equal(addr.match(ipaddr.IPv4.parse('10.5.5.0'), 16), true)
  59. test.equal(addr.match(ipaddr.IPv4.parse('10.4.5.0'), 16), false)
  60. test.equal(addr.match(ipaddr.IPv4.parse('10.4.5.0'), 15), true)
  61. test.equal(addr.match(ipaddr.IPv4.parse('10.5.0.2'), 32), false)
  62. test.equal(addr.match(addr, 32), true)
  63. test.done()
  64. 'parses IPv4 CIDR correctly': (test) ->
  65. addr = new ipaddr.IPv4([10, 5, 0, 1])
  66. test.equal(addr.match(ipaddr.IPv4.parseCIDR('0.0.0.0/0')), true)
  67. test.equal(addr.match(ipaddr.IPv4.parseCIDR('11.0.0.0/8')), false)
  68. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.0.0.0/8')), true)
  69. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.0.0.1/8')), true)
  70. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.0.0.10/8')), true)
  71. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.5.5.0/16')), true)
  72. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.4.5.0/16')), false)
  73. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.4.5.0/15')), true)
  74. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.5.0.2/32')), false)
  75. test.equal(addr.match(ipaddr.IPv4.parseCIDR('10.5.0.1/32')), true)
  76. test.throws ->
  77. ipaddr.IPv4.parseCIDR('10.5.0.1')
  78. test.throws ->
  79. ipaddr.IPv4.parseCIDR('0.0.0.0/-1')
  80. test.throws ->
  81. ipaddr.IPv4.parseCIDR('0.0.0.0/33')
  82. test.done()
  83. 'detects reserved IPv4 networks': (test) ->
  84. test.equal(ipaddr.IPv4.parse('0.0.0.0').range(), 'unspecified')
  85. test.equal(ipaddr.IPv4.parse('0.1.0.0').range(), 'unspecified')
  86. test.equal(ipaddr.IPv4.parse('10.1.0.1').range(), 'private')
  87. test.equal(ipaddr.IPv4.parse('100.64.0.0').range(), 'carrierGradeNat')
  88. test.equal(ipaddr.IPv4.parse('100.127.255.255').range(), 'carrierGradeNat')
  89. test.equal(ipaddr.IPv4.parse('192.168.2.1').range(), 'private')
  90. test.equal(ipaddr.IPv4.parse('224.100.0.1').range(), 'multicast')
  91. test.equal(ipaddr.IPv4.parse('169.254.15.0').range(), 'linkLocal')
  92. test.equal(ipaddr.IPv4.parse('127.1.1.1').range(), 'loopback')
  93. test.equal(ipaddr.IPv4.parse('255.255.255.255').range(), 'broadcast')
  94. test.equal(ipaddr.IPv4.parse('240.1.2.3').range(), 'reserved')
  95. test.equal(ipaddr.IPv4.parse('8.8.8.8').range(), 'unicast')
  96. test.done()
  97. 'checks the conventional IPv4 address format': (test) ->
  98. test.equal(ipaddr.IPv4.isValidFourPartDecimal('192.168.1.1'), true)
  99. test.equal(ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'), false)
  100. test.done()
  101. 'can construct IPv6 from 16bit parts': (test) ->
  102. test.doesNotThrow ->
  103. new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  104. test.done()
  105. 'can construct IPv6 from 8bit parts': (test) ->
  106. test.doesNotThrow ->
  107. new ipaddr.IPv6([0x20, 0x01, 0xd, 0xb8, 0xf5, 0x3a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  108. test.deepEqual(new ipaddr.IPv6([0x20, 0x01, 0xd, 0xb8, 0xf5, 0x3a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
  109. new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1]))
  110. test.done()
  111. 'refuses to construct invalid IPv6': (test) ->
  112. test.throws ->
  113. new ipaddr.IPv6([0xfffff, 0, 0, 0, 0, 0, 0, 1])
  114. test.throws ->
  115. new ipaddr.IPv6([0xfffff, 0, 0, 0, 0, 0, 1])
  116. test.throws ->
  117. new ipaddr.IPv6([0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  118. test.done()
  119. 'converts IPv6 to string correctly': (test) ->
  120. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  121. test.equal(addr.toNormalizedString(), '2001:db8:f53a:0:0:0:0:1')
  122. test.equal(addr.toString(), '2001:db8:f53a::1')
  123. test.equal(new ipaddr.IPv6([0, 0, 0, 0, 0, 0, 0, 1]).toString(), '::1')
  124. test.equal(new ipaddr.IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]).toString(), '2001:db8::')
  125. test.done()
  126. 'returns correct kind for IPv6': (test) ->
  127. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  128. test.equal(addr.kind(), 'ipv6')
  129. test.done()
  130. 'allows to access IPv6 address parts': (test) ->
  131. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 42, 0, 1])
  132. test.equal(addr.parts[5], 42)
  133. test.done()
  134. 'checks IPv6 address format': (test) ->
  135. test.equal(ipaddr.IPv6.isIPv6('2001:db8:F53A::1'), true)
  136. test.equal(ipaddr.IPv6.isIPv6('200001::1'), true)
  137. test.equal(ipaddr.IPv6.isIPv6('::ffff:192.168.1.1'), true)
  138. test.equal(ipaddr.IPv6.isIPv6('::ffff:300.168.1.1'), false)
  139. test.equal(ipaddr.IPv6.isIPv6('::ffff:300.168.1.1:0'), false)
  140. test.equal(ipaddr.IPv6.isIPv6('fe80::wtf'), false)
  141. test.done()
  142. 'validates IPv6 addresses': (test) ->
  143. test.equal(ipaddr.IPv6.isValid('2001:db8:F53A::1'), true)
  144. test.equal(ipaddr.IPv6.isValid('200001::1'), false)
  145. test.equal(ipaddr.IPv6.isValid('::ffff:192.168.1.1'), true)
  146. test.equal(ipaddr.IPv6.isValid('::ffff:300.168.1.1'), false)
  147. test.equal(ipaddr.IPv6.isValid('::ffff:300.168.1.1:0'), false)
  148. test.equal(ipaddr.IPv6.isValid('::ffff:222.1.41.9000'), false)
  149. test.equal(ipaddr.IPv6.isValid('2001:db8::F53A::1'), false)
  150. test.equal(ipaddr.IPv6.isValid('fe80::wtf'), false)
  151. test.equal(ipaddr.IPv6.isValid('2002::2:'), false)
  152. test.equal(ipaddr.IPv6.isValid(undefined), false)
  153. test.done()
  154. 'parses IPv6 in different formats': (test) ->
  155. test.deepEqual(ipaddr.IPv6.parse('2001:db8:F53A:0:0:0:0:1').parts, [0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  156. test.deepEqual(ipaddr.IPv6.parse('fe80::10').parts, [0xfe80, 0, 0, 0, 0, 0, 0, 0x10])
  157. test.deepEqual(ipaddr.IPv6.parse('2001:db8:F53A::').parts, [0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 0])
  158. test.deepEqual(ipaddr.IPv6.parse('::1').parts, [0, 0, 0, 0, 0, 0, 0, 1])
  159. test.deepEqual(ipaddr.IPv6.parse('::').parts, [0, 0, 0, 0, 0, 0, 0, 0])
  160. test.done()
  161. 'barfs at invalid IPv6': (test) ->
  162. test.throws ->
  163. ipaddr.IPv6.parse('fe80::0::1')
  164. test.done()
  165. 'matches IPv6 CIDR correctly': (test) ->
  166. addr = ipaddr.IPv6.parse('2001:db8:f53a::1')
  167. test.equal(addr.match(ipaddr.IPv6.parse('::'), 0), true)
  168. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f53a::1:1'), 64), true)
  169. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f53b::1:1'), 48), false)
  170. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f531::1:1'), 44), true)
  171. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f500::1'), 40), true)
  172. test.equal(addr.match(ipaddr.IPv6.parse('2001:db9:f500::1'), 40), false)
  173. test.equal(addr.match(addr, 128), true)
  174. test.done()
  175. 'parses IPv6 CIDR correctly': (test) ->
  176. addr = ipaddr.IPv6.parse('2001:db8:f53a::1')
  177. test.equal(addr.match(ipaddr.IPv6.parseCIDR('::/0')), true)
  178. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53a::1:1/64')), true)
  179. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53b::1:1/48')), false)
  180. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f531::1:1/44')), true)
  181. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f500::1/40')), true)
  182. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db9:f500::1/40')), false)
  183. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/128')), true)
  184. test.throws ->
  185. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1')
  186. test.throws ->
  187. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/-1')
  188. test.throws ->
  189. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/129')
  190. test.done()
  191. 'converts between IPv4-mapped IPv6 addresses and IPv4 addresses': (test) ->
  192. addr = ipaddr.IPv4.parse('77.88.21.11')
  193. mapped = addr.toIPv4MappedAddress()
  194. test.deepEqual(mapped.parts, [0, 0, 0, 0, 0, 0xffff, 0x4d58, 0x150b])
  195. test.deepEqual(mapped.toIPv4Address().octets, addr.octets)
  196. test.done()
  197. 'refuses to convert non-IPv4-mapped IPv6 address to IPv4 address': (test) ->
  198. test.throws ->
  199. ipaddr.IPv6.parse('2001:db8::1').toIPv4Address()
  200. test.done()
  201. 'detects reserved IPv6 networks': (test) ->
  202. test.equal(ipaddr.IPv6.parse('::').range(), 'unspecified')
  203. test.equal(ipaddr.IPv6.parse('fe80::1234:5678:abcd:0123').range(), 'linkLocal')
  204. test.equal(ipaddr.IPv6.parse('ff00::1234').range(), 'multicast')
  205. test.equal(ipaddr.IPv6.parse('::1').range(), 'loopback')
  206. test.equal(ipaddr.IPv6.parse('fc00::').range(), 'uniqueLocal')
  207. test.equal(ipaddr.IPv6.parse('::ffff:192.168.1.10').range(), 'ipv4Mapped')
  208. test.equal(ipaddr.IPv6.parse('::ffff:0:192.168.1.10').range(), 'rfc6145')
  209. test.equal(ipaddr.IPv6.parse('64:ff9b::1234').range(), 'rfc6052')
  210. test.equal(ipaddr.IPv6.parse('2002:1f63:45e8::1').range(), '6to4')
  211. test.equal(ipaddr.IPv6.parse('2001::4242').range(), 'teredo')
  212. test.equal(ipaddr.IPv6.parse('2001:db8::3210').range(), 'reserved')
  213. test.equal(ipaddr.IPv6.parse('2001:470:8:66::1').range(), 'unicast')
  214. test.done()
  215. 'is able to determine IP address type': (test) ->
  216. test.equal(ipaddr.parse('8.8.8.8').kind(), 'ipv4')
  217. test.equal(ipaddr.parse('2001:db8:3312::1').kind(), 'ipv6')
  218. test.done()
  219. 'throws an error if tried to parse an invalid address': (test) ->
  220. test.throws ->
  221. ipaddr.parse('::some.nonsense')
  222. test.done()
  223. 'correctly processes IPv4-mapped addresses': (test) ->
  224. test.equal(ipaddr.process('8.8.8.8').kind(), 'ipv4')
  225. test.equal(ipaddr.process('2001:db8:3312::1').kind(), 'ipv6')
  226. test.equal(ipaddr.process('::ffff:192.168.1.1').kind(), 'ipv4')
  227. test.done()
  228. 'correctly converts IPv6 and IPv4 addresses to byte arrays': (test) ->
  229. test.deepEqual(ipaddr.parse('1.2.3.4').toByteArray(),
  230. [0x1, 0x2, 0x3, 0x4]);
  231. # Fuck yeah. The first byte of Google's IPv6 address is 42. 42!
  232. test.deepEqual(ipaddr.parse('2a00:1450:8007::68').toByteArray(),
  233. [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68 ])
  234. test.done()
  235. 'correctly parses 1 as an IPv4 address': (test) ->
  236. test.equal(ipaddr.IPv6.isValid('1'), false)
  237. test.equal(ipaddr.IPv4.isValid('1'), true)
  238. test.deepEqual(new ipaddr.IPv4([0, 0, 0, 1]), ipaddr.parse('1'))
  239. test.done()
  240. 'correctly detects IPv4 and IPv6 CIDR addresses': (test) ->
  241. test.deepEqual([ipaddr.IPv6.parse('fc00::'), 64],
  242. ipaddr.parseCIDR('fc00::/64'))
  243. test.deepEqual([ipaddr.IPv4.parse('1.2.3.4'), 5],
  244. ipaddr.parseCIDR('1.2.3.4/5'))
  245. test.done()
  246. 'does not consider a very large or very small number a valid IP address': (test) ->
  247. test.equal(ipaddr.isValid('4999999999'), false)
  248. test.equal(ipaddr.isValid('-1'), false)
  249. test.done()
  250. 'does not hang on ::8:8:8:8:8:8:8:8:8': (test) ->
  251. test.equal(ipaddr.IPv6.isValid('::8:8:8:8:8:8:8:8:8'), false)
  252. test.done()
  253. 'subnetMatch does not fail on empty range': (test) ->
  254. ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {}, false)
  255. ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {subnet: []}, false)
  256. test.done()
  257. 'subnetMatch returns default subnet on empty range': (test) ->
  258. test.equal(ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {}, false), false)
  259. test.equal(ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {subnet: []}, false), false)
  260. test.done()
  261. 'is able to determine IP address type from byte array input': (test) ->
  262. test.equal(ipaddr.fromByteArray([0x7f, 0, 0, 1]).kind(), 'ipv4')
  263. test.equal(ipaddr.fromByteArray([0x20, 0x01, 0xd, 0xb8, 0xf5, 0x3a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).kind(), 'ipv6')
  264. test.throws ->
  265. ipaddr.fromByteArray([1])
  266. test.done()
  267. 'prefixLengthFromSubnetMask returns proper CIDR notation for standard IPv4 masks': (test) ->
  268. test.equal(ipaddr.IPv4.parse('255.255.255.255').prefixLengthFromSubnetMask(), 32)
  269. test.equal(ipaddr.IPv4.parse('255.255.255.254').prefixLengthFromSubnetMask(), 31)
  270. test.equal(ipaddr.IPv4.parse('255.255.255.252').prefixLengthFromSubnetMask(), 30)
  271. test.equal(ipaddr.IPv4.parse('255.255.255.248').prefixLengthFromSubnetMask(), 29)
  272. test.equal(ipaddr.IPv4.parse('255.255.255.240').prefixLengthFromSubnetMask(), 28)
  273. test.equal(ipaddr.IPv4.parse('255.255.255.224').prefixLengthFromSubnetMask(), 27)
  274. test.equal(ipaddr.IPv4.parse('255.255.255.192').prefixLengthFromSubnetMask(), 26)
  275. test.equal(ipaddr.IPv4.parse('255.255.255.128').prefixLengthFromSubnetMask(), 25)
  276. test.equal(ipaddr.IPv4.parse('255.255.255.0').prefixLengthFromSubnetMask(), 24)
  277. test.equal(ipaddr.IPv4.parse('255.255.254.0').prefixLengthFromSubnetMask(), 23)
  278. test.equal(ipaddr.IPv4.parse('255.255.252.0').prefixLengthFromSubnetMask(), 22)
  279. test.equal(ipaddr.IPv4.parse('255.255.248.0').prefixLengthFromSubnetMask(), 21)
  280. test.equal(ipaddr.IPv4.parse('255.255.240.0').prefixLengthFromSubnetMask(), 20)
  281. test.equal(ipaddr.IPv4.parse('255.255.224.0').prefixLengthFromSubnetMask(), 19)
  282. test.equal(ipaddr.IPv4.parse('255.255.192.0').prefixLengthFromSubnetMask(), 18)
  283. test.equal(ipaddr.IPv4.parse('255.255.128.0').prefixLengthFromSubnetMask(), 17)
  284. test.equal(ipaddr.IPv4.parse('255.255.0.0').prefixLengthFromSubnetMask(), 16)
  285. test.equal(ipaddr.IPv4.parse('255.254.0.0').prefixLengthFromSubnetMask(), 15)
  286. test.equal(ipaddr.IPv4.parse('255.252.0.0').prefixLengthFromSubnetMask(), 14)
  287. test.equal(ipaddr.IPv4.parse('255.248.0.0').prefixLengthFromSubnetMask(), 13)
  288. test.equal(ipaddr.IPv4.parse('255.240.0.0').prefixLengthFromSubnetMask(), 12)
  289. test.equal(ipaddr.IPv4.parse('255.224.0.0').prefixLengthFromSubnetMask(), 11)
  290. test.equal(ipaddr.IPv4.parse('255.192.0.0').prefixLengthFromSubnetMask(), 10)
  291. test.equal(ipaddr.IPv4.parse('255.128.0.0').prefixLengthFromSubnetMask(), 9)
  292. test.equal(ipaddr.IPv4.parse('255.0.0.0').prefixLengthFromSubnetMask(), 8)
  293. test.equal(ipaddr.IPv4.parse('254.0.0.0').prefixLengthFromSubnetMask(), 7)
  294. test.equal(ipaddr.IPv4.parse('252.0.0.0').prefixLengthFromSubnetMask(), 6)
  295. test.equal(ipaddr.IPv4.parse('248.0.0.0').prefixLengthFromSubnetMask(), 5)
  296. test.equal(ipaddr.IPv4.parse('240.0.0.0').prefixLengthFromSubnetMask(), 4)
  297. test.equal(ipaddr.IPv4.parse('224.0.0.0').prefixLengthFromSubnetMask(), 3)
  298. test.equal(ipaddr.IPv4.parse('192.0.0.0').prefixLengthFromSubnetMask(), 2)
  299. test.equal(ipaddr.IPv4.parse('128.0.0.0').prefixLengthFromSubnetMask(), 1)
  300. test.equal(ipaddr.IPv4.parse('0.0.0.0').prefixLengthFromSubnetMask(), 0)
  301. # negative cases
  302. test.equal(ipaddr.IPv4.parse('192.168.255.0').prefixLengthFromSubnetMask(), null)
  303. test.equal(ipaddr.IPv4.parse('255.0.255.0').prefixLengthFromSubnetMask(), null)
  304. test.done()
  305. 'subnetMaskFromPrefixLength returns correct subnet mask given prefix length': (test) ->
  306. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("0"), "0.0.0.0");
  307. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("1"), "128.0.0.0")
  308. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("2"), "192.0.0.0")
  309. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("3"), "224.0.0.0")
  310. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("4"), "240.0.0.0")
  311. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("5"), "248.0.0.0")
  312. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("6"), "252.0.0.0")
  313. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("7"), "254.0.0.0")
  314. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("8"), "255.0.0.0");
  315. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("9"), "255.128.0.0")
  316. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("10"), "255.192.0.0")
  317. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("11"), "255.224.0.0")
  318. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("12"), "255.240.0.0")
  319. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("13"), "255.248.0.0")
  320. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("14"), "255.252.0.0")
  321. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("15"), "255.254.0.0")
  322. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("16"), "255.255.0.0")
  323. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("17"), "255.255.128.0")
  324. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("18"), "255.255.192.0")
  325. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("19"), "255.255.224.0")
  326. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("20"), "255.255.240.0")
  327. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("21"), "255.255.248.0")
  328. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("22"), "255.255.252.0")
  329. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("23"), "255.255.254.0")
  330. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("24"), "255.255.255.0")
  331. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("25"), "255.255.255.128")
  332. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("26"), "255.255.255.192")
  333. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("27"), "255.255.255.224")
  334. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("28"), "255.255.255.240")
  335. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("29"), "255.255.255.248")
  336. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("30"), "255.255.255.252")
  337. test.equal(ipaddr.IPv4.subnetMaskFromPrefixLength("31"), "255.255.255.254")
  338. test.done()
  339. 'broadcastAddressFromCIDR returns correct broadcast address': (test) ->
  340. test.equal(ipaddr.IPv4.broadcastAddressFromCIDR("172.0.0.1/24"), "172.0.0.255")
  341. test.equal(ipaddr.IPv4.broadcastAddressFromCIDR("172.0.0.1/26"), "172.0.0.63")
  342. test.done()
  343. 'networkAddressFromCIDR returns correct network address': (test) ->
  344. test.equal(ipaddr.IPv4.networkAddressFromCIDR("172.0.0.1/24"), "172.0.0.0")
  345. test.equal(ipaddr.IPv4.networkAddressFromCIDR("172.0.0.1/5"), "168.0.0.0")
  346. test.done()