Fixed parse method and added some tests

This commit is contained in:
= 2014-05-30 20:57:06 +00:00
parent c1af9a8814
commit b742e286db
2 changed files with 65 additions and 28 deletions

View File

@ -58,13 +58,18 @@ ServerSideSortingControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
var ber = new BerReader(buffer); var ber = new BerReader(buffer);
if (ber.readSequence()) {
this._value = {}; if (ber.readSequence(0x30)) {
this._value.sortResult = ber.readInt(); this._value = [];
this._value.attributeType = ber.readString(asn1.Ber.OctetString, true);
//readString returns '' instead of a zero-length buffer while (ber.readSequence(0x30)) {
if (!this._value.attributeType) var sortKeyListItem = this._parseSortKeyListItem(ber)
this._value.attributeType = new Buffer(0); this._value.push(sortKeyListItem);
}
if (this._value.length == 1) {
this._value = this._value[0];
}
return true; return true;
} }
@ -114,4 +119,19 @@ ServerSideSortingControl.prototype._sortKeyListItemToBer = function(writer, obj)
writer.endSequence(); 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'; ServerSideSortingControl.OID = '1.2.840.113556.1.4.473';

View File

@ -8,11 +8,6 @@ var BerWriter = asn1.BerWriter;
var getControl; var getControl;
var ServerSideSortingControl; var ServerSideSortingControl;
function bufferEqual(t, a, b) {
t.equal(a.toString('hex'), b.toString('hex'));
}
///--- Tests ///--- Tests
@ -45,28 +40,18 @@ test('new with args', function (t) {
t.ok(c.criticality); t.ok(c.criticality);
t.equal(c.value.attributeType, 'sn'); 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(); t.end();
}); });
test('tober', function (t) { test('tober - object', function (t) {
var sssc = new ServerSideSortingControl({ var sssc = new ServerSideSortingControl({
type: '1.2.840.113556.1.4.473', type: '1.2.840.113556.1.4.473',
criticality: true, criticality: true,
value: { value: {
attributeType: 'sn' attributeType: 'sn',
} orderingRule: 'caseIgnoreOrderingMatch',
}); reverseOrder: true
}});
var ber = new BerWriter(); var ber = new BerWriter();
sssc.toBer(ber); sssc.toBer(ber);
@ -76,7 +61,39 @@ test('tober', function (t) {
t.equal(c.type, '1.2.840.113556.1.4.473'); t.equal(c.type, '1.2.840.113556.1.4.473');
t.ok(c.criticality); t.ok(c.criticality);
t.equal(c.value.attributeType, 'sn'); 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(); t.end();
}); });