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 { dn } = require('../lib');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse basic', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const DN_STR = 'cn=mark, ou=people, o=joyent';
|
|
|
|
const name = dn.parse(DN_STR);
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(name);
|
|
|
|
t.ok(name.rdns);
|
|
|
|
t.ok(Array.isArray(name.rdns));
|
|
|
|
t.equal(3, name.rdns.length);
|
2012-02-18 08:15:52 +00:00
|
|
|
name.rdns.forEach(function (rdn) {
|
|
|
|
t.equal('object', typeof (rdn));
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
t.equal(name.toString(), DN_STR);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse escaped', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const DN_STR = 'cn=m\\,ark, ou=people, o=joyent';
|
|
|
|
const name = dn.parse(DN_STR);
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(name);
|
|
|
|
t.ok(name.rdns);
|
|
|
|
t.ok(Array.isArray(name.rdns));
|
|
|
|
t.equal(3, name.rdns.length);
|
2012-02-18 08:15:52 +00:00
|
|
|
name.rdns.forEach(function (rdn) {
|
|
|
|
t.equal('object', typeof (rdn));
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
t.equal(name.toString(), DN_STR);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse compound', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const DN_STR = 'cn=mark+sn=cavage, ou=people, o=joyent';
|
|
|
|
const name = dn.parse(DN_STR);
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(name);
|
|
|
|
t.ok(name.rdns);
|
|
|
|
t.ok(Array.isArray(name.rdns));
|
|
|
|
t.equal(3, name.rdns.length);
|
2012-02-18 08:15:52 +00:00
|
|
|
name.rdns.forEach(function (rdn) {
|
|
|
|
t.equal('object', typeof (rdn));
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
t.equal(name.toString(), DN_STR);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse quoted', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const DN_STR = 'cn="mark+sn=cavage", ou=people, o=joyent';
|
|
|
|
const ESCAPE_STR = 'cn=mark\\+sn\\=cavage, ou=people, o=joyent';
|
|
|
|
const name = dn.parse(DN_STR);
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(name);
|
|
|
|
t.ok(name.rdns);
|
|
|
|
t.ok(Array.isArray(name.rdns));
|
|
|
|
t.equal(3, name.rdns.length);
|
2012-02-18 08:15:52 +00:00
|
|
|
name.rdns.forEach(function (rdn) {
|
|
|
|
t.equal('object', typeof (rdn));
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
2015-02-16 20:53:42 +00:00
|
|
|
t.equal(name.toString(), ESCAPE_STR);
|
2011-08-04 20:32:01 +00:00
|
|
|
t.end();
|
|
|
|
});
|
2011-08-16 00:52:05 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('equals', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = dn.parse('cn=foo,dc=bar');
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(dn1.equals('cn=foo,dc=bar'));
|
|
|
|
t.ok(!dn1.equals('cn=foo1,dc=bar'));
|
|
|
|
t.ok(dn1.equals(dn.parse('cn=foo,dc=bar')));
|
|
|
|
t.ok(!dn1.equals(dn.parse('cn=foo2,dc=bar')));
|
2011-08-16 00:52:05 +00:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('child of', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = dn.parse('cn=foo,dc=bar');
|
2011-08-16 00:52:05 +00:00
|
|
|
t.ok(dn1.childOf('dc=bar'));
|
|
|
|
t.ok(!dn1.childOf('dc=moo'));
|
|
|
|
t.ok(!dn1.childOf('dc=foo'));
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(!dn1.childOf('cn=foo,dc=bar'));
|
2011-08-16 00:52:05 +00:00
|
|
|
|
|
|
|
t.ok(dn1.childOf(dn.parse('dc=bar')));
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parent of', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = dn.parse('cn=foo,dc=bar');
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(dn1.parentOf('cn=moo,cn=foo,dc=bar'));
|
|
|
|
t.ok(!dn1.parentOf('cn=moo,cn=bar,dc=foo'));
|
|
|
|
t.ok(!dn1.parentOf('cn=foo,dc=bar'));
|
2011-08-16 00:52:05 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(dn1.parentOf(dn.parse('cn=moo,cn=foo,dc=bar')));
|
2011-08-16 00:52:05 +00:00
|
|
|
t.end();
|
|
|
|
});
|
2011-09-23 15:54:48 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
test('DN parent', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const _dn = dn.parse('cn=foo,ou=bar');
|
|
|
|
const parent1 = _dn.parent();
|
|
|
|
const parent2 = parent1.parent();
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(parent1.equals('ou=bar'));
|
|
|
|
t.ok(parent2.equals(''));
|
|
|
|
t.equal(parent2.parent(), null);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('empty DNs', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const _dn = dn.parse('');
|
|
|
|
const _dn2 = dn.parse('cn=foo');
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(_dn.isEmpty());
|
|
|
|
t.notOk(_dn2.isEmpty());
|
2011-09-23 15:54:48 +00:00
|
|
|
t.notOk(_dn.equals('cn=foo'));
|
|
|
|
t.notOk(_dn2.equals(''));
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(_dn.parentOf('cn=foo'));
|
2011-09-23 15:54:48 +00:00
|
|
|
t.notOk(_dn.childOf('cn=foo'));
|
|
|
|
t.notOk(_dn2.parentOf(''));
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(_dn2.childOf(''));
|
2011-09-23 15:54:48 +00:00
|
|
|
t.end();
|
|
|
|
});
|
2011-09-29 21:46:10 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
test('case insensitive attribute names', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = dn.parse('CN=foo,dc=bar');
|
2015-02-16 20:53:42 +00:00
|
|
|
t.ok(dn1.equals('cn=foo,dc=bar'));
|
|
|
|
t.ok(dn1.equals(dn.parse('cn=foo,DC=bar')));
|
2011-09-29 21:46:10 +00:00
|
|
|
t.end();
|
|
|
|
});
|
2013-10-24 23:27:42 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
test('format', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const DN_ORDER = dn.parse('sn=bar+cn=foo,ou=test');
|
|
|
|
const DN_QUOTE = dn.parse('cn="foo",ou=test');
|
|
|
|
const DN_QUOTE2 = dn.parse('cn=" foo",ou=test');
|
|
|
|
const DN_SPACE = dn.parse('cn=foo,ou=test');
|
|
|
|
const DN_SPACE2 = dn.parse('cn=foo ,ou=test');
|
|
|
|
const DN_CASE = dn.parse('CN=foo,Ou=test');
|
2015-02-16 20:53:42 +00:00
|
|
|
|
|
|
|
t.equal(DN_ORDER.format({keepOrder: false}), 'cn=foo+sn=bar, ou=test');
|
|
|
|
t.equal(DN_ORDER.format({keepOrder: true}), 'sn=bar+cn=foo, ou=test');
|
|
|
|
|
|
|
|
t.equal(DN_QUOTE.format({keepQuote: false}), 'cn=foo, ou=test');
|
|
|
|
t.equal(DN_QUOTE.format({keepQuote: true}), 'cn="foo", ou=test');
|
|
|
|
t.equal(DN_QUOTE2.format({keepQuote: false}), 'cn=" foo", ou=test');
|
|
|
|
t.equal(DN_QUOTE2.format({keepQuote: true}), 'cn=" foo", ou=test');
|
|
|
|
|
|
|
|
t.equal(DN_SPACE.format({keepSpace: false}), 'cn=foo, ou=test');
|
|
|
|
t.equal(DN_SPACE.format({keepSpace: true}), 'cn=foo,ou=test');
|
|
|
|
t.equal(DN_SPACE.format({skipSpace: true}), 'cn=foo,ou=test');
|
|
|
|
t.equal(DN_SPACE2.format({keepSpace: false}), 'cn=foo, ou=test');
|
|
|
|
t.equal(DN_SPACE2.format({keepSpace: true}), 'cn=foo ,ou=test');
|
|
|
|
t.equal(DN_SPACE2.format({skipSpace: true}), 'cn=foo,ou=test');
|
|
|
|
|
|
|
|
t.equal(DN_CASE.format({keepCase: false}), 'cn=foo, ou=test');
|
|
|
|
t.equal(DN_CASE.format({keepCase: true}), 'CN=foo, Ou=test');
|
|
|
|
t.equal(DN_CASE.format({upperName: true}), 'CN=foo, OU=test');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('set format', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com');
|
2015-02-16 20:53:42 +00:00
|
|
|
t.equal(_dn.toString(), 'uid=user, cn=foo+sn=bar, dc=test, dc=com');
|
|
|
|
_dn.setFormat({keepOrder: true});
|
|
|
|
t.equal(_dn.toString(), 'uid=user, sn=bar+cn=foo, dc=test, dc=com');
|
|
|
|
_dn.setFormat({keepQuote: true});
|
|
|
|
t.equal(_dn.toString(), 'uid="user", cn=foo+sn=bar, dc=test, dc=com');
|
|
|
|
_dn.setFormat({keepSpace: true});
|
|
|
|
t.equal(_dn.toString(), 'uid=user, cn=foo+sn=bar, dc=test , dc=com');
|
|
|
|
_dn.setFormat({keepCase: true});
|
|
|
|
t.equal(_dn.toString(), 'uid=user, cn=foo+sn=bar, dc=test, DC=com');
|
|
|
|
_dn.setFormat({upperName: true});
|
|
|
|
t.equal(_dn.toString(), 'UID=user, CN=foo+SN=bar, DC=test, DC=com');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('format persists across clone', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com');
|
|
|
|
const OUT = 'UID="user", CN=foo+SN=bar, DC=test, DC=com';
|
2015-02-16 20:53:42 +00:00
|
|
|
_dn.setFormat({keepQuote: true, upperName: true});
|
2019-08-27 18:17:33 +00:00
|
|
|
const clone = _dn.clone();
|
2015-02-16 20:53:42 +00:00
|
|
|
t.equals(_dn.toString(), OUT);
|
|
|
|
t.equals(clone.toString(), OUT);
|
2013-10-24 23:27:42 +00:00
|
|
|
t.end();
|
|
|
|
});
|
2014-09-30 23:39:19 +00:00
|
|
|
|
2015-11-03 04:54:35 +00:00
|
|
|
test('initialization', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = new dn.DN();
|
2015-11-03 04:54:35 +00:00
|
|
|
t.ok(dn1);
|
|
|
|
t.equals(dn1.toString(), '');
|
|
|
|
t.ok(dn1.isEmpty(), 'DN with no initializer defaults to null DN');
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const data = [
|
2015-11-03 04:54:35 +00:00
|
|
|
new dn.RDN({ foo: 'bar' }),
|
|
|
|
new dn.RDN({ o: 'base' })
|
|
|
|
];
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn2 = new dn.DN(data);
|
2015-11-03 04:54:35 +00:00
|
|
|
t.ok(dn2);
|
|
|
|
t.equals(dn2.toString(), 'foo=bar, o=base');
|
|
|
|
t.ok(!dn2.isEmpty());
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('array functions', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const dn1 = dn.parse('a=foo, b=bar, c=baz');
|
2015-11-03 04:54:35 +00:00
|
|
|
t.ok(dn1);
|
|
|
|
t.equal(dn1.toString(), 'a=foo, b=bar, c=baz');
|
|
|
|
|
|
|
|
t.ok(dn1.reverse());
|
|
|
|
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
let rdn = dn1.pop();
|
2015-11-03 04:54:35 +00:00
|
|
|
t.ok(rdn);
|
|
|
|
t.equal(dn1.toString(), 'c=baz, b=bar');
|
|
|
|
|
|
|
|
t.ok(dn1.push(rdn));
|
|
|
|
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
|
|
|
|
|
|
|
|
rdn = dn1.shift();
|
|
|
|
t.ok(rdn);
|
|
|
|
t.equal(dn1.toString(), 'b=bar, a=foo');
|
|
|
|
|
|
|
|
t.ok(dn1.unshift(rdn));
|
|
|
|
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
test('isDN duck-testing', function (t) {
|
2019-08-27 18:17:33 +00:00
|
|
|
const valid = dn.parse('cn=foo');
|
|
|
|
const isDN = dn.DN.isDN;
|
2014-09-30 23:39:19 +00:00
|
|
|
t.notOk(isDN(null));
|
|
|
|
t.notOk(isDN('cn=foo'));
|
|
|
|
t.ok(isDN(valid));
|
2019-08-27 18:17:33 +00:00
|
|
|
const duck = {
|
2014-09-30 23:39:19 +00:00
|
|
|
rdns: [ {look: 'ma'}, {a: 'dn'} ],
|
|
|
|
toString: function () { return 'look=ma, a=dn'; }
|
|
|
|
};
|
|
|
|
t.ok(isDN(duck));
|
|
|
|
t.end();
|
|
|
|
});
|