Minor refactoring of PersistentSearchControl commit

This commit is contained in:
Mark Cavage 2011-12-08 14:54:40 -08:00
parent ee89eff6b4
commit ebde1df00c
6 changed files with 132 additions and 129 deletions

View File

@ -7,9 +7,14 @@ var asn1 = require('asn1');
var Protocol = require('../protocol'); var Protocol = require('../protocol');
///--- Globals ///--- Globals
var Ber = asn1.Ber; var Ber = asn1.Ber;
///--- API ///--- API
function Control(options) { function Control(options) {
@ -28,7 +33,7 @@ function Control(options) {
} }
this.type = options.type || ''; this.type = options.type || '';
this.criticality = options.criticality || false; this.criticality = options.critical || options.criticality || false;
this.value = options.value || undefined; this.value = options.value || undefined;
var self = this; var self = this;
@ -42,9 +47,6 @@ function Control(options) {
} }
module.exports = Control; module.exports = Control;
Control.prototype.toString = function() {
return this.json;
};
Control.prototype.toBer = function(ber) { Control.prototype.toBer = function(ber) {
assert.ok(ber); assert.ok(ber);
@ -52,7 +54,18 @@ Control.prototype.toBer = function(ber) {
ber.startSequence(); ber.startSequence();
ber.writeString(this.type || ''); ber.writeString(this.type || '');
ber.writeBoolean(this.criticality); ber.writeBoolean(this.criticality);
if (this.value) if (typeof(this._toBer) === 'function') {
ber.writeString(this.value); this._toBer(ber);
} else {
if (this.value)
ber.writeString(this.value);
}
ber.endSequence(); ber.endSequence();
return;
};
Control.prototype.toString = function() {
return this.json;
}; };

View File

@ -1,38 +1,56 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert'); var assert = require('assert');
var Control = require('./control'); var Control = require('./control');
var PersistentSearchControl = require('./persistent_search_control'); var PersistentSearchControl = require('./persistent_search_control');
var OID_PERSISTENT_SEARCH_CONTROL = '2.16.840.1.113730.3.4.3';
///--- API ///--- API
module.exports = { module.exports = {
getControl: function(ber) {
getControl: function getControl(ber) {
assert.ok(ber); assert.ok(ber);
if (ber.readSequence() === null) if (ber.readSequence() === null)
return; return null;
var type;
var critical = false;
var value;
var end = ber.offset + ber.length;
var options = {};
if (ber.length) { if (ber.length) {
options.type = ber.readString(); var end = ber.offset + ber.length;
type = ber.readString();
if (ber.offset < end) { if (ber.offset < end) {
if (ber.peek() === 0x01) // Boolean, optional if (ber.peek() === 0x01)
options.criticality = ber.readBoolean(); critical = ber.readBoolean();
}
if (ber.offset < end) {
if (options.type == OID_PERSISTENT_SEARCH_CONTROL) {
// send the buffer directly to the PSC
options.value = ber.readString(0x04, true);
return new PersistentSearchControl(options);
} else {
options.value = ber.readString();
return new Control(options);
}
} }
if (ber.offset < end)
value = ber.readString(0x04, true);
} }
var control;
switch (type) {
case PersistentSearchControl.OID:
control = new PersistentSearchControl({
critical: critical,
value: value
});
break;
default:
control = new Control({
type: type,
critical: critical,
value: value.toString('utf8')
});
}
return control;
}, },
Control: Control, Control: Control,

View File

@ -1,109 +1,87 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert'); var assert = require('assert');
var asn1 = require('asn1');
var buffer = require('buffer');
var Control = require('./control');
var util = require('util'); var util = require('util');
var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function PersistentSearchControl(options) { function PersistentSearchControl(options) {
if (options) { if (!options)
if (typeof(options) !== 'object')
throw new TypeError('options must be an object');
if (options.type && typeof(options.type) !== 'string')
throw new TypeError('options.type must be a string');
if (options.criticality !== undefined &&
typeof(options.criticality) !== 'boolean')
throw new TypeError('options.criticality must be a boolean');
if (options.value && !Buffer.isBuffer(options.value))
throw new TypeError('options.value must be a buffer');
} else {
options = {}; options = {};
}
this.type = options.type || '2.16.840.1.113730.3.4.3';
this.criticality = options.criticality || false;
options.type = PersistentSearchControl.OID;
if (options.value) { if (options.value) {
// parse out this.value into the PSC object if (Buffer.isBuffer(options.value)) {
var ber = new asn1.BerReader(options.value); this.parse(options.value);
if (ber.readSequence()) { } else if (typeof(options.value) === 'object') {
this.value = { this._value = options.value;
changeTypes: ber.readInt(), } else {
changesOnly: ber.readBoolean(), throw new TypeError('options.value must be a Buffer or Object');
returnECs: ber.readBoolean()
};
} }
options.value = null;
} }
Control.call(this, options);
var self = this; var self = this;
this.__defineGetter__('value', function() {
return self._value || {};
});
this.__defineGetter__('json', function() { this.__defineGetter__('json', function() {
return { var json = Control.prototype.json.call(self);
controlType: self.type, json.controlValue = self.value;
criticality: self.criticality, return json;
controlValue: self.value
};
}); });
} }
util.inherits(PersistentSearchControl, Control);
module.exports = PersistentSearchControl; module.exports = PersistentSearchControl;
// returns a psc given a fully populated psc object
PersistentSearchControl.prototype.get = function(options) { PersistentSearchControl.prototype.parse = function parse(buffer) {
if (options) { assert.ok(buffer);
if (typeof(options) !== 'object')
throw new TypeError('options must be an object'); var ber = new BerReader(buffer);
if (options.type && typeof(options.type) !== 'string') if (ber.readSequence()) {
throw new TypeError('options.type must be a string'); this._value = {
if (options.criticality !== undefined && changeTypes: ber.readInt(),
typeof(options.criticality) !== 'boolean') changesOnly: ber.readBoolean(),
throw new TypeError('options.criticality must be a boolean'); returnECs: ber.readBoolean()
if (options.value && typeof(options.value) !== 'object') { };
throw new TypeError('options.value must be an object');
} else { return true;
if (options.value.changeTypes &&
typeof(options.value.changeTypes) !== 'number')
throw new TypeError('options.value.changeTypes must be a number');
if (options.value.changesOnly !== undefined &&
typeof(options.value.changesOnly) !== 'boolean')
throw new TypeError('options.value.changesOnly must be a boolean');
if (options.value.returnECs !== undefined &&
typeof(options.value.returnECs) !== 'boolean')
throw new TypeError('options.value.returnECs must be a boolean');
}
} else {
options = {};
} }
this.type = options.type || ''; return false;
this.criticality = options.criticality || false;
this.value = options.value || undefined;
var self = this;
this.__defineGetter__('json', function() {
return {
controlType: self.type,
criticality: self.criticality,
controlValue: self.value
};
});
}; };
PersistentSearchControl.prototype.toBer = function(ber) {
PersistentSearchControl.prototype._toBer = function(ber) {
assert.ok(ber); assert.ok(ber);
ber.startSequence(); if (!this._value)
ber.writeString(this.type); return;
ber.writeBoolean(this.criticality);
var pscWriter = new asn1.BerWriter(); var writer = new BerWriter();
writer.startSequence();
writer.writeInt(this.value.changeTypes);
writer.writeBoolean(this.value.changesOnly);
writer.writeBoolean(this.value.returnECs);
writer.endSequence();
// write the value subsequence ber.writeBuffer(writer.buffer, 0x04);
pscWriter.startSequence();
pscWriter.writeInt(this.value.changeTypes);
pscWriter.writeBoolean(this.value.changesOnly);
pscWriter.writeBoolean(this.value.returnECs);
pscWriter.endSequence();
// write the pscValue as a octetstring to the ber
ber.writeBuffer(pscWriter.buffer, 0x04);
ber.endSequence();
}; };
PersistentSearchControl.OID = '2.16.840.1.113730.3.4.3';

View File

@ -5,17 +5,21 @@ var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
var Control = require('../controls/index').Control; var Control = require('../controls').Control;
var Protocol = require('../protocol'); var Protocol = require('../protocol');
var logStub = require('../log_stub'); var logStub = require('../log_stub');
///--- Globals ///--- Globals
var Ber = asn1.Ber; var Ber = asn1.Ber;
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
var getControl = require('../controls/index').getControl; var getControl = require('../controls').getControl;
///--- API ///--- API
@ -76,7 +80,6 @@ LDAPMessage.prototype.parse = function(ber) {
ber.readSequence(); ber.readSequence();
var end = ber.offset + ber.length; var end = ber.offset + ber.length;
while (ber.offset < end) { while (ber.offset < end) {
var c = getControl(ber); var c = getControl(ber);
if (c) if (c)
this.controls.push(c); this.controls.push(c);

View File

@ -17,7 +17,7 @@
"node": ">=0.4" "node": ">=0.4"
}, },
"dependencies": { "dependencies": {
"asn1": "0.1.8", "asn1": "0.1.10",
"buffertools": "1.0.5", "buffertools": "1.0.5",
"dtrace-provider": "0.0.3", "dtrace-provider": "0.0.3",
"nopt": "1.0.10", "nopt": "1.0.10",

View File

@ -3,24 +3,21 @@
var test = require('tap').test; var test = require('tap').test;
var asn1 = require('asn1'); var asn1 = require('asn1');
var log4js = require('log4js');
var sys = require('sys');
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
var getControl; var getControl;
var PersistentSearchControl; var PersistentSearchControl;
///--- Globals
var LOG = log4js.getLogger('persistent_search_control.test');
///--- Tests ///--- Tests
test('load library', function(t) { test('load library', function(t) {
PersistentSearchControl = PersistentSearchControl =
require('../../lib/controls/index').PersistentSearchControl; require('../../lib/controls').PersistentSearchControl;
t.ok(PersistentSearchControl); t.ok(PersistentSearchControl);
getControl = require('../../lib/controls/index').getControl; getControl = require('../../lib/controls').getControl;
t.ok(getControl); t.ok(getControl);
t.end(); t.end();
}); });
@ -33,7 +30,7 @@ test('new no args', function(t) {
test('new with args', function(t) { test('new with args', function(t) {
var options = { var c = new PersistentSearchControl({
type: '2.16.840.1.113730.3.4.3', type: '2.16.840.1.113730.3.4.3',
criticality: true, criticality: true,
value: { value: {
@ -41,10 +38,7 @@ test('new with args', function(t) {
changesOnly: false, changesOnly: false,
returnECs: false returnECs: false
} }
}; });
var c = new PersistentSearchControl();
c.get(options);
t.ok(c); t.ok(c);
t.equal(c.type, '2.16.840.1.113730.3.4.3'); t.equal(c.type, '2.16.840.1.113730.3.4.3');
t.ok(c.criticality); t.ok(c.criticality);
@ -87,7 +81,6 @@ test('getControl with args', function(t) {
var ber = new BerReader(buf); var ber = new BerReader(buf);
var psc = getControl(ber); var psc = getControl(ber);
LOG.info(psc.value);
t.ok(psc); t.ok(psc);
t.equal(psc.type, '2.16.840.1.113730.3.4.3'); t.equal(psc.type, '2.16.840.1.113730.3.4.3');
t.equal(psc.criticality, false); t.equal(psc.criticality, false);
@ -98,8 +91,7 @@ test('getControl with args', function(t) {
}); });
test('tober', function(t) { test('tober', function(t) {
var ber = new BerWriter(); var psc = new PersistentSearchControl({
var options = {
type: '2.16.840.1.113730.3.4.3', type: '2.16.840.1.113730.3.4.3',
criticality: true, criticality: true,
value: { value: {
@ -107,10 +99,9 @@ test('tober', function(t) {
changesOnly: false, changesOnly: false,
returnECs: false returnECs: false
} }
}; });
var psc = new PersistentSearchControl(); var ber = new BerWriter();
psc.get(options);
psc.toBer(ber); psc.toBer(ber);
var c = getControl(new BerReader(ber.buffer)); var c = getControl(new BerReader(ber.buffer));