dn parentof/childof fixes

This commit is contained in:
Mark Cavage 2011-08-15 17:52:05 -07:00
parent c8ea58fc60
commit 51f944d834
3 changed files with 52 additions and 5 deletions

View File

@ -206,7 +206,7 @@ DN.prototype.childOf = function(dn) {
if (!(dn instanceof DN))
dn = parse(dn);
if (this.rdns.length < dn.rdns.length)
if (this.rdns.length <= dn.rdns.length)
return false;
var diff = this.rdns.length - dn.rdns.length;
@ -229,11 +229,25 @@ DN.prototype.parentOf = function(dn) {
if (!(dn instanceof DN))
dn = parse(dn);
var parent = DN.prototype.childOf.call(dn, this);
if (this.rdns.length >= dn.rdns.length)
return false;
return parent;
var diff = dn.rdns.length - this.rdns.length;
for (var i = this.rdns.length - 1; i >= 0; i--) {
var rdn = this.rdns[i];
for (var k in rdn) {
if (rdn.hasOwnProperty(k)) {
var theirRdn = dn.rdns[i + diff];
if (theirRdn[k] !== rdn[k])
return false;
}
}
}
return true;
};
DN.prototype.equals = function(dn) {
if (!(dn instanceof DN))
dn = parse(dn);

View File

@ -66,8 +66,8 @@ module.exports = {
Control: Control,
log4js: logStub,
_schema: schema,
url: url
url: url,
parseURL: url.parse
};

View File

@ -77,3 +77,36 @@ test('parse quoted', function(t) {
t.equal(name.toString(), DN_STR);
t.end();
});
test('equals', function(t) {
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')));
t.end();
});
test('child of', function(t) {
var dn1 = dn.parse('cn=foo, dc=bar');
t.ok(dn1.childOf('dc=bar'));
t.ok(!dn1.childOf('dc=moo'));
t.ok(!dn1.childOf('dc=foo'));
t.ok(!dn1.childOf('cn=foo, dc=bar'));
t.ok(dn1.childOf(dn.parse('dc=bar')));
t.end();
});
test('parent of', function(t) {
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'));
t.ok(dn1.parentOf(dn.parse('cn=moo, cn=foo, dc=bar')));
t.end();
});