2019-08-27 21:11:49 +00:00
|
|
|
'use strict'
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
const { test } = require('tap')
|
2022-03-27 14:13:16 +00:00
|
|
|
const { BerReader, BerWriter } = require('@ldapjs/asn1')
|
2019-08-27 21:11:49 +00:00
|
|
|
const { Attribute } = require('../lib')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('new no args', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(new Attribute())
|
|
|
|
t.end()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('new with args', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let attr = new Attribute({
|
2011-08-04 20:32:01 +00:00
|
|
|
type: 'cn',
|
|
|
|
vals: ['foo', 'bar']
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.ok(attr)
|
|
|
|
attr.addValue('baz')
|
|
|
|
t.equal(attr.type, 'cn')
|
|
|
|
t.equal(attr.vals.length, 3)
|
|
|
|
t.equal(attr.vals[0], 'foo')
|
|
|
|
t.equal(attr.vals[1], 'bar')
|
|
|
|
t.equal(attr.vals[2], 'baz')
|
2015-10-28 02:21:25 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
attr = new Attribute('not an object')
|
|
|
|
})
|
2015-10-28 02:21:25 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
const typeThatIsNotAString = 1
|
2015-10-28 02:21:25 +00:00
|
|
|
attr = new Attribute({
|
|
|
|
type: typeThatIsNotAString
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
t.end()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('toBer', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const attr = new Attribute({
|
2011-08-04 20:32:01 +00:00
|
|
|
type: 'cn',
|
|
|
|
vals: ['foo', 'bar']
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.ok(attr)
|
|
|
|
const ber = new BerWriter()
|
|
|
|
attr.toBer(ber)
|
|
|
|
const reader = new BerReader(ber.buffer)
|
|
|
|
t.ok(reader.readSequence())
|
|
|
|
t.equal(reader.readString(), 'cn')
|
|
|
|
t.equal(reader.readSequence(), 0x31) // lber set
|
|
|
|
t.equal(reader.readString(), 'foo')
|
|
|
|
t.equal(reader.readString(), 'bar')
|
|
|
|
t.end()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const ber = new BerWriter()
|
|
|
|
ber.startSequence()
|
|
|
|
ber.writeString('cn')
|
|
|
|
ber.startSequence(0x31)
|
|
|
|
ber.writeStringArray(['foo', 'bar'])
|
|
|
|
ber.endSequence()
|
|
|
|
ber.endSequence()
|
|
|
|
|
|
|
|
const attr = new Attribute()
|
|
|
|
t.ok(attr)
|
|
|
|
t.ok(attr.parse(new BerReader(ber.buffer)))
|
|
|
|
|
|
|
|
t.equal(attr.type, 'cn')
|
|
|
|
t.equal(attr.vals.length, 2)
|
|
|
|
t.equal(attr.vals[0], 'foo')
|
|
|
|
t.equal(attr.vals[1], 'bar')
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-17 18:33:02 +00:00
|
|
|
|
2015-10-28 02:21:25 +00:00
|
|
|
test('parse - without 0x31', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const ber = new BerWriter()
|
|
|
|
ber.startSequence()
|
|
|
|
ber.writeString('sn')
|
|
|
|
ber.endSequence()
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
const attr = new Attribute()
|
|
|
|
t.ok(attr)
|
|
|
|
t.ok(attr.parse(new BerReader(ber.buffer)))
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(attr.type, 'sn')
|
|
|
|
t.equal(attr.vals.length, 0)
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2015-10-28 02:21:25 +00:00
|
|
|
test('toString', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const attr = new Attribute({
|
2015-08-18 14:22:11 +00:00
|
|
|
type: 'foobar',
|
|
|
|
vals: ['asdf']
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
const expected = attr.toString()
|
|
|
|
const actual = JSON.stringify(attr.json)
|
|
|
|
t.equal(actual, expected)
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
test('isAttribute', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const isA = Attribute.isAttribute
|
|
|
|
t.notOk(isA(null))
|
|
|
|
t.notOk(isA('asdf'))
|
2014-07-17 18:33:02 +00:00
|
|
|
t.ok(isA(new Attribute({
|
|
|
|
type: 'foobar',
|
|
|
|
vals: ['asdf']
|
2019-08-27 21:11:49 +00:00
|
|
|
})))
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
t.ok(isA({
|
|
|
|
type: 'foo',
|
2019-08-27 18:17:33 +00:00
|
|
|
vals: ['item', Buffer.alloc(5)],
|
2014-07-17 18:33:02 +00:00
|
|
|
toBer: function () { /* placeholder */ }
|
2019-08-27 21:11:49 +00:00
|
|
|
}))
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
// bad type in vals
|
|
|
|
t.notOk(isA({
|
|
|
|
type: 'foo',
|
|
|
|
vals: ['item', null],
|
|
|
|
toBer: function () { /* placeholder */ }
|
2019-08-27 21:11:49 +00:00
|
|
|
}))
|
2014-07-17 18:33:02 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
test('compare', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const comp = Attribute.compare
|
2019-08-27 18:17:33 +00:00
|
|
|
const a = new Attribute({
|
2014-07-17 18:33:02 +00:00
|
|
|
type: 'foo',
|
|
|
|
vals: ['bar']
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
const b = new Attribute({
|
2014-07-17 18:33:02 +00:00
|
|
|
type: 'foo',
|
|
|
|
vals: ['bar']
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
const notAnAttribute = 'this is not an attribute'
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2015-10-28 02:21:25 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
comp(a, notAnAttribute)
|
|
|
|
})
|
2015-10-28 02:21:25 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
comp(notAnAttribute, b)
|
|
|
|
})
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(comp(a, b), 0)
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
// Different types
|
2019-08-27 21:11:49 +00:00
|
|
|
a.type = 'boo'
|
|
|
|
t.equal(comp(a, b), -1)
|
|
|
|
t.equal(comp(b, a), 1)
|
|
|
|
a.type = 'foo'
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
// Different value counts
|
2019-08-27 21:11:49 +00:00
|
|
|
a.vals = ['bar', 'baz']
|
|
|
|
t.equal(comp(a, b), 1)
|
|
|
|
t.equal(comp(b, a), -1)
|
2014-07-17 18:33:02 +00:00
|
|
|
|
|
|
|
// Different value contents (same count)
|
2019-08-27 21:11:49 +00:00
|
|
|
a.vals = ['baz']
|
|
|
|
t.equal(comp(a, b), 1)
|
|
|
|
t.equal(comp(b, a), -1)
|
2014-07-17 18:33:02 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|