UI for Zipcoin Blue

ipaddr.test.coffee 25KB

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