search-result-reference.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict'
  2. const tap = require('tap')
  3. const { operations } = require('@ldapjs/protocol')
  4. const { BerReader, BerWriter } = require('@ldapjs/asn1')
  5. const SearchResultReference = require('./search-result-reference')
  6. const {
  7. searchResultReferenceBytes
  8. } = require('./_fixtures/message-byte-arrays')
  9. tap.test('basic', t => {
  10. t.test('constructor no args', async t => {
  11. const expected = {
  12. messageId: 1,
  13. protocolOp: operations.LDAP_RES_SEARCH_REF,
  14. type: 'SearchResultReference',
  15. uri: [],
  16. controls: []
  17. }
  18. const res = new SearchResultReference()
  19. t.strictSame(res.pojo, expected)
  20. t.equal(res.type, 'SearchResultReference')
  21. })
  22. t.test('constructor with args', async t => {
  23. const expected = {
  24. messageId: 1,
  25. protocolOp: operations.LDAP_RES_SEARCH_REF,
  26. type: 'SearchResultReference',
  27. uri: ['ldap://foo', 'ldap://bar'],
  28. controls: []
  29. }
  30. let res = new SearchResultReference({
  31. uri: ['ldap://foo', 'ldap://bar']
  32. })
  33. t.strictSame(res.pojo, expected)
  34. res = new SearchResultReference({
  35. uris: ['ldap://foo', 'ldap://bar']
  36. })
  37. t.strictSame(res.pojo, expected)
  38. })
  39. t.end()
  40. })
  41. tap.test('.uri/.uris', t => {
  42. t.test('sets/gets', async t => {
  43. const res = new SearchResultReference()
  44. t.strictSame(res.uri, [])
  45. t.strictSame(res.uris, [])
  46. res.uri = ['ldap://foo']
  47. t.strictSame(res.uri, ['ldap://foo'])
  48. res.uris = ['ldap://bar']
  49. t.strictSame(res.uris, ['ldap://bar'])
  50. t.strictSame(res.uri, ['ldap://bar'])
  51. })
  52. t.test('throws for bad input', async t => {
  53. const res = new SearchResultReference()
  54. const expected = Error('uri must be an array of strings')
  55. t.throws(
  56. () => {
  57. res.uri = 'ldap://foo'
  58. },
  59. expected
  60. )
  61. t.throws(
  62. () => {
  63. res.uris = 'ldap://foo'
  64. },
  65. expected
  66. )
  67. t.throws(
  68. () => {
  69. res.uri = ['ldap://foo', { foo: 'foo' }, 'ldap://bar']
  70. },
  71. expected
  72. )
  73. })
  74. t.end()
  75. })
  76. tap.test('_toBer', t => {
  77. tap.test('converts instance to BER', async t => {
  78. const res = new SearchResultReference({
  79. uri: [
  80. 'ldap://ds1.example.com:389/dc=example,dc=com??sub?',
  81. 'ldap://ds2.example.com:389/dc=example,dc=com??sub?'
  82. ]
  83. })
  84. const writer = new BerWriter()
  85. res._toBer(writer)
  86. t.equal(
  87. Buffer.from(searchResultReferenceBytes.slice(5)).compare(writer.buffer),
  88. 0
  89. )
  90. })
  91. t.end()
  92. })
  93. tap.test('_pojo', t => {
  94. t.test('returns a pojo representation', async t => {
  95. const res = new SearchResultReference({
  96. uri: [
  97. 'ldap://ds1.example.com:389/dc=example,dc=com??sub?',
  98. 'ldap://ds2.example.com:389/dc=example,dc=com??sub?'
  99. ]
  100. })
  101. t.strictSame(res._pojo(), {
  102. uri: [
  103. 'ldap://ds1.example.com:389/dc=example,dc=com??sub?',
  104. 'ldap://ds2.example.com:389/dc=example,dc=com??sub?'
  105. ]
  106. })
  107. })
  108. t.end()
  109. })
  110. tap.test('#parseToPojo', t => {
  111. t.test('throws if operation incorrect', async t => {
  112. const reqBuffer = Buffer.from(searchResultReferenceBytes)
  113. reqBuffer[5] = 0x61
  114. const reader = new BerReader(reqBuffer)
  115. reader.readSequence()
  116. reader.readInt()
  117. t.throws(
  118. () => SearchResultReference.parseToPojo(reader),
  119. Error('found wrong protocol operation: 0x61')
  120. )
  121. })
  122. t.test('returns a pojo representation', async t => {
  123. const reqBuffer = Buffer.from(searchResultReferenceBytes)
  124. const reader = new BerReader(reqBuffer)
  125. reader.readSequence()
  126. reader.readInt()
  127. const pojo = SearchResultReference.parseToPojo(reader)
  128. t.equal(pojo.protocolOp, operations.LDAP_RES_SEARCH_REF)
  129. t.equal(pojo.uri[0], 'ldap://ds1.example.com:389/dc=example,dc=com??sub?')
  130. t.equal(pojo.uri[1], 'ldap://ds2.example.com:389/dc=example,dc=com??sub?')
  131. })
  132. t.end()
  133. })