From 6b514b97dfbb5c975d1ce73316d04f133dd330a4 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Tue, 27 Aug 2019 14:17:33 -0400 Subject: [PATCH] Refactor tests to be independent of each other --- lib/attribute.js | 2 +- lib/client/index.js | 5 +- lib/controls/paged_results_control.js | 2 +- lib/index.js | 2 +- lib/logger.js | 6 + lib/messages/del_request.js | 2 +- lib/messages/ext_request.js | 4 +- lib/messages/message.js | 3 +- lib/messages/parser.js | 6 +- test/attribute.test.js | 65 +- test/change.test.js | 68 +- test/client.test.js | 689 ++++++++---------- test/controls/control.test.js | 41 +- .../entry_change_notification_control.test.js | 44 +- test/controls/paged_results_control.test.js | 57 +- .../persistent_search_control.test.js | 2 +- ...erver_side_sorting_control_request.test.js | 43 +- ...rver_side_sorting_control_response.test.js | 55 +- test/dn.test.js | 107 +-- test/errors.test.js | 30 +- test/filters/and.test.js | 36 +- test/filters/approx.test.js | 53 +- test/filters/eq.test.js | 81 +- test/filters/ext.test.js | 50 +- test/filters/ge.test.js | 61 +- test/filters/le.test.js | 62 +- test/filters/not.test.js | 37 +- test/filters/or.test.js | 37 +- test/filters/parse.test.js | 53 +- test/filters/presence.test.js | 55 +- test/filters/substr.test.js | 65 +- test/laundry.test.js | 111 ++- test/messages/add_request.test.js | 58 +- test/messages/add_response.test.js | 38 +- test/messages/bind_request.test.js | 40 +- test/messages/bind_response.test.js | 38 +- test/messages/compare_request.test.js | 41 +- test/messages/compare_response.test.js | 38 +- test/messages/del_request.test.js | 45 +- test/messages/del_response.test.js | 38 +- test/messages/ext_request.test.js | 97 +-- test/messages/ext_response.test.js | 38 +- test/messages/moddn_request.test.js | 40 +- test/messages/moddn_response.test.js | 38 +- test/messages/modify_request.test.js | 46 +- test/messages/modify_response.test.js | 38 +- test/messages/parser.test.js | 36 +- test/messages/search_entry.test.js | 43 +- test/messages/search_request.test.js | 45 +- test/messages/search_response.test.js | 37 +- test/messages/unbind_request.test.js | 38 +- test/server.test.js | 164 ++--- test/url.test.js | 32 +- test/utils.js | 17 + 54 files changed, 983 insertions(+), 1996 deletions(-) create mode 100644 lib/logger.js create mode 100644 test/utils.js diff --git a/lib/attribute.js b/lib/attribute.js index aa73562..4eed475 100644 --- a/lib/attribute.js +++ b/lib/attribute.js @@ -71,7 +71,7 @@ Attribute.prototype.addValue = function addValue(val) { if (Buffer.isBuffer(val)) { this._vals.push(val); } else { - this._vals.push(new Buffer(val + '', _bufferEncoding(this.type))); + this._vals.push(Buffer.from(val + '', _bufferEncoding(this.type))); } }; diff --git a/lib/client/index.js b/lib/client/index.js index 0deed74..c3f4e73 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -1,8 +1,7 @@ // Copyright 2012 Mark Cavage, Inc. All rights reserved. -var logger = Object.create(require('abstract-logging')); - -var Client = require('./client'); +const logger = require('../logger'); +const Client = require('./client'); diff --git a/lib/controls/paged_results_control.js b/lib/controls/paged_results_control.js index a89c661..fcfe468 100644 --- a/lib/controls/paged_results_control.js +++ b/lib/controls/paged_results_control.js @@ -48,7 +48,7 @@ PagedResultsControl.prototype.parse = function parse(buffer) { this._value.cookie = ber.readString(asn1.Ber.OctetString, true); //readString returns '' instead of a zero-length buffer if (!this._value.cookie) - this._value.cookie = new Buffer(0); + this._value.cookie = Buffer.alloc(0); return true; } diff --git a/lib/index.js b/lib/index.js index 940664c..0beb8b4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var logger = Object.create(require('abstract-logging')); +var logger = require('./logger'); var client = require('./client'); var Attribute = require('./attribute'); diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..239d393 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,6 @@ +'use strict'; + +const logger = Object.create(require('abstract-logging')) +logger.child = function () { return logger } + +module.exports = logger diff --git a/lib/messages/del_request.js b/lib/messages/del_request.js index dc66f55..2b97be2 100644 --- a/lib/messages/del_request.js +++ b/lib/messages/del_request.js @@ -44,7 +44,7 @@ DeleteRequest.prototype._parse = function (ber, length) { DeleteRequest.prototype._toBer = function (ber) { assert.ok(ber); - var buf = new Buffer(this.entry.toString()); + var buf = Buffer.from(this.entry.toString()); for (var i = 0; i < buf.length; i++) ber.writeByte(buf[i]); diff --git a/lib/messages/ext_request.js b/lib/messages/ext_request.js index 3dc18d2..1c464bf 100644 --- a/lib/messages/ext_request.js +++ b/lib/messages/ext_request.js @@ -28,7 +28,7 @@ function ExtendedRequest(options) { if(Buffer.isBuffer(this.requestValue)) { this.requestValueBuffer = this.requestValue; } else { - this.requestValueBuffer = new Buffer(this.requestValue || '', 'utf8'); + this.requestValueBuffer = Buffer.from(this.requestValue || '', 'utf8'); } } util.inherits(ExtendedRequest, LDAPMessage); @@ -58,7 +58,7 @@ Object.defineProperties(ExtendedRequest.prototype, { if(Buffer.isBuffer(val)) { this.requestValueBuffer = val; } else { - this.requestValueBuffer = new Buffer(val, 'utf8'); + this.requestValueBuffer = Buffer.from(val, 'utf8'); } this.requestValue = val; diff --git a/lib/messages/message.js b/lib/messages/message.js index aa8ce5e..0495f05 100644 --- a/lib/messages/message.js +++ b/lib/messages/message.js @@ -5,6 +5,7 @@ var util = require('util'); var asn1 = require('asn1'); +var logger = require('../logger') var Control = require('../controls').Control; var Protocol = require('../protocol'); @@ -32,7 +33,7 @@ function LDAPMessage(options) { this.protocolOp = options.protocolOp || undefined; this.controls = options.controls ? options.controls.slice(0) : []; - this.log = options.log; + this.log = options.log || logger; } Object.defineProperties(LDAPMessage.prototype, { id: { diff --git a/lib/messages/parser.js b/lib/messages/parser.js index 60de51e..b71543d 100644 --- a/lib/messages/parser.js +++ b/lib/messages/parser.js @@ -6,6 +6,7 @@ var util = require('util'); var assert = require('assert-plus'); var asn1 = require('asn1'); var VError = require('verror').VError; +var logger = require('../logger') var AbandonRequest = require('./abandon_request'); var AddRequest = require('./add_request'); @@ -43,14 +44,13 @@ var BerReader = asn1.BerReader; ///--- API -function Parser(options) { +function Parser(options = {}) { assert.object(options); - assert.object(options.log); EventEmitter.call(this); this.buffer = null; - this.log = options.log; + this.log = options.log || logger; } util.inherits(Parser, EventEmitter); diff --git a/test/attribute.test.js b/test/attribute.test.js index 861a4cd..faaf87f 100644 --- a/test/attribute.test.js +++ b/test/attribute.test.js @@ -1,34 +1,16 @@ -// 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; - - -///--- Tests - -test('load library', function (t) { - Attribute = require('../lib/index').Attribute; - t.ok(Attribute); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { Attribute } = require('../lib'); test('new no args', function (t) { t.ok(new Attribute()); t.end(); }); - test('new with args', function (t) { - var attr = new Attribute({ + const attr = new Attribute({ type: 'cn', vals: ['foo', 'bar'] }); @@ -43,7 +25,7 @@ test('new with args', function (t) { attr = new Attribute('not an object'); }); t.throws(function () { - var typeThatIsNotAString = 1; + const typeThatIsNotAString = 1; attr = new Attribute({ type: typeThatIsNotAString }); @@ -51,16 +33,15 @@ test('new with args', function (t) { t.end(); }); - test('toBer', function (t) { - var attr = new Attribute({ + const attr = new Attribute({ type: 'cn', vals: ['foo', 'bar'] }); t.ok(attr); - var ber = new BerWriter(); + const ber = new BerWriter(); attr.toBer(ber); - var reader = new BerReader(ber.buffer); + const reader = new BerReader(ber.buffer); t.ok(reader.readSequence()); t.equal(reader.readString(), 'cn'); t.equal(reader.readSequence(), 0x31); // lber set @@ -69,9 +50,8 @@ test('toBer', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.startSequence(); ber.writeString('cn'); ber.startSequence(0x31); @@ -79,7 +59,7 @@ test('parse', function (t) { ber.endSequence(); ber.endSequence(); - var attr = new Attribute(); + const attr = new Attribute(); t.ok(attr); t.ok(attr.parse(new BerReader(ber.buffer))); @@ -91,12 +71,12 @@ test('parse', function (t) { }); test('parse - without 0x31', function (t) { - var ber = new BerWriter; + const ber = new BerWriter; ber.startSequence(); ber.writeString('sn'); ber.endSequence(); - var attr = new Attribute; + const attr = new Attribute; t.ok(attr); t.ok(attr.parse(new BerReader(ber.buffer))); @@ -107,18 +87,18 @@ test('parse - without 0x31', function (t) { }); test('toString', function (t) { - var attr = new Attribute({ + const attr = new Attribute({ type: 'foobar', vals: ['asdf'] }); - var expected = attr.toString(); - var actual = JSON.stringify(attr.json); + const expected = attr.toString(); + const actual = JSON.stringify(attr.json); t.equal(actual, expected); t.end(); }); test('isAttribute', function (t) { - var isA = Attribute.isAttribute; + const isA = Attribute.isAttribute; t.notOk(isA(null)); t.notOk(isA('asdf')); t.ok(isA(new Attribute({ @@ -128,7 +108,7 @@ test('isAttribute', function (t) { t.ok(isA({ type: 'foo', - vals: ['item', new Buffer(5)], + vals: ['item', Buffer.alloc(5)], toBer: function () { /* placeholder */ } })); @@ -142,18 +122,17 @@ test('isAttribute', function (t) { t.end(); }); - test('compare', function (t) { - var comp = Attribute.compare; - var a = new Attribute({ + const comp = Attribute.compare; + const a = new Attribute({ type: 'foo', vals: ['bar'] }); - var b = new Attribute({ + const b = new Attribute({ type: 'foo', vals: ['bar'] }); - var notAnAttribute = 'this is not an attribute'; + const notAnAttribute = 'this is not an attribute'; t.throws(function () { comp(a, notAnAttribute); diff --git a/test/change.test.js b/test/change.test.js index c4c018a..b056c9a 100644 --- a/test/change.test.js +++ b/test/change.test.js @@ -1,37 +1,16 @@ -// 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) { - Attribute = require('../lib/index').Attribute; - Change = require('../lib/index').Change; - t.ok(Attribute); - t.ok(Change); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { Attribute, Change } = require('../lib'); test('new no args', function (t) { t.ok(new Change()); t.end(); }); - test('new with args', function (t) { - var change = new Change({ + const change = new Change({ operation: 'add', modification: new Attribute({ type: 'cn', @@ -49,9 +28,8 @@ test('new with args', function (t) { t.end(); }); - test('validate fields', function (t) { - var c = new Change(); + const c = new Change(); t.ok(c); t.throws(function () { c.operation = 'bogus'; @@ -66,10 +44,9 @@ test('validate fields', function (t) { t.end(); }); - test('GH-31 (multiple attributes per Change)', function (t) { t.throws(function () { - var c = new Change({ + const c = new Change({ operation: 'replace', modification: { cn: 'foo', @@ -81,9 +58,8 @@ test('GH-31 (multiple attributes per Change)', function (t) { t.end(); }); - test('toBer', function (t) { - var change = new Change({ + const change = new Change({ operation: 'Add', modification: new Attribute({ type: 'cn', @@ -92,9 +68,9 @@ test('toBer', function (t) { }); t.ok(change); - var ber = new BerWriter(); + const ber = new BerWriter(); change.toBer(ber); - var reader = new BerReader(ber.buffer); + const reader = new BerReader(ber.buffer); t.ok(reader.readSequence()); t.equal(reader.readEnumeration(), 0x00); t.ok(reader.readSequence()); @@ -105,9 +81,8 @@ test('toBer', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.startSequence(); ber.writeEnumeration(0x00); ber.startSequence(); @@ -118,7 +93,7 @@ test('parse', function (t) { ber.endSequence(); ber.endSequence(); - var change = new Change(); + const change = new Change(); t.ok(change); t.ok(change.parse(new BerReader(ber.buffer))); @@ -131,24 +106,23 @@ test('parse', function (t) { t.end(); }); - test('apply - replace', function (t) { - var res; - var single = new Change({ + let res; + const single = new Change({ operation: 'replace', modification: { type: 'cn', vals: ['new'] } }); - var twin = new Change({ + const twin = new Change({ operation: 'replace', modification: { type: 'cn', vals: ['new', 'two'] } }); - var empty = new Change({ + const empty = new Change({ operation: 'replace', modification: { type: 'cn', @@ -184,10 +158,9 @@ test('apply - replace', function (t) { t.end(); }); - test('apply - add', function (t) { - var res; - var single = new Change({ + let res; + const single = new Change({ operation: 'add', modification: { type: 'cn', @@ -222,10 +195,9 @@ test('apply - add', function (t) { t.end(); }); - test('apply - delete', function (t) { - var res; - var single = new Change({ + let res; + const single = new Change({ operation: 'delete', modification: { type: 'cn', diff --git a/test/client.test.js b/test/client.test.js index 199c7ee..7c698c9 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -1,44 +1,23 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. +'use strict'; -var logger = Object.create(require('abstract-logging')); +const util = require('util'); +const tap = require('tap'); +const uuid = require('uuid'); +const vasync = require('vasync'); +const { getSock } = require('./utils'); +const ldap = require('../lib'); +const { Attribute, Change } = ldap; -var test = require('tap').test; -var uuid = require('uuid'); -var vasync = require('vasync'); -var util = require('util'); +const SUFFIX = 'dc=test'; +const LDAP_CONNECT_TIMEOUT = process.env.LDAP_CONNECT_TIMEOUT || 0; +const BIND_DN = 'cn=root'; +const BIND_PW = 'secret'; +tap.beforeEach((done, t) => { + t.context.socketPath = getSock(); + t.context.server = ldap.createServer(); -///--- Globals - -var BIND_DN = 'cn=root'; -var BIND_PW = 'secret'; -var SOCKET = process.platform === 'win32' ? '\\\\.\\pipe\\' + uuid() : '/tmp/.' + uuid(); - -var SUFFIX = 'dc=test'; - -var ldap; -var Attribute; -var Change; -var client; -var server; - - -///--- Tests - -test('setup', function (t) { - ldap = require('../lib/index'); - t.ok(ldap); - t.ok(ldap.createClient); - t.ok(ldap.createServer); - t.ok(ldap.Attribute); - t.ok(ldap.Change); - - Attribute = ldap.Attribute; - Change = ldap.Change; - - server = ldap.createServer(); - t.ok(server); - + const server = t.context.server; server.bind(BIND_DN, function (req, res, next) { if (req.credentials !== BIND_PW) return next(new ldap.InvalidCredentialsError('Invalid password')); @@ -95,11 +74,10 @@ test('setup', function (t) { }); server.search('dc=timeout', function (req, res, next) { - // Haha client! + // Cause the client to timeout by not sending a response. }); server.search(SUFFIX, function (req, res, next) { - if (req.dn.equals('cn=ref,' + SUFFIX)) { res.send(res.createSearchReference('ldap://localhost')); } else if (req.dn.equals('cn=bin,' + SUFFIX)) { @@ -107,7 +85,7 @@ test('setup', function (t) { objectName: req.dn, attributes: { 'foo;binary': 'wr0gKyDCvCA9IMK+', - 'gb18030': new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]), + 'gb18030': Buffer.from([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]), 'objectclass': 'binary' } })); @@ -123,15 +101,13 @@ test('setup', function (t) { res.send(e); } - res.end(); return next(); }); server.search('cn=sizelimit', function (req, res, next) { - var sizeLimit = 200; - var i; - for (i = 0; i < 1000; i++) { + const sizeLimit = 200; + for (let i = 0; i < 1000; i++) { if (req.sizeLimit > 0 && i >= req.sizeLimit) { break; } else if (i > sizeLimit) { @@ -151,13 +127,13 @@ test('setup', function (t) { }); server.search('cn=paged', function (req, res, next) { - var min = 0; - var max = 1000; + const min = 0; + const max = 1000; function sendResults(start, end) { start = (start < min) ? min : start; end = (end > max || end < min) ? max : end; - var i; + let i; for (i = start; i < end; i++) { res.send({ dn: util.format('o=%d, cn=paged', i), @@ -170,8 +146,8 @@ test('setup', function (t) { return i; } - var cookie = null; - var pageSize = 0; + let cookie = null; + let pageSize = 0; req.controls.forEach(function (control) { if (control.type === ldap.PagedResultsControl.OID) { pageSize = control.value.size; @@ -181,17 +157,17 @@ test('setup', function (t) { if (cookie && Buffer.isBuffer(cookie)) { // Do simple paging - var first = min; + let first = min; if (cookie.length !== 0) { first = parseInt(cookie.toString(), 10); } - var last = sendResults(first, first + pageSize); + const last = sendResults(first, first + pageSize); - var resultCookie; + let resultCookie; if (last < max) { - resultCookie = new Buffer(last.toString()); + resultCookie = Buffer.from(last.toString()); } else { - resultCookie = new Buffer(''); + resultCookie = Buffer.from(''); } res.controls.push(new ldap.PagedResultsControl({ value: { @@ -208,7 +184,7 @@ test('setup', function (t) { }); server.search('cn=pagederr', function (req, res, next) { - var cookie = null; + let cookie = null; req.controls.forEach(function (control) { if (control.type === ldap.PagedResultsControl.OID) { cookie = control.value.cookie; @@ -226,7 +202,7 @@ test('setup', function (t) { res.controls.push(new ldap.PagedResultsControl({ value: { size: 2, - cookie: new Buffer('a') + cookie: Buffer.from('a') } })); res.end(); @@ -276,21 +252,25 @@ test('setup', function (t) { return next(); }); - server.listen(SOCKET, function () { - client = ldap.createClient({ - connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), - socketPath: SOCKET, - log: logger + server.listen(t.context.socketPath, function () { + const client = ldap.createClient({ + connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10), + socketPath: t.context.socketPath }); - t.ok(client); - t.end(); - }); + t.context.client = client; + client.on('connect', () => done()) + }) +}) -}); +tap.afterEach((done, t) => { + t.context.client.unbind((err) => { + t.error(err); + t.context.server.close(() => done()); + }) +}) - -test('simple bind failure', function (t) { - client.bind(BIND_DN, uuid(), function (err, res) { +tap.test('simple bind failure', function (t) { + t.context.client.bind(BIND_DN, uuid(), function (err, res) { t.ok(err); t.notOk(res); @@ -304,33 +284,29 @@ test('simple bind failure', function (t) { }); }); - -test('simple bind success', function (t) { - client.bind(BIND_DN, BIND_PW, function (err, res) { - t.ifError(err); +tap.test('simple bind success', function (t) { + t.context.client.bind(BIND_DN, BIND_PW, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('simple anonymous bind (empty credentials)', function (t) { - client.bind('', '', function (err, res) { - t.ifError(err); +tap.test('simple anonymous bind (empty credentials)', function (t) { + t.context.client.bind('', '', function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('auto-bind bad credentials', function (t) { - var clt = ldap.createClient({ - socketPath: SOCKET, +tap.test('auto-bind bad credentials', function (t) { + const clt = ldap.createClient({ + socketPath: t.context.socketPath, bindDN: BIND_DN, - bindCredentials: 'totallybogus', - log: logger + bindCredentials: 'totallybogus' }); clt.once('error', function (err) { t.equal(err.code, ldap.LDAP_INVALID_CREDENTIALS); @@ -340,13 +316,11 @@ test('auto-bind bad credentials', function (t) { }); }); - -test('auto-bind success', function (t) { - var clt = ldap.createClient({ - socketPath: SOCKET, +tap.test('auto-bind success', function (t) { + const clt = ldap.createClient({ + socketPath: t.context.socketPath, bindDN: BIND_DN, - bindCredentials: BIND_PW, - log: logger + bindCredentials: BIND_PW }); clt.once('connect', function () { t.ok(clt); @@ -355,65 +329,54 @@ test('auto-bind success', function (t) { }); }); - -test('add success', function (t) { - var attrs = [ +tap.test('add success', function (t) { + const attrs = [ new Attribute({ type: 'cn', vals: ['test'] }) ]; - client.add('cn=add, ' + SUFFIX, attrs, function (err, res) { - t.ifError(err); + t.context.client.add('cn=add, ' + SUFFIX, attrs, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('add success with object', function (t) { - var entry = { +tap.test('add success with object', function (t) { + const entry = { cn: ['unit', 'add'], sn: 'test' }; - client.add('cn=add, ' + SUFFIX, entry, function (err, res) { - t.ifError(err); + t.context.client.add('cn=add, ' + SUFFIX, entry, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('compare success', function (t) { - client.compare('cn=compare, ' + SUFFIX, 'cn', 'test', function (err, - matched, - res) { - t.ifError(err); +tap.test('compare success', function (t) { + t.context.client.compare('cn=compare, ' + SUFFIX, 'cn', 'test', function (err, matched, res) { + t.error(err); t.ok(matched); t.ok(res); t.end(); }); }); - -test('compare false', function (t) { - client.compare('cn=compare, ' + SUFFIX, 'cn', 'foo', function (err, - matched, - res) { - t.ifError(err); +tap.test('compare false', function (t) { + t.context.client.compare('cn=compare, ' + SUFFIX, 'cn', 'foo', function (err, matched, res) { + t.error(err); t.notOk(matched); t.ok(res); t.end(); }); }); - -test('compare bad suffix', function (t) { - client.compare('cn=' + uuid(), 'cn', 'foo', function (err, - matched, - res) { +tap.test('compare bad suffix', function (t) { + t.context.client.compare('cn=' + uuid(), 'cn', 'foo', function (err, matched, res) { t.ok(err); t.ok(err instanceof ldap.NoSuchObjectError); t.notOk(matched); @@ -422,32 +385,29 @@ test('compare bad suffix', function (t) { }); }); - -test('delete success', function (t) { - client.del('cn=delete, ' + SUFFIX, function (err, res) { - t.ifError(err); +tap.test('delete success', function (t) { + t.context.client.del('cn=delete, ' + SUFFIX, function (err, res) { + t.error(err); t.ok(res); t.end(); }); }); - -test('delete with control (GH-212)', function (t) { - var control = new ldap.Control({ +tap.test('delete with control (GH-212)', function (t) { + const control = new ldap.Control({ type: '1.2.3.4', criticality: false }); - client.del('cn=delete, ' + SUFFIX, control, function (err, res) { - t.ifError(err); + t.context.client.del('cn=delete, ' + SUFFIX, control, function (err, res) { + t.error(err); t.ok(res); t.end(); }); }); - -test('exop success', function (t) { - client.exop('1.3.6.1.4.1.4203.1.11.3', function (err, value, res) { - t.ifError(err); +tap.test('exop success', function (t) { + t.context.client.exop('1.3.6.1.4.1.4203.1.11.3', function (err, value, res) { + t.error(err); t.ok(value); t.ok(res); t.equal(value, 'u:xxyyz@EXAMPLE.NET'); @@ -455,9 +415,8 @@ test('exop success', function (t) { }); }); - -test('exop invalid', function (t) { - client.exop('1.2.3.4', function (err, res) { +tap.test('exop invalid', function (t) { + t.context.client.exop('1.2.3.4', function (err, res) { t.ok(err); t.ok(err instanceof ldap.ProtocolError); t.notOk(res); @@ -465,66 +424,60 @@ test('exop invalid', function (t) { }); }); - -test('bogus exop (GH-17)', function (t) { - client.exop('cn=root', function (err, value) { +tap.test('bogus exop (GH-17)', function (t) { + t.context.client.exop('cn=root', function (err) { t.ok(err); t.end(); }); }); - -test('modify success', function (t) { - var change = new Change({ +tap.test('modify success', function (t) { + const change = new Change({ type: 'Replace', modification: new Attribute({ type: 'cn', vals: ['test'] }) }); - client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { - t.ifError(err); + t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('modify change plain object success', function (t) { - var change = new Change({ +tap.test('modify change plain object success', function (t) { + const change = new Change({ type: 'Replace', modification: { cn: 'test' } }); - client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { - t.ifError(err); + t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - // https://github.com/ldapjs/node-ldapjs/pull/435 -test('can delete attributes', function (t) { - try { - var change = new Change({ - type: 'Delete', - modification: { cn: null } - }); - t.ok(true); +tap.test('can delete attributes', function (t) { + const change = new Change({ + type: 'Delete', + modification: { cn: null } + }); + t.context.client.modify('cn=modify,' + SUFFIX, change, function (err, res) { + t.error(err); + t.ok(res); + t.equal(res.status, 0); t.end(); - } catch (err) { - t.ifError(err); - t.end(); - } + }); }); - -test('modify array success', function (t) { - var changes = [ +tap.test('modify array success', function (t) { + const changes = [ new Change({ operation: 'Replace', modification: new Attribute({ @@ -539,57 +492,53 @@ test('modify array success', function (t) { }) }) ]; - client.modify('cn=modify, ' + SUFFIX, changes, function (err, res) { - t.ifError(err); + t.context.client.modify('cn=modify, ' + SUFFIX, changes, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('modify change plain object success (GH-31)', function (t) { - var change = { +tap.test('modify change plain object success (GH-31)', function (t) { + const change = { type: 'replace', modification: { cn: 'test', sn: 'bar' } }; - client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { - t.ifError(err); + t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('modify DN new RDN only', function (t) { - client.modifyDN('cn=old, ' + SUFFIX, 'cn=new', function (err, res) { - t.ifError(err); +tap.test('modify DN new RDN only', function (t) { + t.context.client.modifyDN('cn=old, ' + SUFFIX, 'cn=new', function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('modify DN new superior', function (t) { - client.modifyDN('cn=old, ' + SUFFIX, 'cn=new, dc=foo', function (err, res) { - t.ifError(err); +tap.test('modify DN new superior', function (t) { + t.context.client.modifyDN('cn=old, ' + SUFFIX, 'cn=new, dc=foo', function (err, res) { + t.error(err); t.ok(res); t.equal(res.status, 0); t.end(); }); }); - -test('search basic', function (t) { - client.search('cn=test, ' + SUFFIX, '(objectclass=*)', function (err, res) { - t.ifError(err); +tap.test('search basic', function (t) { + t.context.client.search('cn=test, ' + SUFFIX, '(objectclass=*)', function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; + let gotEntry = 0; res.on('searchEntry', function (entry) { t.ok(entry); t.ok(entry instanceof ldap.SearchEntry); @@ -614,11 +563,10 @@ test('search basic', function (t) { }); }); - -test('search sizeLimit', function (t) { +tap.test('search sizeLimit', function (t) { t.test('over limit', function (t2) { - client.search('cn=sizelimit', {}, function (err, res) { - t2.ifError(err); + t.context.client.search('cn=sizelimit', {}, function (err, res) { + t2.error(err); res.on('error', function (error) { t2.equal(error.name, 'SizeLimitExceededError'); t2.end(); @@ -627,10 +575,10 @@ test('search sizeLimit', function (t) { }); t.test('under limit', function (t2) { - var limit = 100; - client.search('cn=sizelimit', {sizeLimit: limit}, function (err, res) { - t2.ifError(err); - var count = 0; + const limit = 100; + t.context.client.search('cn=sizelimit', {sizeLimit: limit}, function (err, res) { + t2.error(err); + let count = 0; res.on('searchEntry', function (entry) { count++; }); @@ -639,68 +587,78 @@ test('search sizeLimit', function (t) { t2.equal(count, limit); t2.end(); }); - res.on('error', t2.ifError.bind(t)); + res.on('error', t2.error.bind(t)); }); }); t.end() }); - -test('search paged', {timeout: 10000}, function (t) { +tap.test('search paged', { timeout: 10000 }, function (t) { t.test('paged - no pauses', function (t2) { - var countEntries = 0; - var countPages = 0; - client.search('cn=paged', {paged: {pageSize: 100}}, function (err, res) { - t2.ifError(err); - res.on('searchEntry', function () { - countEntries++; - }); - res.on('page', function () { - countPages++; - }); - res.on('error', t2.ifError.bind(t2)); + let countEntries = 0; + let countPages = 0; + t.context.client.search('cn=paged', {paged: {pageSize: 100}}, function (err, res) { + t2.error(err); + res.on('searchEntry', entryListener); + res.on('page', pageListener); + res.on('error', (err) => t2.error(err)); res.on('end', function () { t2.equal(countEntries, 1000); t2.equal(countPages, 10); t2.end(); }); + + t2.tearDown(() => { + res.removeListener('searchEntry', entryListener); + res.removeListener('page', pageListener); + }) + + function entryListener() { + countEntries += 1; + } + + function pageListener () { + countPages += 1; + } }); }); t.test('paged - pauses', function (t2) { - var countPages = 0; - client.search('cn=paged', { + let countPages = 0; + t.context.client.search('cn=paged', { paged: { pageSize: 100, pagePause: true } }, function (err, res) { - t2.ifError(err); - res.on('page', function (result, cb) { - countPages++; - // cancel after 9 to verify callback usage - if (countPages === 9) { - // another page should never be encountered - res.removeAllListeners('page') - .on('page', t2.fail.bind(null, 'unexpected page')); - return cb(new Error()); - } - return cb(); - }); - res.on('error', t2.ifError.bind(t2)); + t2.error(err); + res.on('page', pageListener); + res.on('error', (err) => t2.error(err)); res.on('end', function () { t2.equal(countPages, 9); t2.end(); }); + + function pageListener (result, cb) { + countPages++; + // cancel after 9 to verify callback usage + if (countPages === 9) { + // another page should never be encountered + res.removeListener('page', pageListener) + .on('page', t2.fail.bind(null, 'unexpected page')); + return cb(new Error()); + } + return cb(); + } }); }); t.test('paged - no support (err handled)', function (t2) { - client.search(SUFFIX, { + t.context.client.search(SUFFIX, { paged: { pageSize: 100 } }, function (err, res) { - t2.ifError(err); + t2.error(err); res.on('pageError', t2.ok.bind(t2)); res.on('end', function () { t2.pass(); @@ -710,10 +668,10 @@ test('search paged', {timeout: 10000}, function (t) { }); t.test('paged - no support (err not handled)', function (t2) { - client.search(SUFFIX, { + t.context.client.search(SUFFIX, { paged: { pageSize: 100 } }, function (err, res) { - t2.ifError(err); + t2.error(err); res.on('end', t2.fail.bind(t2)); res.on('error', function (error) { t2.ok(error); @@ -724,7 +682,7 @@ test('search paged', {timeout: 10000}, function (t) { t.test('paged - redundant control', function (t2) { try { - client.search(SUFFIX, { + t.context.client.search(SUFFIX, { paged: { pageSize: 100 } }, new ldap.PagedResultsControl(), function (err, res) { @@ -737,12 +695,12 @@ test('search paged', {timeout: 10000}, function (t) { }); t.test('paged - handle later error', function (t2) { - var countEntries = 0; - var countPages = 0; - client.search('cn=pagederr', { + let countEntries = 0; + let countPages = 0; + t.context.client.search('cn=pagederr', { paged: { pageSize: 1 } }, function (err, res) { - t2.ifError(err); + t2.error(err); res.on('searchEntry', function () { t2.ok(++countEntries); }); @@ -763,13 +721,12 @@ test('search paged', {timeout: 10000}, function (t) { t.end(); }); - -test('search referral', function (t) { - client.search('cn=ref, ' + SUFFIX, '(objectclass=*)', function (err, res) { - t.ifError(err); +tap.test('search referral', function (t) { + t.context.client.search('cn=ref, ' + SUFFIX, '(objectclass=*)', function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; - var gotReferral = false; + let gotEntry = 0; + let gotReferral = false; res.on('searchEntry', function (entry) { gotEntry++; }); @@ -794,10 +751,9 @@ test('search referral', function (t) { }); }); - -test('search rootDSE', function (t) { - client.search('', '(objectclass=*)', function (err, res) { - t.ifError(err); +tap.test('search rootDSE', function (t) { + t.context.client.search('', '(objectclass=*)', function (err, res) { + t.error(err); t.ok(res); res.on('searchEntry', function (entry) { t.ok(entry); @@ -817,12 +773,11 @@ test('search rootDSE', function (t) { }); }); - -test('search empty attribute', function (t) { - client.search('dc=empty', '(objectclass=*)', function (err, res) { - t.ifError(err); +tap.test('search empty attribute', function (t) { + t.context.client.search('dc=empty', '(objectclass=*)', function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; + let gotEntry = 0; res.on('searchEntry', function (entry) { var obj = entry.toObject(); t.equal('dc=empty', obj.dn); @@ -845,14 +800,13 @@ test('search empty attribute', function (t) { }); }); - -test('GH-21 binary attributes', function (t) { - client.search('cn=bin, ' + SUFFIX, '(objectclass=*)', function (err, res) { - t.ifError(err); +tap.test('GH-21 binary attributes', function (t) { + t.context.client.search('cn=bin, ' + SUFFIX, '(objectclass=*)', function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; - var expect = new Buffer('\u00bd + \u00bc = \u00be', 'utf8'); - var expect2 = new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]); + let gotEntry = 0; + const expect = Buffer.from('\u00bd + \u00bc = \u00be', 'utf8'); + const expect2 = Buffer.from([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]); res.on('searchEntry', function (entry) { t.ok(entry); t.ok(entry instanceof ldap.SearchEntry); @@ -886,16 +840,15 @@ test('GH-21 binary attributes', function (t) { }); }); - -test('GH-23 case insensitive attribute filtering', function (t) { - var opts = { +tap.test('GH-23 case insensitive attribute filtering', function (t) { + const opts = { filter: '(objectclass=*)', attributes: ['Cn'] }; - client.search('cn=test, ' + SUFFIX, opts, function (err, res) { - t.ifError(err); + t.context.client.search('cn=test, ' + SUFFIX, opts, function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; + let gotEntry = 0; res.on('searchEntry', function (entry) { t.ok(entry); t.ok(entry instanceof ldap.SearchEntry); @@ -919,16 +872,15 @@ test('GH-23 case insensitive attribute filtering', function (t) { }); }); - -test('GH-24 attribute selection of *', function (t) { - var opts = { +tap.test('GH-24 attribute selection of *', function (t) { + const opts = { filter: '(objectclass=*)', attributes: ['*'] }; - client.search('cn=test, ' + SUFFIX, opts, function (err, res) { - t.ifError(err); + t.context.client.search('cn=test, ' + SUFFIX, opts, function (err, res) { + t.error(err); t.ok(res); - var gotEntry = 0; + let gotEntry = 0; res.on('searchEntry', function (entry) { t.ok(entry); t.ok(entry instanceof ldap.SearchEntry); @@ -953,52 +905,49 @@ test('GH-24 attribute selection of *', function (t) { }); }); - -test('idle timeout', function (t) { - client.idleTimeout = 250; +tap.test('idle timeout', function (t) { + t.context.client.idleTimeout = 250; function premature() { - t.ifError(true); + t.error(true); } - client.on('idle', premature); - client.search('dc=slow', 'objectclass=*', function (err, res) { - t.ifError(err); + t.context.client.on('idle', premature); + t.context.client.search('dc=slow', 'objectclass=*', function (err, res) { + t.error(err); res.on('searchEntry', function (res) { t.ok(res); }); res.on('error', function (err) { - t.ifError(err); + t.error(err); }); res.on('end', function () { var late = setTimeout(function () { - t.ifError(false, 'too late'); + t.error(false, 'too late'); }, 500); // It's ok to go idle now - client.removeListener('idle', premature); - client.on('idle', function () { + t.context.client.removeListener('idle', premature); + t.context.client.on('idle', function () { clearTimeout(late); - client.removeAllListeners('idle'); - client.idleTimeout = 0; + t.context.client.removeAllListeners('idle'); + t.context.client.idleTimeout = 0; t.end(); }); }); }); }); - -test('setup action', function (t) { - var setupClient = ldap.createClient({ - connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), - socketPath: SOCKET, - log: logger +tap.test('setup action', function (t) { + const setupClient = ldap.createClient({ + connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10), + socketPath: t.context.socketPath }); setupClient.on('setup', function (clt, cb) { clt.bind(BIND_DN, BIND_PW, function (err, res) { - t.ifError(err); + t.error(err); cb(err); }); }); setupClient.search(SUFFIX, {scope: 'base'}, function (err, res) { - t.ifError(err); + t.error(err); t.ok(res); res.on('end', function () { setupClient.destroy(); @@ -1007,36 +956,35 @@ test('setup action', function (t) { }); }); - -test('setup reconnect', function (t) { - var rClient = ldap.createClient({ - connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), - socketPath: SOCKET, - reconnect: true, - log: logger +tap.test('setup reconnect', function (t) { + const rClient = ldap.createClient({ + connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10), + socketPath: t.context.socketPath, + reconnect: true }); rClient.on('setup', function (clt, cb) { clt.bind(BIND_DN, BIND_PW, function (err, res) { - t.ifError(err); + t.error(err); cb(err); }); }); function doSearch(_, cb) { rClient.search(SUFFIX, {scope: 'base'}, function (err, res) { - t.ifError(err); + t.error(err); res.on('end', function () { cb(); }); }); } + vasync.pipeline({ funcs: [ doSearch, function cleanDisconnect(_, cb) { t.ok(rClient.connected); rClient.once('close', function (had_err) { - t.ifError(had_err); + t.error(had_err); t.equal(rClient.connected, false); cb(); }); @@ -1044,7 +992,7 @@ test('setup reconnect', function (t) { }, doSearch, function simulateError(_, cb) { - var msg = 'fake socket error'; + const msg = 'fake socket error'; rClient.once('error', function (err) { t.equal(err.message, msg); t.ok(err); @@ -1057,22 +1005,20 @@ test('setup reconnect', function (t) { }, doSearch ] - }, function (err, res) { - t.ifError(err); + }, function (err) { + t.error(err); rClient.destroy(); t.end(); }); }); - -test('setup abort', function (t) { - var setupClient = ldap.createClient({ - connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), - socketPath: SOCKET, - reconnect: true, - log: logger +tap.test('setup abort', function (t) { + const setupClient = ldap.createClient({ + connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10), + socketPath: t.context.socketPath, + reconnect: true }); - var message = 'It\'s a trap!'; + const message = "It's a trap!"; setupClient.on('setup', function (clt, cb) { // simulate failure t.ok(clt); @@ -1086,13 +1032,11 @@ test('setup abort', function (t) { }); }); - -test('abort reconnect', function (t) { - var abortClient = ldap.createClient({ - connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), - socketPath: '/dev/null', - reconnect: true, - log: logger +tap.test('abort reconnect', function (t) { + const abortClient = ldap.createClient({ + connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10), + socketPath: 'an invalid path', + reconnect: true }); var retryCount = 0; abortClient.on('connectError', function () { @@ -1108,21 +1052,19 @@ test('abort reconnect', function (t) { }); }); - -test('reconnect max retries', function (t) { - var RETRIES = 5; - var rClient = ldap.createClient({ +tap.test('reconnect max retries', function (t) { + const RETRIES = 5; + const rClient = ldap.createClient({ connectTimeout: 100, - socketPath: '/dev/null', + socketPath: 'an invalid path', reconnect: { failAfter: RETRIES, // Keep the test duration low initialDelay: 10, maxDelay: 100 - }, - log: logger + } }); - var count = 0; + let count = 0; rClient.on('connectError', function () { count++; }); @@ -1133,16 +1075,14 @@ test('reconnect max retries', function (t) { }); }); - -test('reconnect on server close', function (t) { - var clt = ldap.createClient({ - socketPath: SOCKET, - reconnect: true, - log: logger +tap.test('reconnect on server close', function (t) { + const clt = ldap.createClient({ + socketPath: t.context.socketPath, + reconnect: true }); clt.on('setup', function (sclt, cb) { sclt.bind(BIND_DN, BIND_PW, function (err, res) { - t.ifError(err); + t.error(err); cb(err); }); }); @@ -1159,22 +1099,20 @@ test('reconnect on server close', function (t) { }); }); - -test('no auto-reconnect on unbind', function (t) { - var clt = ldap.createClient({ - socketPath: SOCKET, - reconnect: true, - log: logger +tap.test('no auto-reconnect on unbind', function (t) { + const clt = ldap.createClient({ + socketPath: t.context.socketPath, + reconnect: true }); clt.on('setup', function (sclt, cb) { sclt.bind(BIND_DN, BIND_PW, function (err, res) { - t.ifError(err); + t.error(err); cb(err); }); }); clt.once('connect', function () { clt.once('connect', function () { - t.ifError(new Error('client should not reconnect')); + t.error(new Error('client should not reconnect')); }); clt.once('close', function () { t.ok(true, 'initial close'); @@ -1190,69 +1128,58 @@ test('no auto-reconnect on unbind', function (t) { }); }); - -test('abandon (GH-27)', function (t) { +tap.test('abandon (GH-27)', function (t) { // FIXME: test abandoning a real request - client.abandon(401876543, function (err) { - t.ifError(err); + t.context.client.abandon(401876543, function (err) { + t.error(err); t.end(); }); }); - -test('search timeout (GH-51)', function (t) { - client.timeout = 250; - client.search('dc=timeout', 'objectclass=*', function (err, res) { - t.ifError(err); +tap.test('search timeout (GH-51)', function (t) { + t.context.client.timeout = 250; + t.context.client.search('dc=timeout', 'objectclass=*', function (err, res) { + t.error(err); res.on('error', function () { t.end(); }); }); }); - -test('resultError handling', function (t) { - t.plan(3); - vasync.pipeline({ - funcs: [ - function errSearch(_, cb) { - client.once('resultError', function (error) { - t.equal(error.name, 'BusyError'); - }); - client.search('cn=busy', {}, function (err, res) { - res.once('error', function (error) { - t.equal(error.name, 'BusyError'); - cb(); - }); - }); - }, - function cleanSearch(_, cb) { - client.on('resultError', t.ifError.bind(null)); - client.search(SUFFIX, {}, function (err, res) { - res.once('end', function () { - t.ok(true); - cb(); - }); - }); - } - ] - }, function (err, res) { - client.removeAllListeners('resultError'); - }); -}); - - -test('unbind (GH-30)', function (t) { - client.unbind(function (err) { - t.ifError(err); +tap.test('resultError handling', function (t) { + const client = t.context.client; + vasync.pipeline({ funcs: [errSearch, cleanSearch] }, function (err) { + t.error(err); + client.removeListener('resultError', error1); + client.removeListener('resultError', error2); t.end(); }); -}); + function errSearch(_, cb) { + client.once('resultError', error1); + client.search('cn=busy', {}, function (err, res) { + res.once('error', function (error) { + t.equal(error.name, 'BusyError'); + cb(); + }); + }); + } -test('shutdown', function (t) { - server.on('close', function () { - t.end(); - }); - server.close(); + function cleanSearch(_, cb) { + client.on('resultError', error2); + client.search(SUFFIX, {}, function (err, res) { + res.once('end', function () { + t.pass(); + cb(); + }); + }); + } + + function error1 (error) { + t.equal(error.name, 'BusyError'); + } + + function error2 () { + t.fail('should not get error') + } }); diff --git a/test/controls/control.test.js b/test/controls/control.test.js index 3b7da62..b654261 100644 --- a/test/controls/control.test.js +++ b/test/controls/control.test.js @@ -1,37 +1,16 @@ -// 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 Control; -var getControl; - - -///--- Tests - -test('load library', function (t) { - Control = require('../../lib/index').Control; - t.ok(Control); - getControl = require('../../lib/index').getControl; - t.ok(getControl); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { Control, getControl } = require('../../lib'); test('new no args', function (t) { t.ok(new Control()); t.end(); }); - test('new with args', function (t) { - var c = new Control({ + const c = new Control({ type: '2.16.840.1.113730.3.4.2', criticality: true }); @@ -41,16 +20,15 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.startSequence(); ber.writeString('2.16.840.1.113730.3.4.2'); ber.writeBoolean(true); ber.writeString('foo'); ber.endSequence(); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '2.16.840.1.113730.3.4.2'); @@ -59,14 +37,13 @@ test('parse', function (t) { t.end(); }); - test('parse no value', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.startSequence(); ber.writeString('2.16.840.1.113730.3.4.2'); ber.endSequence(); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '2.16.840.1.113730.3.4.2'); diff --git a/test/controls/entry_change_notification_control.test.js b/test/controls/entry_change_notification_control.test.js index e9a9965..8b410c3 100644 --- a/test/controls/entry_change_notification_control.test.js +++ b/test/controls/entry_change_notification_control.test.js @@ -1,36 +1,16 @@ +'use strict'; -var test = require('tap').test; - -var asn1 = require('asn1'); - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var getControl; -var EntryChangeNotificationControl; - - - -///--- Tests - - -test('load library', function (t) { - EntryChangeNotificationControl = - require('../../lib').EntryChangeNotificationControl; - t.ok(EntryChangeNotificationControl); - getControl = require('../../lib').getControl; - t.ok(getControl); - t.end(); -}); - +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { getControl, EntryChangeNotificationControl } = require('../../lib'); test('new no args', function (t) { t.ok(new EntryChangeNotificationControl()); t.end(); }); - test('new with args', function (t) { - var c = new EntryChangeNotificationControl({ + const c = new EntryChangeNotificationControl({ type: '2.16.840.1.113730.3.4.7', criticality: true, value: { @@ -46,13 +26,11 @@ test('new with args', function (t) { t.equal(c.value.previousDN, 'cn=foobarbazcar'); t.equal(c.value.changeNumber, 123456789); - - var writer = new BerWriter(); + const writer = new BerWriter(); c.toBer(writer); - var reader = new BerReader(writer.buffer); - var psc = getControl(reader); + const reader = new BerReader(writer.buffer); + const psc = getControl(reader); t.ok(psc); - console.log('psc', psc.value); t.equal(psc.type, '2.16.840.1.113730.3.4.7'); t.ok(psc.criticality); t.equal(psc.value.changeType, 8); @@ -63,7 +41,7 @@ test('new with args', function (t) { }); test('tober', function (t) { - var psc = new EntryChangeNotificationControl({ + const psc = new EntryChangeNotificationControl({ type: '2.16.840.1.113730.3.4.7', criticality: true, value: { @@ -73,10 +51,10 @@ test('tober', function (t) { } }); - var ber = new BerWriter(); + const ber = new BerWriter(); psc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '2.16.840.1.113730.3.4.7'); t.ok(c.criticality); diff --git a/test/controls/paged_results_control.test.js b/test/controls/paged_results_control.test.js index e8c2f07..bbb3476 100644 --- a/test/controls/paged_results_control.test.js +++ b/test/controls/paged_results_control.test.js @@ -1,86 +1,61 @@ +'use strict'; -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(); -}); - +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { getControl, PagedResultsControl } = require('../../lib'); test('new no args', function (t) { t.ok(new PagedResultsControl()); t.end(); }); - test('new with args', function (t) { - var c = new PagedResultsControl({ + const c = new PagedResultsControl({ type: '1.2.840.113556.1.4.319', criticality: true, value: { size: 1000, - cookie: new Buffer([1, 2, 3]) + cookie: Buffer.from([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])); + t.is(Buffer.compare(c.value.cookie, Buffer.from([1, 2, 3])), 0) - - var writer = new BerWriter(); + const writer = new BerWriter(); c.toBer(writer); - var reader = new BerReader(writer.buffer); - var psc = getControl(reader); + const reader = new BerReader(writer.buffer); + const 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.is(Buffer.compare(psc.value.cookie, Buffer.from([1, 2, 3])), 0) t.end(); }); test('tober', function (t) { - var psc = new PagedResultsControl({ + const psc = new PagedResultsControl({ type: '1.2.840.113556.1.4.319', criticality: true, value: { size: 20, - cookie: new Buffer(0) + cookie: Buffer.alloc(0) } }); - var ber = new BerWriter(); + const ber = new BerWriter(); psc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const 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.is(Buffer.compare(c.value.cookie, Buffer.alloc(0)), 0) t.end(); }); diff --git a/test/controls/persistent_search_control.test.js b/test/controls/persistent_search_control.test.js index 37a7667..0ab1df8 100644 --- a/test/controls/persistent_search_control.test.js +++ b/test/controls/persistent_search_control.test.js @@ -62,7 +62,7 @@ test('new with args', function (t) { }); test('getControl with args', function (t) { - var buf = new Buffer([ + var buf = Buffer.from([ 0x30, 0x26, 0x04, 0x17, 0x32, 0x2e, 0x31, 0x36, 0x2e, 0x38, 0x34, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2e, 0x33, 0x2e, 0x34, 0x2e, 0x33, 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01, diff --git a/test/controls/server_side_sorting_control_request.test.js b/test/controls/server_side_sorting_control_request.test.js index e2787dd..3dbdbaa 100644 --- a/test/controls/server_side_sorting_control_request.test.js +++ b/test/controls/server_side_sorting_control_request.test.js @@ -1,23 +1,8 @@ +'use strict'; -var test = require('tap').test; - -var asn1 = require('asn1'); - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var getControl; -var SSSRControl; - -///--- Tests - - -test('load library', function (t) { - SSSRControl = require('../../lib').ServerSideSortingRequestControl; - t.ok(SSSRControl); - getControl = require('../../lib').getControl; - t.ok(getControl); - t.end(); -}); +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { getControl, ServerSideSortingRequestControl: SSSRControl } = require('../../lib'); test('new no args', function (t) { t.ok(new SSSRControl()); @@ -25,7 +10,7 @@ test('new no args', function (t) { }); test('new with args', function (t) { - var c = new SSSRControl({ + const c = new SSSRControl({ criticality: true, value: { attributeType: 'sn' @@ -41,7 +26,7 @@ test('new with args', function (t) { }); test('toBer - object', function (t) { - var sssc = new SSSRControl({ + const sssc = new SSSRControl({ criticality: true, value: { attributeType: 'sn', @@ -49,10 +34,10 @@ test('toBer - object', function (t) { reverseOrder: true }}); - var ber = new BerWriter(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '1.2.840.113556.1.4.473'); t.ok(c.criticality); @@ -64,7 +49,7 @@ test('toBer - object', function (t) { }); test('toBer - array', function (t) { - var sssc = new SSSRControl({ + const sssc = new SSSRControl({ criticality: true, value: [ { @@ -79,10 +64,10 @@ test('toBer - array', function (t) { ] }); - var ber = new BerWriter(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '1.2.840.113556.1.4.473'); t.ok(c.criticality); @@ -97,11 +82,11 @@ test('toBer - array', function (t) { }); test('toBer - empty', function (t) { - var sssc = new SSSRControl(); - var ber = new BerWriter(); + const sssc = new SSSRControl(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '1.2.840.113556.1.4.473'); t.equal(c.value.length, 0); diff --git a/test/controls/server_side_sorting_control_response.test.js b/test/controls/server_side_sorting_control_response.test.js index 432edb7..f9e6926 100644 --- a/test/controls/server_side_sorting_control_response.test.js +++ b/test/controls/server_side_sorting_control_response.test.js @@ -1,28 +1,13 @@ -var test = require('tap').test; +'use strict'; -var asn1 = require('asn1'); - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var ldap; -var getControl; -var SSSResponseControl; -var OID = '1.2.840.113556.1.4.474'; - -///--- Tests - - -test('load library', function (t) { - ldap = require('../../lib'); - SSSResponseControl = ldap.ServerSideSortingResponseControl; - t.ok(SSSResponseControl); - getControl = ldap.getControl; - t.ok(getControl); - t.end(); -}); +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const ldap = require('../../lib'); +const { getControl, ServerSideSortingResponseControl: SSSResponseControl } = ldap +const OID = '1.2.840.113556.1.4.474'; test('new no args', function (t) { - var c = new SSSResponseControl(); + const c = new SSSResponseControl(); t.ok(c); t.equal(c.type, OID); t.equal(c.criticality, false); @@ -30,7 +15,7 @@ test('new no args', function (t) { }); test('new with args', function (t) { - var c = new SSSResponseControl({ + const c = new SSSResponseControl({ criticality: true, value: { result: ldap.LDAP_SUCCESS, @@ -47,16 +32,16 @@ test('new with args', function (t) { }); test('toBer - success', function (t) { - var sssc = new SSSResponseControl({ + const sssc = new SSSResponseControl({ value: { result: ldap.LDAP_SUCCESS, failedAttribute: 'foobar' }}); - var ber = new BerWriter(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, '1.2.840.113556.1.4.474'); t.equal(c.criticality, false); @@ -66,15 +51,15 @@ test('toBer - success', function (t) { }); test('toBer - simple failure', function (t) { - var sssc = new SSSResponseControl({ + const sssc = new SSSResponseControl({ value: { result: ldap.LDAP_NO_SUCH_ATTRIBUTE }}); - var ber = new BerWriter(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, OID); t.equal(c.criticality, false); @@ -84,16 +69,16 @@ test('toBer - simple failure', function (t) { }); test('toBer - detailed failure', function (t) { - var sssc = new SSSResponseControl({ + const sssc = new SSSResponseControl({ value: { result: ldap.LDAP_NO_SUCH_ATTRIBUTE, failedAttribute: 'foobar' }}); - var ber = new BerWriter(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, OID); t.equal(c.criticality, false); @@ -103,11 +88,11 @@ test('toBer - detailed failure', function (t) { }); test('toBer - empty', function (t) { - var sssc = new SSSResponseControl(); - var ber = new BerWriter(); + const sssc = new SSSResponseControl(); + const ber = new BerWriter(); sssc.toBer(ber); - var c = getControl(new BerReader(ber.buffer)); + const c = getControl(new BerReader(ber.buffer)); t.ok(c); t.equal(c.type, OID); t.equal(c.criticality, false); diff --git a/test/dn.test.js b/test/dn.test.js index fc2f312..055928f 100644 --- a/test/dn.test.js +++ b/test/dn.test.js @@ -1,25 +1,11 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - - -///--- Globals - -var dn; - - -///--- Tests - -test('load library', function (t) { - dn = require('../lib/index').dn; - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { dn } = require('../lib'); test('parse basic', function (t) { - var DN_STR = 'cn=mark, ou=people, o=joyent'; - var name = dn.parse(DN_STR); + const DN_STR = 'cn=mark, ou=people, o=joyent'; + const name = dn.parse(DN_STR); t.ok(name); t.ok(name.rdns); t.ok(Array.isArray(name.rdns)); @@ -31,10 +17,9 @@ test('parse basic', function (t) { t.end(); }); - test('parse escaped', function (t) { - var DN_STR = 'cn=m\\,ark, ou=people, o=joyent'; - var name = dn.parse(DN_STR); + const DN_STR = 'cn=m\\,ark, ou=people, o=joyent'; + const name = dn.parse(DN_STR); t.ok(name); t.ok(name.rdns); t.ok(Array.isArray(name.rdns)); @@ -46,10 +31,9 @@ test('parse escaped', function (t) { t.end(); }); - test('parse compound', function (t) { - var DN_STR = 'cn=mark+sn=cavage, ou=people, o=joyent'; - var name = dn.parse(DN_STR); + const DN_STR = 'cn=mark+sn=cavage, ou=people, o=joyent'; + const name = dn.parse(DN_STR); t.ok(name); t.ok(name.rdns); t.ok(Array.isArray(name.rdns)); @@ -61,11 +45,10 @@ test('parse compound', function (t) { t.end(); }); - test('parse quoted', function (t) { - var DN_STR = 'cn="mark+sn=cavage", ou=people, o=joyent'; - var ESCAPE_STR = 'cn=mark\\+sn\\=cavage, ou=people, o=joyent'; - var name = dn.parse(DN_STR); + const DN_STR = 'cn="mark+sn=cavage", ou=people, o=joyent'; + const ESCAPE_STR = 'cn=mark\\+sn\\=cavage, ou=people, o=joyent'; + const name = dn.parse(DN_STR); t.ok(name); t.ok(name.rdns); t.ok(Array.isArray(name.rdns)); @@ -77,9 +60,8 @@ test('parse quoted', function (t) { t.end(); }); - test('equals', function (t) { - var dn1 = dn.parse('cn=foo,dc=bar'); + const dn1 = dn.parse('cn=foo,dc=bar'); t.ok(dn1.equals('cn=foo,dc=bar')); t.ok(!dn1.equals('cn=foo1,dc=bar')); t.ok(dn1.equals(dn.parse('cn=foo,dc=bar'))); @@ -87,9 +69,8 @@ test('equals', function (t) { t.end(); }); - test('child of', function (t) { - var dn1 = dn.parse('cn=foo,dc=bar'); + const dn1 = dn.parse('cn=foo,dc=bar'); t.ok(dn1.childOf('dc=bar')); t.ok(!dn1.childOf('dc=moo')); t.ok(!dn1.childOf('dc=foo')); @@ -99,9 +80,8 @@ test('child of', function (t) { t.end(); }); - test('parent of', function (t) { - var dn1 = dn.parse('cn=foo,dc=bar'); + const dn1 = dn.parse('cn=foo,dc=bar'); t.ok(dn1.parentOf('cn=moo,cn=foo,dc=bar')); t.ok(!dn1.parentOf('cn=moo,cn=bar,dc=foo')); t.ok(!dn1.parentOf('cn=foo,dc=bar')); @@ -110,21 +90,19 @@ test('parent of', function (t) { t.end(); }); - test('DN parent', function (t) { - var _dn = dn.parse('cn=foo,ou=bar'); - var parent1 = _dn.parent(); - var parent2 = parent1.parent(); + const _dn = dn.parse('cn=foo,ou=bar'); + const parent1 = _dn.parent(); + const parent2 = parent1.parent(); t.ok(parent1.equals('ou=bar')); t.ok(parent2.equals('')); t.equal(parent2.parent(), null); t.end(); }); - test('empty DNs', function (t) { - var _dn = dn.parse(''); - var _dn2 = dn.parse('cn=foo'); + const _dn = dn.parse(''); + const _dn2 = dn.parse('cn=foo'); t.ok(_dn.isEmpty()); t.notOk(_dn2.isEmpty()); t.notOk(_dn.equals('cn=foo')); @@ -136,22 +114,20 @@ test('empty DNs', function (t) { t.end(); }); - test('case insensitive attribute names', function (t) { - var dn1 = dn.parse('CN=foo,dc=bar'); + const dn1 = dn.parse('CN=foo,dc=bar'); t.ok(dn1.equals('cn=foo,dc=bar')); t.ok(dn1.equals(dn.parse('cn=foo,DC=bar'))); t.end(); }); - test('format', function (t) { - var DN_ORDER = dn.parse('sn=bar+cn=foo,ou=test'); - var DN_QUOTE = dn.parse('cn="foo",ou=test'); - var DN_QUOTE2 = dn.parse('cn=" foo",ou=test'); - var DN_SPACE = dn.parse('cn=foo,ou=test'); - var DN_SPACE2 = dn.parse('cn=foo ,ou=test'); - var DN_CASE = dn.parse('CN=foo,Ou=test'); + const DN_ORDER = dn.parse('sn=bar+cn=foo,ou=test'); + const DN_QUOTE = dn.parse('cn="foo",ou=test'); + const DN_QUOTE2 = dn.parse('cn=" foo",ou=test'); + const DN_SPACE = dn.parse('cn=foo,ou=test'); + const DN_SPACE2 = dn.parse('cn=foo ,ou=test'); + const DN_CASE = dn.parse('CN=foo,Ou=test'); t.equal(DN_ORDER.format({keepOrder: false}), 'cn=foo+sn=bar, ou=test'); t.equal(DN_ORDER.format({keepOrder: true}), 'sn=bar+cn=foo, ou=test'); @@ -174,9 +150,8 @@ test('format', function (t) { t.end(); }); - test('set format', function (t) { - var _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com'); + const _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com'); t.equal(_dn.toString(), 'uid=user, cn=foo+sn=bar, dc=test, dc=com'); _dn.setFormat({keepOrder: true}); t.equal(_dn.toString(), 'uid=user, sn=bar+cn=foo, dc=test, dc=com'); @@ -191,29 +166,27 @@ test('set format', function (t) { t.end(); }); - test('format persists across clone', function (t) { - var _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com'); - var OUT = 'UID="user", CN=foo+SN=bar, DC=test, DC=com'; + const _dn = dn.parse('uid="user", sn=bar+cn=foo, dc=test , DC=com'); + const OUT = 'UID="user", CN=foo+SN=bar, DC=test, DC=com'; _dn.setFormat({keepQuote: true, upperName: true}); - var clone = _dn.clone(); + const clone = _dn.clone(); t.equals(_dn.toString(), OUT); t.equals(clone.toString(), OUT); t.end(); }); - test('initialization', function (t) { - var dn1 = new dn.DN(); + const dn1 = new dn.DN(); t.ok(dn1); t.equals(dn1.toString(), ''); t.ok(dn1.isEmpty(), 'DN with no initializer defaults to null DN'); - var data = [ + const data = [ new dn.RDN({ foo: 'bar' }), new dn.RDN({ o: 'base' }) ]; - var dn2 = new dn.DN(data); + const dn2 = new dn.DN(data); t.ok(dn2); t.equals(dn2.toString(), 'foo=bar, o=base'); t.ok(!dn2.isEmpty()); @@ -221,16 +194,15 @@ test('initialization', function (t) { t.end(); }); - test('array functions', function (t) { - var dn1 = dn.parse('a=foo, b=bar, c=baz'); + const dn1 = dn.parse('a=foo, b=bar, c=baz'); t.ok(dn1); t.equal(dn1.toString(), 'a=foo, b=bar, c=baz'); t.ok(dn1.reverse()); t.equal(dn1.toString(), 'c=baz, b=bar, a=foo'); - var rdn = dn1.pop(); + let rdn = dn1.pop(); t.ok(rdn); t.equal(dn1.toString(), 'c=baz, b=bar'); @@ -247,14 +219,13 @@ test('array functions', function (t) { t.end(); }); - test('isDN duck-testing', function (t) { - var valid = dn.parse('cn=foo'); - var isDN = dn.DN.isDN; + const valid = dn.parse('cn=foo'); + const isDN = dn.DN.isDN; t.notOk(isDN(null)); t.notOk(isDN('cn=foo')); t.ok(isDN(valid)); - var duck = { + const duck = { rdns: [ {look: 'ma'}, {a: 'dn'} ], toString: function () { return 'look=ma, a=dn'; } }; diff --git a/test/errors.test.js b/test/errors.test.js index 2632166..637ae79 100644 --- a/test/errors.test.js +++ b/test/errors.test.js @@ -1,36 +1,32 @@ -// Copyright 2015 Joyent, Inc. +'use strict'; -var test = require('tap').test; - -var ldap = require('../lib/index'); - - -///--- Tests +const { test } = require('tap'); +const { LDAPError, ConnectionError, AbandonedError, TimeoutError, LDAP_OTHER } = require('../lib'); test('basic error', function (t) { - var msg = 'mymsg'; - var err = new ldap.LDAPError(msg, null, null); + const msg = 'mymsg'; + const err = new LDAPError(msg, null, null); t.ok(err); t.equal(err.name, 'LDAPError'); - t.equal(err.code, ldap.LDAP_OTHER); + t.equal(err.code, LDAP_OTHER); t.equal(err.dn, ''); t.equal(err.message, msg); t.end(); }); test('"custom" errors', function (t) { - var errors = [ - { name: 'ConnectionError', func: ldap.ConnectionError }, - { name: 'AbandonedError', func: ldap.AbandonedError }, - { name: 'TimeoutError', func: ldap.TimeoutError } + const errors = [ + { name: 'ConnectionError', func: ConnectionError }, + { name: 'AbandonedError', func: AbandonedError }, + { name: 'TimeoutError', func: TimeoutError } ]; errors.forEach(function (entry) { - var msg = entry.name + 'msg'; - var err = new entry.func(msg); + const msg = entry.name + 'msg'; + const err = new entry.func(msg); t.ok(err); t.equal(err.name, entry.name); - t.equal(err.code, ldap.LDAP_OTHER); + t.equal(err.code, LDAP_OTHER); t.equal(err.dn, ''); t.equal(err.message, msg); }); diff --git a/test/filters/and.test.js b/test/filters/and.test.js index c7b22c3..7fd9235 100644 --- a/test/filters/and.test.js +++ b/test/filters/and.test.js @@ -1,28 +1,7 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. +'use strict'; -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var EqualityFilter; -var AndFilter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - EqualityFilter = filters.EqualityFilter; - AndFilter = filters.AndFilter; - t.ok(EqualityFilter); - t.ok(AndFilter); - t.end(); -}); +const { test } = require('tap'); +const { filters: { EqualityFilter, AndFilter } } = require('../../lib'); test('Construct no args', function (t) { @@ -30,9 +9,8 @@ test('Construct no args', function (t) { t.end(); }); - test('Construct args', function (t) { - var f = new AndFilter(); + const f = new AndFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -46,9 +24,8 @@ test('Construct args', function (t) { t.end(); }); - test('match true', function (t) { - var f = new AndFilter(); + const f = new AndFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -62,9 +39,8 @@ test('match true', function (t) { t.end(); }); - test('match false', function (t) { - var f = new AndFilter(); + const f = new AndFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' diff --git a/test/filters/approx.test.js b/test/filters/approx.test.js index 44b0ed3..a35144a 100644 --- a/test/filters/approx.test.js +++ b/test/filters/approx.test.js @@ -1,40 +1,19 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var ApproximateFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - ApproximateFilter = filters.ApproximateFilter; - t.ok(ApproximateFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { ApproximateFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new ApproximateFilter(); + const f = new ApproximateFilter(); t.ok(f); t.ok(!f.attribute); t.ok(!f.value); t.end(); }); - test('Construct args', function (t) { - var f = new ApproximateFilter({ + const f = new ApproximateFilter({ attribute: 'foo', value: 'bar' }); @@ -45,9 +24,8 @@ test('Construct args', function (t) { t.end(); }); - test('GH-109 = escape value only in toString()', function (t) { - var f = new ApproximateFilter({ + const f = new ApproximateFilter({ attribute: 'foo', value: 'ba(r)' }); @@ -58,25 +36,23 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeString('bar'); - var f = new ApproximateFilter(); + const f = new ApproximateFilter(); t.ok(f); t.ok(f.parse(new BerReader(writer.buffer))); t.end(); }); - test('parse bad', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeInt(20); - var f = new ApproximateFilter(); + const f = new ApproximateFilter(); t.ok(f); try { f.parse(new BerReader(writer.buffer)); @@ -87,20 +63,19 @@ test('parse bad', function (t) { t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new ApproximateFilter({ + let f = new ApproximateFilter({ attribute: 'foo', value: 'ba(r)' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new ApproximateFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); diff --git a/test/filters/eq.test.js b/test/filters/eq.test.js index f6a51ad..9a09b30 100644 --- a/test/filters/eq.test.js +++ b/test/filters/eq.test.js @@ -1,40 +1,19 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var EqualityFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - EqualityFilter = filters.EqualityFilter; - t.ok(EqualityFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { EqualityFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new EqualityFilter(); + const f = new EqualityFilter(); t.ok(f); t.ok(!f.attribute); t.ok(!f.value); t.end(); }); - test('Construct args', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'foo', value: 'bar' }); @@ -45,9 +24,8 @@ test('Construct args', function (t) { t.end(); }); - test('GH-109 = escape value only in toString()', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'foo', value: 'ba(r)' }); @@ -58,9 +36,8 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('match true', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'foo', value: 'bar' }); @@ -69,9 +46,8 @@ test('match true', function (t) { t.end(); }); - test('match multiple', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'foo', value: 'bar' }); @@ -80,9 +56,8 @@ test('match multiple', function (t) { t.end(); }); - test('match false', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'foo', value: 'bar' }); @@ -91,13 +66,12 @@ test('match false', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeString('bar'); - var f = new EqualityFilter(); + const f = new EqualityFilter(); t.ok(f); t.ok(f.parse(new BerReader(writer.buffer))); t.ok(f.matches({ foo: 'bar' })); @@ -106,9 +80,8 @@ test('parse ok', function (t) { t.end(); }); - test('escape EqualityFilter inputs', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: '(|(foo', value: 'bar))(' }); @@ -119,13 +92,12 @@ test('escape EqualityFilter inputs', function (t) { t.end(); }); - test('parse bad', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeInt(20); - var f = new EqualityFilter(); + const f = new EqualityFilter(); t.ok(f); try { f.parse(new BerReader(writer.buffer)); @@ -136,20 +108,19 @@ test('parse bad', function (t) { t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new EqualityFilter({ + let f = new EqualityFilter({ attribute: 'foo', value: 'ba(r)' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new EqualityFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); @@ -158,34 +129,32 @@ test('GH-109 = to ber uses plain values', function (t) { t.end(); }); - test('handle values passed via buffer', function (t) { - var b = new Buffer([32, 64, 128, 254]); - var f = new EqualityFilter({ + const b = Buffer.from([32, 64, 128, 254]); + const f = new EqualityFilter({ attribute: 'foo', value: b }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); - var f2 = new EqualityFilter(); + const f2 = new EqualityFilter(); t.ok(f2.parse(reader)); t.equal(f2.value, b.toString()); t.equal(f2.raw.length, b.length); - for (var i = 0; i < b.length; i++) { + for (let i = 0; i < b.length; i++) { t.equal(f2.raw[i], b[i]); } t.end(); }); - test('GH-277 objectClass should be case-insensitive', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'objectClass', value: 'CaseInsensitiveObj' }); diff --git a/test/filters/ext.test.js b/test/filters/ext.test.js index a84f1fd..78f27c7 100644 --- a/test/filters/ext.test.js +++ b/test/filters/ext.test.js @@ -1,38 +1,16 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var ExtensibleFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var filters; - - -///--- Tests - -test('load library', function (t) { - filters = require('../../lib/index').filters; - t.ok(filters); - ExtensibleFilter = filters.ExtensibleFilter; - t.ok(ExtensibleFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { filters: { parseString, ExtensibleFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new ExtensibleFilter(); + const f = new ExtensibleFilter(); t.ok(f); t.end(); }); - test('Construct args', function (t) { - var f = new ExtensibleFilter({ + const f = new ExtensibleFilter({ matchType: 'foo', value: 'bar' }); @@ -43,9 +21,8 @@ test('Construct args', function (t) { t.end(); }); - test('parse RFC example 1', function (t) { - var f = filters.parseString('(cn:caseExactMatch:=Fred Flintstone)'); + const f = parseString('(cn:caseExactMatch:=Fred Flintstone)'); t.ok(f); t.equal(f.matchType, 'cn'); t.equal(f.matchingRule, 'caseExactMatch'); @@ -54,9 +31,8 @@ test('parse RFC example 1', function (t) { t.end(); }); - test('parse RFC example 2', function (t) { - var f = filters.parseString('(cn:=Betty Rubble)'); + const f = parseString('(cn:=Betty Rubble)'); t.ok(f); t.equal(f.matchType, 'cn'); t.equal(f.matchValue, 'Betty Rubble'); @@ -65,9 +41,8 @@ test('parse RFC example 2', function (t) { t.end(); }); - test('parse RFC example 3', function (t) { - var f = filters.parseString('(sn:dn:2.4.6.8.10:=Barney Rubble)'); + const f = parseString('(sn:dn:2.4.6.8.10:=Barney Rubble)'); t.ok(f); t.equal(f.matchType, 'sn'); t.equal(f.matchingRule, '2.4.6.8.10'); @@ -76,9 +51,8 @@ test('parse RFC example 3', function (t) { t.end(); }); - test('parse RFC example 3', function (t) { - var f = filters.parseString('(o:dn:=Ace Industry)'); + const f = parseString('(o:dn:=Ace Industry)'); t.ok(f); t.equal(f.matchType, 'o'); t.notOk(f.matchingRule); @@ -87,9 +61,8 @@ test('parse RFC example 3', function (t) { t.end(); }); - test('parse RFC example 4', function (t) { - var f = filters.parseString('(:1.2.3:=Wilma Flintstone)'); + const f = parseString('(:1.2.3:=Wilma Flintstone)'); t.ok(f); t.notOk(f.matchType); t.equal(f.matchingRule, '1.2.3'); @@ -98,9 +71,8 @@ test('parse RFC example 4', function (t) { t.end(); }); - test('parse RFC example 5', function (t) { - var f = filters.parseString('(:DN:2.4.6.8.10:=Dino)'); + const f = parseString('(:DN:2.4.6.8.10:=Dino)'); t.ok(f); t.notOk(f.matchType); t.equal(f.matchingRule, '2.4.6.8.10'); diff --git a/test/filters/ge.test.js b/test/filters/ge.test.js index e67780e..44e0738 100644 --- a/test/filters/ge.test.js +++ b/test/filters/ge.test.js @@ -1,40 +1,19 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var GreaterThanEqualsFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - GreaterThanEqualsFilter = filters.GreaterThanEqualsFilter; - t.ok(GreaterThanEqualsFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { GreaterThanEqualsFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new GreaterThanEqualsFilter(); + const f = new GreaterThanEqualsFilter(); t.ok(f); t.ok(!f.attribute); t.ok(!f.value); t.end(); }); - test('Construct args', function (t) { - var f = new GreaterThanEqualsFilter({ + const f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -45,9 +24,8 @@ test('Construct args', function (t) { t.end(); }); - test('GH-109 = escape value only in toString()', function (t) { - var f = new GreaterThanEqualsFilter({ + const f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'ba(r)' }); @@ -58,9 +36,8 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('match true', function (t) { - var f = new GreaterThanEqualsFilter({ + const f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -69,9 +46,8 @@ test('match true', function (t) { t.end(); }); - test('match multiple', function (t) { - var f = new GreaterThanEqualsFilter({ + const f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -81,7 +57,7 @@ test('match multiple', function (t) { }); test('match false', function (t) { - var f = new GreaterThanEqualsFilter({ + const f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -90,26 +66,24 @@ test('match false', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeString('bar'); - var f = new GreaterThanEqualsFilter(); + const f = new GreaterThanEqualsFilter(); t.ok(f); t.ok(f.parse(new BerReader(writer.buffer))); t.ok(f.matches({ foo: 'bar' })); t.end(); }); - test('parse bad', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeInt(20); - var f = new GreaterThanEqualsFilter(); + const f = new GreaterThanEqualsFilter(); t.ok(f); try { f.parse(new BerReader(writer.buffer)); @@ -120,20 +94,19 @@ test('parse bad', function (t) { t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new GreaterThanEqualsFilter({ + let f = new GreaterThanEqualsFilter({ attribute: 'foo', value: 'ba(r)' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new GreaterThanEqualsFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); diff --git a/test/filters/le.test.js b/test/filters/le.test.js index d4c41d7..b97f2bc 100644 --- a/test/filters/le.test.js +++ b/test/filters/le.test.js @@ -1,40 +1,19 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var LessThanEqualsFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - LessThanEqualsFilter = filters.LessThanEqualsFilter; - t.ok(LessThanEqualsFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { LessThanEqualsFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new LessThanEqualsFilter(); + const f = new LessThanEqualsFilter(); t.ok(f); t.ok(!f.attribute); t.ok(!f.value); t.end(); }); - test('Construct args', function (t) { - var f = new LessThanEqualsFilter({ + const f = new LessThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -45,9 +24,8 @@ test('Construct args', function (t) { t.end(); }); - test('GH-109 = escape value only in toString()', function (t) { - var f = new LessThanEqualsFilter({ + const f = new LessThanEqualsFilter({ attribute: 'foo', value: 'ba(r)' }); @@ -58,9 +36,8 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('match true', function (t) { - var f = new LessThanEqualsFilter({ + const f = new LessThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -69,9 +46,8 @@ test('match true', function (t) { t.end(); }); - test('match multiple', function (t) { - var f = new LessThanEqualsFilter({ + const f = new LessThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -80,9 +56,8 @@ test('match multiple', function (t) { t.end(); }); - test('match false', function (t) { - var f = new LessThanEqualsFilter({ + const f = new LessThanEqualsFilter({ attribute: 'foo', value: 'bar' }); @@ -91,26 +66,24 @@ test('match false', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeString('bar'); - var f = new LessThanEqualsFilter(); + const f = new LessThanEqualsFilter(); t.ok(f); t.ok(f.parse(new BerReader(writer.buffer))); t.ok(f.matches({ foo: 'bar' })); t.end(); }); - test('parse bad', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeInt(20); - var f = new LessThanEqualsFilter(); + const f = new LessThanEqualsFilter(); t.ok(f); try { f.parse(new BerReader(writer.buffer)); @@ -121,20 +94,19 @@ test('parse bad', function (t) { t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new LessThanEqualsFilter({ + let f = new LessThanEqualsFilter({ attribute: 'foo', value: 'ba(r)' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new LessThanEqualsFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); diff --git a/test/filters/not.test.js b/test/filters/not.test.js index 5fc9566..f91dbc7 100644 --- a/test/filters/not.test.js +++ b/test/filters/not.test.js @@ -1,38 +1,15 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var EqualityFilter; -var NotFilter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - EqualityFilter = filters.EqualityFilter; - NotFilter = filters.NotFilter; - t.ok(EqualityFilter); - t.ok(NotFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { filters: { EqualityFilter, NotFilter } } = require('../../lib'); test('Construct no args', function (t) { t.ok(new NotFilter()); t.end(); }); - test('Construct args', function (t) { - var f = new NotFilter({ + const f = new NotFilter({ filter: new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -43,9 +20,8 @@ test('Construct args', function (t) { t.end(); }); - test('match true', function (t) { - var f = new NotFilter({ + const f = new NotFilter({ filter: new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -56,9 +32,8 @@ test('match true', function (t) { t.end(); }); - test('match false', function (t) { - var f = new NotFilter({ + const f = new NotFilter({ filter: new EqualityFilter({ attribute: 'foo', value: 'bar' diff --git a/test/filters/or.test.js b/test/filters/or.test.js index 8622f78..cb95826 100644 --- a/test/filters/or.test.js +++ b/test/filters/or.test.js @@ -1,38 +1,15 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var EqualityFilter; -var OrFilter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - EqualityFilter = filters.EqualityFilter; - OrFilter = filters.OrFilter; - t.ok(EqualityFilter); - t.ok(OrFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { filters: { EqualityFilter, OrFilter } } = require('../../lib'); test('Construct no args', function (t) { t.ok(new OrFilter()); t.end(); }); - test('Construct args', function (t) { - var f = new OrFilter(); + const f = new OrFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -46,9 +23,8 @@ test('Construct args', function (t) { t.end(); }); - test('match true', function (t) { - var f = new OrFilter(); + const f = new OrFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' @@ -62,9 +38,8 @@ test('match true', function (t) { t.end(); }); - test('match false', function (t) { - var f = new OrFilter(); + const f = new OrFilter(); f.addFilter(new EqualityFilter({ attribute: 'foo', value: 'bar' diff --git a/test/filters/parse.test.js b/test/filters/parse.test.js index f1acf1a..0f9cd08 100644 --- a/test/filters/parse.test.js +++ b/test/filters/parse.test.js @@ -1,14 +1,11 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var parse = require('../../lib/index').parseFilter; - +'use strict'; +const { test } = require('tap'); +const { parseFilter: parse } = require('../../lib'); test('GH-48 XML Strings in filter', function (t) { - var str = '(&(CentralUIEnrollments=\\*)(objectClass=User))'; - var f = parse(str); + const str = '(&(CentralUIEnrollments=\\*)(objectClass=User))'; + const f = parse(str); t.ok(f); t.ok(f.filters); t.equal(f.filters.length, 2); @@ -18,11 +15,10 @@ test('GH-48 XML Strings in filter', function (t) { t.end(); }); - test('GH-50 = in filter', function (t) { - var str = '(uniquemember=uuid=930896af-bf8c-48d4-885c-6573a94b1853, ' + + const str = '(uniquemember=uuid=930896af-bf8c-48d4-885c-6573a94b1853, ' + 'ou=users, o=smartdc)'; - var f = parse(str); + const f = parse(str); t.ok(f); t.equal(f.attribute, 'uniquemember'); t.equal(f.value, @@ -30,10 +26,9 @@ test('GH-50 = in filter', function (t) { t.end(); }); - test('( in filter', function (t) { - var str = '(foo=bar\\()'; - var f = parse(str); + const str = '(foo=bar\\()'; + const f = parse(str); t.ok(f); t.equal(f.attribute, 'foo'); t.equal(f.value, 'bar('); @@ -42,8 +37,8 @@ test('( in filter', function (t) { }); test(') in filter', function (t) { - var str = '(foo=bar\\))'; - var f = parse(str); + const str = '(foo=bar\\))'; + const f = parse(str); t.ok(f); t.equal(f.attribute, 'foo'); t.equal(f.value, 'bar)'); @@ -51,10 +46,9 @@ test(') in filter', function (t) { t.end(); }); - test('\\ in filter', function (t) { - var str = '(foo=bar\\\\)'; - var f = parse(str); + const str = '(foo=bar\\\\)'; + const f = parse(str); t.ok(f); t.equal(f.attribute, 'foo'); t.equal(f.value, 'bar\\'); @@ -62,10 +56,9 @@ test('\\ in filter', function (t) { t.end(); }); - test('* in equality filter', function (t) { - var str = '(foo=bar\\*)'; - var f = parse(str); + const str = '(foo=bar\\*)'; + const f = parse(str); t.ok(f); t.equal(f.attribute, 'foo'); t.equal(f.value, 'bar*'); @@ -73,10 +66,9 @@ test('* in equality filter', function (t) { t.end(); }); - test('* substr filter (prefix)', function (t) { - var str = '(foo=bar*)'; - var f = parse(str); + const str = '(foo=bar*)'; + const f = parse(str); t.ok(f); t.equal(f.attribute, 'foo'); t.equal(f.initial, 'bar'); @@ -84,10 +76,9 @@ test('* substr filter (prefix)', function (t) { t.end(); }); - test('GH-53 NotFilter', function (t) { - var str = '(&(objectClass=person)(!(objectClass=shadowAccount)))'; - var f = parse(str); + const str = '(&(objectClass=person)(!(objectClass=shadowAccount)))'; + const f = parse(str); t.ok(f); t.equal(f.type, 'and'); t.equal(f.filters.length, 2); @@ -99,9 +90,8 @@ test('GH-53 NotFilter', function (t) { t.end(); }); - test('presence filter', function (t) { - var f = parse('(foo=*)'); + const f = parse('(foo=*)'); t.ok(f); t.equal(f.type, 'present'); t.equal(f.attribute, 'foo'); @@ -109,7 +99,6 @@ test('presence filter', function (t) { t.end(); }); - test('bogus filter', function (t) { t.throws(function () { parse('foo>1'); @@ -117,7 +106,6 @@ test('bogus filter', function (t) { t.end(); }); - test('bogus filter !=', function (t) { t.throws(function () { parse('foo!=1'); @@ -125,7 +113,6 @@ test('bogus filter !=', function (t) { t.end(); }); - test('mismatched parens', function (t) { t.throws(function () { parse('(&(foo=bar)(!(state=done))'); diff --git a/test/filters/presence.test.js b/test/filters/presence.test.js index 3ed6f99..9d7ce64 100644 --- a/test/filters/presence.test.js +++ b/test/filters/presence.test.js @@ -1,39 +1,18 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var PresenceFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - PresenceFilter = filters.PresenceFilter; - t.ok(PresenceFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { PresenceFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new PresenceFilter(); + const f = new PresenceFilter(); t.ok(f); t.ok(!f.attribute); t.end(); }); - test('Construct args', function (t) { - var f = new PresenceFilter({ + const f = new PresenceFilter({ attribute: 'foo' }); t.ok(f); @@ -43,7 +22,7 @@ test('Construct args', function (t) { }); test('GH-109 = escape value only in toString()', function (t) { - var f = new PresenceFilter({ + const f = new PresenceFilter({ attribute: 'fo)o' }); t.ok(f); @@ -52,9 +31,8 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('match true', function (t) { - var f = new PresenceFilter({ + const f = new PresenceFilter({ attribute: 'foo' }); t.ok(f); @@ -62,9 +40,8 @@ test('match true', function (t) { t.end(); }); - test('match false', function (t) { - var f = new PresenceFilter({ + const f = new PresenceFilter({ attribute: 'foo' }); t.ok(f); @@ -72,34 +49,32 @@ test('match false', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo', 0x87); - var f = new PresenceFilter(); + const f = new PresenceFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); t.ok(f.matches({ foo: 'bar' })); t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new PresenceFilter({ + let f = new PresenceFilter({ attribute: 'f(o)o' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new PresenceFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); diff --git a/test/filters/substr.test.js b/test/filters/substr.test.js index e8bc9f8..bb62b88 100644 --- a/test/filters/substr.test.js +++ b/test/filters/substr.test.js @@ -1,40 +1,19 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - - -///--- Globals - -var SubstringFilter; -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; - - - -///--- Tests - -test('load library', function (t) { - var filters = require('../../lib/index').filters; - t.ok(filters); - SubstringFilter = filters.SubstringFilter; - t.ok(SubstringFilter); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { filters: { SubstringFilter } } = require('../../lib'); test('Construct no args', function (t) { - var f = new SubstringFilter(); + const f = new SubstringFilter(); t.ok(f); t.ok(!f.attribute); t.ok(!f.value); t.end(); }); - test('Construct args', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'foo', initial: 'bar', any: ['zig', 'zag'], @@ -51,9 +30,8 @@ test('Construct args', function (t) { t.end(); }); - test('GH-109 = escape value only in toString()', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'fo(o', initial: 'ba(r)', any: ['zi)g', 'z(ag'], @@ -70,9 +48,8 @@ test('GH-109 = escape value only in toString()', function (t) { t.end(); }); - test('match true', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'foo', initial: 'bar', any: ['zig', 'zag'], @@ -83,9 +60,8 @@ test('match true', function (t) { t.end(); }); - test('match false', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'foo', initial: 'bar', foo: ['zig', 'zag'], @@ -96,9 +72,8 @@ test('match false', function (t) { t.end(); }); - test('match any', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'foo', initial: 'bar' }); @@ -107,9 +82,8 @@ test('match any', function (t) { t.end(); }); - test('GH-109 = escape for regex in matches', function (t) { - var f = new SubstringFilter({ + const f = new SubstringFilter({ attribute: 'fo(o', initial: 'ba(r)', any: ['zi)g', 'z(ag'], @@ -120,29 +94,27 @@ test('GH-109 = escape for regex in matches', function (t) { t.end(); }); - test('parse ok', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.startSequence(); writer.writeString('bar', 0x80); writer.writeString('bad', 0x81); writer.writeString('baz', 0x82); writer.endSequence(); - var f = new SubstringFilter(); + const f = new SubstringFilter(); t.ok(f); t.ok(f.parse(new BerReader(writer.buffer))); t.ok(f.matches({ foo: 'bargoobadgoobaz' })); t.end(); }); - test('parse bad', function (t) { - var writer = new BerWriter(); + const writer = new BerWriter(); writer.writeString('foo'); writer.writeInt(20); - var f = new SubstringFilter(); + const f = new SubstringFilter(); t.ok(f); try { f.parse(new BerReader(writer.buffer)); @@ -152,22 +124,21 @@ test('parse bad', function (t) { t.end(); }); - test('GH-109 = to ber uses plain values', function (t) { - var f = new SubstringFilter({ + let f = new SubstringFilter({ attribute: 'fo(o', initial: 'ba(r)', any: ['zi)g', 'z(ag'], 'final': '(baz)' }); t.ok(f); - var writer = new BerWriter(); + const writer = new BerWriter(); f.toBer(writer); f = new SubstringFilter(); t.ok(f); - var reader = new BerReader(writer.buffer); + const reader = new BerReader(writer.buffer); reader.readSequence(); t.ok(f.parse(reader)); diff --git a/test/laundry.test.js b/test/laundry.test.js index 69c78b6..fb9b3e2 100644 --- a/test/laundry.test.js +++ b/test/laundry.test.js @@ -1,64 +1,43 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. +'use strict'; -var test = require('tap').test; -var uuid = require('uuid'); - -var ldap = require('../lib/index'); - - -///--- Globals - -var SOCKET = process.platform === 'win32' ? '\\\\.\\pipe\\' + uuid() : '/tmp/.' + uuid(); -var SUFFIX = 'dc=' + uuid(); - -var client; -var server; - - - -///--- Helper +const tap = require('tap'); +const uuid = require('uuid') +const { getSock } = require('./utils') +const ldap = require('../lib'); function search(t, options, callback) { - client.search(SUFFIX, options, function (err, res) { - t.ifError(err); + t.context.client.search(t.context.suffix, options, function (err, res) { + t.error(err); t.ok(res); - var found = false; + let found = false; res.on('searchEntry', function (entry) { t.ok(entry); found = true; }); res.on('end', function () { - t.ok(found); - if (callback) - return callback(); - + t.true(found); + if (callback) return callback(); return t.end(); }); }); } +tap.beforeEach((done, t) => { + const suffix = `dc=${uuid()}`; + const server = ldap.createServer(); - -///--- Tests - -test('setup', function (t) { - server = ldap.createServer(); - t.ok(server); - server.listen(SOCKET, function () { - client = ldap.createClient({ - socketPath: SOCKET - }); - t.ok(client); - t.end(); - }); + t.context.server = server; + t.context.socketPath = getSock(); + t.context.suffix = suffix; server.bind('cn=root', function (req, res, next) { res.end(); return next(); }); - server.search(SUFFIX, function (req, res, next) { + + server.search(suffix, function (req, res, next) { var entry = { - dn: 'cn=foo, ' + SUFFIX, + dn: 'cn=foo, ' + suffix, attributes: { objectclass: ['person', 'top'], cn: 'Pogo Stick', @@ -73,12 +52,32 @@ test('setup', function (t) { res.end(); }); -}); + server.listen(t.context.socketPath, function () { + t.context.client = ldap.createClient({ + socketPath: t.context.socketPath + }); -test('Evolution search filter (GH-3)', function (t) { + t.context.client.on('connectError', (err) => { + t.context.server.close(() => done(err)); + }) + t.context.client.on('connect', (socket) => { + t.context.socket = socket; + done(); + }) + }); +}) + +tap.afterEach((done, t) => { + if (!t.context.client) return done() + t.context.client.unbind(() => { + t.context.server.close(done) + }) +}) + +tap.test('Evolution search filter (GH-3)', function (t) { // This is what Evolution sends, when searching for a contact 'ogo'. Wow. - var filter = + const filter = '(|(cn=ogo*)(givenname=ogo*)(sn=ogo*)(mail=ogo*)(member=ogo*)' + '(primaryphone=ogo*)(telephonenumber=ogo*)(homephone=ogo*)(mobile=ogo*)' + '(carphone=ogo*)(facsimiletelephonenumber=ogo*)' + @@ -99,9 +98,8 @@ test('Evolution search filter (GH-3)', function (t) { return search(t, filter); }); - -test('GH-49 Client errors on bad attributes', function (t) { - var searchOpts = { +tap.test('GH-49 Client errors on bad attributes', function (t) { + const searchOpts = { filter: 'cn=*ogo*', scope: 'one', attributes: 'dn' @@ -109,17 +107,16 @@ test('GH-49 Client errors on bad attributes', function (t) { return search(t, searchOpts); }); - -test('GH-55 Client emits connect multiple times', function (t) { - var c = ldap.createClient({ - socketPath: SOCKET +tap.test('GH-55 Client emits connect multiple times', function (t) { + const c = ldap.createClient({ + socketPath: t.context.socketPath }); - var count = 0; + let count = 0; c.on('connect', function (socket) { t.ok(socket); count++; - c.bind('cn=root', 'secret', function (err, res) { + c.bind('cn=root', 'secret', function (err) { t.ifError(err); c.unbind(function () { t.equal(count, 1); @@ -128,13 +125,3 @@ test('GH-55 Client emits connect multiple times', function (t) { }); }); }); - - -test('shutdown', function (t) { - client.unbind(function () { - server.on('close', function () { - t.end(); - }); - server.close(); - }); -}); diff --git a/test/messages/add_request.test.js b/test/messages/add_request.test.js index 37043f9..1def8e4 100644 --- a/test/messages/add_request.test.js +++ b/test/messages/add_request.test.js @@ -1,39 +1,16 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. +'use strict'; -var test = require('tap').test; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { AddRequest, Attribute, dn } = require('../../lib'); -var asn1 = require('asn1'); - - -///--- Globals - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var AddRequest; -var Attribute; -var dn; - -///--- Tests - -test('load library', function (t) { - AddRequest = require('../../lib/index').AddRequest; - Attribute = require('../../lib/index').Attribute; - dn = require('../../lib/index').dn; - t.ok(AddRequest); - t.ok(Attribute); - t.ok(dn); - t.end(); -}); - - -test('new no args', function (t) { +test('new no args', t => { t.ok(new AddRequest()); t.end(); }); - -test('new with args', function (t) { - var req = new AddRequest({ +test('new with args', t => { + const req = new AddRequest({ entry: dn.parse('cn=foo, o=test'), attributes: [new Attribute({type: 'cn', vals: ['foo']}), new Attribute({type: 'objectclass', vals: ['person']})] @@ -48,9 +25,8 @@ test('new with args', function (t) { t.end(); }); - -test('parse', function (t) { - var ber = new BerWriter(); +test('parse', t => { + const ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.startSequence(); @@ -71,7 +47,7 @@ test('parse', function (t) { ber.endSequence(); - var req = new AddRequest(); + const req = new AddRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.attributes.length, 2); @@ -82,9 +58,8 @@ test('parse', function (t) { t.end(); }); - -test('toBer', function (t) { - var req = new AddRequest({ +test('toBer', t => { + const req = new AddRequest({ messageID: 123, entry: dn.parse('cn=foo, o=test'), attributes: [new Attribute({type: 'cn', vals: ['foo']}), @@ -93,7 +68,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); @@ -114,9 +89,8 @@ test('toBer', function (t) { t.end(); }); - -test('toObject', function (t) { - var req = new AddRequest({ +test('toObject', t => { + const req = new AddRequest({ entry: dn.parse('cn=foo, o=test'), attributes: [new Attribute({type: 'cn', vals: ['foo', 'bar']}), new Attribute({type: 'objectclass', vals: ['person']})] @@ -124,7 +98,7 @@ test('toObject', function (t) { t.ok(req); - var obj = req.toObject(); + const obj = req.toObject(); t.ok(obj); t.ok(obj.dn); diff --git a/test/messages/add_response.test.js b/test/messages/add_response.test.js index 4b362e1..5727aad 100644 --- a/test/messages/add_response.test.js +++ b/test/messages/add_response.test.js @@ -1,34 +1,16 @@ -// 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 AddResponse; - - -///--- Tests - -test('load library', function (t) { - AddResponse = require('../../lib/index').AddResponse; - t.ok(AddResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { AddResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new AddResponse()); t.end(); }); - test('new with args', function (t) { - var res = new AddResponse({ + const res = new AddResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new AddResponse(); + const res = new AddResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new AddResponse({ + const res = new AddResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/bind_request.test.js b/test/messages/bind_request.test.js index 02522c6..9cc2926 100644 --- a/test/messages/bind_request.test.js +++ b/test/messages/bind_request.test.js @@ -1,36 +1,16 @@ -// 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 BindRequest; -var dn; - -///--- Tests - -test('load library', function (t) { - BindRequest = require('../../lib/index').BindRequest; - dn = require('../../lib/index').dn; - t.ok(BindRequest); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { BindRequest, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new BindRequest()); t.end(); }); - test('new with args', function (t) { - var req = new BindRequest({ + const req = new BindRequest({ version: 3, name: dn.parse('cn=root'), credentials: 'secret' @@ -42,14 +22,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeInt(3); ber.writeString('cn=root'); ber.writeString('secret', 0x80); - var req = new BindRequest(); + const req = new BindRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.version, 3); t.equal(req.dn.toString(), 'cn=root'); @@ -57,9 +36,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var req = new BindRequest({ + const req = new BindRequest({ messageID: 123, version: 3, name: dn.parse('cn=root'), @@ -67,7 +45,7 @@ test('toBer', function (t) { }); t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/bind_response.test.js b/test/messages/bind_response.test.js index fd8caf7..c9f8493 100644 --- a/test/messages/bind_response.test.js +++ b/test/messages/bind_response.test.js @@ -1,34 +1,16 @@ -// 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 BindResponse; - - -///--- Tests - -test('load library', function (t) { - BindResponse = require('../../lib/index').BindResponse; - t.ok(BindResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { BindResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new BindResponse()); t.end(); }); - test('new with args', function (t) { - var res = new BindResponse({ + const res = new BindResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new BindResponse(); + const res = new BindResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new BindResponse({ + const res = new BindResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/compare_request.test.js b/test/messages/compare_request.test.js index c28b7d5..744fc60 100644 --- a/test/messages/compare_request.test.js +++ b/test/messages/compare_request.test.js @@ -1,36 +1,16 @@ -// 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 CompareRequest; -var dn; - -///--- Tests - -test('load library', function (t) { - CompareRequest = require('../../lib/index').CompareRequest; - dn = require('../../lib/index').dn; - t.ok(CompareRequest); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { CompareRequest, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new CompareRequest()); t.end(); }); - test('new with args', function (t) { - var req = new CompareRequest({ + const req = new CompareRequest({ entry: dn.parse('cn=foo, o=test'), attribute: 'sn', value: 'testy' @@ -42,9 +22,8 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.startSequence(); @@ -52,8 +31,7 @@ test('parse', function (t) { ber.writeString('testy'); ber.endSequence(); - - var req = new CompareRequest(); + const req = new CompareRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.dn, 'cn=foo, o=test'); t.equal(req.attribute, 'sn'); @@ -61,9 +39,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var req = new CompareRequest({ + const req = new CompareRequest({ messageID: 123, entry: dn.parse('cn=foo, o=test'), attribute: 'sn', @@ -72,7 +49,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/compare_response.test.js b/test/messages/compare_response.test.js index fd4594c..f7945af 100644 --- a/test/messages/compare_response.test.js +++ b/test/messages/compare_response.test.js @@ -1,34 +1,16 @@ -// 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 CompareResponse; - - -///--- Tests - -test('load library', function (t) { - CompareResponse = require('../../lib/index').CompareResponse; - t.ok(CompareResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { CompareResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new CompareResponse()); t.end(); }); - test('new with args', function (t) { - var res = new CompareResponse({ + const res = new CompareResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new CompareResponse(); + const res = new CompareResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new CompareResponse({ + const res = new CompareResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/del_request.test.js b/test/messages/del_request.test.js index d5a8c74..d7c59d3 100644 --- a/test/messages/del_request.test.js +++ b/test/messages/del_request.test.js @@ -1,37 +1,16 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - - -var asn1 = require('asn1'); -var logger = Object.create(require('abstract-logging')); -var test = require('tap').test; - - -///--- Globals - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var DeleteRequest; -var dn; - - -///--- Tests - -test('load library', function (t) { - DeleteRequest = require('../../lib/index').DeleteRequest; - dn = require('../../lib/index').dn; - t.ok(DeleteRequest); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { DeleteRequest, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new DeleteRequest()); t.end(); }); - test('new with args', function (t) { - var req = new DeleteRequest({ + const req = new DeleteRequest({ entry: dn.parse('cn=test') }); t.ok(req); @@ -39,30 +18,26 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('cn=test', 0x4a); - var req = new DeleteRequest({ - log: logger - }); - var reader = new BerReader(ber.buffer); + const req = new DeleteRequest(); + const reader = new BerReader(ber.buffer); reader.readSequence(0x4a); t.ok(req.parse(reader, reader.length)); t.equal(req.dn.toString(), 'cn=test'); t.end(); }); - test('toBer', function (t) { - var req = new DeleteRequest({ + const req = new DeleteRequest({ messageID: 123, entry: dn.parse('cn=test') }); t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/del_response.test.js b/test/messages/del_response.test.js index ebecb5c..33e3f00 100644 --- a/test/messages/del_response.test.js +++ b/test/messages/del_response.test.js @@ -1,34 +1,16 @@ -// 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 DeleteResponse; - - -///--- Tests - -test('load library', function (t) { - DeleteResponse = require('../../lib/index').DeleteResponse; - t.ok(DeleteResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { DeleteResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new DeleteResponse()); t.end(); }); - test('new with args', function (t) { - var res = new DeleteResponse({ + const res = new DeleteResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new DeleteResponse(); + const res = new DeleteResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new DeleteResponse({ + const res = new DeleteResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/ext_request.test.js b/test/messages/ext_request.test.js index 4c378ab..c0da1ad 100644 --- a/test/messages/ext_request.test.js +++ b/test/messages/ext_request.test.js @@ -1,51 +1,8 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - -var asn1 = require('asn1'); - -var Buffer = require('buffer').Buffer; - -// Buffer.compare was only added on node 0.11.13 -var areBuffersEqual = function areBuffersEqual(bufA, bufB) { - if(!(Buffer.isBuffer(bufA) && Buffer.isBuffer(bufB))) { - return false; - } - - if(typeof Buffer.compare === 'function') { - return Buffer.compare(bufA, bufB) === 0; - } - - var len = bufA.length; - if (len !== bufB.length) { - return false; - } - for (var i = 0; i < len; i++) { - if (bufA.readUInt8(i) !== bufB.readUInt8(i)) { - return false; - } - } - return true; -}; - - -///--- Globals - -var BerReader = asn1.BerReader; -var BerWriter = asn1.BerWriter; -var ExtendedRequest; -var dn; - -///--- Tests - -test('load library', function (t) { - ExtendedRequest = require('../../lib/index').ExtendedRequest; - dn = require('../../lib/index').dn; - t.ok(ExtendedRequest); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ExtendedRequest } = require('../../lib'); test('new no args', function (t) { t.ok(new ExtendedRequest()); @@ -54,37 +11,37 @@ test('new no args', function (t) { test('new with args', function (t) { - var req = new ExtendedRequest({ + const req = new ExtendedRequest({ requestName: '1.2.3.4', requestValue: 'test' }); t.ok(req); t.equal(req.requestName, '1.2.3.4'); t.equal(req.requestValue, 'test'); - t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.requestValueBuffer, Buffer.from('test', 'utf8')), 0); t.equal(req.value, 'test'); - t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.valueBuffer, Buffer.from('test', 'utf8')), 0); t.end(); }); test('new with buffer args', function (t) { - var req = new ExtendedRequest({ + const req = new ExtendedRequest({ requestName: '1.2.3.4', - requestValue: new Buffer('test', 'utf8') + requestValue: Buffer.from('test', 'utf8') }); t.ok(req); t.equal(req.requestName, '1.2.3.4'); t.equal(req.requestValue, req.requestValueBuffer); - t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.requestValueBuffer, Buffer.from('test', 'utf8')), 0); t.equal(req.value, req.valueBuffer); - t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.valueBuffer, Buffer.from('test', 'utf8')), 0); t.end(); }); test('new no args set args', function (t) { - var req = new ExtendedRequest(); + const req = new ExtendedRequest(); t.ok(req); req.name = '1.2.3.4'; @@ -92,50 +49,50 @@ test('new no args set args', function (t) { req.value = 'test'; t.equal(req.requestValue, 'test'); - t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.requestValueBuffer, Buffer.from('test', 'utf8')), 0); t.equal(req.value, 'test'); - t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.valueBuffer, Buffer.from('test', 'utf8')), 0); t.end(); }); test('new no args set args buffer', function (t) { - var req = new ExtendedRequest(); + const req = new ExtendedRequest(); t.ok(req); req.name = '1.2.3.4'; t.equal(req.requestName, '1.2.3.4'); - req.value = new Buffer('test', 'utf8'); + req.value = Buffer.from('test', 'utf8'); t.equal(req.requestValue, req.requestValueBuffer); - t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.requestValueBuffer, Buffer.from('test', 'utf8')), 0); t.equal(req.value, req.valueBuffer); - t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.valueBuffer, Buffer.from('test', 'utf8')), 0); t.end(); }); test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('1.2.3.4', 0x80); ber.writeString('test', 0x81); - var req = new ExtendedRequest(); + const req = new ExtendedRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.requestName, '1.2.3.4'); t.equal(req.requestValue, 'test'); - t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.requestValueBuffer, Buffer.from('test', 'utf8')), 0); t.equal(req.value, 'test'); - t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.is(Buffer.compare(req.valueBuffer, Buffer.from('test', 'utf8')), 0); t.end(); }); test('toBer', function (t) { - var req = new ExtendedRequest({ + const req = new ExtendedRequest({ messageID: 123, requestName: '1.2.3.4', requestValue: 'test' @@ -143,7 +100,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); @@ -156,15 +113,15 @@ test('toBer', function (t) { test('toBer from buffer', function (t) { - var req = new ExtendedRequest({ + const req = new ExtendedRequest({ messageID: 123, requestName: '1.2.3.4', - requestValue: new Buffer('test', 'utf8') + requestValue: Buffer.from('test', 'utf8') }); t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/ext_response.test.js b/test/messages/ext_response.test.js index b31559a..577ffa6 100644 --- a/test/messages/ext_response.test.js +++ b/test/messages/ext_response.test.js @@ -1,34 +1,16 @@ -// 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 ExtendedResponse; - - -///--- Tests - -test('load library', function (t) { - ExtendedResponse = require('../../lib/index').ExtendedResponse; - t.ok(ExtendedResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ExtendedResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new ExtendedResponse()); t.end(); }); - test('new with args', function (t) { - var res = new ExtendedResponse({ + const res = new ExtendedResponse({ messageID: 123, status: 0, responseName: '1.2.3.4', @@ -42,16 +24,15 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); ber.writeString('1.2.3.4', 0x8a); ber.writeString('test', 0x8b); - var res = new ExtendedResponse(); + const res = new ExtendedResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -61,9 +42,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new ExtendedResponse({ + const res = new ExtendedResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -73,7 +53,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/moddn_request.test.js b/test/messages/moddn_request.test.js index 6b55ea7..f252cc5 100644 --- a/test/messages/moddn_request.test.js +++ b/test/messages/moddn_request.test.js @@ -1,36 +1,16 @@ -// 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 ModifyDNRequest; -var dn; - -///--- Tests - -test('load library', function (t) { - ModifyDNRequest = require('../../lib/index').ModifyDNRequest; - dn = require('../../lib/index').dn; - t.ok(ModifyDNRequest); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ModifyDNRequest, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new ModifyDNRequest()); t.end(); }); - test('new with args', function (t) { - var req = new ModifyDNRequest({ + const req = new ModifyDNRequest({ entry: dn.parse('cn=foo, o=test'), newRdn: dn.parse('cn=foo2'), deleteOldRdn: true @@ -42,14 +22,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.writeString('cn=foo2'); ber.writeBoolean(true); - var req = new ModifyDNRequest(); + const req = new ModifyDNRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.newRdn.toString(), 'cn=foo2'); @@ -58,9 +37,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var req = new ModifyDNRequest({ + const req = new ModifyDNRequest({ messageID: 123, entry: dn.parse('cn=foo, o=test'), newRdn: dn.parse('cn=foo2'), @@ -69,7 +47,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/moddn_response.test.js b/test/messages/moddn_response.test.js index e257c8d..783a317 100644 --- a/test/messages/moddn_response.test.js +++ b/test/messages/moddn_response.test.js @@ -1,34 +1,16 @@ -// 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 ModifyDNResponse; - - -///--- Tests - -test('load library', function (t) { - ModifyDNResponse = require('../../lib/index').ModifyDNResponse; - t.ok(ModifyDNResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ModifyDNResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new ModifyDNResponse()); t.end(); }); - test('new with args', function (t) { - var res = new ModifyDNResponse({ + const res = new ModifyDNResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new ModifyDNResponse(); + const res = new ModifyDNResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new ModifyDNResponse({ + const res = new ModifyDNResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/modify_request.test.js b/test/messages/modify_request.test.js index 046e216..c2f598b 100644 --- a/test/messages/modify_request.test.js +++ b/test/messages/modify_request.test.js @@ -1,42 +1,16 @@ -// 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 ModifyRequest; -var Attribute; -var Change; -var dn; - -///--- Tests - -test('load library', function (t) { - ModifyRequest = require('../../lib/index').ModifyRequest; - Attribute = require('../../lib/index').Attribute; - Change = require('../../lib/index').Change; - dn = require('../../lib/index').dn; - t.ok(ModifyRequest); - t.ok(Attribute); - t.ok(Change); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ModifyRequest, Attribute, Change, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new ModifyRequest()); t.end(); }); - test('new with args', function (t) { - var req = new ModifyRequest({ + const req = new ModifyRequest({ object: dn.parse('cn=foo, o=test'), changes: [new Change({ operation: 'Replace', @@ -52,9 +26,8 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.startSequence(); @@ -72,7 +45,7 @@ test('parse', function (t) { ber.endSequence(); - var req = new ModifyRequest(); + const req = new ModifyRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.changes.length, 1); @@ -82,9 +55,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var req = new ModifyRequest({ + const req = new ModifyRequest({ messageID: 123, object: dn.parse('cn=foo, o=test'), changes: [new Change({ @@ -95,7 +67,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/modify_response.test.js b/test/messages/modify_response.test.js index f63993e..af9ba5d 100644 --- a/test/messages/modify_response.test.js +++ b/test/messages/modify_response.test.js @@ -1,34 +1,16 @@ -// 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 ModifyResponse; - - -///--- Tests - -test('load library', function (t) { - ModifyResponse = require('../../lib/index').ModifyResponse; - t.ok(ModifyResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { ModifyResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new ModifyResponse()); t.end(); }); - test('new with args', function (t) { - var res = new ModifyResponse({ + const res = new ModifyResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new ModifyResponse(); + const res = new ModifyResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -53,9 +34,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new ModifyResponse({ + const res = new ModifyResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +43,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/parser.test.js b/test/messages/parser.test.js index f6d431d..7dca311 100644 --- a/test/messages/parser.test.js +++ b/test/messages/parser.test.js @@ -1,24 +1,10 @@ -// Copyright 2014 Joyent, Inc. All rights reserved. +'use strict'; -var test = require('tap').test; -var logger = Object.create(require('abstract-logging')); - -///--- Globals - -var lib; -var Parser; - -///--- Tests -test('load library', function (t) { - lib = require('../../lib/'); - Parser = lib.Parser; - - t.ok(Parser); - t.end(); -}); +const { test } = require('tap'); +const { Parser, LDAPMessage, LDAP_REQ_EXTENSION } = require('../../lib'); test('wrong protocol error', function (t) { - var p = new Parser({log: logger}); + const p = new Parser(); p.once('error', function (err) { t.ok(err); @@ -26,12 +12,12 @@ test('wrong protocol error', function (t) { }); // Send some bogus data to incur an error - p.write(new Buffer([16, 1, 4])); + p.write(Buffer.from([16, 1, 4])); }); test('bad protocol op', function (t) { - var p = new Parser({log: logger}); - var message = new lib.LDAPMessage({ + const p = new Parser(); + const message = new LDAPMessage({ protocolOp: 254 // bogus (at least today) }); p.once('error', function (err) { @@ -43,14 +29,14 @@ test('bad protocol op', function (t) { }); test('bad message structure', function (t) { - var p = new Parser({log: logger}); + const p = new Parser(); // message with bogus structure - var message = new lib.LDAPMessage({ - protocolOp: lib.LDAP_REQ_EXTENSION + const message = new LDAPMessage({ + protocolOp: LDAP_REQ_EXTENSION }); message._toBer = function (writer) { - writer.writeBuffer(new Buffer([16, 1, 4]), 80); + writer.writeBuffer(Buffer.from([16, 1, 4]), 80); return writer; }; diff --git a/test/messages/search_entry.test.js b/test/messages/search_entry.test.js index 7b41d53..cf5ddd7 100644 --- a/test/messages/search_entry.test.js +++ b/test/messages/search_entry.test.js @@ -1,39 +1,16 @@ -// 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 SearchEntry; -var Attribute; -var dn; - -///--- Tests - -test('load library', function (t) { - SearchEntry = require('../../lib/index').SearchEntry; - Attribute = require('../../lib/index').Attribute; - dn = require('../../lib/index').dn; - t.ok(SearchEntry); - t.ok(dn); - t.ok(Attribute); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { SearchEntry, Attribute, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new SearchEntry()); t.end(); }); - test('new with args', function (t) { - var res = new SearchEntry({ + const res = new SearchEntry({ messageID: 123, objectName: dn.parse('cn=foo, o=test'), attributes: [new Attribute({type: 'cn', vals: ['foo']}), @@ -50,9 +27,8 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.startSequence(); @@ -73,7 +49,7 @@ test('parse', function (t) { ber.endSequence(); - var res = new SearchEntry(); + const res = new SearchEntry(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.dn, 'cn=foo, o=test'); t.equal(res.attributes.length, 2); @@ -84,9 +60,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var res = new SearchEntry({ + const res = new SearchEntry({ messageID: 123, objectName: dn.parse('cn=foo, o=test'), attributes: [new Attribute({type: 'cn', vals: ['foo']}), @@ -94,7 +69,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/search_request.test.js b/test/messages/search_request.test.js index 30ec9c4..455b90d 100644 --- a/test/messages/search_request.test.js +++ b/test/messages/search_request.test.js @@ -1,39 +1,16 @@ -// 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 SearchRequest; -var EqualityFilter; -var dn; - -///--- Tests - -test('load library', function (t) { - SearchRequest = require('../../lib/index').SearchRequest; - EqualityFilter = require('../../lib/index').EqualityFilter; - dn = require('../../lib/index').dn; - t.ok(SearchRequest); - t.ok(EqualityFilter); - t.ok(dn); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { SearchRequest, EqualityFilter, dn } = require('../../lib'); test('new no args', function (t) { t.ok(new SearchRequest()); t.end(); }); - test('new with args', function (t) { - var req = new SearchRequest({ + const req = new SearchRequest({ baseObject: dn.parse('cn=foo, o=test'), filter: new EqualityFilter({ attribute: 'email', @@ -50,14 +27,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var f = new EqualityFilter({ + const f = new EqualityFilter({ attribute: 'email', value: 'foo@bar.com' }); - var ber = new BerWriter(); + let ber = new BerWriter(); ber.writeString('cn=foo, o=test'); ber.writeEnumeration(0); ber.writeEnumeration(0); @@ -66,7 +42,7 @@ test('parse', function (t) { ber.writeBoolean(false); ber = f.toBer(ber); - var req = new SearchRequest(); + const req = new SearchRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.dn.toString(), 'cn=foo, o=test'); t.equal(req.scope, 'base'); @@ -79,9 +55,8 @@ test('parse', function (t) { t.end(); }); - test('toBer', function (t) { - var req = new SearchRequest({ + const req = new SearchRequest({ messageID: 123, baseObject: dn.parse('cn=foo, o=test'), scope: 1, @@ -98,7 +73,7 @@ test('toBer', function (t) { t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/search_response.test.js b/test/messages/search_response.test.js index 83ffeb7..1dc1d6c 100644 --- a/test/messages/search_response.test.js +++ b/test/messages/search_response.test.js @@ -1,34 +1,16 @@ -// 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 SearchResponse; - - -///--- Tests - -test('load library', function (t) { - SearchResponse = require('../../lib/index').SearchResponse; - t.ok(SearchResponse); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { SearchResponse } = require('../../lib'); test('new no args', function (t) { t.ok(new SearchResponse()); t.end(); }); - test('new with args', function (t) { - var res = new SearchResponse({ + const res = new SearchResponse({ messageID: 123, status: 0 }); @@ -38,14 +20,13 @@ test('new with args', function (t) { t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); ber.writeEnumeration(0); ber.writeString('cn=root'); ber.writeString('foo'); - var res = new SearchResponse(); + const res = new SearchResponse(); t.ok(res._parse(new BerReader(ber.buffer))); t.equal(res.status, 0); t.equal(res.matchedDN, 'cn=root'); @@ -55,7 +36,7 @@ test('parse', function (t) { test('toBer', function (t) { - var res = new SearchResponse({ + const res = new SearchResponse({ messageID: 123, status: 3, matchedDN: 'cn=root', @@ -63,7 +44,7 @@ test('toBer', function (t) { }); t.ok(res); - var ber = new BerReader(res.toBer()); + const ber = new BerReader(res.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/messages/unbind_request.test.js b/test/messages/unbind_request.test.js index 058a124..10ee45d 100644 --- a/test/messages/unbind_request.test.js +++ b/test/messages/unbind_request.test.js @@ -1,55 +1,35 @@ -// 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 UnbindRequest; - - -///--- Tests - -test('load library', function (t) { - UnbindRequest = require('../../lib/index').UnbindRequest; - t.ok(UnbindRequest); - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { BerReader, BerWriter } = require('asn1'); +const { UnbindRequest } = require('../../lib'); test('new no args', function (t) { t.ok(new UnbindRequest()); t.end(); }); - test('new with args', function (t) { - var req = new UnbindRequest({}); + const req = new UnbindRequest({}); t.ok(req); t.end(); }); - test('parse', function (t) { - var ber = new BerWriter(); + const ber = new BerWriter(); - var req = new UnbindRequest(); + const req = new UnbindRequest(); t.ok(req._parse(new BerReader(ber.buffer))); t.end(); }); - test('toBer', function (t) { - var req = new UnbindRequest({ + const req = new UnbindRequest({ messageID: 123 }); t.ok(req); - var ber = new BerReader(req.toBer()); + const ber = new BerReader(req.toBer()); t.ok(ber); t.equal(ber.readSequence(), 0x30); t.equal(ber.readInt(), 123); diff --git a/test/server.test.js b/test/server.test.js index acbdacd..59571a1 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -1,38 +1,12 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. +'use strict'; -var logger = Object.create(require('abstract-logging')); +const tap = require('tap'); +const vasync = require('vasync'); +const { getSock } = require('./utils'); +const ldap = require('../lib'); -var fs = require('fs') -var tap = require('tap'); -var uuid = require('uuid'); -var vasync = require('vasync'); - - -///--- Globals - -var BIND_DN = 'cn=root'; -var BIND_PW = 'secret'; - -var SUFFIX = 'dc=test'; - -var SERVER_PORT = process.env.SERVER_PORT || 1389; - -var ldap; -var Attribute; -var Change; -var client; -var server; -var sock; - -function getSock() { - if (process.platform === 'win32') { - return '\\\\.\\pipe\\' + uuid(); - } else { - return '/tmp/.' + uuid(); - } -} - -///--- Tests +const SERVER_PORT = process.env.SERVER_PORT || 1389; +const SUFFIX = 'dc=test'; tap.beforeEach(function (done, t) { // We do not need a `.afterEach` to clean up the sock files because that @@ -41,12 +15,6 @@ tap.beforeEach(function (done, t) { done() }) -tap.test('load library', function (t) { - ldap = require('../lib/index'); - t.ok(ldap.createServer); - t.end(); -}); - tap.test('basic create', function (t) { const server = ldap.createServer(); t.ok(server); @@ -66,49 +34,42 @@ tap.test('properties', function (t) { server.listen(0, 'localhost', function () { t.ok(server.url); - server.close(); - t.end(); + server.close(() => t.end()); }); }); tap.test('listen on unix/named socket', function (t) { - t.plan(2); const server = ldap.createServer(); server.listen(t.context.sock, function () { t.ok(server.url); t.equal(server.url.split(':')[0], 'ldapi'); - server.close(); - t.end(); + server.close(() => t.end()); }); }); tap.test('listen on static port', function (t) { - t.plan(2); const server = ldap.createServer(); server.listen(SERVER_PORT, '127.0.0.1', function () { - var addr = server.address(); + const addr = server.address(); t.equal(addr.port, parseInt(SERVER_PORT, 10)); - t.equals(server.url, 'ldap://127.0.0.1:' + SERVER_PORT); - server.close(); - t.end(); + t.equals(server.url, `ldap://127.0.0.1:${SERVER_PORT}`); + server.close(() => t.end()); }); }); tap.test('listen on ephemeral port', function (t) { - t.plan(2); const server = ldap.createServer(); server.listen(0, 'localhost', function () { - var addr = server.address(); + const addr = server.address(); t.ok(addr.port > 0); t.ok(addr.port < 65535); - server.close(); - t.end(); + server.close(() => t.end()); }); }); tap.test('route order', function (t) { function generateHandler(response) { - var func = function handler(req, res, next) { + const func = function handler(req, res, next) { res.send({ dn: response, attributes: { } @@ -119,11 +80,11 @@ tap.test('route order', function (t) { return func; } - server = ldap.createServer(); - sock = getSock(); - var dnShort = SUFFIX; - var dnMed = 'dc=sub, ' + SUFFIX; - var dnLong = 'dc=long, dc=sub, ' + SUFFIX; + const server = ldap.createServer(); + const sock = t.context.sock; + const dnShort = SUFFIX; + const dnMed = 'dc=sub, ' + SUFFIX; + const dnLong = 'dc=long, dc=sub, ' + SUFFIX; // Mount routes out of order server.search(dnMed, generateHandler(dnMed)); @@ -131,10 +92,21 @@ tap.test('route order', function (t) { server.search(dnLong, generateHandler(dnLong)); server.listen(sock, function () { t.ok(true, 'server listen'); - client = ldap.createClient({ socketPath: sock }); + const client = ldap.createClient({ socketPath: sock }); + client.on('connect', () => { + vasync.forEachParallel({ + 'func': runSearch, + 'inputs': [dnShort, dnMed, dnLong] + }, function (err, results) { + t.error(err); + client.unbind(); + server.close(() => t.end()); + }); + }) + function runSearch(value, cb) { client.search(value, '(objectclass=*)', function (err, res) { - t.ifError(err); + t.error(err); t.ok(res); res.on('searchEntry', function (entry) { t.equal(entry.dn.toString(), value); @@ -144,23 +116,13 @@ tap.test('route order', function (t) { }); }); } - - vasync.forEachParallel({ - 'func': runSearch, - 'inputs': [dnShort, dnMed, dnLong] - }, function (err, results) { - t.notOk(err); - client.unbind(); - server.close(); - t.end(); - }); }); }); tap.test('route absent', function (t) { const server = ldap.createServer(); - var DN_ROUTE = 'dc=base'; - var DN_MISSING = 'dc=absent'; + const DN_ROUTE = 'dc=base'; + const DN_MISSING = 'dc=absent'; server.bind(DN_ROUTE, function (req, res, next) { res.end(); @@ -170,9 +132,9 @@ tap.test('route absent', function (t) { server.listen(t.context.sock, function () { t.ok(true, 'server startup'); vasync.parallel({ - 'funcs': [ + funcs: [ function presentBind(cb) { - var clt = ldap.createClient({ socketPath: t.context.sock }); + const clt = ldap.createClient({ socketPath: t.context.sock }); clt.bind(DN_ROUTE, '', function (err) { t.notOk(err); clt.unbind(); @@ -180,7 +142,7 @@ tap.test('route absent', function (t) { }); }, function absentBind(cb) { - var clt = ldap.createClient({ socketPath: t.context.sock }); + const clt = ldap.createClient({ socketPath: t.context.sock }); clt.bind(DN_MISSING, '', function (err) { t.ok(err); t.equal(err.code, ldap.LDAP_NO_SUCH_OBJECT); @@ -191,14 +153,12 @@ tap.test('route absent', function (t) { ] }, function (err, result) { t.notOk(err); - server.close(); - t.end(); + server.close(() => t.end()); }); }); }); tap.test('route unbind', function (t) { - t.plan(4); const server = ldap.createServer(); server.unbind(function (req, res, next) { @@ -209,23 +169,22 @@ tap.test('route unbind', function (t) { server.listen(t.context.sock, function () { t.ok(true, 'server startup'); - client = ldap.createClient({ socketPath: t.context.sock }); + const client = ldap.createClient({ socketPath: t.context.sock }); client.bind('', '', function (err) { - t.ifError(err, 'client bind error'); + t.error(err, 'client bind error'); client.unbind(function (err) { - t.ifError(err, 'client unbind error'); - server.close(); - t.end(); + t.error(err, 'client unbind error'); + server.close(() => t.end()); }); }); }); }); tap.test('strict routing', function (t) { - var testDN = 'cn=valid'; - var clt; - var server; - var sock = t.context.sock; + const testDN = 'cn=valid'; + let clt; + let server; + const sock = t.context.sock; vasync.pipeline({ funcs: [ function setup(_, cb) { @@ -251,7 +210,7 @@ tap.test('strict routing', function (t) { }, function testBad(_, cb) { clt.search('not a dn', {scope: 'base'}, function (err, res) { - t.ifError(err); + t.error(err); res.once('error', function (err2) { t.ok(err2); t.equal(err2.code, ldap.LDAP_INVALID_DN_SYNTAX); @@ -265,9 +224,9 @@ tap.test('strict routing', function (t) { }, function testGood(_, cb) { clt.search(testDN, {scope: 'base'}, function (err, res) { - t.ifError(err); + t.error(err); res.once('error', function (err2) { - t.ifError(err2); + t.error(err2); cb(err2); }); res.once('end', function (result) { @@ -281,8 +240,7 @@ tap.test('strict routing', function (t) { if (clt) { clt.destroy(); } - server.close(); - t.end(); + server.close(() => t.end()); }); }); @@ -290,7 +248,7 @@ tap.test('non-strict routing', function (t) { const server = ldap.createServer({ strictDN: false }); - var testDN = 'this ain\'t a DN'; + const testDN = 'this ain\'t a DN'; // invalid DNs go to default handler server.search('', function (req, res, next) { @@ -303,16 +261,15 @@ tap.test('non-strict routing', function (t) { server.listen(t.context.sock, function () { t.ok(true, 'server startup'); - var clt = ldap.createClient({ + const clt = ldap.createClient({ socketPath: t.context.sock, strictDN: false }); clt.search(testDN, {scope: 'base'}, function (err, res) { - t.ifError(err); + t.error(err); res.on('end', function () { clt.destroy(); - server.close(); - t.end(); + server.close(() => t.end()); }); }); }); @@ -321,15 +278,20 @@ tap.test('non-strict routing', function (t) { tap.test('close accept a callback', function (t) { const server = ldap.createServer(); // callback is called when the server is closed - server.close(function(err){ - t.end(); - }); + server.listen(0, function(err) { + t.error(err); + server.close(function(err){ + t.error(err) + t.end(); + }); + }) }); tap.test('close without error calls callback', function (t) { const server = ldap.createServer(); // when the server is closed without error, the callback parameter is undefined server.listen(1389,'127.0.0.1',function(err){ + t.error(err); server.close(function(err){ t.error(err); t.end(); diff --git a/test/url.test.js b/test/url.test.js index 47882c78..c59c1a1 100644 --- a/test/url.test.js +++ b/test/url.test.js @@ -1,28 +1,10 @@ -// Copyright 2011 Mark Cavage, Inc. All rights reserved. - -var test = require('tap').test; - - - -///--- Globals - -var url; -var parseURL; - - - -///--- Tests - -test('load library', function (t) { - parseURL = require('../lib/index').parseURL; - t.ok(parseURL); - - t.end(); -}); +'use strict'; +const { test } = require('tap'); +const { parseURL } = require('../lib'); test('parse empty', function (t) { - var u = parseURL('ldap:///'); + const u = parseURL('ldap:///'); t.equal(u.hostname, 'localhost'); t.equal(u.port, 389); t.ok(!u.DN); @@ -33,7 +15,7 @@ test('parse empty', function (t) { test('parse hostname', function (t) { - var u = parseURL('ldap://example.com/'); + const u = parseURL('ldap://example.com/'); t.equal(u.hostname, 'example.com'); t.equal(u.port, 389); t.ok(!u.DN); @@ -44,7 +26,7 @@ test('parse hostname', function (t) { test('parse host and port', function (t) { - var u = parseURL('ldap://example.com:1389/'); + const u = parseURL('ldap://example.com:1389/'); t.equal(u.hostname, 'example.com'); t.equal(u.port, 1389); t.ok(!u.DN); @@ -56,7 +38,7 @@ test('parse host and port', function (t) { test('parse full', function (t) { - var u = parseURL('ldaps://ldap.example.com:1389/dc=example%20,dc=com' + + const u = parseURL('ldaps://ldap.example.com:1389/dc=example%20,dc=com' + '?cn,sn?sub?(cn=Babs%20Jensen)'); t.equal(u.secure, true); diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..54336d1 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,17 @@ +'use strict'; + +const os = require('os'); +const path = require('path'); +const uuid = require('uuid'); + +function getSock() { + if (process.platform === 'win32') { + return '\\\\.\\pipe\\' + uuid(); + } else { + return path.join(os.tmpdir(), uuid()); + } +} + +module.exports = { + getSock +}