From be3d5fa7faf7a1d9aaa24d0bf8171c30816ffaef Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Fri, 23 Sep 2011 08:54:48 -0700 Subject: [PATCH] GH-16: dn.equals broken on empty strings --- lib/dn.js | 19 ++++++++++++------- tst/dn.test.js | 13 +++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/dn.js b/lib/dn.js index 45b7cb1..c987ae4 100644 --- a/lib/dn.js +++ b/lib/dn.js @@ -268,14 +268,19 @@ DN.prototype.equals = function(dn) { for (var i = 0; i < this.rdns.length; i++) { var ours = this.rdns[i]; var theirs = dn.rdns[i]; - for (var k in ours) { - if (ours.hasOwnProperty(k)) { - if (!theirs.hasOwnProperty(k)) - return false; + var ourKeys = Object.keys(ours); + var theirKeys = Object.keys(theirs); + if (ourKeys.length !== theirKeys.length) + return false; - if (ours[k] !== theirs[k]) - return false; - } + ourKeys.sort(); + theirKeys.sort(); + + for (var j = 0; j < ourKeys.length; j++) { + if (ourKeys[j] !== theirKeys[j]) + return false; + if (ours[ourKeys[j]] !== theirs[ourKeys[j]]) + return false; } } diff --git a/tst/dn.test.js b/tst/dn.test.js index cfc2de4..ccdf06e 100644 --- a/tst/dn.test.js +++ b/tst/dn.test.js @@ -110,3 +110,16 @@ test('parent of', function(t) { t.ok(dn1.parentOf(dn.parse('cn=moo, cn=foo, dc=bar'))); t.end(); }); + + +test('empty DNs GH-16', function(t) { + var _dn = dn.parse(''); + var _dn2 = dn.parse('cn=foo'); + t.notOk(_dn.equals('cn=foo')); + t.notOk(_dn2.equals('')); + t.notOk(_dn.parentOf('cn=foo')); + t.notOk(_dn.childOf('cn=foo')); + t.notOk(_dn2.parentOf('')); + t.notOk(_dn2.childOf('')); + t.end(); +});