2014-11-05 15:05:07 +03:00
< ? php
/**
2016-07-21 17:49:16 +03:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
* @ author Aaron Wood < aaronjwood @ gmail . com >
2016-05-26 20:56:05 +03:00
* @ author Arthur Schiwon < blizzz @ arthur - schiwon . de >
2020-03-31 11:49:10 +03:00
* @ author Christoph Wurst < christoph @ winzerhof - wurst . at >
2016-07-21 17:49:16 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2015-03-26 13:44:34 +03:00
* @ author Morris Jobke < hey @ morrisjobke . de >
2017-11-06 17:56:42 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2016-05-26 20:56:05 +03:00
* @ author Stefan Weil < sw @ weilnetz . de >
2015-03-26 13:44:34 +03:00
*
* @ license AGPL - 3.0
*
* This code is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License , version 3 ,
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License , version 3 ,
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />
2015-03-26 13:44:34 +03:00
*
*/
2015-02-26 13:37:37 +03:00
2016-05-12 18:14:59 +03:00
namespace OCA\User_LDAP\Tests\Mapping ;
2014-11-05 15:05:07 +03:00
2017-02-17 20:45:33 +03:00
use OCA\User_LDAP\Mapping\AbstractMapping ;
2016-09-02 12:02:12 +03:00
use OCP\IDBConnection ;
2014-11-05 15:05:07 +03:00
abstract class AbstractMappingTest extends \Test\TestCase {
abstract public function getMapper ( \OCP\IDBConnection $dbMock );
/**
* kiss test on isColNameValid
*/
public function testIsColNameValid () {
2016-09-02 12:02:12 +03:00
$dbMock = $this -> createMock ( IDBConnection :: class );
2014-11-05 15:05:07 +03:00
$mapper = $this -> getMapper ( $dbMock );
$this -> assertTrue ( $mapper -> isColNameValid ( 'ldap_dn' ));
$this -> assertFalse ( $mapper -> isColNameValid ( 'foobar' ));
}
/**
* returns an array of test entries with dn , name and uuid as keys
* @ return array
*/
protected function getTestData () {
2020-03-26 11:30:18 +03:00
$data = [
[
2014-11-05 15:05:07 +03:00
'dn' => 'uid=foobar,dc=example,dc=org' ,
'name' => 'Foobar' ,
'uuid' => '1111-AAAA-1234-CDEF' ,
2020-03-26 11:30:18 +03:00
],
[
2014-11-05 15:05:07 +03:00
'dn' => 'uid=barfoo,dc=example,dc=org' ,
'name' => 'Barfoo' ,
'uuid' => '2222-BBBB-1234-CDEF' ,
2020-03-26 11:30:18 +03:00
],
[
2014-11-05 15:05:07 +03:00
'dn' => 'uid=barabara,dc=example,dc=org' ,
'name' => 'BaraBara' ,
'uuid' => '3333-CCCC-1234-CDEF' ,
2020-03-26 11:30:18 +03:00
]
];
2014-11-05 15:05:07 +03:00
return $data ;
}
/**
* calls map () on the given mapper and asserts result for true
* @ param \OCA\User_LDAP\Mapping\AbstractMapping $mapper
* @ param array $data
*/
protected function mapEntries ( $mapper , $data ) {
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$done = $mapper -> map ( $entry [ 'dn' ], $entry [ 'name' ], $entry [ 'uuid' ]);
$this -> assertTrue ( $done );
}
}
/**
* initalizes environment for a test run and returns an array with
* test objects . Preparing environment means that all mappings are cleared
* first and then filled with test entries .
* @ return array 0 = \OCA\User_LDAP\Mapping\AbstractMapping , 1 = array of
* users or groups
*/
private function initTest () {
$dbc = \OC :: $server -> getDatabaseConnection ();
$mapper = $this -> getMapper ( $dbc );
$data = $this -> getTestData ();
// make sure DB is pristine, then fill it with test entries
$mapper -> clear ();
$this -> mapEntries ( $mapper , $data );
2020-03-26 11:30:18 +03:00
return [ $mapper , $data ];
2014-11-05 15:05:07 +03:00
}
/**
* tests map () method with input that should result in not - mapping .
* Hint : successful mapping is tested inherently with mapEntries () .
*/
public function testMap () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
// test that mapping will not happen when it shall not
2017-01-25 19:10:51 +03:00
$tooLongDN = 'uid=joann,ou=Secret Small Specialized Department,ou=Some Tremendously Important Department,ou=Another Very Important Department,ou=Pretty Meaningful Derpartment,ou=Quite Broad And General Department,ou=The Topmost Department,dc=hugelysuccessfulcompany,dc=com' ;
2020-03-26 11:30:18 +03:00
$paramKeys = [ '' , 'dn' , 'name' , 'uuid' , $tooLongDN ];
2020-04-10 15:19:56 +03:00
foreach ( $paramKeys as $key ) {
2014-11-05 15:05:07 +03:00
$failEntry = $data [ 0 ];
2020-04-10 15:19:56 +03:00
if ( ! empty ( $key )) {
2014-11-05 15:05:07 +03:00
$failEntry [ $key ] = 'do-not-get-mapped' ;
}
$isMapped = $mapper -> map ( $failEntry [ 'dn' ], $failEntry [ 'name' ], $failEntry [ 'uuid' ]);
$this -> assertFalse ( $isMapped );
}
}
/**
2016-04-12 13:52:51 +03:00
* tests unmap () for both successful and unsuccessful removing of
2014-11-05 15:05:07 +03:00
* mapping entries
*/
public function testUnmap () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$result = $mapper -> unmap ( $entry [ 'name' ]);
$this -> assertTrue ( $result );
}
$result = $mapper -> unmap ( 'notAnEntry' );
$this -> assertFalse ( $result );
}
/**
* tests getDNByName (), getNameByDN () and getNameByUUID () for successful
* and unsuccessful requests .
*/
public function testGetMethods () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$fdn = $mapper -> getDNByName ( $entry [ 'name' ]);
$this -> assertSame ( $fdn , $entry [ 'dn' ]);
}
$fdn = $mapper -> getDNByName ( 'nosuchname' );
$this -> assertFalse ( $fdn );
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$name = $mapper -> getNameByDN ( $entry [ 'dn' ]);
$this -> assertSame ( $name , $entry [ 'name' ]);
}
$name = $mapper -> getNameByDN ( 'nosuchdn' );
$this -> assertFalse ( $name );
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertSame ( $name , $entry [ 'name' ]);
}
$name = $mapper -> getNameByUUID ( 'nosuchuuid' );
$this -> assertFalse ( $name );
}
/**
* tests getNamesBySearch () for successful and unsuccessful requests .
*/
public function testSearch () {
2021-01-12 12:15:48 +03:00
[ $mapper ,] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
2016-07-20 15:20:45 +03:00
$names = $mapper -> getNamesBySearch ( 'oo' , '%' , '%' );
2014-11-05 15:05:07 +03:00
$this -> assertTrue ( is_array ( $names ));
$this -> assertSame ( 2 , count ( $names ));
$this -> assertTrue ( in_array ( 'Foobar' , $names ));
$this -> assertTrue ( in_array ( 'Barfoo' , $names ));
$names = $mapper -> getNamesBySearch ( 'nada' );
$this -> assertTrue ( is_array ( $names ));
$this -> assertSame ( 0 , count ( $names ));
}
/**
* tests setDNbyUUID () for successful and unsuccessful update .
*/
2017-02-17 20:45:33 +03:00
public function testSetDNMethod () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
$newDN = 'uid=modified,dc=example,dc=org' ;
$done = $mapper -> setDNbyUUID ( $newDN , $data [ 0 ][ 'uuid' ]);
$this -> assertTrue ( $done );
$fdn = $mapper -> getDNByName ( $data [ 0 ][ 'name' ]);
$this -> assertSame ( $fdn , $newDN );
$newDN = 'uid=notme,dc=example,dc=org' ;
$done = $mapper -> setDNbyUUID ( $newDN , 'iamnothere' );
$this -> assertFalse ( $done );
$name = $mapper -> getNameByDN ( $newDN );
$this -> assertFalse ( $name );
2017-02-17 20:45:33 +03:00
}
/**
* tests setUUIDbyDN () for successful and unsuccessful update .
*/
public function testSetUUIDMethod () {
/** @var AbstractMapping $mapper */
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
2017-02-17 20:45:33 +03:00
$newUUID = 'ABC737-DEF754' ;
$done = $mapper -> setUUIDbyDN ( $newUUID , 'uid=notme,dc=example,dc=org' );
$this -> assertFalse ( $done );
$name = $mapper -> getNameByUUID ( $newUUID );
$this -> assertFalse ( $name );
$done = $mapper -> setUUIDbyDN ( $newUUID , $data [ 0 ][ 'dn' ]);
$this -> assertTrue ( $done );
$uuid = $mapper -> getUUIDByDN ( $data [ 0 ][ 'dn' ]);
$this -> assertSame ( $uuid , $newUUID );
2014-11-05 15:05:07 +03:00
}
/**
* tests clear () for successful update .
*/
public function testClear () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 15:05:07 +03:00
$done = $mapper -> clear ();
$this -> assertTrue ( $done );
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2014-11-05 15:05:07 +03:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertFalse ( $name );
}
}
2015-01-07 14:39:04 +03:00
2018-03-15 16:16:43 +03:00
/**
* tests clear () for successful update .
*/
public function testClearCb () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2018-03-15 16:16:43 +03:00
$callbackCalls = 0 ;
$test = $this ;
$callback = function ( string $id ) use ( $test , & $callbackCalls ) {
$test -> assertTrue ( trim ( $id ) !== '' );
$callbackCalls ++ ;
};
$done = $mapper -> clearCb ( $callback , $callback );
$this -> assertTrue ( $done );
$this -> assertSame ( count ( $data ) * 2 , $callbackCalls );
2020-04-10 15:19:56 +03:00
foreach ( $data as $entry ) {
2018-03-15 16:16:43 +03:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertFalse ( $name );
}
}
2015-01-07 14:39:04 +03:00
/**
* tests getList () method
*/
public function testList () {
2021-01-12 12:15:48 +03:00
[ $mapper , $data ] = $this -> initTest ();
2015-01-07 14:39:04 +03:00
// get all entries without specifying offset or limit
$results = $mapper -> getList ();
$this -> assertSame ( 3 , count ( $results ));
// get all-1 entries by specifying offset, and an high limit
// specifying only offset without limit will not work by underlying lib
$results = $mapper -> getList ( 1 , 999 );
$this -> assertSame ( count ( $data ) - 1 , count ( $results ));
// get first 2 entries by limit, but not offset
$results = $mapper -> getList ( null , 2 );
$this -> assertSame ( 2 , count ( $results ));
// get 2nd entry by specifying both offset and limit
$results = $mapper -> getList ( 1 , 1 );
$this -> assertSame ( 1 , count ( $results ));
}
2021-01-08 13:54:07 +03:00
public function testGetListOfIdsByDn () {
/** @var AbstractMapping $mapper */
2021-01-12 12:15:48 +03:00
[ $mapper ,] = $this -> initTest ();
2021-01-08 13:54:07 +03:00
$listOfDNs = [];
for ( $i = 0 ; $i < 66640 ; $i ++ ) {
// Postgres has a limit of 65535 values in a single IN list
$name = 'as_' . $i ;
$dn = 'uid=' . $name . ',dc=example,dc=org' ;
$listOfDNs [] = $dn ;
if ( $i % 20 === 0 ) {
$mapper -> map ( $dn , $name , 'fake-uuid-' . $i );
}
}
$result = $mapper -> getListOfIdsByDn ( $listOfDNs );
$this -> assertSame ( 66640 / 20 , count ( $result ));
}
2014-11-05 15:05:07 +03:00
}