read-attribute-value.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. const tap = require('tap')
  3. const readAttributeValue = require('./read-attribute-value')
  4. const { BerReader } = require('@ldapjs/asn1')
  5. const missingError = Error('attribute value does not start with equals sign')
  6. tap.test('throws if missing equals sign', async t => {
  7. let input = Buffer.from('foo')
  8. t.throws(
  9. () => readAttributeValue({ searchBuffer: input, startPos: 3 }),
  10. missingError
  11. )
  12. input = Buffer.from('foo ≠')
  13. t.throws(
  14. () => readAttributeValue({ searchBuffer: input, startPos: 3 }),
  15. missingError
  16. )
  17. })
  18. tap.test('handles empty attribute value', async t => {
  19. const input = Buffer.from('foo=')
  20. const result = readAttributeValue({ searchBuffer: input, startPos: 3 })
  21. t.same(result, { endPos: 4, value: '' })
  22. })
  23. tap.test('returns attribute value', async t => {
  24. const input = Buffer.from('foo=bar')
  25. const result = readAttributeValue({ searchBuffer: input, startPos: 3 })
  26. t.same(result, { endPos: 7, value: 'bar' })
  27. })
  28. tap.test('quoted values', t => {
  29. t.test('throws if quote is unexpected', async t => {
  30. const input = Buffer.from('=foo"bar')
  31. t.throws(
  32. () => readAttributeValue({ searchBuffer: input, startPos: 0 }),
  33. 'unexpected quote (") in rdn string at position 4'
  34. )
  35. })
  36. t.test('handles a basic quoted value', async t => {
  37. const input = Buffer.from('="bar"')
  38. const result = readAttributeValue({ searchBuffer: input, startPos: 0 })
  39. t.same(result, { endPos: 6, value: 'bar' })
  40. })
  41. t.test('handles quote followed by end char', async t => {
  42. const input = Buffer.from('="bar",another=rdn')
  43. const result = readAttributeValue({ searchBuffer: input, startPos: 0 })
  44. t.same(result, { endPos: 6, value: 'bar' })
  45. })
  46. t.test('significant spaces in quoted values are part of the value', async t => {
  47. const input = Buffer.from('="foo bar "')
  48. const result = readAttributeValue({ searchBuffer: input, startPos: 0 })
  49. t.same(result, { endPos: 13, value: 'foo bar' })
  50. })
  51. t.test('throws if next significant char is not an end char', async t => {
  52. const input = Buffer.from('="foo bar" baz')
  53. t.throws(
  54. () => readAttributeValue({ searchBuffer: input, startPos: 0 }),
  55. 'significant rdn character found outside of quotes at position 7'
  56. )
  57. })
  58. t.test('throws if ending quote not found', async t => {
  59. const input = Buffer.from('="foo')
  60. t.throws(
  61. () => readAttributeValue({ searchBuffer: input, startPos: 0 }),
  62. 'missing ending double quote for attribute value'
  63. )
  64. })
  65. t.end()
  66. })
  67. tap.test('leading and trailing spaces are omitted', async t => {
  68. const input = Buffer.from('= foo ')
  69. const result = readAttributeValue({ searchBuffer: input, startPos: 0 })
  70. t.same(result, { endPos: 9, value: 'foo' })
  71. })
  72. tap.test('parses escaped attribute values', async t => {
  73. const input = Buffer.from('foo=foo\\#bar')
  74. const result = readAttributeValue({ searchBuffer: input, startPos: 3 })
  75. t.same(result, { endPos: 12, value: 'foo#bar' })
  76. })
  77. tap.test('stops reading at all ending characters', async t => {
  78. const tests = [
  79. { input: '=foo,bar', expected: { endPos: 4, value: 'foo' } },
  80. { input: '=foo+bar', expected: { endPos: 4, value: 'foo' } },
  81. { input: '=foo;bar', expected: { endPos: 4, value: 'foo' } }
  82. ]
  83. for (const test of tests) {
  84. const result = readAttributeValue({
  85. searchBuffer: Buffer.from(test.input),
  86. startPos: 0
  87. })
  88. t.same(result, test.expected)
  89. }
  90. })
  91. tap.test('reads hex encoded string', async t => {
  92. const input = Buffer.from('=#0403666f6f')
  93. const result = readAttributeValue({ searchBuffer: input, startPos: 0 })
  94. const expected = {
  95. endPos: 12,
  96. value: new BerReader(Buffer.from([0x04, 0x03, 0x66, 0x6f, 0x6f]))
  97. }
  98. t.same(result, expected)
  99. t.equal(result.value.buffer.compare(expected.value.buffer), 0)
  100. })