diff --git a/lib/controls/server_side_sorting_control.js b/lib/controls/server_side_sorting_control.js index 995a369..0f6308a 100644 --- a/lib/controls/server_side_sorting_control.js +++ b/lib/controls/server_side_sorting_control.js @@ -58,13 +58,18 @@ ServerSideSortingControl.prototype.parse = function parse(buffer) { assert.ok(buffer); var ber = new BerReader(buffer); - if (ber.readSequence()) { - this._value = {}; - this._value.sortResult = ber.readInt(); - this._value.attributeType = ber.readString(asn1.Ber.OctetString, true); - //readString returns '' instead of a zero-length buffer - if (!this._value.attributeType) - this._value.attributeType = new Buffer(0); + + if (ber.readSequence(0x30)) { + this._value = []; + + while (ber.readSequence(0x30)) { + var sortKeyListItem = this._parseSortKeyListItem(ber) + this._value.push(sortKeyListItem); + } + + if (this._value.length == 1) { + this._value = this._value[0]; + } return true; } @@ -114,4 +119,19 @@ ServerSideSortingControl.prototype._sortKeyListItemToBer = function(writer, obj) 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'; diff --git a/test/controls/server_side_sorting_control_test.js b/test/controls/server_side_sorting_control_test.js index 9cca748..dc28a75 100644 --- a/test/controls/server_side_sorting_control_test.js +++ b/test/controls/server_side_sorting_control_test.js @@ -8,11 +8,6 @@ var BerWriter = asn1.BerWriter; var getControl; var ServerSideSortingControl; -function bufferEqual(t, a, b) { - t.equal(a.toString('hex'), b.toString('hex')); -} - - ///--- Tests @@ -45,28 +40,18 @@ test('new with args', function (t) { t.ok(c.criticality); t.equal(c.value.attributeType, 'sn'); - var writer = new BerWriter(); - c.toBer(writer); - var reader = new BerReader(writer.buffer); - var sssc = getControl(reader); - t.ok(sssc); - console.log('sssc', sssc.value); - t.equal(sssc.type, '1.2.840.113556.1.4.473'); - t.ok(sssc.criticality); - t.equal(sssc.value.attributeType, 'sn'); - bufferEqual(t, sssc.value.cookie, new Buffer(['sn'])); - t.end(); }); -test('tober', function (t) { +test('tober - object', function (t) { var sssc = new ServerSideSortingControl({ type: '1.2.840.113556.1.4.473', criticality: true, value: { - attributeType: 'sn' - } - }); + attributeType: 'sn', + orderingRule: 'caseIgnoreOrderingMatch', + reverseOrder: true + }}); var ber = new BerWriter(); sssc.toBer(ber); @@ -76,7 +61,39 @@ test('tober', function (t) { t.equal(c.type, '1.2.840.113556.1.4.473'); t.ok(c.criticality); t.equal(c.value.attributeType, 'sn'); - bufferEqual(t, c.value.cookie, new Buffer(0)); + t.equal(c.value.orderingRule, 'caseIgnoreOrderingMatch'); + t.equal(c.value.reverseOrder, true); + + t.end(); +}); + +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' + }] + }); + + 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.ok(c.criticality); + t.equal(c.value[0].attributeType, 'sn'); + t.equal(c.value[0].orderingRule, 'caseIgnoreOrderingMatch'); + t.equal(c.value[0].reverseOrder, true); + t.equal(c.value[1].attributeType, 'givenName'); + t.equal(c.value[1].orderingRule, 'caseIgnoreOrderingMatch'); t.end(); });