control.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict'
  2. const tap = require('tap')
  3. const { BerWriter } = require('@ldapjs/asn1')
  4. const Control = require('./control')
  5. tap.test('constructor', t => {
  6. t.test('new no args', function (t) {
  7. t.ok(new Control())
  8. t.equal(Object.prototype.toString.call(new Control()), '[object LdapControl]')
  9. t.end()
  10. })
  11. t.test('new with args', function (t) {
  12. const c = new Control({
  13. type: '2.16.840.1.113730.3.4.2',
  14. criticality: true
  15. })
  16. t.ok(c)
  17. t.equal(c.type, '2.16.840.1.113730.3.4.2')
  18. t.ok(c.criticality)
  19. t.end()
  20. })
  21. t.end()
  22. })
  23. tap.test('pojo', t => {
  24. t.test('passes through _pojo', async t => {
  25. class Foo extends Control {
  26. _pojo (obj) {
  27. obj.foo = 'foo'
  28. }
  29. }
  30. const control = new Foo()
  31. t.strictSame(control.pojo, {
  32. type: '',
  33. value: null,
  34. criticality: false,
  35. foo: 'foo'
  36. })
  37. })
  38. t.test('returns basic object', async t => {
  39. const control = new Control({ type: '1.2.3', criticality: false, value: 'foo' })
  40. t.strictSame(control.pojo, {
  41. type: '1.2.3',
  42. value: 'foo',
  43. criticality: false
  44. })
  45. })
  46. t.end()
  47. })
  48. tap.test('toBer', t => {
  49. t.test('converts empty instance to BER', async t => {
  50. const target = new BerWriter()
  51. target.startSequence()
  52. target.writeString('')
  53. target.writeBoolean(false)
  54. target.endSequence()
  55. const control = new Control()
  56. const ber = control.toBer()
  57. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  58. })
  59. t.test('converts instance to BER', async t => {
  60. const target = new BerWriter()
  61. target.startSequence()
  62. target.writeString('2.16.840.1.113730.3.4.2')
  63. target.writeBoolean(true)
  64. target.writeString('foo')
  65. target.endSequence()
  66. const control = new Control({
  67. type: '2.16.840.1.113730.3.4.2',
  68. criticality: true,
  69. value: Buffer.from('foo', 'utf8')
  70. })
  71. const ber = control.toBer()
  72. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  73. })
  74. t.test('converts instance to BER (side effect manner)', async t => {
  75. const target = new BerWriter()
  76. target.startSequence()
  77. target.writeString('2.16.840.1.113730.3.4.2')
  78. target.writeBoolean(true)
  79. target.writeString('foo')
  80. target.endSequence()
  81. const control = new Control({
  82. type: '2.16.840.1.113730.3.4.2',
  83. criticality: true,
  84. value: Buffer.from('foo', 'utf8')
  85. })
  86. const ber = new BerWriter()
  87. control.toBer(ber)
  88. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  89. })
  90. t.test('converts instance to BER with string value', async t => {
  91. const target = new BerWriter()
  92. target.startSequence()
  93. target.writeString('2.16.840.1.113730.3.4.2')
  94. target.writeBoolean(true)
  95. target.writeString('foo')
  96. target.endSequence()
  97. const control = new Control({
  98. type: '2.16.840.1.113730.3.4.2',
  99. criticality: true,
  100. value: 'foo'
  101. })
  102. const ber = control.toBer()
  103. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  104. })
  105. t.test('ignores unrecognized value', async t => {
  106. const target = new BerWriter()
  107. target.startSequence()
  108. target.writeString('2.16.840.1.113730.3.4.2')
  109. target.writeBoolean(true)
  110. target.writeBoolean(false)
  111. target.endSequence()
  112. const control = new Control({
  113. type: '2.16.840.1.113730.3.4.2',
  114. criticality: true,
  115. value: false
  116. })
  117. const ber = control.toBer()
  118. t.not(Buffer.compare(ber.buffer, target.buffer), 0)
  119. })
  120. t.test('passes through _toBer', async t => {
  121. t.plan(2)
  122. const target = new BerWriter()
  123. target.startSequence()
  124. target.writeString('')
  125. target.writeBoolean(false)
  126. target.endSequence()
  127. const control = new Control()
  128. control._toBer = (ber) => t.ok(ber)
  129. const ber = control.toBer()
  130. t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  131. })
  132. t.end()
  133. })