index.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict'
  2. const tap = require('tap')
  3. const { BerReader, BerWriter } = require('@ldapjs/asn1')
  4. const controls = require('.')
  5. tap.test('#getControl', t => {
  6. t.test('requires a BER to parse', async t => {
  7. try {
  8. controls.getControl()
  9. t.fail('should throw exception')
  10. } catch (error) {
  11. t.match(error, /ber must be provided/)
  12. }
  13. })
  14. t.test('returns null for empty BER', async t => {
  15. const result = controls.getControl(new BerReader(Buffer.alloc(0)))
  16. t.equal(result, null)
  17. })
  18. t.test('parses a BER (control)', async t => {
  19. const ber = new BerWriter()
  20. ber.startSequence()
  21. ber.writeString('2.16.840.1.113730.3.4.2')
  22. ber.writeBoolean(true)
  23. ber.writeString('foo')
  24. ber.endSequence()
  25. const control = controls.getControl(new BerReader(ber.buffer))
  26. t.ok(control)
  27. t.equal(control.type, '2.16.840.1.113730.3.4.2')
  28. t.ok(control.criticality)
  29. t.equal(control.value.toString('utf8'), 'foo')
  30. t.end()
  31. })
  32. t.test('parses BER with no value', function (t) {
  33. const ber = new BerWriter()
  34. ber.startSequence()
  35. ber.writeString('2.16.840.1.113730.3.4.2')
  36. ber.endSequence()
  37. const control = controls.getControl(new BerReader(ber.buffer))
  38. t.ok(control)
  39. t.equal(control.type, '2.16.840.1.113730.3.4.2')
  40. t.equal(control.criticality, false)
  41. t.notOk(control.value, null)
  42. t.end()
  43. })
  44. t.test('returns a EntryChangeNotificationControl', async t => {
  45. const ecnc = new controls.EntryChangeNotificationControl({
  46. type: controls.EntryChangeNotificationControl.OID,
  47. criticality: true,
  48. value: {
  49. changeType: 8,
  50. previousDN: 'cn=foobarbazcar',
  51. changeNumber: 123456789
  52. }
  53. })
  54. const ber = new BerWriter()
  55. ecnc.toBer(ber)
  56. const c = controls.getControl(new BerReader(ber.buffer))
  57. t.ok(c)
  58. t.equal(c.type, controls.EntryChangeNotificationControl.OID)
  59. t.ok(c.criticality)
  60. t.equal(c.value.changeType, 8)
  61. t.equal(c.value.previousDN, 'cn=foobarbazcar')
  62. t.equal(c.value.changeNumber, 123456789)
  63. })
  64. t.test('returns a PagedResultsControl', async t => {
  65. const prc = new controls.PagedResultsControl({
  66. type: controls.PagedResultsControl.OID,
  67. criticality: true,
  68. value: {
  69. size: 20,
  70. cookie: Buffer.alloc(0)
  71. }
  72. })
  73. const ber = new BerWriter()
  74. prc.toBer(ber)
  75. const c = controls.getControl(new BerReader(ber.buffer))
  76. t.ok(c)
  77. t.equal(c.type, controls.PagedResultsControl.OID)
  78. t.ok(c.criticality)
  79. t.equal(c.value.size, 20)
  80. t.equal(Buffer.compare(c.value.cookie, Buffer.alloc(0)), 0)
  81. })
  82. t.test('returns a PasswordPolicyControl', async t => {
  83. const ppc = new controls.PasswordPolicyControl({
  84. type: controls.PasswordPolicyControl.OID,
  85. criticality: true,
  86. value: {
  87. error: 1,
  88. timeBeforeExpiration: 2
  89. }
  90. })
  91. const ber = new BerWriter()
  92. ppc.toBer(ber)
  93. const c = controls.getControl(new BerReader(ber.buffer))
  94. t.ok(c)
  95. t.equal(c.type, controls.PasswordPolicyControl.OID)
  96. t.ok(c.criticality)
  97. t.equal(c.value.error, 1)
  98. t.equal(c.value.timeBeforeExpiration, 2)
  99. })
  100. t.test('returns a PersistentSearchControl', async t => {
  101. const buf = Buffer.from([
  102. 0x30, 0x26, 0x04, 0x17, 0x32, 0x2e, 0x31, 0x36, 0x2e, 0x38, 0x34, 0x30,
  103. 0x2e, 0x31, 0x2e, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2e, 0x33, 0x2e,
  104. 0x34, 0x2e, 0x33, 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01,
  105. 0xff, 0x01, 0x01, 0xff])
  106. const ber = new BerReader(buf)
  107. const psc = controls.getControl(ber)
  108. t.ok(psc)
  109. t.equal(psc.type, controls.PersistentSearchControl.OID)
  110. t.equal(psc.criticality, false)
  111. t.equal(psc.value.changeTypes, 15)
  112. t.equal(psc.value.changesOnly, true)
  113. t.equal(psc.value.returnECs, true)
  114. })
  115. t.test('returns a ServerSideSortingRequestControl', async t => {
  116. const sssc = new controls.ServerSideSortingRequestControl()
  117. const ber = new BerWriter()
  118. sssc.toBer(ber)
  119. const c = controls.getControl(new BerReader(ber.buffer))
  120. t.ok(c)
  121. t.equal(c.type, controls.ServerSideSortingRequestControl.OID)
  122. t.equal(c.value.length, 0)
  123. })
  124. t.test('returns a ServerSideSortingResponseControl', async t => {
  125. const sssc = new controls.ServerSideSortingResponseControl()
  126. const ber = new BerWriter()
  127. sssc.toBer(ber)
  128. const c = controls.getControl(new BerReader(ber.buffer))
  129. t.ok(c)
  130. t.equal(c.type, controls.ServerSideSortingResponseControl.OID)
  131. t.equal(c.criticality, false)
  132. t.notOk(c.value.result)
  133. t.notOk(c.value.failedAttribute)
  134. })
  135. t.end()
  136. })