Merge ServerSideSort controls

Fix mcavage/node-ldapjs#187
This commit is contained in:
Patrick Mooney 2014-06-30 11:56:50 -05:00
commit 8f29f3b68d
5 changed files with 464 additions and 1 deletions

View File

@ -7,6 +7,10 @@ var EntryChangeNotificationControl =
require('./entry_change_notification_control');
var PersistentSearchControl = require('./persistent_search_control');
var PagedResultsControl = require('./paged_results_control');
var ServerSideSortingRequestControl =
require('./server_side_sorting_request_control.js');
var ServerSideSortingResponseControl =
require('./server_side_sorting_response_control.js');
@ -57,6 +61,18 @@ module.exports = {
value: value
});
break;
case ServerSideSortingRequestControl.OID:
control = new ServerSideSortingRequestControl({
critical: critical,
value: value
});
break;
case ServerSideSortingResponseControl.OID:
control = new ServerSideSortingResponseControl({
critical: critical,
value: value
});
break;
default:
control = new Control({
type: type,
@ -72,5 +88,7 @@ module.exports = {
Control: Control,
EntryChangeNotificationControl: EntryChangeNotificationControl,
PagedResultsControl: PagedResultsControl,
PersistentSearchControl: PersistentSearchControl
PersistentSearchControl: PersistentSearchControl,
ServerSideSortingRequestControl: ServerSideSortingRequestControl,
ServerSideSortingResponseControl: ServerSideSortingResponseControl
};

View File

@ -0,0 +1,114 @@
var assert = require('assert');
var util = require('util');
var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function ServerSideSortingRequestControl(options) {
if (!options)
options = {};
options.type = ServerSideSortingRequestControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
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') {
throw new TypeError('Elements of options.value must be Objects');
} else if (!options.value[i].hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType');
}
}
this._value = options.value;
} else if (typeof (options.value) === 'object') {
if (!options.value.hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType');
}
this._value = [options.value];
} else {
throw new TypeError('options.value must be a Buffer, Array or Object');
}
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || [];
});
}
util.inherits(ServerSideSortingRequestControl, Control);
module.exports = ServerSideSortingRequestControl;
ServerSideSortingRequestControl.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)) {
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);
}
return true;
}
return false;
};
ServerSideSortingRequestControl.prototype._toBer = function (ber) {
assert.ok(ber);
if (!this._value || this.value.length === 0)
return;
var writer = new BerWriter();
writer.startSequence(0x30);
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);
}
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);
};
ServerSideSortingRequestControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
ServerSideSortingRequestControl.OID = '1.2.840.113556.1.4.473';

View File

@ -0,0 +1,105 @@
var assert = require('assert');
var util = require('util');
var asn1 = require('asn1');
var Control = require('./control');
var CODES = require('../errors/index');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var VALID_CODES = [
CODES.LDAP_SUCCESS,
CODES.LDAP_OPERATIONS_ERROR,
CODES.LDAP_TIME_LIMIT_EXCEEDED,
CODES.LDAP_STRONG_AUTH_REQUIRED,
CODES.LDAP_ADMIN_LIMIT_EXCEEDED,
CODES.LDAP_NO_SUCH_ATTRIBUTE,
CODES.LDAP_INAPPROPRIATE_MATCHING,
CODES.LDAP_INSUFFICIENT_ACCESS_RIGHTS,
CODES.LDAP_BUSY,
CODES.LDAP_UNWILLING_TO_PERFORM,
CODES.LDAP_OTHER
];
function ServerSideSortingResponseControl(options) {
if (!options)
options = {};
options.type = ServerSideSortingResponseControl.OID;
options.criticality = false;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
this.parse(options.value);
} else if (typeof (options.value) === 'object') {
if (VALID_CODES.indexOf(options.value.result) === -1) {
throw new Error('Invalid result code');
}
if (options.value.failedAttribute &&
typeof (options.value.failedAttribute) !== 'string') {
throw new Error('failedAttribute must be String');
}
this._value = options.value;
} else {
throw new TypeError('options.value must be a Buffer or Object');
}
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
}
util.inherits(ServerSideSortingResponseControl, Control);
module.exports = ServerSideSortingResponseControl;
ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
var ber = new BerReader(buffer);
if (ber.readSequence(0x30)) {
this._value = {};
this._value.result = ber.readEnumeration();
if (ber.peek() == 0x80) {
this._value.failedAttribute = ber.readString(0x80);
}
return true;
}
return false;
};
ServerSideSortingResponseControl.prototype._toBer = function (ber) {
assert.ok(ber);
if (!this._value || this.value.length === 0)
return;
var writer = new BerWriter();
writer.startSequence(0x30);
writer.writeEnumeration(this.value.result);
if (this.value.result !== CODES.LDAP_SUCCESS && this.value.failedAttribute) {
writer.writeString(this.value.failedAttribute, 0x80);
}
writer.endSequence();
ber.writeBuffer(writer.buffer, 0x04);
};
ServerSideSortingResponseControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
ServerSideSortingResponseControl.OID = '1.2.840.113556.1.4.474';

View File

@ -0,0 +1,109 @@
var test = require('tap').test;
var asn1 = require('asn1');
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var getControl;
var SSSRControl;
///--- Tests
test('load library', function (t) {
SSSRControl = require('../../lib').ServerSideSortingRequestControl;
t.ok(SSSRControl);
getControl = require('../../lib').getControl;
t.ok(getControl);
t.end();
});
test('new no args', function (t) {
t.ok(new SSSRControl());
t.end();
});
test('new with args', function (t) {
var c = new SSSRControl({
criticality: true,
value: {
attributeType: 'sn'
}
});
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.473');
t.ok(c.criticality);
t.equal(c.value.length, 1);
t.equal(c.value[0].attributeType, 'sn');
t.end();
});
test('toBer - object', function (t) {
var sssc = new SSSRControl({
criticality: true,
value: {
attributeType: 'sn',
orderingRule: 'caseIgnoreOrderingMatch',
reverseOrder: true
}});
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.end();
});
test('toBer - array', function (t) {
var sssc = new SSSRControl({
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.length, 2);
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();
});
test('toBer - empty', function (t) {
var sssc = new SSSRControl();
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();
});

View File

@ -0,0 +1,117 @@
var test = require('tap').test;
var asn1 = require('asn1');
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var ldap;
var getControl;
var SSSResponseControl;
var OID = '1.2.840.113556.1.4.474';
///--- Tests
test('load library', function (t) {
ldap = require('../../lib');
SSSResponseControl = ldap.ServerSideSortingResponseControl;
t.ok(SSSResponseControl);
getControl = ldap.getControl;
t.ok(getControl);
t.end();
});
test('new no args', function (t) {
var c = new SSSResponseControl();
t.ok(c);
t.equal(c.type, OID);
t.equal(c.criticality, false);
t.end();
});
test('new with args', function (t) {
var c = new SSSResponseControl({
criticality: true,
value: {
result: ldap.LDAP_SUCCESS,
failedAttribute: 'cn'
}
});
t.ok(c);
t.equal(c.type, OID);
t.equal(c.criticality, false);
t.equal(c.value.result, ldap.LDAP_SUCCESS);
t.equal(c.value.failedAttribute, 'cn');
t.end();
});
test('toBer - success', function (t) {
var sssc = new SSSResponseControl({
value: {
result: ldap.LDAP_SUCCESS,
failedAttribute: 'foobar'
}});
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.474');
t.equal(c.criticality, false);
t.equal(c.value.result, ldap.LDAP_SUCCESS);
t.notOk(c.value.failedAttribute);
t.end();
});
test('toBer - simple failure', function (t) {
var sssc = new SSSResponseControl({
value: {
result: ldap.LDAP_NO_SUCH_ATTRIBUTE
}});
var ber = new BerWriter();
sssc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, OID);
t.equal(c.criticality, false);
t.equal(c.value.result, ldap.LDAP_NO_SUCH_ATTRIBUTE);
t.notOk(c.value.failedAttribute);
t.end();
});
test('toBer - detailed failure', function (t) {
var sssc = new SSSResponseControl({
value: {
result: ldap.LDAP_NO_SUCH_ATTRIBUTE,
failedAttribute: 'foobar'
}});
var ber = new BerWriter();
sssc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, OID);
t.equal(c.criticality, false);
t.equal(c.value.result, ldap.LDAP_NO_SUCH_ATTRIBUTE);
t.equal(c.value.failedAttribute, 'foobar');
t.end();
});
test('toBer - empty', function (t) {
var sssc = new SSSResponseControl();
var ber = new BerWriter();
sssc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, OID);
t.equal(c.criticality, false);
t.notOk(c.value.result);
t.notOk(c.value.failedAttribute);
t.end();
});