Refactor ServerSideSortingControl

- Fix lint errors
- Always store SSSC sort fields as array
- Rename test file to match 'make test' pattern
This commit is contained in:
Patrick Mooney 2014-06-06 17:23:27 -05:00
parent 349e0f91bd
commit f1d4b667c3
2 changed files with 58 additions and 71 deletions

View File

@ -13,7 +13,6 @@ var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function ServerSideSortingControl(options) {
@ -26,7 +25,7 @@ function ServerSideSortingControl(options) {
this.parse(options.value);
} else if (Array.isArray(options.value)) {
for (var i = 0; i < options.value.length; i++) {
if (!typeof (options.value[i]) === 'object') {
if (typeof (options.value[i]) !== 'object') {
throw new TypeError('Elements of options.value must be Objects');
} else if (!options.value[i].hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType');
@ -37,7 +36,7 @@ function ServerSideSortingControl(options) {
if (!options.value.hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType');
}
this._value = options.value;
this._value = [options.value];
} else {
throw new TypeError('options.value must be a Buffer, Array or Object');
}
@ -47,7 +46,7 @@ function ServerSideSortingControl(options) {
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
return self._value || [];
});
}
util.inherits(ServerSideSortingControl, Control);
@ -58,22 +57,23 @@ ServerSideSortingControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
var ber = new BerReader(buffer);
var item;
if (ber.readSequence(0x30)) {
this._value = [];
while (ber.readSequence(0x30)) {
var sortKeyListItem = this._parseSortKeyListItem(ber)
this._value.push(sortKeyListItem);
item = {};
item.attributeType = ber.readString(asn1.Ber.OctetString);
if (ber.peek() == 0x80) {
item.orderingRule = ber.readString(0x80);
}
if (ber.peek() == 0x81) {
item.reverseOrder = (ber._readTag(0x81) === 0 ? false : true);
}
this._value.push(item);
}
if (this._value.length == 1) {
this._value = this._value[0];
}
return true;
}
return false;
};
@ -81,20 +81,25 @@ ServerSideSortingControl.prototype.parse = function parse(buffer) {
ServerSideSortingControl.prototype._toBer = function (ber) {
assert.ok(ber);
if (!this._value)
if (!this._value || this.value.length === 0)
return;
var writer = new BerWriter();
writer.startSequence(0x30);
if (Array.isArray(this.value)) {
for (var i = 0; i < this.value.length; i++) {
this._sortKeyListItemToBer(writer, this.value[i]);
for (var i = 0; i < this.value.length; i++) {
var item = this.value[i];
writer.startSequence(0x30);
if (item.attributeType) {
writer.writeString(item.attributeType, asn1.Ber.OctetString);
}
} else if (typeof (this.value) === 'object') {
this._sortKeyListItemToBer(writer, this.value);
if (item.orderingRule) {
writer.writeString(item.orderingRule, 0x80);
}
if (item.reverseOrder) {
writer.writeBoolean(item.reverseOrder, 0x81);
}
writer.endSequence();
}
writer.endSequence();
ber.writeBuffer(writer.buffer, 0x04);
};
@ -105,33 +110,5 @@ ServerSideSortingControl.prototype._json = function (obj) {
return obj;
};
ServerSideSortingControl.prototype._sortKeyListItemToBer = function(writer, obj) {
writer.startSequence(0x30);
if (obj.attributeType) {
writer.writeString(obj.attributeType, asn1.Ber.OctetString);
}
if (obj.orderingRule) {
writer.writeString(obj.orderingRule, 0x80);
}
if (obj.reverseOrder) {
writer.writeBoolean(obj.reverseOrder, 0x81);
}
writer.endSequence();
};
ServerSideSortingControl.prototype._parseSortKeyListItem = function(reader) {
var sortKeyListItem = {};
sortKeyListItem.attributeType = reader.readString(asn1.Ber.OctetString);
if (reader.peek() == 0x80) {
sortKeyListItem.orderingRule = reader.readString(0x80);
}
if (reader.peek() == 0x81) {
sortKeyListItem.reverseOrder = (reader._readTag(0x81) === 0 ? false : true);
}
return sortKeyListItem;
};
ServerSideSortingControl.OID = '1.2.840.113556.1.4.473';

View File

@ -12,24 +12,20 @@ var ServerSideSortingControl;
test('load library', function (t) {
ServerSideSortingControl =
require('../../lib').ServerSideSortingControl;
ServerSideSortingControl = require('../../lib').ServerSideSortingControl;
t.ok(ServerSideSortingControl);
getControl = require('../../lib').getControl;
t.ok(getControl);
t.end();
});
test('new no args', function (t) {
t.ok(new ServerSideSortingControl());
t.end();
});
test('new with args', function (t) {
var c = new ServerSideSortingControl({
type: '1.2.840.113556.1.4.473',
criticality: true,
value: {
attributeType: 'sn'
@ -38,14 +34,14 @@ test('new with args', function (t) {
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.473');
t.ok(c.criticality);
t.equal(c.value.attributeType, 'sn');
t.equal(c.value.length, 1);
t.equal(c.value[0].attributeType, 'sn');
t.end();
});
test('tober - object', function (t) {
test('toBer - object', function (t) {
var sssc = new ServerSideSortingControl({
type: '1.2.840.113556.1.4.473',
criticality: true,
value: {
attributeType: 'sn',
@ -60,26 +56,27 @@ test('tober - object', function (t) {
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.473');
t.ok(c.criticality);
t.equal(c.value.attributeType, 'sn');
t.equal(c.value.orderingRule, 'caseIgnoreOrderingMatch');
t.equal(c.value.reverseOrder, true);
t.equal(c.value[0].attributeType, 'sn');
t.equal(c.value[0].orderingRule, 'caseIgnoreOrderingMatch');
t.equal(c.value[0].reverseOrder, true);
t.end();
});
test('tober - array', function (t) {
test('toBer - array', function (t) {
var sssc = new ServerSideSortingControl({
type: '1.2.840.113556.1.4.473',
criticality: true,
value: [{
attributeType: 'sn',
orderingRule: 'caseIgnoreOrderingMatch',
reverseOrder: true
},
{
attributeType: 'givenName',
orderingRule: 'caseIgnoreOrderingMatch'
}]
value: [
{
attributeType: 'sn',
orderingRule: 'caseIgnoreOrderingMatch',
reverseOrder: true
},
{
attributeType: 'givenName',
orderingRule: 'caseIgnoreOrderingMatch'
}
]
});
var ber = new BerWriter();
@ -89,6 +86,7 @@ test('tober - array', function (t) {
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.473');
t.ok(c.criticality);
t.equal(c.value.length, 2);
t.equal(c.value[0].attributeType, 'sn');
t.equal(c.value[0].orderingRule, 'caseIgnoreOrderingMatch');
t.equal(c.value[0].reverseOrder, true);
@ -97,3 +95,15 @@ test('tober - array', function (t) {
t.end();
});
test('toBer - empty', function (t) {
var sssc = new ServerSideSortingControl();
var ber = new BerWriter();
sssc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.473');
t.equal(c.value.length, 0);
t.end();
});