2013-07-10 12:59:46 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should test all our connection methods to the LDAP server
|
|
|
|
* and return success and failures.
|
|
|
|
*
|
|
|
|
* @package Kohana/LDAP
|
|
|
|
* @category Test
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2013 phpLDAPadmin Development Team
|
|
|
|
* @license http://dev.phpldapadmin.org/license.html
|
|
|
|
* @group ldap
|
|
|
|
* @group ldap.server
|
|
|
|
*/
|
|
|
|
Class LDAPConnection extends Unittest_TestCase {
|
|
|
|
function hosts() {
|
|
|
|
return array(
|
|
|
|
array('localhost','389','a',TRUE),
|
|
|
|
array('localhost','389','b',TRUE),
|
|
|
|
array('localhost','390','a',FALSE),
|
|
|
|
array('localhost','390','b',FALSE),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that we can connect to an LDAP server
|
|
|
|
* @dataProvider hosts
|
|
|
|
*/
|
|
|
|
function testConnect($host,$port,$instance,$expect) {
|
2013-07-13 12:42:02 +00:00
|
|
|
$connection = Arr::merge(Kohana::$config->load('database.default'),array(
|
2013-07-10 12:59:46 +00:00
|
|
|
'connection'=>array('hostname'=>$host,'port'=>$port),
|
2013-07-13 12:42:02 +00:00
|
|
|
));
|
2013-07-10 12:59:46 +00:00
|
|
|
|
2013-07-12 00:35:54 +00:00
|
|
|
$x = LDAP::factory($instance,NULL,$connection);
|
2013-07-10 12:59:46 +00:00
|
|
|
$x->connect();
|
|
|
|
|
|
|
|
if ($expect)
|
|
|
|
$this->assertAttributeInternalType('resource','_connection',$x);
|
|
|
|
// In OpenLDAP, this still returns a resource, even though it should be a failure.
|
|
|
|
else
|
|
|
|
$this->assertAttributeInternalType('resource','_connection',$x);
|
|
|
|
|
|
|
|
$x->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
function auths() {
|
|
|
|
return array(
|
|
|
|
array('bart','eatmyshorts',TRUE),
|
|
|
|
array('bart','Eatmyshorts',FALSE),
|
|
|
|
array('nobart','Eatmyshorts',FALSE),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider auths
|
|
|
|
* @depends testConnect
|
|
|
|
*/
|
|
|
|
function testAuth($user,$password,$expect) {
|
|
|
|
if ($expect)
|
|
|
|
$this->assertTrue(Auth::instance()->login($user,$password));
|
|
|
|
else
|
|
|
|
$this->assertFalse(Auth::instance()->login($user,$password));
|
|
|
|
|
|
|
|
Auth::instance()->logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
function authconfig() {
|
|
|
|
return array(
|
|
|
|
array('','',TRUE),
|
|
|
|
array('bart','x',FALSE),
|
|
|
|
array('cn=Manager','Eatmyshorts',FALSE),
|
|
|
|
array('cn=Manager,dc=example.com','NotAllowed',TRUE),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider authconfig
|
|
|
|
* @depends testConnect
|
|
|
|
*/
|
|
|
|
function testAuthConfiguration($user,$password,$expect) {
|
2013-07-13 12:42:02 +00:00
|
|
|
$connection = Arr::merge(Kohana::$config->load('database.default'),array(
|
2013-07-10 12:59:46 +00:00
|
|
|
'login_attr'=>'uid',
|
|
|
|
'connection'=>array(
|
|
|
|
'hostname'=>'localhost',
|
|
|
|
'port'=>389,
|
|
|
|
'username'=>$user,
|
|
|
|
'password'=>$password
|
|
|
|
),
|
2013-07-13 12:42:02 +00:00
|
|
|
));
|
2013-07-10 12:59:46 +00:00
|
|
|
|
|
|
|
// Ensure we start with a clean auth connection.
|
2013-07-12 00:35:54 +00:00
|
|
|
LDAP::factory('auth')->disconnect();
|
|
|
|
LDAP::factory('default')->disconnect();
|
2013-07-10 12:59:46 +00:00
|
|
|
|
2013-07-12 00:35:54 +00:00
|
|
|
$x = LDAP::factory('default',NULL,$connection);
|
2013-07-10 12:59:46 +00:00
|
|
|
$x->bind('bart','eatmyshorts');
|
|
|
|
|
|
|
|
if ($expect)
|
|
|
|
$this->assertTrue($x->connected());
|
|
|
|
else
|
|
|
|
$this->assertFalse($x->connected());
|
|
|
|
}
|
|
|
|
}
|