2019-08-27 18:17:33 +00:00
|
|
|
'use strict';
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const { test } = require('tap');
|
|
|
|
const { BerReader, BerWriter } = require('asn1');
|
|
|
|
const { AddRequest, Attribute, dn } = require('../../lib');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
test('new no args', t => {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(new AddRequest());
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
test('new with args', t => {
|
|
|
|
const req = new AddRequest({
|
2011-08-04 20:32:01 +00:00
|
|
|
entry: dn.parse('cn=foo, o=test'),
|
|
|
|
attributes: [new Attribute({type: 'cn', vals: ['foo']}),
|
|
|
|
new Attribute({type: 'objectclass', vals: ['person']})]
|
|
|
|
});
|
|
|
|
t.ok(req);
|
|
|
|
t.equal(req.dn.toString(), 'cn=foo, o=test');
|
|
|
|
t.equal(req.attributes.length, 2);
|
|
|
|
t.equal(req.attributes[0].type, 'cn');
|
|
|
|
t.equal(req.attributes[0].vals[0], 'foo');
|
|
|
|
t.equal(req.attributes[1].type, 'objectclass');
|
|
|
|
t.equal(req.attributes[1].vals[0], 'person');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
test('parse', t => {
|
|
|
|
const ber = new BerWriter();
|
2013-10-24 23:27:42 +00:00
|
|
|
ber.writeString('cn=foo, o=test');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeString('cn');
|
|
|
|
ber.startSequence(0x31);
|
|
|
|
ber.writeString('foo');
|
|
|
|
ber.endSequence();
|
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeString('objectclass');
|
|
|
|
ber.startSequence(0x31);
|
|
|
|
ber.writeString('person');
|
|
|
|
ber.endSequence();
|
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
ber.endSequence();
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const req = new AddRequest();
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(req._parse(new BerReader(ber.buffer)));
|
|
|
|
t.equal(req.dn.toString(), 'cn=foo, o=test');
|
|
|
|
t.equal(req.attributes.length, 2);
|
|
|
|
t.equal(req.attributes[0].type, 'cn');
|
|
|
|
t.equal(req.attributes[0].vals[0], 'foo');
|
|
|
|
t.equal(req.attributes[1].type, 'objectclass');
|
|
|
|
t.equal(req.attributes[1].vals[0], 'person');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
test('toBer', t => {
|
|
|
|
const req = new AddRequest({
|
2011-08-04 20:32:01 +00:00
|
|
|
messageID: 123,
|
|
|
|
entry: dn.parse('cn=foo, o=test'),
|
|
|
|
attributes: [new Attribute({type: 'cn', vals: ['foo']}),
|
|
|
|
new Attribute({type: 'objectclass', vals: ['person']})]
|
|
|
|
});
|
|
|
|
|
|
|
|
t.ok(req);
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const ber = new BerReader(req.toBer());
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(ber);
|
|
|
|
t.equal(ber.readSequence(), 0x30);
|
|
|
|
t.equal(ber.readInt(), 123);
|
|
|
|
t.equal(ber.readSequence(), 0x68);
|
|
|
|
t.equal(ber.readString(), 'cn=foo, o=test');
|
|
|
|
t.ok(ber.readSequence());
|
|
|
|
|
|
|
|
t.ok(ber.readSequence());
|
|
|
|
t.equal(ber.readString(), 'cn');
|
|
|
|
t.equal(ber.readSequence(), 0x31);
|
|
|
|
t.equal(ber.readString(), 'foo');
|
|
|
|
|
|
|
|
t.ok(ber.readSequence());
|
|
|
|
t.equal(ber.readString(), 'objectclass');
|
|
|
|
t.equal(ber.readSequence(), 0x31);
|
|
|
|
t.equal(ber.readString(), 'person');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2011-08-12 23:37:47 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
test('toObject', t => {
|
|
|
|
const req = new AddRequest({
|
2011-08-12 23:37:47 +00:00
|
|
|
entry: dn.parse('cn=foo, o=test'),
|
|
|
|
attributes: [new Attribute({type: 'cn', vals: ['foo', 'bar']}),
|
|
|
|
new Attribute({type: 'objectclass', vals: ['person']})]
|
|
|
|
});
|
|
|
|
|
|
|
|
t.ok(req);
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
const obj = req.toObject();
|
2011-08-12 23:37:47 +00:00
|
|
|
t.ok(obj);
|
|
|
|
|
|
|
|
t.ok(obj.dn);
|
|
|
|
t.equal(obj.dn, 'cn=foo, o=test');
|
|
|
|
t.ok(obj.attributes);
|
|
|
|
t.ok(obj.attributes.cn);
|
|
|
|
t.ok(Array.isArray(obj.attributes.cn));
|
|
|
|
t.equal(obj.attributes.cn.length, 2);
|
|
|
|
t.equal(obj.attributes.cn[0], 'foo');
|
|
|
|
t.equal(obj.attributes.cn[1], 'bar');
|
|
|
|
t.ok(obj.attributes.objectclass);
|
|
|
|
t.ok(Array.isArray(obj.attributes.objectclass));
|
|
|
|
t.equal(obj.attributes.objectclass.length, 1);
|
|
|
|
t.equal(obj.attributes.objectclass[0], 'person');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|