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.

282 lines
13 KiB

7 years ago
  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('192.168.2.1').range(), 'private')
  88. test.equal(ipaddr.IPv4.parse('224.100.0.1').range(), 'multicast')
  89. test.equal(ipaddr.IPv4.parse('169.254.15.0').range(), 'linkLocal')
  90. test.equal(ipaddr.IPv4.parse('127.1.1.1').range(), 'loopback')
  91. test.equal(ipaddr.IPv4.parse('255.255.255.255').range(), 'broadcast')
  92. test.equal(ipaddr.IPv4.parse('240.1.2.3').range(), 'reserved')
  93. test.equal(ipaddr.IPv4.parse('8.8.8.8').range(), 'unicast')
  94. test.done()
  95. 'can construct IPv6 from parts': (test) ->
  96. test.doesNotThrow ->
  97. new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  98. test.done()
  99. 'refuses to construct invalid IPv6': (test) ->
  100. test.throws ->
  101. new ipaddr.IPv6([0xfffff, 0, 0, 0, 0, 0, 0, 1])
  102. test.throws ->
  103. new ipaddr.IPv6([0xfffff, 0, 0, 0, 0, 0, 1])
  104. test.done()
  105. 'converts IPv6 to string correctly': (test) ->
  106. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  107. test.equal(addr.toNormalizedString(), '2001:db8:f53a:0:0:0:0:1')
  108. test.equal(addr.toString(), '2001:db8:f53a::1')
  109. test.equal(new ipaddr.IPv6([0, 0, 0, 0, 0, 0, 0, 1]).toString(), '::1')
  110. test.equal(new ipaddr.IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]).toString(), '2001:db8::')
  111. test.done()
  112. 'returns correct kind for IPv6': (test) ->
  113. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  114. test.equal(addr.kind(), 'ipv6')
  115. test.done()
  116. 'allows to access IPv6 address parts': (test) ->
  117. addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 42, 0, 1])
  118. test.equal(addr.parts[5], 42)
  119. test.done()
  120. 'checks IPv6 address format': (test) ->
  121. test.equal(ipaddr.IPv6.isIPv6('2001:db8:F53A::1'), true)
  122. test.equal(ipaddr.IPv6.isIPv6('200001::1'), true)
  123. test.equal(ipaddr.IPv6.isIPv6('::ffff:192.168.1.1'), true)
  124. test.equal(ipaddr.IPv6.isIPv6('::ffff:300.168.1.1'), true)
  125. test.equal(ipaddr.IPv6.isIPv6('::ffff:300.168.1.1:0'), false)
  126. test.equal(ipaddr.IPv6.isIPv6('fe80::wtf'), false)
  127. test.done()
  128. 'validates IPv6 addresses': (test) ->
  129. test.equal(ipaddr.IPv6.isValid('2001:db8:F53A::1'), true)
  130. test.equal(ipaddr.IPv6.isValid('200001::1'), false)
  131. test.equal(ipaddr.IPv6.isValid('::ffff:192.168.1.1'), true)
  132. test.equal(ipaddr.IPv6.isValid('::ffff:300.168.1.1'), false)
  133. test.equal(ipaddr.IPv6.isValid('::ffff:300.168.1.1:0'), false)
  134. test.equal(ipaddr.IPv6.isValid('2001:db8::F53A::1'), false)
  135. test.equal(ipaddr.IPv6.isValid('fe80::wtf'), false)
  136. test.equal(ipaddr.IPv6.isValid('2002::2:'), false)
  137. test.equal(ipaddr.IPv6.isValid(undefined), false)
  138. test.done()
  139. 'parses IPv6 in different formats': (test) ->
  140. test.deepEqual(ipaddr.IPv6.parse('2001:db8:F53A:0:0:0:0:1').parts, [0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
  141. test.deepEqual(ipaddr.IPv6.parse('fe80::10').parts, [0xfe80, 0, 0, 0, 0, 0, 0, 0x10])
  142. test.deepEqual(ipaddr.IPv6.parse('2001:db8:F53A::').parts, [0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 0])
  143. test.deepEqual(ipaddr.IPv6.parse('::1').parts, [0, 0, 0, 0, 0, 0, 0, 1])
  144. test.deepEqual(ipaddr.IPv6.parse('::').parts, [0, 0, 0, 0, 0, 0, 0, 0])
  145. test.done()
  146. 'barfs at invalid IPv6': (test) ->
  147. test.throws ->
  148. ipaddr.IPv6.parse('fe80::0::1')
  149. test.done()
  150. 'matches IPv6 CIDR correctly': (test) ->
  151. addr = ipaddr.IPv6.parse('2001:db8:f53a::1')
  152. test.equal(addr.match(ipaddr.IPv6.parse('::'), 0), true)
  153. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f53a::1:1'), 64), true)
  154. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f53b::1:1'), 48), false)
  155. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f531::1:1'), 44), true)
  156. test.equal(addr.match(ipaddr.IPv6.parse('2001:db8:f500::1'), 40), true)
  157. test.equal(addr.match(ipaddr.IPv6.parse('2001:db9:f500::1'), 40), false)
  158. test.equal(addr.match(addr, 128), true)
  159. test.done()
  160. 'parses IPv6 CIDR correctly': (test) ->
  161. addr = ipaddr.IPv6.parse('2001:db8:f53a::1')
  162. test.equal(addr.match(ipaddr.IPv6.parseCIDR('::/0')), true)
  163. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53a::1:1/64')), true)
  164. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53b::1:1/48')), false)
  165. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f531::1:1/44')), true)
  166. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f500::1/40')), true)
  167. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db9:f500::1/40')), false)
  168. test.equal(addr.match(ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/128')), true)
  169. test.throws ->
  170. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1')
  171. test.throws ->
  172. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/-1')
  173. test.throws ->
  174. ipaddr.IPv6.parseCIDR('2001:db8:f53a::1/129')
  175. test.done()
  176. 'converts between IPv4-mapped IPv6 addresses and IPv4 addresses': (test) ->
  177. addr = ipaddr.IPv4.parse('77.88.21.11')
  178. mapped = addr.toIPv4MappedAddress()
  179. test.deepEqual(mapped.parts, [0, 0, 0, 0, 0, 0xffff, 0x4d58, 0x150b])
  180. test.deepEqual(mapped.toIPv4Address().octets, addr.octets)
  181. test.done()
  182. 'refuses to convert non-IPv4-mapped IPv6 address to IPv4 address': (test) ->
  183. test.throws ->
  184. ipaddr.IPv6.parse('2001:db8::1').toIPv4Address()
  185. test.done()
  186. 'detects reserved IPv6 networks': (test) ->
  187. test.equal(ipaddr.IPv6.parse('::').range(), 'unspecified')
  188. test.equal(ipaddr.IPv6.parse('fe80::1234:5678:abcd:0123').range(), 'linkLocal')
  189. test.equal(ipaddr.IPv6.parse('ff00::1234').range(), 'multicast')
  190. test.equal(ipaddr.IPv6.parse('::1').range(), 'loopback')
  191. test.equal(ipaddr.IPv6.parse('fc00::').range(), 'uniqueLocal')
  192. test.equal(ipaddr.IPv6.parse('::ffff:192.168.1.10').range(), 'ipv4Mapped')
  193. test.equal(ipaddr.IPv6.parse('::ffff:0:192.168.1.10').range(), 'rfc6145')
  194. test.equal(ipaddr.IPv6.parse('64:ff9b::1234').range(), 'rfc6052')
  195. test.equal(ipaddr.IPv6.parse('2002:1f63:45e8::1').range(), '6to4')
  196. test.equal(ipaddr.IPv6.parse('2001::4242').range(), 'teredo')
  197. test.equal(ipaddr.IPv6.parse('2001:db8::3210').range(), 'reserved')
  198. test.equal(ipaddr.IPv6.parse('2001:470:8:66::1').range(), 'unicast')
  199. test.done()
  200. 'is able to determine IP address type': (test) ->
  201. test.equal(ipaddr.parse('8.8.8.8').kind(), 'ipv4')
  202. test.equal(ipaddr.parse('2001:db8:3312::1').kind(), 'ipv6')
  203. test.done()
  204. 'throws an error if tried to parse an invalid address': (test) ->
  205. test.throws ->
  206. ipaddr.parse('::some.nonsense')
  207. test.done()
  208. 'correctly processes IPv4-mapped addresses': (test) ->
  209. test.equal(ipaddr.process('8.8.8.8').kind(), 'ipv4')
  210. test.equal(ipaddr.process('2001:db8:3312::1').kind(), 'ipv6')
  211. test.equal(ipaddr.process('::ffff:192.168.1.1').kind(), 'ipv4')
  212. test.done()
  213. 'correctly converts IPv6 and IPv4 addresses to byte arrays': (test) ->
  214. test.deepEqual(ipaddr.parse('1.2.3.4').toByteArray(),
  215. [0x1, 0x2, 0x3, 0x4]);
  216. # Fuck yeah. The first byte of Google's IPv6 address is 42. 42!
  217. test.deepEqual(ipaddr.parse('2a00:1450:8007::68').toByteArray(),
  218. [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68 ])
  219. test.done()
  220. 'correctly parses 1 as an IPv4 address': (test) ->
  221. test.equal(ipaddr.IPv6.isValid('1'), false)
  222. test.equal(ipaddr.IPv4.isValid('1'), true)
  223. test.deepEqual(new ipaddr.IPv4([0, 0, 0, 1]), ipaddr.parse('1'))
  224. test.done()
  225. 'correctly detects IPv4 and IPv6 CIDR addresses': (test) ->
  226. test.deepEqual([ipaddr.IPv6.parse('fc00::'), 64],
  227. ipaddr.parseCIDR('fc00::/64'))
  228. test.deepEqual([ipaddr.IPv4.parse('1.2.3.4'), 5],
  229. ipaddr.parseCIDR('1.2.3.4/5'))
  230. test.done()
  231. 'does not consider a very large or very small number a valid IP address': (test) ->
  232. test.equal(ipaddr.isValid('4999999999'), false)
  233. test.equal(ipaddr.isValid('-1'), false)
  234. test.done()
  235. 'does not hang on ::8:8:8:8:8:8:8:8:8': (test) ->
  236. test.equal(ipaddr.IPv6.isValid('::8:8:8:8:8:8:8:8:8'), false)
  237. test.done()
  238. 'subnetMatch does not fail on empty range': (test) ->
  239. ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {}, false)
  240. ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {subnet: []}, false)
  241. test.done()
  242. 'subnetMatch returns default subnet on empty range': (test) ->
  243. test.equal(ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {}, false), false)
  244. test.equal(ipaddr.subnetMatch(new ipaddr.IPv4([1,2,3,4]), {subnet: []}, false), false)
  245. test.done()