ipv6-validation.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict'
  2. const test = require('tape')
  3. const fastURI = require('..')
  4. const HOST_ERROR = 'URI host is malformed.'
  5. const malformedLiterals = [
  6. '::not-valid',
  7. 'fc00::not-hex',
  8. 'fe80::not-hex',
  9. '1:2:3',
  10. '1:2:3:4:5:6:7',
  11. '1:2:3:4:5:6:7:8:9',
  12. '1::2::3',
  13. '1:::2',
  14. ':::1',
  15. '12345::',
  16. '1:2:3:4:5:6:7::8',
  17. '::ffff:192.0.2.999',
  18. '::ffff:192.0.2',
  19. '::ffff:192.168.001.1',
  20. '::192.0.2.1:1',
  21. '1:2:3:4:5:192.0.2.1::',
  22. 'v.foo',
  23. 'v1.',
  24. 'v1.foo%25bar',
  25. 'v1.K',
  26. 'fe80::1%25',
  27. 'fe80::1%25eth 0',
  28. 'fe80::1%25eth%ZZ',
  29. 'fe80::1%25K',
  30. 'not-an-ip'
  31. ]
  32. test('malformed bracketed IP literals fail without being rewritten', (t) => {
  33. for (const literal of malformedLiterals) {
  34. const uri = `http://[${literal}]/private`
  35. const parsed = fastURI.parse(uri)
  36. t.equal(parsed.error, HOST_ERROR, `parse rejects ${literal}`)
  37. t.equal(parsed.host, `[${literal.toLowerCase()}]`, `parse does not truncate ${literal}`)
  38. t.equal(fastURI.normalize(uri), uri, `normalize preserves ${literal}`)
  39. t.equal(fastURI.equal(uri, uri), false, `equal rejects ${literal}`)
  40. }
  41. t.end()
  42. })
  43. test('resolve throws for malformed bracketed IP literals', (t) => {
  44. for (const literal of malformedLiterals) {
  45. const uri = `http://[${literal}]/private`
  46. t.throws(
  47. () => fastURI.resolve(uri, 'child'),
  48. /URI host is malformed\./,
  49. `rejects malformed base ${literal}`
  50. )
  51. t.throws(
  52. () => fastURI.resolve('http://example.com/', uri),
  53. /URI host is malformed\./,
  54. `rejects malformed relative input ${literal}`
  55. )
  56. }
  57. t.end()
  58. })
  59. test('valid IPv6, IPvFuture, embedded IPv4, and zone forms normalize safely', (t) => {
  60. const cases = [
  61. ['http://[::]/', 'http://[::]/', '::'],
  62. ['http://[::1]/', 'http://[::1]/', '::1'],
  63. ['http://[1::]/', 'http://[1::]/', '1::'],
  64. ['http://[2001:0DB8::0001]/', 'http://[2001:db8::1]/', '2001:db8::1'],
  65. ['http://[0:0:0:0:0:0:0:0]/', 'http://[::]/', '::'],
  66. ['http://[::ffff:192.0.2.1]/', 'http://[::ffff:192.0.2.1]/', '::ffff:192.0.2.1'],
  67. ['http://[1:2:3:4:5:6:192.0.2.1]/', 'http://[1:2:3:4:5:6:192.0.2.1]/', '1:2:3:4:5:6:192.0.2.1'],
  68. ['http://[fe80::A%25EN1]/', 'http://[fe80::a%25EN1]/', 'fe80::a%EN1'],
  69. ['http://[fe80::a%en1]/', 'http://[fe80::a%25en1]/', 'fe80::a%en1'],
  70. ['http://[fe80::a%25eth%2D0]/', 'http://[fe80::a%25eth%2D0]/', 'fe80::a%eth%2D0'],
  71. ['http://[v1.example]/', 'http://[v1.example]/', '[v1.example]'],
  72. ['http://[vF.A:b]/', 'http://[vf.a:b]/', '[vf.a:b]']
  73. ]
  74. for (const [uri, normalized, host] of cases) {
  75. const parsed = fastURI.parse(uri)
  76. t.equal(parsed.error, undefined, `${uri} parses without error`)
  77. t.equal(parsed.host, host, `${uri} has the expected host`)
  78. t.equal(fastURI.normalize(uri), normalized, `${uri} normalizes safely`)
  79. }
  80. t.end()
  81. })