2019-08-27 18:17:33 +00:00
|
|
|
'use strict';
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +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) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(new Change());
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
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']
|
|
|
|
})
|
|
|
|
});
|
|
|
|
t.ok(change);
|
|
|
|
|
2011-08-12 23:37:47 +00:00
|
|
|
t.equal(change.operation, 'add');
|
2011-08-04 20:32:01 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2015-10-31 14:37:58 +00:00
|
|
|
test('validate fields', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const c = new Change();
|
2015-10-31 14:37:58 +00:00
|
|
|
t.ok(c);
|
|
|
|
t.throws(function () {
|
|
|
|
c.operation = 'bogus';
|
|
|
|
});
|
|
|
|
t.throws(function () {
|
|
|
|
c.modification = {too: 'many', fields: 'here'};
|
|
|
|
});
|
|
|
|
c.modification = {
|
|
|
|
foo: ['bar', 'baz']
|
|
|
|
};
|
|
|
|
t.ok(c.modification);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
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'
|
|
|
|
}
|
2015-10-31 14:37:58 +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']
|
|
|
|
})
|
|
|
|
});
|
|
|
|
t.ok(change);
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const ber = new BerWriter();
|
2011-08-04 20:32:01 +00:00
|
|
|
change.toBer(ber);
|
2019-08-27 18:17:33 +00:00
|
|
|
const reader = new BerReader(ber.buffer);
|
2011-08-04 20:32:01 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const ber = new BerWriter();
|
2011-08-04 20:32:01 +00:00
|
|
|
ber.startSequence();
|
|
|
|
ber.writeEnumeration(0x00);
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeString('cn');
|
|
|
|
ber.startSequence(0x31);
|
|
|
|
ber.writeStringArray(['foo', 'bar']);
|
|
|
|
ber.endSequence();
|
|
|
|
ber.endSequence();
|
|
|
|
ber.endSequence();
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const change = new Change();
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(change);
|
|
|
|
t.ok(change.parse(new BerReader(ber.buffer)));
|
|
|
|
|
2011-08-12 23:37:47 +00:00
|
|
|
t.equal(change.operation, 'add');
|
2011-08-04 20:32:01 +00:00
|
|
|
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 18:17:33 +00:00
|
|
|
let res;
|
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'replace',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['new']
|
|
|
|
}
|
|
|
|
});
|
2019-08-27 18:17:33 +00:00
|
|
|
const twin = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'replace',
|
|
|
|
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: []
|
|
|
|
}
|
|
|
|
});
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// plain
|
|
|
|
res = Change.apply(single, { cn: ['old'] });
|
2014-07-16 20:03:15 +00:00
|
|
|
t.deepEqual(res.cn, ['new']);
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// multiple
|
|
|
|
res = Change.apply(single, { cn: ['old', 'also'] });
|
2014-07-16 20:03:15 +00:00
|
|
|
t.deepEqual(res.cn, ['new']);
|
|
|
|
|
|
|
|
// empty
|
|
|
|
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
|
|
|
|
|
|
|
//absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] });
|
2014-07-16 20:03:15 +00:00
|
|
|
t.deepEqual(res.cn, ['new']);
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
// scalar formatting "success"
|
|
|
|
res = Change.apply(single, { cn: 'old' }, true);
|
|
|
|
t.equal(res.cn, 'new');
|
|
|
|
|
|
|
|
// scalar formatting "failure"
|
|
|
|
res = Change.apply(twin, { cn: 'old' }, true);
|
2014-07-16 20:03:15 +00:00
|
|
|
t.deepEqual(res.cn, ['new', 'two']);
|
2014-07-16 19:53:13 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('apply - add', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
let res;
|
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'add',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['new']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// plain
|
|
|
|
res = Change.apply(single, { cn: ['old'] });
|
|
|
|
t.deepEqual(res.cn, ['old', 'new']);
|
|
|
|
|
|
|
|
// multiple
|
|
|
|
res = Change.apply(single, { cn: ['old', 'also'] });
|
|
|
|
t.deepEqual(res.cn, ['old', 'also', 'new']);
|
|
|
|
|
|
|
|
//absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] });
|
|
|
|
t.deepEqual(res.cn, ['new']);
|
|
|
|
|
|
|
|
// scalar formatting "success"
|
|
|
|
res = Change.apply(single, { }, true);
|
|
|
|
t.equal(res.cn, 'new');
|
|
|
|
|
|
|
|
// scalar formatting "failure"
|
|
|
|
res = Change.apply(single, { cn: 'old' }, true);
|
|
|
|
t.deepEqual(res.cn, ['old', 'new']);
|
|
|
|
|
|
|
|
// duplicate add
|
|
|
|
res = Change.apply(single, { cn: 'new' });
|
|
|
|
t.deepEqual(res.cn, ['new']);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('apply - delete', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
let res;
|
|
|
|
const single = new Change({
|
2014-07-16 19:53:13 +00:00
|
|
|
operation: 'delete',
|
|
|
|
modification: {
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['old']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// plain
|
|
|
|
res = Change.apply(single, { cn: ['old', 'new'] });
|
|
|
|
t.deepEqual(res.cn, ['new']);
|
|
|
|
|
|
|
|
// empty
|
|
|
|
res = Change.apply(single, { cn: ['old'] });
|
|
|
|
t.equal(res.cn, undefined);
|
|
|
|
t.ok(Object.keys(res).indexOf('cn') === -1);
|
|
|
|
|
|
|
|
// scalar formatting "success"
|
|
|
|
res = Change.apply(single, { cn: ['old', 'one'] }, true);
|
|
|
|
t.equal(res.cn, 'one');
|
|
|
|
|
|
|
|
// scalar formatting "failure"
|
|
|
|
res = Change.apply(single, { cn: ['old', 'several', 'items'] }, true);
|
|
|
|
t.deepEqual(res.cn, ['several', 'items']);
|
|
|
|
|
|
|
|
|
|
|
|
//absent
|
|
|
|
res = Change.apply(single, { dn: ['otherjunk'] });
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.cn, undefined);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|