node-ldapjs/test/change.test.js

117 lines
2.3 KiB
JavaScript
Raw Normal View History

2011-08-04 20:32:01 +00:00
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var test = require('tap').test;
var asn1 = require('asn1');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
var Attribute;
var Change;
///--- Tests
test('load library', function (t) {
2011-08-04 20:32:01 +00:00
Attribute = require('../lib/index').Attribute;
Change = require('../lib/index').Change;
t.ok(Attribute);
t.ok(Change);
t.end();
});
test('new no args', function (t) {
2011-08-04 20:32:01 +00:00
t.ok(new Change());
t.end();
});
test('new with args', function (t) {
2011-08-04 20:32:01 +00:00
var change = new Change({
operation: 0x00,
modification: new Attribute({
type: 'cn',
vals: ['foo', 'bar']
})
});
t.ok(change);
t.equal(change.operation, 'add');
2011-08-04 20:32:01 +00:00
t.equal(change.modification.type, 'cn');
t.equal(change.modification.vals.length, 2);
t.equal(change.modification.vals[0], 'foo');
t.equal(change.modification.vals[1], 'bar');
t.end();
});
test('GH-31 (multiple attributes per Change)', function (t) {
try {
t.notOk(new Change({
operation: 'replace',
modification: {
cn: 'foo',
sn: 'bar'
}
}), 'should have thrown');
} catch (e) {
t.ok(e);
t.end();
}
});
test('toBer', function (t) {
2011-08-04 20:32:01 +00:00
var change = new Change({
operation: 'Add',
modification: new Attribute({
type: 'cn',
vals: ['foo', 'bar']
})
});
t.ok(change);
var ber = new BerWriter();
change.toBer(ber);
var reader = new BerReader(ber.buffer);
t.ok(reader.readSequence());
t.equal(reader.readEnumeration(), 0x00);
t.ok(reader.readSequence());
t.equal(reader.readString(), 'cn');
t.equal(reader.readSequence(), 0x31); // lber set
t.equal(reader.readString(), 'foo');
t.equal(reader.readString(), 'bar');
t.end();
});
test('parse', function (t) {
2011-08-04 20:32:01 +00:00
var ber = new BerWriter();
ber.startSequence();
ber.writeEnumeration(0x00);
ber.startSequence();
ber.writeString('cn');
ber.startSequence(0x31);
ber.writeStringArray(['foo', 'bar']);
ber.endSequence();
ber.endSequence();
ber.endSequence();
var change = new Change();
t.ok(change);
t.ok(change.parse(new BerReader(ber.buffer)));
t.equal(change.operation, 'add');
2011-08-04 20:32:01 +00:00
t.equal(change.modification.type, 'cn');
t.equal(change.modification.vals.length, 2);
t.equal(change.modification.vals[0], 'foo');
t.equal(change.modification.vals[1], 'bar');
t.end();
});