Merge pull request #67 from nrogers/master

add support for RFC 2696 paged results controls
This commit is contained in:
Mark Cavage 2012-03-27 12:09:09 -07:00
commit 34b9d7f8fd
3 changed files with 181 additions and 0 deletions

View File

@ -6,6 +6,7 @@ var Control = require('./control');
var EntryChangeNotificationControl =
require('./entry_change_notification_control');
var PersistentSearchControl = require('./persistent_search_control');
var PagedResultsControl = require('./paged_results_control');
@ -50,6 +51,12 @@ module.exports = {
value: value
});
break;
case PagedResultsControl.OID:
control = new PagedResultsControl({
critical: critical,
value: value
});
break;
default:
control = new Control({
type: type,
@ -64,5 +71,6 @@ module.exports = {
Control: Control,
EntryChangeNotificationControl: EntryChangeNotificationControl,
PagedResultsControl: PagedResultsControl,
PersistentSearchControl: PersistentSearchControl
};

View File

@ -0,0 +1,87 @@
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 PagedResultsControl(options) {
if (!options)
options = {};
options.type = PagedResultsControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
this.parse(options.value);
} else if (typeof (options.value) === 'object') {
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(PagedResultsControl, Control);
module.exports = PagedResultsControl;
PagedResultsControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
var ber = new BerReader(buffer);
if (ber.readSequence()) {
this._value = {};
this._value.size = ber.readInt();
this._value.cookie = ber.readString(asn1.Ber.OctetString, true);
if(!this._value.cookie) this._value.cookie = new Buffer(0); //readString returns '' instead of a zero-length buffer
return true;
}
return false;
};
PagedResultsControl.prototype._toBer = function (ber) {
assert.ok(ber);
if (!this._value)
return;
var writer = new BerWriter();
writer.startSequence();
writer.writeInt(this.value.size);
if(this.value.cookie && this.value.cookie.length>0)
writer.writeBuffer(this.value.cookie, asn1.Ber.OctetString);
else
writer.writeString(''); //writeBuffer rejects zero-length buffers
writer.endSequence();
ber.writeBuffer(writer.buffer, 0x04);
};
PagedResultsControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
PagedResultsControl.OID = '1.2.840.113556.1.4.319';

View File

@ -0,0 +1,86 @@
var test = require('tap').test;
var asn1 = require('asn1');
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var getControl;
var PagedResultsControl;
function bufferEqual(t, a, b) {
t.equal(a.toString('hex'), b.toString('hex'))
}
///--- Tests
test('load library', function (t) {
PagedResultsControl =
require('../../lib').PagedResultsControl;
t.ok(PagedResultsControl);
getControl = require('../../lib').getControl;
t.ok(getControl);
t.end();
});
test('new no args', function (t) {
t.ok(new PagedResultsControl());
t.end();
});
test('new with args', function (t) {
var c = new PagedResultsControl({
type: '1.2.840.113556.1.4.319',
criticality: true,
value: {
size: 1000,
cookie: new Buffer([1,2,3])
}
});
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.319');
t.ok(c.criticality);
t.equal(c.value.size, 1000);
bufferEqual(t,c.value.cookie, new Buffer([1,2,3]));
var writer = new BerWriter();
c.toBer(writer);
var reader = new BerReader(writer.buffer);
var psc = getControl(reader);
t.ok(psc);
console.log('psc', psc.value);
t.equal(psc.type, '1.2.840.113556.1.4.319');
t.ok(psc.criticality);
t.equal(psc.value.size, 1000);
bufferEqual(t,psc.value.cookie, new Buffer([1,2,3]));
t.end();
});
test('tober', function (t) {
var psc = new PagedResultsControl({
type: '1.2.840.113556.1.4.319',
criticality: true,
value: {
size: 20,
cookie: new Buffer(0)
}
});
var ber = new BerWriter();
psc.toBer(ber);
var c = getControl(new BerReader(ber.buffer));
t.ok(c);
t.equal(c.type, '1.2.840.113556.1.4.319');
t.ok(c.criticality);
t.equal(c.value.size, 20);
bufferEqual(t,c.value.cookie, new Buffer(0));
t.end();
});