Clean up DN asserts and tests

This commit is contained in:
Patrick Mooney 2015-11-02 22:54:35 -06:00
parent dcce6ebe1e
commit 69c72dc45f
2 changed files with 94 additions and 84 deletions

131
lib/dn.js
View File

@ -1,6 +1,10 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert-plus');
///--- Helpers
function invalidDN(name) { function invalidDN(name) {
var e = new Error(); var e = new Error();
@ -9,18 +13,26 @@ function invalidDN(name) {
return e; return e;
} }
function isAlphaNumeric(c) { function isAlphaNumeric(c) {
var re = /[A-Za-z0-9]/; var re = /[A-Za-z0-9]/;
return re.test(c); return re.test(c);
} }
function isWhitespace(c) { function isWhitespace(c) {
var re = /\s/; var re = /\s/;
return re.test(c); return re.test(c);
} }
function repeatChar(c, n) {
var out = '';
var max = n ? n : 0;
for (var i = 0; i < max; i++)
out += c;
return out;
}
///--- API
function RDN(obj) { function RDN(obj) {
var self = this; var self = this;
this.attrs = {}; this.attrs = {};
@ -32,12 +44,10 @@ function RDN(obj) {
} }
} }
RDN.prototype.set = function rdnSet(name, value, opts) {
assert.string(name, 'name (string) required');
assert.string(value, 'value (string) required');
RDN.prototype.set = function set(name, value, opts) {
if (typeof (name) !== 'string')
throw new TypeError('name (string) required');
if (typeof (value) !== 'string')
throw new TypeError('value (string) required');
var self = this; var self = this;
var lname = name.toLowerCase(); var lname = name.toLowerCase();
this.attrs[lname] = { this.attrs[lname] = {
@ -52,8 +62,7 @@ RDN.prototype.set = function set(name, value, opts) {
} }
}; };
RDN.prototype.equals = function rdnEquals(rdn) {
RDN.prototype.equals = function equals(rdn) {
if (typeof (rdn) !== 'object') if (typeof (rdn) !== 'object')
return false; return false;
@ -79,13 +88,10 @@ RDN.prototype.equals = function equals(rdn) {
* Convert RDN to string according to specified formatting options. * Convert RDN to string according to specified formatting options.
* (see: DN.format for option details) * (see: DN.format for option details)
*/ */
RDN.prototype.format = function format(options) { RDN.prototype.format = function rdnFormat(options) {
if (options) { assert.optionalObject(options, 'options must be an object');
if (typeof (options) !== 'object') options = options || {};
throw new TypeError('options must be an object');
} else {
options = {};
}
var self = this; var self = this;
var str = ''; var str = '';
@ -154,8 +160,7 @@ RDN.prototype.format = function format(options) {
return str; return str;
}; };
RDN.prototype.toString = function rdnToString() {
RDN.prototype.toString = function toString() {
return this.format(); return this.format();
}; };
@ -314,26 +319,18 @@ function parse(name) {
} }
///--- API
function DN(rdns) { function DN(rdns) {
if (!Array.isArray(rdns)) assert.optionalArrayOfObject(rdns, '[object] required');
throw new TypeError('rdns ([object]) required');
rdns.forEach(function (rdn) {
if (typeof (rdn) !== 'object')
throw new TypeError('rdns ([object]) required');
});
this.rdns = rdns.slice(); this.rdns = rdns ? rdns.slice() : [];
this._format = {}; this._format = {};
this.__defineGetter__('length', function () {
return this.rdns.length;
});
} }
Object.defineProperties(DN.prototype, {
length: {
get: function getLength() { return this.rdns.length; },
configurable: false
}
});
/** /**
* Convert DN to string according to specified formatting options. * Convert DN to string according to specified formatting options.
@ -358,21 +355,11 @@ function DN(rdns) {
* - upperName: RDN names will be uppercased instead of lowercased. * - upperName: RDN names will be uppercased instead of lowercased.
* - skipSpace: Disable trailing space after RDN separators * - skipSpace: Disable trailing space after RDN separators
*/ */
DN.prototype.format = function (options) { DN.prototype.format = function dnFormat(options) {
if (options) { assert.optionalObject(options, 'options must be an object');
if (typeof (options) !== 'object') options = options || this._format;
throw new TypeError('options must be an object');
} else {
options = this._format;
}
var str = ''; var str = '';
function repeatChar(c, n) {
var out = '';
var max = n ? n : 0;
for (var i = 0; i < max; i++)
out += c;
return out;
}
this.rdns.forEach(function (rdn) { this.rdns.forEach(function (rdn) {
var rdnString = rdn.format(options); var rdnString = rdn.format(options);
if (str.length !== 0) { if (str.length !== 0) {
@ -390,22 +377,19 @@ DN.prototype.format = function (options) {
return str; return str;
}; };
/** /**
* Set default string formatting options. * Set default string formatting options.
*/ */
DN.prototype.setFormat = function setFormat(options) { DN.prototype.setFormat = function setFormat(options) {
if (typeof (options) !== 'object') assert.object(options, 'options must be an object');
throw new TypeError('options must be an object');
this._format = options; this._format = options;
}; };
DN.prototype.toString = function dnToString() {
DN.prototype.toString = function () {
return this.format(); return this.format();
}; };
DN.prototype.parentOf = function parentOf(dn) { DN.prototype.parentOf = function parentOf(dn) {
if (typeof (dn) !== 'object') if (typeof (dn) !== 'object')
dn = parse(dn); dn = parse(dn);
@ -425,20 +409,17 @@ DN.prototype.parentOf = function parentOf(dn) {
return true; return true;
}; };
DN.prototype.childOf = function childOf(dn) { DN.prototype.childOf = function childOf(dn) {
if (typeof (dn) !== 'object') if (typeof (dn) !== 'object')
dn = parse(dn); dn = parse(dn);
return dn.parentOf(this); return dn.parentOf(this);
}; };
DN.prototype.isEmpty = function isEmpty() { DN.prototype.isEmpty = function isEmpty() {
return (this.rdns.length === 0); return (this.rdns.length === 0);
}; };
DN.prototype.equals = function dnEquals(dn) {
DN.prototype.equals = function (dn) {
if (typeof (dn) !== 'object') if (typeof (dn) !== 'object')
dn = parse(dn); dn = parse(dn);
@ -453,8 +434,7 @@ DN.prototype.equals = function (dn) {
return true; return true;
}; };
DN.prototype.parent = function dnParent() {
DN.prototype.parent = function () {
if (this.rdns.length !== 0) { if (this.rdns.length !== 0) {
var save = this.rdns.shift(); var save = this.rdns.shift();
var dn = new DN(this.rdns); var dn = new DN(this.rdns);
@ -465,47 +445,38 @@ DN.prototype.parent = function () {
return null; return null;
}; };
DN.prototype.clone = function dnClone() {
DN.prototype.clone = function () {
var dn = new DN(this.rdns); var dn = new DN(this.rdns);
dn._format = this._format; dn._format = this._format;
return dn; return dn;
}; };
DN.prototype.reverse = function dnReverse() {
DN.prototype.reverse = function () {
this.rdns.reverse(); this.rdns.reverse();
return this; return this;
}; };
DN.prototype.pop = function dnPop() {
DN.prototype.pop = function () {
return this.rdns.pop(); return this.rdns.pop();
}; };
DN.prototype.push = function dnPush(rdn) {
DN.prototype.push = function (rdn) { assert.object(rdn, 'rdn (RDN) required');
if (typeof (rdn) !== 'object')
throw new TypeError('rdn (RDN) required');
return this.rdns.push(rdn); return this.rdns.push(rdn);
}; };
DN.prototype.shift = function dnShift() {
DN.prototype.shift = function () {
return this.rdns.shift(); return this.rdns.shift();
}; };
DN.prototype.unshift = function dnUnshift(rdn) {
DN.prototype.unshift = function (rdn) { assert.object(rdn, 'rdn (RDN) required');
if (typeof (rdn) !== 'object')
throw new TypeError('rdn (RDN) required');
return this.rdns.unshift(rdn); return this.rdns.unshift(rdn);
}; };
DN.isDN = function isDN(dn) {
DN.isDN = function (dn) {
if (!dn || typeof (dn) !== 'object') { if (!dn || typeof (dn) !== 'object') {
return false; return false;
} }
@ -523,11 +494,7 @@ DN.isDN = function (dn) {
///--- Exports ///--- Exports
module.exports = { module.exports = {
parse: parse, parse: parse,
DN: DN, DN: DN,
RDN: RDN RDN: RDN
}; };

View File

@ -3,13 +3,11 @@
var test = require('tape').test; var test = require('tape').test;
///--- Globals ///--- Globals
var dn; var dn;
///--- Tests ///--- Tests
test('load library', function (t) { test('load library', function (t) {
@ -205,6 +203,51 @@ test('format persists across clone', function (t) {
}); });
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();
});
test('isDN duck-testing', function (t) { test('isDN duck-testing', function (t) {
var valid = dn.parse('cn=foo'); var valid = dn.parse('cn=foo');
var isDN = dn.DN.isDN; var isDN = dn.DN.isDN;