Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 69c72dc

Browse files
committed
Clean up DN asserts and tests
1 parent dcce6eb commit 69c72dc

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed

lib/dn.js

Lines changed: 49 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
22

33

4+
var assert = require('assert-plus');
5+
6+
7+
///--- Helpers
48

59
function invalidDN(name) {
610
var e = new Error();
@@ -9,18 +13,26 @@ function invalidDN(name) {
913
return e;
1014
}
1115

12-
1316
function isAlphaNumeric(c) {
1417
var re = /[A-Za-z0-9]/;
1518
return re.test(c);
1619
}
1720

18-
1921
function isWhitespace(c) {
2022
var re = /\s/;
2123
return re.test(c);
2224
}
2325

26+
function repeatChar(c, n) {
27+
var out = '';
28+
var max = n ? n : 0;
29+
for (var i = 0; i < max; i++)
30+
out += c;
31+
return out;
32+
}
33+
34+
///--- API
35+
2436
function RDN(obj) {
2537
var self = this;
2638
this.attrs = {};
@@ -32,12 +44,10 @@ function RDN(obj) {
3244
}
3345
}
3446

47+
RDN.prototype.set = function rdnSet(name, value, opts) {
48+
assert.string(name, 'name (string) required');
49+
assert.string(value, 'value (string) required');
3550

36-
RDN.prototype.set = function set(name, value, opts) {
37-
if (typeof (name) !== 'string')
38-
throw new TypeError('name (string) required');
39-
if (typeof (value) !== 'string')
40-
throw new TypeError('value (string) required');
4151
var self = this;
4252
var lname = name.toLowerCase();
4353
this.attrs[lname] = {
@@ -52,8 +62,7 @@ RDN.prototype.set = function set(name, value, opts) {
5262
}
5363
};
5464

55-
56-
RDN.prototype.equals = function equals(rdn) {
65+
RDN.prototype.equals = function rdnEquals(rdn) {
5766
if (typeof (rdn) !== 'object')
5867
return false;
5968

@@ -79,13 +88,10 @@ RDN.prototype.equals = function equals(rdn) {
7988
* Convert RDN to string according to specified formatting options.
8089
* (see: DN.format for option details)
8190
*/
82-
RDN.prototype.format = function format(options) {
83-
if (options) {
84-
if (typeof (options) !== 'object')
85-
throw new TypeError('options must be an object');
86-
} else {
87-
options = {};
88-
}
91+
RDN.prototype.format = function rdnFormat(options) {
92+
assert.optionalObject(options, 'options must be an object');
93+
options = options || {};
94+
8995
var self = this;
9096
var str = '';
9197

@@ -154,8 +160,7 @@ RDN.prototype.format = function format(options) {
154160
return str;
155161
};
156162

157-
158-
RDN.prototype.toString = function toString() {
163+
RDN.prototype.toString = function rdnToString() {
159164
return this.format();
160165
};
161166

@@ -314,26 +319,18 @@ function parse(name) {
314319
}
315320

316321

317-
318-
///--- API
319-
320-
321322
function DN(rdns) {
322-
if (!Array.isArray(rdns))
323-
throw new TypeError('rdns ([object]) required');
324-
rdns.forEach(function (rdn) {
325-
if (typeof (rdn) !== 'object')
326-
throw new TypeError('rdns ([object]) required');
327-
});
323+
assert.optionalArrayOfObject(rdns, '[object] required');
328324

329-
this.rdns = rdns.slice();
325+
this.rdns = rdns ? rdns.slice() : [];
330326
this._format = {};
331-
332-
this.__defineGetter__('length', function () {
333-
return this.rdns.length;
334-
});
335327
}
336-
328+
Object.defineProperties(DN.prototype, {
329+
length: {
330+
get: function getLength() { return this.rdns.length; },
331+
configurable: false
332+
}
333+
});
337334

338335
/**
339336
* Convert DN to string according to specified formatting options.
@@ -358,21 +355,11 @@ function DN(rdns) {
358355
* - upperName: RDN names will be uppercased instead of lowercased.
359356
* - skipSpace: Disable trailing space after RDN separators
360357
*/
361-
DN.prototype.format = function (options) {
362-
if (options) {
363-
if (typeof (options) !== 'object')
364-
throw new TypeError('options must be an object');
365-
} else {
366-
options = this._format;
367-
}
358+
DN.prototype.format = function dnFormat(options) {
359+
assert.optionalObject(options, 'options must be an object');
360+
options = options || this._format;
361+
368362
var str = '';
369-
function repeatChar(c, n) {
370-
var out = '';
371-
var max = n ? n : 0;
372-
for (var i = 0; i < max; i++)
373-
out += c;
374-
return out;
375-
}
376363
this.rdns.forEach(function (rdn) {
377364
var rdnString = rdn.format(options);
378365
if (str.length !== 0) {
@@ -390,22 +377,19 @@ DN.prototype.format = function (options) {
390377
return str;
391378
};
392379

393-
394380
/**
395381
* Set default string formatting options.
396382
*/
397383
DN.prototype.setFormat = function setFormat(options) {
398-
if (typeof (options) !== 'object')
399-
throw new TypeError('options must be an object');
384+
assert.object(options, 'options must be an object');
385+
400386
this._format = options;
401387
};
402388

403-
404-
DN.prototype.toString = function () {
389+
DN.prototype.toString = function dnToString() {
405390
return this.format();
406391
};
407392

408-
409393
DN.prototype.parentOf = function parentOf(dn) {
410394
if (typeof (dn) !== 'object')
411395
dn = parse(dn);
@@ -425,20 +409,17 @@ DN.prototype.parentOf = function parentOf(dn) {
425409
return true;
426410
};
427411

428-
429412
DN.prototype.childOf = function childOf(dn) {
430413
if (typeof (dn) !== 'object')
431414
dn = parse(dn);
432415
return dn.parentOf(this);
433416
};
434417

435-
436418
DN.prototype.isEmpty = function isEmpty() {
437419
return (this.rdns.length === 0);
438420
};
439421

440-
441-
DN.prototype.equals = function (dn) {
422+
DN.prototype.equals = function dnEquals(dn) {
442423
if (typeof (dn) !== 'object')
443424
dn = parse(dn);
444425

@@ -453,8 +434,7 @@ DN.prototype.equals = function (dn) {
453434
return true;
454435
};
455436

456-
457-
DN.prototype.parent = function () {
437+
DN.prototype.parent = function dnParent() {
458438
if (this.rdns.length !== 0) {
459439
var save = this.rdns.shift();
460440
var dn = new DN(this.rdns);
@@ -465,47 +445,38 @@ DN.prototype.parent = function () {
465445
return null;
466446
};
467447

468-
469-
DN.prototype.clone = function () {
448+
DN.prototype.clone = function dnClone() {
470449
var dn = new DN(this.rdns);
471450
dn._format = this._format;
472451
return dn;
473452
};
474453

475-
476-
DN.prototype.reverse = function () {
454+
DN.prototype.reverse = function dnReverse() {
477455
this.rdns.reverse();
478456
return this;
479457
};
480458

481-
482-
DN.prototype.pop = function () {
459+
DN.prototype.pop = function dnPop() {
483460
return this.rdns.pop();
484461
};
485462

486-
487-
DN.prototype.push = function (rdn) {
488-
if (typeof (rdn) !== 'object')
489-
throw new TypeError('rdn (RDN) required');
463+
DN.prototype.push = function dnPush(rdn) {
464+
assert.object(rdn, 'rdn (RDN) required');
490465

491466
return this.rdns.push(rdn);
492467
};
493468

494-
495-
DN.prototype.shift = function () {
469+
DN.prototype.shift = function dnShift() {
496470
return this.rdns.shift();
497471
};
498472

499-
500-
DN.prototype.unshift = function (rdn) {
501-
if (typeof (rdn) !== 'object')
502-
throw new TypeError('rdn (RDN) required');
473+
DN.prototype.unshift = function dnUnshift(rdn) {
474+
assert.object(rdn, 'rdn (RDN) required');
503475

504476
return this.rdns.unshift(rdn);
505477
};
506478

507-
508-
DN.isDN = function (dn) {
479+
DN.isDN = function isDN(dn) {
509480
if (!dn || typeof (dn) !== 'object') {
510481
return false;
511482
}
@@ -523,11 +494,7 @@ DN.isDN = function (dn) {
523494
///--- Exports
524495

525496
module.exports = {
526-
527497
parse: parse,
528-
529498
DN: DN,
530-
531499
RDN: RDN
532-
533500
};

test/dn.test.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
var test = require('tape').test;
44

55

6-
76
///--- Globals
87

98
var dn;
109

1110

12-
1311
///--- Tests
1412

1513
test('load library', function (t) {
@@ -205,6 +203,51 @@ test('format persists across clone', function (t) {
205203
});
206204

207205

206+
test('initialization', function (t) {
207+
var dn1 = new dn.DN();
208+
t.ok(dn1);
209+
t.equals(dn1.toString(), '');
210+
t.ok(dn1.isEmpty(), 'DN with no initializer defaults to null DN');
211+
212+
var data = [
213+
new dn.RDN({ foo: 'bar' }),
214+
new dn.RDN({ o: 'base' })
215+
];
216+
var dn2 = new dn.DN(data);
217+
t.ok(dn2);
218+
t.equals(dn2.toString(), 'foo=bar, o=base');
219+
t.ok(!dn2.isEmpty());
220+
221+
t.end();
222+
});
223+
224+
225+
test('array functions', function (t) {
226+
var dn1 = dn.parse('a=foo, b=bar, c=baz');
227+
t.ok(dn1);
228+
t.equal(dn1.toString(), 'a=foo, b=bar, c=baz');
229+
230+
t.ok(dn1.reverse());
231+
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
232+
233+
var rdn = dn1.pop();
234+
t.ok(rdn);
235+
t.equal(dn1.toString(), 'c=baz, b=bar');
236+
237+
t.ok(dn1.push(rdn));
238+
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
239+
240+
rdn = dn1.shift();
241+
t.ok(rdn);
242+
t.equal(dn1.toString(), 'b=bar, a=foo');
243+
244+
t.ok(dn1.unshift(rdn));
245+
t.equal(dn1.toString(), 'c=baz, b=bar, a=foo');
246+
247+
t.end();
248+
});
249+
250+
208251
test('isDN duck-testing', function (t) {
209252
var valid = dn.parse('cn=foo');
210253
var isDN = dn.DN.isDN;

0 commit comments

Comments
 (0)