2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2014-07-08 19:31:57 +00:00
|
|
|
var test = require('tape').test;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var dn;
|
|
|
|
|
|
|
|
|
|
|
|
///--- Tests
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('load library', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
dn = require('../lib/index').dn;
|
|
|
|
t.ok(dn);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('parse basic', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var DN_STR = 'cn=mark, ou=people, o=joyent';
|
|
|
|
var name = dn.parse(DN_STR);
|
|
|
|
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) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var DN_STR = 'cn=m\\,ark, ou=people, o=joyent';
|
|
|
|
var name = dn.parse(DN_STR);
|
|
|
|
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) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var DN_STR = 'cn=mark+sn=cavage, ou=people, o=joyent';
|
|
|
|
var name = dn.parse(DN_STR);
|
|
|
|
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) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var DN_STR = 'cn="mark+sn=cavage", ou=people, o=joyent';
|
2015-02-16 20:53:42 +00:00
|
|
|
var ESCAPE_STR = 'cn=mark\\+sn\\=cavage, ou=people, o=joyent';
|
2011-08-04 20:32:01 +00:00
|
|
|
var name = dn.parse(DN_STR);
|
|
|
|
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) {
|
2015-02-16 20:53:42 +00:00
|
|
|
var dn1 = dn.parse('cn=foo,dc=bar');
|
|
|
|
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) {
|
2015-02-16 20:53:42 +00:00
|
|
|
var 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) {
|
2015-02-16 20:53:42 +00:00
|
|
|
var dn1 = dn.parse('cn=foo,dc=bar');
|
|
|
|
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) {
|
|
|
|
var _dn = dn.parse('cn=foo,ou=bar');
|
|
|
|
var parent1 = _dn.parent();
|
|
|
|
var parent2 = parent1.parent();
|
|
|
|
t.ok(parent1.equals('ou=bar'));
|
|
|
|
t.ok(parent2.equals(''));
|
|
|
|
t.equal(parent2.parent(), null);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('empty DNs', function (t) {
|
2011-09-23 15:54:48 +00:00
|
|
|
var _dn = dn.parse('');
|
|
|
|
var _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) {
|
|
|
|
var dn1 = dn.parse('CN=foo,dc=bar');
|
|
|
|
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) {
|
|
|
|
var DN_ORDER = dn.parse('sn=bar+cn=foo,ou=test');
|
|
|
|
var DN_QUOTE = dn.parse('cn="foo",ou=test');
|
|
|
|
var DN_QUOTE2 = dn.parse('cn=" foo",ou=test');
|
|
|
|
var DN_SPACE = dn.parse('cn=foo,ou=test');
|
|
|
|
var DN_SPACE2 = dn.parse('cn=foo ,ou=test');
|
|
|
|
var DN_CASE = dn.parse('CN=foo,Ou=test');
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com');
|
|
|
|
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) {
|
|
|
|
var _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com');
|
|
|
|
var OUT = 'UID="user", CN=foo+SN=bar, DC=test, DC=com';
|
|
|
|
_dn.setFormat({keepQuote: true, upperName: true});
|
|
|
|
var clone = _dn.clone();
|
|
|
|
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) {
|
|
|
|
var dn1 = new dn.DN();
|
|
|
|
t.ok(dn1);
|
|
|
|
t.equals(dn1.toString(), '');
|
|
|
|
t.ok(dn1.isEmpty(), 'DN with no initializer defaults to null DN');
|
|
|
|
|
|
|
|
var data = [
|
|
|
|
new dn.RDN({ foo: 'bar' }),
|
|
|
|
new dn.RDN({ o: 'base' })
|
|
|
|
];
|
|
|
|
var dn2 = new dn.DN(data);
|
|
|
|
t.ok(dn2);
|
|
|
|
t.equals(dn2.toString(), 'foo=bar, o=base');
|
|
|
|
t.ok(!dn2.isEmpty());
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('array functions', function (t) {
|
|
|
|
var dn1 = dn.parse('a=foo, b=bar, c=baz');
|
|
|
|
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');
|
|
|
|
|
|
|
|
var rdn = dn1.pop();
|
|
|
|
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) {
|
|
|
|
var valid = dn.parse('cn=foo');
|
|
|
|
var isDN = dn.DN.isDN;
|
|
|
|
t.notOk(isDN(null));
|
|
|
|
t.notOk(isDN('cn=foo'));
|
|
|
|
t.ok(isDN(valid));
|
|
|
|
var duck = {
|
|
|
|
rdns: [ {look: 'ma'}, {a: 'dn'} ],
|
|
|
|
toString: function () { return 'look=ma, a=dn'; }
|
|
|
|
};
|
|
|
|
t.ok(isDN(duck));
|
|
|
|
t.end();
|
|
|
|
});
|