persistent-search-control.test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict'
  2. const tap = require('tap')
  3. const { BerWriter } = require('@ldapjs/asn1')
  4. const PSC = require('./persistent-search-control')
  5. const Control = require('../control')
  6. tap.test('contructor', t => {
  7. t.test('new no args', async t => {
  8. const control = new PSC()
  9. t.ok(control)
  10. t.type(control, PSC)
  11. t.type(control, Control)
  12. t.equal(control.type, PSC.OID)
  13. t.same(control.value, {
  14. changeTypes: 15,
  15. changesOnly: true,
  16. returnECs: true
  17. })
  18. })
  19. t.test('new with args', async t => {
  20. const control = new PSC({
  21. type: '2.16.840.1.113730.3.4.3',
  22. criticality: true,
  23. value: {
  24. changeTypes: 1,
  25. changesOnly: false,
  26. returnECs: true
  27. }
  28. })
  29. t.ok(control)
  30. t.equal(control.type, '2.16.840.1.113730.3.4.3')
  31. t.ok(control.criticality)
  32. t.same(control.value, {
  33. changeTypes: 1,
  34. changesOnly: false,
  35. returnECs: true
  36. })
  37. })
  38. t.test('with value buffer', async t => {
  39. const value = new BerWriter()
  40. value.startSequence()
  41. value.writeInt(2)
  42. value.writeBoolean(true)
  43. value.writeBoolean(false)
  44. value.endSequence()
  45. const control = new PSC({ value: value.buffer })
  46. t.same(control.value, {
  47. changeTypes: 2,
  48. changesOnly: true,
  49. returnECs: false
  50. })
  51. })
  52. t.test('throws for bad value', async t => {
  53. t.throws(() => new PSC({ value: 42 }))
  54. })
  55. t.end()
  56. })
  57. tap.test('pojo', t => {
  58. t.test('adds control value', async t => {
  59. const control = new PSC()
  60. t.same(control.pojo, {
  61. type: PSC.OID,
  62. criticality: false,
  63. value: {
  64. changeTypes: 15,
  65. changesOnly: true,
  66. returnECs: true
  67. }
  68. })
  69. })
  70. t.end()
  71. })
  72. tap.test('toBer', t => {
  73. t.test('converts empty instance to BER', async t => {
  74. const target = new BerWriter()
  75. target.startSequence()
  76. target.writeString(PSC.OID)
  77. target.writeBoolean(false) // Control.criticality
  78. const value = new BerWriter()
  79. value.startSequence()
  80. value.writeInt(15)
  81. value.writeBoolean(true)
  82. value.writeBoolean(true)
  83. value.endSequence()
  84. target.writeBuffer(value.buffer, 0x04)
  85. target.endSequence()
  86. const control = new PSC()
  87. const ber = control.toBer()
  88. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  89. })
  90. t.end()
  91. })