wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
 
const tap = require('tap')
const { BerWriter } = require('@ldapjs/asn1')
const PPC = require('./password-policy-control')
const Control = require('../control')
 
tap.test('contructor', t => {
  t.test('new no args', async t => {
    const control = new PPC()
    t.ok(control)
    t.type(control, PPC)
    t.type(control, Control)
    t.equal(control.type, PPC.OID)
    t.same(control.value, {})
  })
 
  t.test('new with args', async t => {
    const control = new PPC({
      type: '1.3.6.1.4.1.42.2.27.8.5.1',
      criticality: true,
      value: {
        error: 1,
        timeBeforeExpiration: 2
      }
    })
    t.ok(control)
    t.equal(control.type, '1.3.6.1.4.1.42.2.27.8.5.1')
    t.ok(control.criticality)
    t.same(control.value, {
      error: 1,
      timeBeforeExpiration: 2
    })
  })
 
  t.test('with value buffer', async t => {
    const value = new BerWriter()
    value.startSequence()
    value.writeInt(5, 0x81)
    value.endSequence()
 
    const control = new PPC({ value: value.buffer })
    t.same(control.value, {
      error: 5
    })
  })
 
  t.test('throws for bad value', async t => {
    t.throws(() => new PPC({ value: 42 }))
    t.throws(() => new PPC({ value: { timeBeforeExpiration: 1, graceAuthNsRemaining: 2 } }))
  })
 
  t.end()
})
 
tap.test('pojo', t => {
  t.test('adds control value', async t => {
    const control = new PPC()
    t.same(control.pojo, {
      type: PPC.OID,
      criticality: false,
      value: {}
    })
  })
 
  t.end()
})
 
tap.test('toBer', t => {
  t.test('converts empty instance to BER', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString(PPC.OID)
    target.writeBoolean(false) // Control.criticality
    target.endSequence()
 
    const control = new PPC()
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('converts full instance to BER', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString(PPC.OID)
    target.writeBoolean(true) // Control.criticality
 
    const value = new BerWriter()
    value.startSequence()
    value.startSequence(0xa0)
    value.writeInt(2, 0x81)
    value.endSequence()
    value.writeInt(1, 0x81)
    value.endSequence()
 
    target.writeBuffer(value.buffer, 0x04)
    target.endSequence()
 
    const control = new PPC({
      criticality: true,
      value: {
        error: 1,
        graceAuthNsRemaining: 2
      }
    })
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.end()
})