2019-08-27 21:11:49 +00:00
|
|
|
'use strict'
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-09-06 10:27:37 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2019-08-27 21:11:49 +00:00
|
|
|
const { test } = require('tap')
|
|
|
|
const { BerReader, BerWriter } = require('asn1')
|
|
|
|
const { Attribute, Change } = 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 Change())
|
|
|
|
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 18:17:33 +00:00
|
|
|
const change = new Change({
|
2015-10-31 14:37:58 +00:00
|
|
|
operation: 'add',
|
2011-08-04 20:32:01 +00:00
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['foo', 'bar']
|
|
|
|
})
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.ok(change)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(change.operation, 'add')
|
|
|
|
t.equal(change.modification.type, 'cn')
|
|
|
|
t.equal(change.modification.vals.length, 2)
|
|
|
|
t.equal(change.modification.vals[0], 'foo')
|
|
|
|
t.equal(change.modification.vals[1], 'bar')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-09-06 10:27:37 +00:00
|
|
|
test('new with args and buffer', function (t) {
|
|
|
|
var img = fs.readFileSync(path.join(__dirname, '/imgs/test.jpg'))
|
|
|
|
|
|
|
|
var change = new Change({
|
|
|
|
operation: 'add',
|
|
|
|
modification: {
|
|
|
|
thumbnailPhoto: img
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.ok(change)
|
|
|
|
|
|
|
|
t.equal(change.operation, 'add')
|
|
|
|
t.equal(change.modification.type, 'thumbnailPhoto')
|
|
|
|
t.equal(change.modification.vals.length, 1)
|
|
|
|
t.equal(change.modification.buffers[0].compare(img), 0)
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
2015-10-31 14:37:58 +00:00
|
|
|
test('validate fields', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const c = new Change()
|
|
|
|
t.ok(c)
|
2015-10-31 14:37:58 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
c.operation = 'bogus'
|
|
|
|
})
|
2015-10-31 14:37:58 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
c.modification = { too: 'many', fields: 'here' }
|
|
|
|
})
|
2015-10-31 14:37:58 +00:00
|
|
|
c.modification = {
|
|
|
|
foo: ['bar', 'baz']
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
|
|
|
t.ok(c.modification)
|
|
|
|
t.end()
|
|
|
|
})
|
2015-10-31 14:37:58 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('GH-31 (multiple attributes per Change)', function (t) {
|
2015-10-31 14:37:58 +00:00
|
|
|
t.throws(function () {
|
2019-08-27 18:17:33 +00:00
|
|
|
const c = new Change({
|
2011-11-08 21:31:00 +00:00
|
|
|
operation: 'replace',
|
|
|
|
modification: {
|
|
|
|
cn: 'foo',
|
|
|
|
sn: 'bar'
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.notOk(c)
|
|
|
|
})
|
|
|
|
t.end()
|
|
|
|
})
|
2011-11-08 21:31:00 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('toBer', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const change = new Change({
|
2011-08-04 20:32:01 +00:00
|
|
|
operation: 'Add',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['foo', 'bar']
|
|
|
|
})
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.ok(change)
|
|
|
|
|
|
|
|
const ber = new BerWriter()
|
|
|
|
change.toBer(ber)
|
|
|
|
const reader = new BerReader(ber.buffer)
|
|
|
|
t.ok(reader.readSequence())
|
|
|
|
t.equal(reader.readEnumeration(), 0x00)
|
|
|
|
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.writeEnumeration(0x00)
|
|
|
|
ber.startSequence()
|
|
|
|
ber.writeString('cn')
|
|
|
|
ber.startSequence(0x31)
|
|
|
|
ber.writeStringArray(['foo', 'bar'])
|
|
|
|
ber.endSequence()
|
|
|
|
ber.endSequence()
|
|
|
|
ber.endSequence()
|
|
|
|
|
|
|
|
const change = new Change()
|
|
|
|
t.ok(change)
|
|
|
|
t.ok(change.parse(new BerReader(ber.buffer)))
|
|
|
|
|
|
|
|
t.equal(change.operation, 'add')
|
|
|
|
t.equal(change.modification.type, 'cn')
|
|
|
|
t.equal(change.modification.vals.length, 2)
|
|
|
|
t.equal(change.modification.vals[0], 'foo')
|
|
|
|
t.equal(change.modification.vals[1], 'bar')
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
test('apply - replace', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let res
|
2019-08-27 18:17:33 +00:00
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'replace',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['new']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
const twin = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'replace',
|
2019-08-27 21:11:49 +00:00
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['new', 'two']
|
|
|
|
}
|
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
const empty = new Change({
|
2014-07-16 20:03:15 +00:00
|
|
|
operation: 'replace',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: []
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// plain
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old'] })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// multiple
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old', 'also'] })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 20:03:15 +00:00
|
|
|
|
|
|
|
// empty
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(empty, { cn: ['existing'] })
|
|
|
|
t.equal(res.cn, undefined)
|
|
|
|
t.ok(Object.keys(res).indexOf('cn') === -1)
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "success"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: 'old' }, true)
|
|
|
|
t.equal(res.cn, 'new')
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "failure"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(twin, { cn: 'old' }, true)
|
|
|
|
t.deepEqual(res.cn, ['new', 'two'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
test('apply - add', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let res
|
2019-08-27 18:17:33 +00:00
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'add',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['new']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// plain
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old'] })
|
|
|
|
t.deepEqual(res.cn, ['old', 'new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// multiple
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old', 'also'] })
|
|
|
|
t.deepEqual(res.cn, ['old', 'also', 'new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "success"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { }, true)
|
|
|
|
t.equal(res.cn, 'new')
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "failure"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: 'old' }, true)
|
|
|
|
t.deepEqual(res.cn, ['old', 'new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// duplicate add
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: 'new' })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
test('apply - delete', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let res
|
2019-08-27 18:17:33 +00:00
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'delete',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['old']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// plain
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old', 'new'] })
|
|
|
|
t.deepEqual(res.cn, ['new'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// empty
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old'] })
|
|
|
|
t.equal(res.cn, undefined)
|
|
|
|
t.ok(Object.keys(res).indexOf('cn') === -1)
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "success"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old', 'one'] }, true)
|
|
|
|
t.equal(res.cn, 'one')
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "failure"
|
2019-08-27 21:11:49 +00:00
|
|
|
res = Change.apply(single, { cn: ['old', 'several', 'items'] }, true)
|
|
|
|
t.deepEqual(res.cn, ['several', 'items'])
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] })
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.cn, undefined)
|
2014-07-16 19:53:13 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|