2018-12-12 03:41:58 +03:00
#!/usr/bin/env python3
2007-12-17 05:25:28 +03:00
# Simple tests for the ldb python bindings.
2007-11-21 13:47:55 +03:00
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
2007-12-17 05:25:28 +03:00
2011-11-10 16:40:03 +04:00
import os
2011-11-10 23:20:00 +04:00
from unittest import TestCase
2015-07-29 13:45:43 +03:00
import sys
2017-04-07 00:32:05 +03:00
import gc
import time
2007-11-21 13:47:55 +03:00
import ldb
2017-04-25 11:14:33 +03:00
import shutil
2007-11-21 13:47:55 +03:00
2015-07-29 13:45:43 +03:00
PY3 = sys . version_info > ( 3 , 0 )
2018-03-05 23:13:31 +03:00
TDB_PREFIX = " tdb:// "
MDB_PREFIX = " mdb:// "
2018-03-05 23:13:31 +03:00
MDB_INDEX_OBJ = {
" dn " : " @INDEXLIST " ,
" @IDXONE " : [ b " 1 " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ]
}
2010-10-04 22:56:20 +04:00
2018-07-30 09:20:39 +03:00
2017-04-25 11:14:33 +03:00
def tempdir ( ) :
2010-11-18 22:42:26 +03:00
import tempfile
try :
dir_prefix = os . path . join ( os . environ [ " SELFTEST_PREFIX " ] , " tmp " )
except KeyError :
dir_prefix = None
2017-04-25 11:14:33 +03:00
return tempfile . mkdtemp ( dir = dir_prefix )
2008-12-21 06:36:16 +03:00
2011-11-10 23:20:00 +04:00
class NoContextTests ( TestCase ) :
2009-06-17 20:25:21 +04:00
2007-11-21 13:47:55 +03:00
def test_valid_attr_name ( self ) :
self . assertTrue ( ldb . valid_attr_name ( " foo " ) )
self . assertFalse ( ldb . valid_attr_name ( " 24foo " ) )
def test_timestring ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( " 19700101000000.0Z " , ldb . timestring ( 0 ) )
self . assertEqual ( " 20071119191012.0Z " , ldb . timestring ( 1195499412 ) )
2007-11-21 13:47:55 +03:00
def test_string_to_time ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , ldb . string_to_time ( " 19700101000000.0Z " ) )
self . assertEqual ( 1195499412 , ldb . string_to_time ( " 20071119191012.0Z " ) )
2007-11-21 13:47:55 +03:00
2011-07-28 11:03:06 +04:00
def test_binary_encode ( self ) :
2015-07-29 13:45:43 +03:00
encoded = ldb . binary_encode ( b ' test \\ x ' )
2011-11-10 23:20:00 +04:00
decoded = ldb . binary_decode ( encoded )
2015-07-29 13:45:43 +03:00
self . assertEqual ( decoded , b ' test \\ x ' )
encoded2 = ldb . binary_encode ( ' test \\ x ' )
self . assertEqual ( encoded2 , encoded )
2007-11-21 13:47:55 +03:00
2018-03-05 23:13:31 +03:00
class LdbBaseTest ( TestCase ) :
def setUp ( self ) :
super ( LdbBaseTest , self ) . setUp ( )
try :
if self . prefix is None :
self . prefix = TDB_PREFIX
except AttributeError :
self . prefix = TDB_PREFIX
def tearDown ( self ) :
super ( LdbBaseTest , self ) . tearDown ( )
def url ( self ) :
return self . prefix + self . filename
def flags ( self ) :
if self . prefix == MDB_PREFIX :
return ldb . FLG_NOSYNC
else :
return 0
class SimpleLdb ( LdbBaseTest ) :
2009-06-17 20:25:21 +04:00
2017-04-25 11:14:33 +03:00
def setUp ( self ) :
super ( SimpleLdb , self ) . setUp ( )
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " test.ldb " )
2018-03-05 23:13:31 +03:00
self . ldb = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2018-03-06 05:30:10 +03:00
try :
self . ldb . add ( self . index )
except AttributeError :
pass
2017-04-25 11:14:33 +03:00
def tearDown ( self ) :
shutil . rmtree ( self . testdir )
2017-08-24 07:29:58 +03:00
super ( SimpleLdb , self ) . tearDown ( )
2017-09-21 05:15:32 +03:00
# Ensure the LDB is closed now, so we close the FD
del ( self . ldb )
2007-11-21 13:47:55 +03:00
def test_connect ( self ) :
2018-03-05 23:13:31 +03:00
ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
def test_connect_none ( self ) :
ldb . Ldb ( )
def test_connect_later ( self ) :
x = ldb . Ldb ( )
2018-03-05 23:13:31 +03:00
x . connect ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
2008-05-20 01:12:13 +04:00
def test_repr ( self ) :
x = ldb . Ldb ( )
self . assertTrue ( repr ( x ) . startswith ( " <ldb connection " ) )
2007-11-21 13:47:55 +03:00
def test_set_create_perms ( self ) :
x = ldb . Ldb ( )
2015-06-09 11:36:26 +03:00
x . set_create_perms ( 0o600 )
2007-11-21 13:47:55 +03:00
2008-09-19 18:17:52 +04:00
def test_modules_none ( self ) :
x = ldb . Ldb ( )
2015-06-10 11:21:24 +03:00
self . assertEqual ( [ ] , x . modules ( ) )
2008-09-19 18:17:52 +04:00
def test_modules_tdb ( self ) :
2018-03-05 23:13:31 +03:00
x = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " [<ldb module ' tdb ' >] " , repr ( x . modules ( ) ) )
2008-09-19 18:17:52 +04:00
2015-08-21 11:07:17 +03:00
def test_firstmodule_none ( self ) :
x = ldb . Ldb ( )
self . assertEqual ( x . firstmodule , None )
def test_firstmodule_tdb ( self ) :
2018-03-05 23:13:31 +03:00
x = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-08-21 11:07:17 +03:00
mod = x . firstmodule
self . assertEqual ( repr ( mod ) , " <ldb module ' tdb ' > " )
2007-11-21 13:47:55 +03:00
def test_search ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
2008-01-11 05:25:22 +03:00
def test_search_controls ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( controls = [ " paged_results:0:5 " ] ) ) , 0 )
2008-01-11 05:25:22 +03:00
2018-09-24 16:37:50 +03:00
def test_utf8_ldb_Dn ( self ) :
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
dn = ldb . Dn ( l , ( b ' a= ' + b ' \xc4 \x85 \xc4 \x87 \xc4 \x99 \xc5 \x82 \xc5 \x84 \xc3 \xb3 \xc5 \x9b \xc5 \xba \xc5 \xbc ' ) . decode ( ' utf8 ' ) )
2018-11-12 20:42:51 +03:00
def test_utf8_encoded_ldb_Dn ( self ) :
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
dn_encoded_utf8 = b ' a= ' + b ' \xc4 \x85 \xc4 \x87 \xc4 \x99 \xc5 \x82 \xc5 \x84 \xc3 \xb3 \xc5 \x9b \xc5 \xba \xc5 \xbc '
try :
dn = ldb . Dn ( l , dn_encoded_utf8 )
except UnicodeDecodeError as e :
raise
except TypeError as te :
if PY3 :
p3errors = [ " argument 2 must be str, not bytes " ,
" Can ' t convert ' bytes ' object to str implicitly " ]
self . assertIn ( str ( te ) , p3errors )
else :
raise
2007-11-21 13:47:55 +03:00
def test_search_attrs ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ldb . Dn ( l , " " ) , ldb . SCOPE_SUBTREE , " (dc=*) " , [ " dc " ] ) ) , 0 )
2007-11-21 13:47:55 +03:00
2007-12-24 04:19:41 +03:00
def test_search_string_dn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( " " , ldb . SCOPE_SUBTREE , " (dc=*) " , [ " dc " ] ) ) , 0 )
2007-12-24 04:19:41 +03:00
2008-05-20 01:07:04 +04:00
def test_search_attr_string ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2008-05-20 01:07:04 +04:00
self . assertRaises ( TypeError , l . search , attrs = " dc " )
2015-07-29 13:45:43 +03:00
self . assertRaises ( TypeError , l . search , attrs = b " dc " )
2008-05-20 01:07:04 +04:00
2007-11-21 13:47:55 +03:00
def test_opaque ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
l . set_opaque ( " my_opaque " , l )
self . assertTrue ( l . get_opaque ( " my_opaque " ) is not None )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , l . get_opaque ( " unknown " ) )
2007-11-21 13:47:55 +03:00
2017-04-07 00:32:05 +03:00
def test_search_scope_base_empty_db ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2017-04-07 00:32:05 +03:00
self . assertEqual ( len ( l . search ( ldb . Dn ( l , " dc=foo1 " ) ,
2018-07-30 09:16:12 +03:00
ldb . SCOPE_BASE ) ) , 0 )
2017-04-07 00:32:05 +03:00
def test_search_scope_onelevel_empty_db ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ldb . Dn ( l , " dc=foo1 " ) ,
2018-07-30 09:16:12 +03:00
ldb . SCOPE_ONELEVEL ) ) , 0 )
2007-11-21 13:47:55 +03:00
def test_delete ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2009-06-17 22:17:56 +04:00
self . assertRaises ( ldb . LdbError , lambda : l . delete ( ldb . Dn ( l , " dc=foo2 " ) ) )
2007-11-21 13:47:55 +03:00
2010-06-19 00:17:54 +04:00
def test_delete_w_unhandled_ctrl ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2010-06-19 00:17:54 +04:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo1 " )
2015-07-29 13:45:43 +03:00
m [ " b " ] = [ b " a " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2010-06-19 00:17:54 +04:00
l . add ( m )
self . assertRaises ( ldb . LdbError , lambda : l . delete ( m . dn , [ " search_options:1:2 " ] ) )
l . delete ( m . dn )
2007-11-21 13:47:55 +03:00
def test_contains ( self ) :
2018-03-05 23:13:31 +03:00
name = self . url ( )
l = ldb . Ldb ( name , flags = self . flags ( ) )
2009-06-17 22:17:56 +04:00
self . assertFalse ( ldb . Dn ( l , " dc=foo3 " ) in l )
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( name , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2009-06-17 22:17:56 +04:00
m . dn = ldb . Dn ( l , " dc=foo3 " )
2007-11-21 13:47:55 +03:00
m [ " b " ] = [ " a " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2009-06-17 22:17:56 +04:00
self . assertTrue ( ldb . Dn ( l , " dc=foo3 " ) in l )
2015-08-21 11:22:22 +03:00
self . assertFalse ( ldb . Dn ( l , " dc=foo4 " ) in l )
2007-11-21 13:47:55 +03:00
finally :
l . delete ( m . dn )
def test_get_config_basedn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , l . get_config_basedn ( ) )
2007-11-21 13:47:55 +03:00
def test_get_root_basedn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , l . get_root_basedn ( ) )
2007-11-21 13:47:55 +03:00
def test_get_schema_basedn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , l . get_schema_basedn ( ) )
2007-11-21 13:47:55 +03:00
def test_get_default_basedn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , l . get_default_basedn ( ) )
2007-11-21 13:47:55 +03:00
def test_add ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo4 " )
m [ " bla " ] = b " bla "
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-07-29 13:45:43 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
l . add ( m )
try :
self . assertEqual ( len ( l . search ( ) ) , 1 )
finally :
l . delete ( ldb . Dn ( l , " dc=foo4 " ) )
2016-02-01 04:30:56 +03:00
def test_search_iterator ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2016-02-01 04:30:56 +03:00
s = l . search_iterator ( )
s . abandon ( )
try :
for me in s :
self . fail ( )
self . fail ( )
except RuntimeError as re :
pass
try :
s . abandon ( )
self . fail ( )
except RuntimeError as re :
pass
try :
s . result ( )
self . fail ( )
except RuntimeError as re :
pass
s = l . search_iterator ( )
count = 0
for me in s :
self . assertTrue ( isinstance ( me , ldb . Message ) )
count + = 1
r = s . result ( )
self . assertEqual ( len ( r ) , 0 )
self . assertEqual ( count , 0 )
m1 = ldb . Message ( )
m1 . dn = ldb . Dn ( l , " dc=foo4 " )
m1 [ " bla " ] = b " bla "
2018-03-06 05:30:10 +03:00
m1 [ " objectUUID " ] = b " 0123456789abcdef "
2016-02-01 04:30:56 +03:00
l . add ( m1 )
try :
s = l . search_iterator ( )
msgs = [ ]
for me in s :
self . assertTrue ( isinstance ( me , ldb . Message ) )
count + = 1
msgs . append ( me )
r = s . result ( )
self . assertEqual ( len ( r ) , 0 )
self . assertEqual ( len ( msgs ) , 1 )
self . assertEqual ( msgs [ 0 ] . dn , m1 . dn )
m2 = ldb . Message ( )
m2 . dn = ldb . Dn ( l , " dc=foo5 " )
m2 [ " bla " ] = b " bla "
2018-03-06 05:30:10 +03:00
m2 [ " objectUUID " ] = b " 0123456789abcdee "
2016-02-01 04:30:56 +03:00
l . add ( m2 )
s = l . search_iterator ( )
msgs = [ ]
for me in s :
self . assertTrue ( isinstance ( me , ldb . Message ) )
count + = 1
msgs . append ( me )
r = s . result ( )
self . assertEqual ( len ( r ) , 0 )
self . assertEqual ( len ( msgs ) , 2 )
if msgs [ 0 ] . dn == m1 . dn :
self . assertEqual ( msgs [ 0 ] . dn , m1 . dn )
self . assertEqual ( msgs [ 1 ] . dn , m2 . dn )
else :
self . assertEqual ( msgs [ 0 ] . dn , m2 . dn )
self . assertEqual ( msgs [ 1 ] . dn , m1 . dn )
s = l . search_iterator ( )
msgs = [ ]
for me in s :
self . assertTrue ( isinstance ( me , ldb . Message ) )
count + = 1
msgs . append ( me )
break
try :
s . result ( )
self . fail ( )
except RuntimeError as re :
pass
for me in s :
self . assertTrue ( isinstance ( me , ldb . Message ) )
count + = 1
msgs . append ( me )
break
for me in s :
self . fail ( )
r = s . result ( )
self . assertEqual ( len ( r ) , 0 )
self . assertEqual ( len ( msgs ) , 2 )
if msgs [ 0 ] . dn == m1 . dn :
self . assertEqual ( msgs [ 0 ] . dn , m1 . dn )
self . assertEqual ( msgs [ 1 ] . dn , m2 . dn )
else :
self . assertEqual ( msgs [ 0 ] . dn , m2 . dn )
self . assertEqual ( msgs [ 1 ] . dn , m1 . dn )
finally :
l . delete ( ldb . Dn ( l , " dc=foo4 " ) )
l . delete ( ldb . Dn ( l , " dc=foo5 " ) )
2015-07-29 13:45:43 +03:00
def test_add_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2009-06-17 22:17:56 +04:00
m . dn = ldb . Dn ( l , " dc=foo4 " )
2007-11-21 13:47:55 +03:00
m [ " bla " ] = " bla "
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-11-21 13:47:55 +03:00
finally :
2009-06-17 22:17:56 +04:00
l . delete ( ldb . Dn ( l , " dc=foo4 " ) )
2007-11-21 13:47:55 +03:00
2009-09-23 00:51:25 +04:00
def test_add_w_unhandled_ctrl ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2009-09-23 00:51:25 +04:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo4 " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = b " bla "
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2018-07-30 09:19:05 +03:00
self . assertRaises ( ldb . LdbError , lambda : l . add ( m , [ " search_options:1:2 " ] ) )
2009-09-23 00:51:25 +04:00
2007-11-21 13:47:55 +03:00
def test_add_dict ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = { " dn " : ldb . Dn ( l , " dc=foo5 " ) ,
2018-03-06 05:30:10 +03:00
" bla " : b " bla " ,
" objectUUID " : b " 0123456789abcdef " }
2015-07-29 13:45:43 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
l . add ( m )
try :
self . assertEqual ( len ( l . search ( ) ) , 1 )
finally :
l . delete ( ldb . Dn ( l , " dc=foo5 " ) )
def test_add_dict_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2009-06-17 22:17:56 +04:00
m = { " dn " : ldb . Dn ( l , " dc=foo5 " ) ,
2018-03-06 05:30:10 +03:00
" bla " : " bla " ,
" objectUUID " : b " 0123456789abcdef " }
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-11-21 13:47:55 +03:00
finally :
2009-06-17 22:17:56 +04:00
l . delete ( ldb . Dn ( l , " dc=foo5 " ) )
2007-11-21 13:47:55 +03:00
2007-12-20 01:27:34 +03:00
def test_add_dict_string_dn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2018-03-06 05:30:10 +03:00
m = { " dn " : " dc=foo6 " , " bla " : b " bla " ,
" objectUUID " : b " 0123456789abcdef " }
2015-07-29 13:45:43 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
l . add ( m )
try :
self . assertEqual ( len ( l . search ( ) ) , 1 )
finally :
l . delete ( ldb . Dn ( l , " dc=foo6 " ) )
def test_add_dict_bytes_dn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2018-03-06 05:30:10 +03:00
m = { " dn " : b " dc=foo6 " , " bla " : b " bla " ,
2018-07-30 09:15:34 +03:00
" objectUUID " : b " 0123456789abcdef " }
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2007-12-20 01:27:34 +03:00
l . add ( m )
try :
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-12-20 01:27:34 +03:00
finally :
2009-06-17 22:17:56 +04:00
l . delete ( ldb . Dn ( l , " dc=foo6 " ) )
2007-12-20 01:27:34 +03:00
2007-11-21 13:47:55 +03:00
def test_rename ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2009-06-17 22:17:56 +04:00
m . dn = ldb . Dn ( l , " dc=foo7 " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = b " bla "
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2009-06-17 22:17:56 +04:00
l . rename ( ldb . Dn ( l , " dc=foo7 " ) , ldb . Dn ( l , " dc=bar " ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-11-21 13:47:55 +03:00
finally :
l . delete ( ldb . Dn ( l , " dc=bar " ) )
2007-12-24 04:19:41 +03:00
def test_rename_string_dns ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-12-24 04:19:41 +03:00
m = ldb . Message ( )
2009-06-17 22:17:56 +04:00
m . dn = ldb . Dn ( l , " dc=foo8 " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = b " bla "
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 0 )
2011-11-10 16:40:03 +04:00
l . add ( m )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-12-24 04:19:41 +03:00
try :
2009-06-17 22:17:56 +04:00
l . rename ( " dc=foo8 " , " dc=bar " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( len ( l . search ( ) ) , 1 )
2007-12-24 04:19:41 +03:00
finally :
l . delete ( ldb . Dn ( l , " dc=bar " ) )
2018-05-21 06:25:58 +03:00
def test_rename_bad_string_dns ( self ) :
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo8 " )
m [ " bla " ] = b " bla "
m [ " objectUUID " ] = b " 0123456789abcdef "
self . assertEqual ( len ( l . search ( ) ) , 0 )
l . add ( m )
self . assertEqual ( len ( l . search ( ) ) , 1 )
self . assertRaises ( ldb . LdbError , lambda : l . rename ( " dcXfoo8 " , " dc=bar " ) )
self . assertRaises ( ldb . LdbError , lambda : l . rename ( " dc=foo8 " , " dcXbar " ) )
l . delete ( ldb . Dn ( l , " dc=foo8 " ) )
2015-11-18 05:25:20 +03:00
def test_empty_dn ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-11-18 05:25:20 +03:00
self . assertEqual ( 0 , len ( l . search ( ) ) )
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=empty " )
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-11-18 05:25:20 +03:00
l . add ( m )
rm = l . search ( )
self . assertEqual ( 1 , len ( rm ) )
2018-03-06 05:30:10 +03:00
self . assertEqual ( set ( [ " dn " , " distinguishedName " , " objectUUID " ] ) ,
set ( rm [ 0 ] . keys ( ) ) )
2015-11-18 05:25:20 +03:00
rm = l . search ( m . dn )
self . assertEqual ( 1 , len ( rm ) )
2018-03-06 05:30:10 +03:00
self . assertEqual ( set ( [ " dn " , " distinguishedName " , " objectUUID " ] ) ,
set ( rm [ 0 ] . keys ( ) ) )
2015-11-18 05:25:20 +03:00
rm = l . search ( m . dn , attrs = [ " blah " ] )
self . assertEqual ( 1 , len ( rm ) )
self . assertEqual ( 0 , len ( rm [ 0 ] ) )
2007-11-21 13:47:55 +03:00
def test_modify_delete ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=modifydelete " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = [ b " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-07-29 13:45:43 +03:00
l . add ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEqual ( [ b " 1234 " ] , list ( rm [ " bla " ] ) )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=modifydelete " )
m [ " bla " ] = ldb . MessageElement ( [ ] , ldb . FLAG_MOD_DELETE , " bla " )
self . assertEqual ( ldb . FLAG_MOD_DELETE , m [ " bla " ] . flags ( ) )
l . modify ( m )
2015-11-18 05:25:20 +03:00
rm = l . search ( m . dn )
2015-07-29 13:45:43 +03:00
self . assertEqual ( 1 , len ( rm ) )
2018-03-06 05:30:10 +03:00
self . assertEqual ( set ( [ " dn " , " distinguishedName " , " objectUUID " ] ) ,
set ( rm [ 0 ] . keys ( ) ) )
2015-07-29 13:45:43 +03:00
rm = l . search ( m . dn , attrs = [ " bla " ] )
2015-11-18 05:25:20 +03:00
self . assertEqual ( 1 , len ( rm ) )
self . assertEqual ( 0 , len ( rm [ 0 ] ) )
2015-07-29 13:45:43 +03:00
finally :
l . delete ( ldb . Dn ( l , " dc=modifydelete " ) )
def test_modify_delete_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=modifydelete " )
m . text [ " bla " ] = [ " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2007-11-21 13:47:55 +03:00
l . add ( m )
rm = l . search ( m . dn ) [ 0 ]
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " 1234 " ] , list ( rm . text [ " bla " ] ) )
2007-11-21 13:47:55 +03:00
try :
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=modifydelete " )
2009-08-26 09:01:12 +04:00
m [ " bla " ] = ldb . MessageElement ( [ ] , ldb . FLAG_MOD_DELETE , " bla " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( ldb . FLAG_MOD_DELETE , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
2015-11-18 05:25:20 +03:00
rm = l . search ( m . dn )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( rm ) )
2018-03-06 05:30:10 +03:00
self . assertEqual ( set ( [ " dn " , " distinguishedName " , " objectUUID " ] ) ,
set ( rm [ 0 ] . keys ( ) ) )
2010-11-23 23:39:34 +03:00
rm = l . search ( m . dn , attrs = [ " bla " ] )
2015-11-18 05:25:20 +03:00
self . assertEqual ( 1 , len ( rm ) )
self . assertEqual ( 0 , len ( rm [ 0 ] ) )
2007-11-21 13:47:55 +03:00
finally :
2008-01-11 05:25:22 +03:00
l . delete ( ldb . Dn ( l , " dc=modifydelete " ) )
2007-11-21 13:47:55 +03:00
def test_modify_add ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=add " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = [ b " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-07-29 13:45:43 +03:00
l . add ( m )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m [ " bla " ] = ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
self . assertEqual ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ b " 1234 " , b " 456 " ] , list ( rm [ " bla " ] ) )
finally :
l . delete ( ldb . Dn ( l , " dc=add " ) )
def test_modify_add_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m . text [ " bla " ] = [ " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=add " )
2009-08-26 09:01:12 +04:00
m [ " bla " ] = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " 1234 " , " 456 " ] , list ( rm . text [ " bla " ] ) )
2007-11-21 13:47:55 +03:00
finally :
2008-01-11 05:25:22 +03:00
l . delete ( ldb . Dn ( l , " dc=add " ) )
2007-11-21 13:47:55 +03:00
2009-08-26 09:01:12 +04:00
def test_modify_replace ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=modify2 " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = [ b " 1234 " , b " 456 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-07-29 13:45:43 +03:00
l . add ( m )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=modify2 " )
m [ " bla " ] = ldb . MessageElement ( [ b " 789 " ] , ldb . FLAG_MOD_REPLACE , " bla " )
self . assertEqual ( ldb . FLAG_MOD_REPLACE , m [ " bla " ] . flags ( ) )
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ b " 789 " ] , list ( rm [ " bla " ] ) )
rm = l . search ( m . dn , attrs = [ " bla " ] ) [ 0 ]
self . assertEqual ( 1 , len ( rm ) )
finally :
l . delete ( ldb . Dn ( l , " dc=modify2 " ) )
def test_modify_replace_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=modify2 " )
m . text [ " bla " ] = [ " 1234 " , " 456 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
m = ldb . Message ( )
2008-01-11 05:25:22 +03:00
m . dn = ldb . Dn ( l , " dc=modify2 " )
2009-08-26 09:01:12 +04:00
m [ " bla " ] = ldb . MessageElement ( [ " 789 " ] , ldb . FLAG_MOD_REPLACE , " bla " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( ldb . FLAG_MOD_REPLACE , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " 789 " ] , list ( rm . text [ " bla " ] ) )
2009-08-26 09:01:12 +04:00
rm = l . search ( m . dn , attrs = [ " bla " ] ) [ 0 ]
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( rm ) )
2007-11-21 13:47:55 +03:00
finally :
2008-01-11 05:25:22 +03:00
l . delete ( ldb . Dn ( l , " dc=modify2 " ) )
2007-11-21 13:47:55 +03:00
2009-08-26 09:01:12 +04:00
def test_modify_flags_change ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2009-08-26 09:01:12 +04:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
2015-07-29 13:45:43 +03:00
m [ " bla " ] = [ b " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2015-07-29 13:45:43 +03:00
l . add ( m )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m [ " bla " ] = ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
self . assertEqual ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ b " 1234 " , b " 456 " ] , list ( rm [ " bla " ] ) )
# Now create another modify, but switch the flags before we do it
m [ " bla " ] = ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
m [ " bla " ] . set_flags ( ldb . FLAG_MOD_DELETE )
l . modify ( m )
rm = l . search ( m . dn , attrs = [ " bla " ] ) [ 0 ]
self . assertEqual ( 1 , len ( rm ) )
self . assertEqual ( [ b " 1234 " ] , list ( rm [ " bla " ] ) )
finally :
l . delete ( ldb . Dn ( l , " dc=add " ) )
def test_modify_flags_change_text ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2015-07-29 13:45:43 +03:00
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m . text [ " bla " ] = [ " 1234 " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2009-08-26 09:01:12 +04:00
l . add ( m )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m [ " bla " ] = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
2009-08-26 09:01:12 +04:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
2018-03-06 05:30:10 +03:00
self . assertEqual ( 3 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " 1234 " , " 456 " ] , list ( rm . text [ " bla " ] ) )
2010-12-21 16:05:18 +03:00
# Now create another modify, but switch the flags before we do it
2009-08-26 09:01:12 +04:00
m [ " bla " ] = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
m [ " bla " ] . set_flags ( ldb . FLAG_MOD_DELETE )
l . modify ( m )
rm = l . search ( m . dn , attrs = [ " bla " ] ) [ 0 ]
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( rm ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " 1234 " ] , list ( rm . text [ " bla " ] ) )
2009-08-26 09:01:12 +04:00
finally :
l . delete ( ldb . Dn ( l , " dc=add " ) )
2007-11-21 13:47:55 +03:00
def test_transaction_commit ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
l . transaction_start ( )
2009-06-17 22:17:56 +04:00
m = ldb . Message ( ldb . Dn ( l , " dc=foo9 " ) )
2015-07-29 13:45:43 +03:00
m [ " foo " ] = [ b " bar " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdef "
2007-11-21 13:47:55 +03:00
l . add ( m )
l . transaction_commit ( )
l . delete ( m . dn )
def test_transaction_cancel ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
l . transaction_start ( )
2009-06-17 22:17:56 +04:00
m = ldb . Message ( ldb . Dn ( l , " dc=foo10 " ) )
2015-07-29 13:45:43 +03:00
m [ " foo " ] = [ b " bar " ]
2018-03-06 05:30:10 +03:00
m [ " objectUUID " ] = b " 0123456789abcdee "
2007-11-21 13:47:55 +03:00
l . add ( m )
l . transaction_cancel ( )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , len ( l . search ( ldb . Dn ( l , " dc=foo10 " ) ) ) )
2007-11-21 13:47:55 +03:00
def test_set_debug ( self ) :
def my_report_fn ( level , text ) :
pass
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2007-11-21 13:47:55 +03:00
l . set_debug ( my_report_fn )
2009-03-21 00:58:15 +03:00
def test_zero_byte_string ( self ) :
""" Testing we do not get trapped in the \0 byte in a property string. """
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2009-03-21 00:58:15 +03:00
l . add ( {
2018-07-30 09:17:14 +03:00
" dn " : b " dc=somedn " ,
" objectclass " : b " user " ,
" cN " : b " LDAPtestUSER " ,
" givenname " : b " ldap " ,
" displayname " : b " foo \0 bar " ,
" objectUUID " : b " 0123456789abcdef "
2009-03-21 00:58:15 +03:00
} )
res = l . search ( expression = " (dn=dc=somedn) " )
2015-07-29 13:45:43 +03:00
self . assertEqual ( b " foo \0 bar " , res [ 0 ] [ " displayname " ] [ 0 ] )
2009-03-21 00:58:15 +03:00
2010-10-04 22:56:20 +04:00
def test_no_crash_broken_expr ( self ) :
2018-03-05 23:13:31 +03:00
l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2018-07-30 09:19:05 +03:00
self . assertRaises ( ldb . LdbError , lambda : l . search ( " " , ldb . SCOPE_SUBTREE , " &(dc=*)(dn=*) " , [ " dc " ] ) )
2010-10-04 22:56:20 +04:00
2018-03-05 23:13:31 +03:00
# Run the SimpleLdb tests against an lmdb backend
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class SimpleLdbLmdb ( SimpleLdb ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
self . index = MDB_INDEX_OBJ
super ( SimpleLdbLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( SimpleLdbLmdb , self ) . tearDown ( )
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class SearchTests ( LdbBaseTest ) :
2017-08-24 08:01:14 +03:00
def tearDown ( self ) :
shutil . rmtree ( self . testdir )
super ( SearchTests , self ) . tearDown ( )
2017-09-21 05:15:32 +03:00
# Ensure the LDB is closed now, so we close the FD
del ( self . l )
2017-08-24 08:01:14 +03:00
def setUp ( self ) :
super ( SearchTests , self ) . setUp ( )
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " search_test.ldb " )
2018-05-23 08:15:38 +03:00
options = [ " modules:rdn_name " ]
if hasattr ( self , ' IDXCHECK ' ) :
options . append ( " disable_full_db_scan_for_self_test:1 " )
2018-03-05 23:13:31 +03:00
self . l = ldb . Ldb ( self . url ( ) ,
flags = self . flags ( ) ,
2018-05-23 08:15:38 +03:00
options = options )
2018-03-06 05:30:10 +03:00
try :
self . l . add ( self . index )
except AttributeError :
pass
2017-08-24 08:01:14 +03:00
2017-08-29 01:40:22 +03:00
self . l . add ( { " dn " : " @ATTRIBUTES " ,
" DC " : " CASE_INSENSITIVE " } )
2017-08-25 03:06:48 +03:00
# Note that we can't use the name objectGUID here, as we
# want to stay clear of the objectGUID handler in LDB and
# instead use just the 16 bytes raw, which we just keep
# to printable chars here for ease of handling.
self . l . add ( { " dn " : " DC=SAMBA,DC=ORG " ,
" name " : b " samba.org " ,
2018-03-06 05:30:10 +03:00
" objectUUID " : b " 0123456789abcdef " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
2017-08-25 03:06:48 +03:00
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=USERS,DC=SAMBA,DC=ORG " ,
" name " : b " Users " ,
2017-08-25 03:06:48 +03:00
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU1,DC=SAMBA,DC=ORG " ,
" name " : b " OU #1 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde3 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU2,DC=SAMBA,DC=ORG " ,
" name " : b " OU #2 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde4 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU3,DC=SAMBA,DC=ORG " ,
" name " : b " OU #3 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde5 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU4,DC=SAMBA,DC=ORG " ,
" name " : b " OU #4 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde6 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU5,DC=SAMBA,DC=ORG " ,
" name " : b " OU #5 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde7 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU6,DC=SAMBA,DC=ORG " ,
" name " : b " OU #6 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde8 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU7,DC=SAMBA,DC=ORG " ,
" name " : b " OU #7 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde9 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU8,DC=SAMBA,DC=ORG " ,
" name " : b " OU #8 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde0 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU9,DC=SAMBA,DC=ORG " ,
" name " : b " OU #9 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcdea " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU10,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcdeb " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU11,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " a " ,
" objectUUID " : b " 0123456789abcdec " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU12,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " y " , " y " : " b " ,
" objectUUID " : b " 0123456789abcded " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU13,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcdee " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU14,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd01 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU15,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd02 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU16,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd03 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU17,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd04 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU18,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd05 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU19,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd06 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU20,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " b " ,
" objectUUID " : b " 0123456789abcd07 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU21,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " c " ,
" objectUUID " : b " 0123456789abcd08 " } )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " OU=OU22,DC=SAMBA,DC=ORG " ,
" name " : b " OU #10 " ,
2017-08-25 03:06:48 +03:00
" x " : " x " , " y " : " c " ,
" objectUUID " : b " 0123456789abcd09 " } )
2017-08-24 08:01:14 +03:00
def test_base ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE )
self . assertEqual ( len ( res11 ) , 1 )
2017-08-29 01:40:22 +03:00
def test_base_lower ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=samba,DC=org " ,
scope = ldb . SCOPE_BASE )
self . assertEqual ( len ( res11 ) , 1 )
2017-08-24 08:01:14 +03:00
def test_base_or ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (|(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_base_or2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (|(x=y)(y=b)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_base_and ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (&(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_base_and2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (&(x=y)(y=a)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_base_false ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (|(ou=ou13)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_check_base_false ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (|(ou=ou13)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_check_base_error ( self ) :
""" Testing a search """
2017-12-18 06:22:01 +03:00
checkbaseonsearch = { " dn " : " @OPTIONS " ,
" checkBaseOnSearch " : b " TRUE " }
try :
self . l . add ( checkbaseonsearch )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
m = ldb . Message . from_dict ( self . l ,
checkbaseonsearch )
self . l . modify ( m )
2017-08-24 08:01:14 +03:00
try :
res11 = self . l . search ( base = " OU=OU11x,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (|(ou=ou13)(ou=ou12)) " )
self . fail ( " Should have failed on missing base " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_NO_SUCH_OBJECT )
def test_subtree_and ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_subtree_and2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(x=y)(|(y=b)(y=c))) " )
self . assertEqual ( len ( res11 ) , 1 )
2017-08-29 01:40:22 +03:00
def test_subtree_and2_lower ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(x=y)(|(y=b)(y=c))) " )
self . assertEqual ( len ( res11 ) , 1 )
2017-08-24 08:01:14 +03:00
def test_subtree_or ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (|(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 2 )
def test_subtree_or2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (|(x=y)(y=b)) " )
self . assertEqual ( len ( res11 ) , 20 )
def test_subtree_or3 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (|(x=y)(y=b)(y=c)) " )
self . assertEqual ( len ( res11 ) , 22 )
def test_one_and ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (&(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_one_and2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (&(x=y)(y=b)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_one_or ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (|(ou=ou11)(ou=ou12)) " )
self . assertEqual ( len ( res11 ) , 2 )
def test_one_or2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (|(x=y)(y=b)) " )
self . assertEqual ( len ( res11 ) , 20 )
2017-08-29 01:40:22 +03:00
def test_one_or2_lower ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (|(x=y)(y=b)) " )
self . assertEqual ( len ( res11 ) , 20 )
2018-05-23 08:15:38 +03:00
def test_one_unindexable ( self ) :
""" Testing a search """
try :
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (y=b*) " )
if hasattr ( self , ' IDX ' ) and \
not hasattr ( self , ' IDXONE ' ) and \
hasattr ( self , ' IDXCHECK ' ) :
self . fail ( " Should have failed as un-indexed search " )
self . assertEqual ( len ( res11 ) , 9 )
except ldb . LdbError as err :
enum = err . args [ 0 ]
estr = err . args [ 1 ]
self . assertEqual ( enum , ldb . ERR_INAPPROPRIATE_MATCHING )
self . assertIn ( estr , " ldb FULL SEARCH disabled " )
def test_one_unindexable_presence ( self ) :
""" Testing a search """
try :
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (y=*) " )
if hasattr ( self , ' IDX ' ) and \
not hasattr ( self , ' IDXONE ' ) and \
hasattr ( self , ' IDXCHECK ' ) :
self . fail ( " Should have failed as un-indexed search " )
self . assertEqual ( len ( res11 ) , 24 )
except ldb . LdbError as err :
enum = err . args [ 0 ]
estr = err . args [ 1 ]
self . assertEqual ( enum , ldb . ERR_INAPPROPRIATE_MATCHING )
self . assertIn ( estr , " ldb FULL SEARCH disabled " )
2017-08-24 08:01:14 +03:00
def test_subtree_and_or ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(|(x=z)(y=b))(x=x)(y=c)) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_subtree_and_or2 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(x=x)(y=c)(|(x=z)(y=b))) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_subtree_and_or3 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(|(ou=ou11)(ou=ou10))(|(x=y)(y=b)(y=c))) " )
self . assertEqual ( len ( res11 ) , 2 )
def test_subtree_and_or4 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(|(x=y)(y=b)(y=c))(|(ou=ou11)(ou=ou10))) " )
self . assertEqual ( len ( res11 ) , 2 )
def test_subtree_and_or5 ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(|(x=y)(y=b)(y=c))(ou=ou11)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_subtree_or_and ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (|(x=x)(y=c)(&(x=z)(y=b))) " )
self . assertEqual ( len ( res11 ) , 10 )
def test_subtree_large_and_unique ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(ou=ou10)(y=a)) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_subtree_and_none ( self ) :
""" Testing a search """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (&(ou=ouX)(y=a)) " )
self . assertEqual ( len ( res11 ) , 0 )
2017-09-13 06:49:43 +03:00
def test_subtree_and_idx_record ( self ) :
""" Testing a search against the index record """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (@IDXDN=DC=SAMBA,DC=ORG) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_subtree_and_idxone_record ( self ) :
""" Testing a search against the index record """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (@IDXONE=DC=SAMBA,DC=ORG) " )
self . assertEqual ( len ( res11 ) , 0 )
2018-05-23 08:15:38 +03:00
def test_subtree_unindexable ( self ) :
""" Testing a search """
try :
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (y=b*) " )
if hasattr ( self , ' IDX ' ) and \
hasattr ( self , ' IDXCHECK ' ) :
self . fail ( " Should have failed as un-indexed search " )
self . assertEqual ( len ( res11 ) , 9 )
except ldb . LdbError as err :
enum = err . args [ 0 ]
estr = err . args [ 1 ]
self . assertEqual ( enum , ldb . ERR_INAPPROPRIATE_MATCHING )
self . assertIn ( estr , " ldb FULL SEARCH disabled " )
def test_subtree_unindexable_presence ( self ) :
""" Testing a search """
try :
res11 = self . l . search ( base = " DC=samba,DC=org " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (y=*) " )
if hasattr ( self , ' IDX ' ) and \
hasattr ( self , ' IDXCHECK ' ) :
self . fail ( " Should have failed as un-indexed search " )
self . assertEqual ( len ( res11 ) , 24 )
except ldb . LdbError as err :
enum = err . args [ 0 ]
estr = err . args [ 1 ]
self . assertEqual ( enum , ldb . ERR_INAPPROPRIATE_MATCHING )
self . assertIn ( estr , " ldb FULL SEARCH disabled " )
2017-12-18 06:22:01 +03:00
def test_dn_filter_one ( self ) :
""" Testing that a dn= filter succeeds
( or fails with disallowDNFilter
set and IDXGUID or ( IDX and not IDXONE ) mode )
when the scope is SCOPE_ONELEVEL .
This should be made more consistent , but for now lock in
the behaviour
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (dn=OU=OU1,DC=SAMBA,DC=ORG) " )
if hasattr ( self , ' disallowDNFilter ' ) and \
hasattr ( self , ' IDX ' ) and \
2018-07-30 09:22:01 +03:00
( hasattr ( self , ' IDXGUID ' ) or
2017-12-18 06:22:01 +03:00
( ( hasattr ( self , ' IDXONE ' ) == False and hasattr ( self , ' IDX ' ) ) ) ) :
self . assertEqual ( len ( res11 ) , 0 )
else :
self . assertEqual ( len ( res11 ) , 1 )
def test_dn_filter_subtree ( self ) :
""" Testing that a dn= filter succeeds
( or fails with disallowDNFilter set )
when the scope is SCOPE_SUBTREE """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (dn=OU=OU1,DC=SAMBA,DC=ORG) " )
if hasattr ( self , ' disallowDNFilter ' ) \
and hasattr ( self , ' IDX ' ) :
self . assertEqual ( len ( res11 ) , 0 )
else :
self . assertEqual ( len ( res11 ) , 1 )
def test_dn_filter_base ( self ) :
""" Testing that (incorrectly) a dn= filter works
when the scope is SCOPE_BASE """
res11 = self . l . search ( base = " OU=OU1,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (dn=OU=OU1,DC=SAMBA,DC=ORG) " )
# At some point we should fix this, but it isn't trivial
self . assertEqual ( len ( res11 ) , 1 )
2018-05-21 06:25:58 +03:00
def test_distinguishedName_filter_one ( self ) :
""" Testing that a distinguishedName= filter succeeds
when the scope is SCOPE_ONELEVEL .
This should be made more consistent , but for now lock in
the behaviour
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DC=ORG) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_distinguishedName_filter_subtree ( self ) :
""" Testing that a distinguishedName= filter succeeds
when the scope is SCOPE_SUBTREE """
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DC=ORG) " )
self . assertEqual ( len ( res11 ) , 1 )
def test_distinguishedName_filter_base ( self ) :
""" Testing that (incorrectly) a distinguishedName= filter works
when the scope is SCOPE_BASE """
res11 = self . l . search ( base = " OU=OU1,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DC=ORG) " )
# At some point we should fix this, but it isn't trivial
self . assertEqual ( len ( res11 ) , 1 )
def test_bad_dn_filter_base ( self ) :
""" Testing that a dn= filter on an invalid DN works
when the scope is SCOPE_BASE but
returns zero results """
res11 = self . l . search ( base = " OU=OU1,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (dn=OU=OU1,DC=SAMBA,DCXXXX) " )
# At some point we should fix this, but it isn't trivial
self . assertEqual ( len ( res11 ) , 0 )
def test_bad_dn_filter_one ( self ) :
""" Testing that a dn= filter succeeds but returns zero
results when the DN is not valid on a SCOPE_ONELEVEL search
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (dn=OU=OU1,DC=SAMBA,DCXXXX) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_bad_dn_filter_subtree ( self ) :
""" Testing that a dn= filter succeeds but returns zero
results when the DN is not valid on a SCOPE_SUBTREE search
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (dn=OU=OU1,DC=SAMBA,DCXXXX) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_bad_distinguishedName_filter_base ( self ) :
""" Testing that a distinguishedName= filter on an invalid DN works
when the scope is SCOPE_BASE but
returns zero results """
res11 = self . l . search ( base = " OU=OU1,DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_BASE ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DCXXXX) " )
# At some point we should fix this, but it isn't trivial
self . assertEqual ( len ( res11 ) , 0 )
def test_bad_distinguishedName_filter_one ( self ) :
""" Testing that a distinguishedName= filter succeeds but returns zero
results when the DN is not valid on a SCOPE_ONELEVEL search
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_ONELEVEL ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DCXXXX) " )
self . assertEqual ( len ( res11 ) , 0 )
def test_bad_distinguishedName_filter_subtree ( self ) :
""" Testing that a distinguishedName= filter succeeds but returns zero
results when the DN is not valid on a SCOPE_SUBTREE search
"""
res11 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (distinguishedName=OU=OU1,DC=SAMBA,DCXXXX) " )
self . assertEqual ( len ( res11 ) , 0 )
2018-07-04 04:26:16 +03:00
def test_bad_dn_search_base ( self ) :
""" Testing with a bad base DN (SCOPE_BASE) """
try :
res11 = self . l . search ( base = " OU=OU1,DC=SAMBA,DCXXX " ,
scope = ldb . SCOPE_BASE )
self . fail ( " Should have failed with ERR_INVALID_DN_SYNTAX " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
def test_bad_dn_search_one ( self ) :
""" Testing with a bad base DN (SCOPE_ONELEVEL) """
try :
res11 = self . l . search ( base = " DC=SAMBA,DCXXXX " ,
2018-09-03 16:05:52 +03:00
scope = ldb . SCOPE_ONELEVEL )
2018-07-04 04:26:16 +03:00
self . fail ( " Should have failed with ERR_INVALID_DN_SYNTAX " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
def test_bad_dn_search_subtree ( self ) :
""" Testing with a bad base DN (SCOPE_SUBTREE) """
try :
res11 = self . l . search ( base = " DC=SAMBA,DCXXXX " ,
scope = ldb . SCOPE_SUBTREE )
self . fail ( " Should have failed with ERR_INVALID_DN_SYNTAX " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
2017-12-18 06:22:01 +03:00
2018-03-05 23:13:31 +03:00
# Run the search tests against an lmdb backend
class SearchTestsLmdb ( SearchTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
self . index = MDB_INDEX_OBJ
super ( SearchTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( SearchTestsLmdb , self ) . tearDown ( )
2017-08-24 08:01:14 +03:00
class IndexedSearchTests ( SearchTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-08-24 08:01:14 +03:00
def setUp ( self ) :
super ( IndexedSearchTests , self ) . setUp ( )
2017-12-18 06:22:01 +03:00
self . l . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] } )
self . IDX = True
2018-07-30 09:20:39 +03:00
2018-05-23 08:15:38 +03:00
class IndexedCheckSearchTests ( IndexedSearchTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things ( full scan disabled ) """
2018-07-30 09:19:59 +03:00
2018-05-23 08:15:38 +03:00
def setUp ( self ) :
self . IDXCHECK = True
super ( IndexedCheckSearchTests , self ) . setUp ( )
2018-07-30 09:20:39 +03:00
2017-12-18 06:22:01 +03:00
class IndexedSearchDnFilterTests ( SearchTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-12-18 06:22:01 +03:00
def setUp ( self ) :
super ( IndexedSearchDnFilterTests , self ) . setUp ( )
self . l . add ( { " dn " : " @OPTIONS " ,
" disallowDNFilter " : " TRUE " } )
self . disallowDNFilter = True
self . l . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] } )
self . IDX = True
2018-07-30 09:20:39 +03:00
2017-12-18 06:22:01 +03:00
class IndexedAndOneLevelSearchTests ( SearchTests ) :
""" Test searches using the index including @IDXONE, to ensure
the index doesn ' t break things " " "
2018-07-30 09:19:59 +03:00
2017-12-18 06:22:01 +03:00
def setUp ( self ) :
super ( IndexedAndOneLevelSearchTests , self ) . setUp ( )
2017-08-24 08:01:14 +03:00
self . l . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXONE " : [ b " 1 " ] } )
2017-12-18 06:22:01 +03:00
self . IDX = True
2018-05-23 08:15:38 +03:00
self . IDXONE = True
2018-07-30 09:20:39 +03:00
2018-05-23 08:15:38 +03:00
class IndexedCheckedAndOneLevelSearchTests ( IndexedAndOneLevelSearchTests ) :
""" Test searches using the index including @IDXONE, to ensure
the index doesn ' t break things (full scan disabled) " " "
2018-07-30 09:19:59 +03:00
2018-05-23 08:15:38 +03:00
def setUp ( self ) :
self . IDXCHECK = True
super ( IndexedCheckedAndOneLevelSearchTests , self ) . setUp ( )
2017-12-18 06:22:01 +03:00
2018-07-30 09:20:39 +03:00
2017-12-18 06:22:01 +03:00
class IndexedAndOneLevelDNFilterSearchTests ( SearchTests ) :
""" Test searches using the index including @IDXONE, to ensure
the index doesn ' t break things " " "
2018-07-30 09:19:59 +03:00
2017-12-18 06:22:01 +03:00
def setUp ( self ) :
super ( IndexedAndOneLevelDNFilterSearchTests , self ) . setUp ( )
self . l . add ( { " dn " : " @OPTIONS " ,
2018-07-04 04:26:16 +03:00
" disallowDNFilter " : " TRUE " ,
" checkBaseOnSearch " : " TRUE " } )
2017-12-18 06:22:01 +03:00
self . disallowDNFilter = True
2018-07-04 04:26:16 +03:00
self . checkBaseOnSearch = True
2017-12-18 06:22:01 +03:00
self . l . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXONE " : [ b " 1 " ] } )
self . IDX = True
self . IDXONE = True
2017-08-24 08:01:14 +03:00
2018-07-30 09:20:39 +03:00
2017-08-25 06:34:31 +03:00
class GUIDIndexedSearchTests ( SearchTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-08-25 06:34:31 +03:00
def setUp ( self ) :
2018-03-06 05:30:10 +03:00
self . index = { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ] }
2017-08-25 06:34:31 +03:00
super ( GUIDIndexedSearchTests , self ) . setUp ( )
2017-12-18 06:22:01 +03:00
self . IDXGUID = True
self . IDXONE = True
2018-03-05 23:13:31 +03:00
2017-12-18 06:22:01 +03:00
class GUIDIndexedDNFilterSearchTests ( SearchTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-12-18 06:22:01 +03:00
def setUp ( self ) :
2018-03-06 05:30:10 +03:00
self . index = { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ] }
2017-12-18 06:22:01 +03:00
super ( GUIDIndexedDNFilterSearchTests , self ) . setUp ( )
self . l . add ( { " dn " : " @OPTIONS " ,
2018-07-04 04:26:16 +03:00
" disallowDNFilter " : " TRUE " ,
" checkBaseOnSearch " : " TRUE " } )
2017-12-18 06:22:01 +03:00
self . disallowDNFilter = True
2018-07-04 04:26:16 +03:00
self . checkBaseOnSearch = True
2017-12-18 06:22:01 +03:00
self . IDX = True
self . IDXGUID = True
2018-07-30 09:20:39 +03:00
2017-12-18 06:22:01 +03:00
class GUIDAndOneLevelIndexedSearchTests ( SearchTests ) :
""" Test searches using the index including @IDXONE, to ensure
the index doesn ' t break things " " "
2018-07-30 09:19:59 +03:00
2017-12-18 06:22:01 +03:00
def setUp ( self ) :
2018-03-06 05:30:10 +03:00
self . index = { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ] }
2017-12-18 06:22:01 +03:00
super ( GUIDAndOneLevelIndexedSearchTests , self ) . setUp ( )
self . l . add ( { " dn " : " @OPTIONS " ,
2018-07-04 04:26:16 +03:00
" disallowDNFilter " : " TRUE " ,
" checkBaseOnSearch " : " TRUE " } )
2017-12-18 06:22:01 +03:00
self . disallowDNFilter = True
2018-07-04 04:26:16 +03:00
self . checkBaseOnSearch = True
2017-12-18 06:22:01 +03:00
self . IDX = True
self . IDXGUID = True
self . IDXONE = True
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class GUIDIndexedSearchTestsLmdb ( GUIDIndexedSearchTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
super ( GUIDIndexedSearchTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( GUIDIndexedSearchTestsLmdb , self ) . tearDown ( )
class GUIDIndexedDNFilterSearchTestsLmdb ( GUIDIndexedDNFilterSearchTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
super ( GUIDIndexedDNFilterSearchTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( GUIDIndexedDNFilterSearchTestsLmdb , self ) . tearDown ( )
class GUIDAndOneLevelIndexedSearchTestsLmdb ( GUIDAndOneLevelIndexedSearchTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
super ( GUIDAndOneLevelIndexedSearchTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( GUIDAndOneLevelIndexedSearchTestsLmdb , self ) . tearDown ( )
2017-08-25 06:34:31 +03:00
2018-03-05 23:13:31 +03:00
class AddModifyTests ( LdbBaseTest ) :
2017-08-25 13:22:27 +03:00
def tearDown ( self ) :
shutil . rmtree ( self . testdir )
super ( AddModifyTests , self ) . tearDown ( )
2017-09-21 05:15:32 +03:00
# Ensure the LDB is closed now, so we close the FD
del ( self . l )
2017-08-25 13:22:27 +03:00
def setUp ( self ) :
super ( AddModifyTests , self ) . setUp ( )
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " add_test.ldb " )
2018-03-05 23:13:31 +03:00
self . l = ldb . Ldb ( self . url ( ) ,
flags = self . flags ( ) ,
options = [ " modules:rdn_name " ] )
2018-03-06 05:30:10 +03:00
try :
self . l . add ( self . index )
except AttributeError :
pass
2017-08-25 13:22:27 +03:00
self . l . add ( { " dn " : " DC=SAMBA,DC=ORG " ,
" name " : b " samba.org " ,
" objectUUID " : b " 0123456789abcdef " } )
2017-09-13 02:28:23 +03:00
self . l . add ( { " dn " : " @ATTRIBUTES " ,
" objectUUID " : " UNIQUE_INDEX " } )
2017-08-25 13:22:27 +03:00
def test_add_dup ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
try :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
self . fail ( " Should have failed adding dupliate entry " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
2018-05-21 06:25:58 +03:00
def test_add_bad ( self ) :
try :
self . l . add ( { " dn " : " BAD,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . fail ( " Should have failed adding entry with invalid DN " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
2017-08-25 13:22:27 +03:00
def test_add_del_add ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . l . delete ( " OU=DUP,DC=SAMBA,DC=ORG " )
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
def test_add_move_add ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
2017-09-13 02:28:23 +03:00
def test_add_move_fail_move_move ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
2018-03-20 02:14:10 +03:00
res2 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (objectUUID=0123456789abcde1) " )
self . assertEqual ( len ( res2 ) , 1 )
self . assertEqual ( str ( res2 [ 0 ] . dn ) , " OU=DUP,DC=SAMBA,DC=ORG " )
res3 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (objectUUID=0123456789abcde2) " )
self . assertEqual ( len ( res3 ) , 1 )
self . assertEqual ( str ( res3 [ 0 ] . dn ) , " OU=DUP2,DC=SAMBA,DC=ORG " )
2017-09-13 02:28:23 +03:00
try :
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on duplicate DN " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
self . l . rename ( " OU=DUP2,DC=SAMBA,DC=ORG " ,
" OU=DUP3,DC=SAMBA,DC=ORG " )
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
res2 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (objectUUID=0123456789abcde1) " )
self . assertEqual ( len ( res2 ) , 1 )
self . assertEqual ( str ( res2 [ 0 ] . dn ) , " OU=DUP2,DC=SAMBA,DC=ORG " )
res3 = self . l . search ( base = " DC=SAMBA,DC=ORG " ,
scope = ldb . SCOPE_SUBTREE ,
expression = " (objectUUID=0123456789abcde2) " )
self . assertEqual ( len ( res3 ) , 1 )
self . assertEqual ( str ( res3 [ 0 ] . dn ) , " OU=DUP3,DC=SAMBA,DC=ORG " )
def test_move_missing ( self ) :
try :
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on missing " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_NO_SUCH_OBJECT )
def test_move_missing2 ( self ) :
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
try :
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on missing " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_NO_SUCH_OBJECT )
2018-05-21 06:25:58 +03:00
def test_move_bad ( self ) :
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
try :
self . l . rename ( " OUXDUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on invalid DN " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
def test_move_bad2 ( self ) :
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
try :
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OUXDUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on missing " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_INVALID_DN_SYNTAX )
2017-09-13 02:28:23 +03:00
def test_move_fail_move_add ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
try :
self . l . rename ( " OU=DUP,DC=SAMBA,DC=ORG " ,
" OU=DUP2,DC=SAMBA,DC=ORG " )
self . fail ( " Should have failed on duplicate DN " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
self . l . rename ( " OU=DUP2,DC=SAMBA,DC=ORG " ,
" OU=DUP3,DC=SAMBA,DC=ORG " )
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde3 " } )
2017-08-25 13:22:27 +03:00
2018-03-05 23:13:31 +03:00
2018-03-05 23:13:31 +03:00
class AddModifyTestsLmdb ( AddModifyTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
self . index = MDB_INDEX_OBJ
super ( AddModifyTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( AddModifyTestsLmdb , self ) . tearDown ( )
2018-07-30 09:20:39 +03:00
2017-08-25 13:22:27 +03:00
class IndexedAddModifyTests ( AddModifyTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-08-25 13:22:27 +03:00
def setUp ( self ) :
2018-03-06 05:30:10 +03:00
if not hasattr ( self , ' index ' ) :
self . index = { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " , b " objectUUID " ] ,
" @IDXONE " : [ b " 1 " ] }
2017-08-25 13:22:27 +03:00
super ( IndexedAddModifyTests , self ) . setUp ( )
def test_duplicate_GUID ( self ) :
try :
self . l . add ( { " dn " : " OU=DUPGUID,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcdef " } )
self . fail ( " Should have failed adding dupliate GUID " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_CONSTRAINT_VIOLATION )
2017-09-13 02:28:23 +03:00
def test_duplicate_name_dup_GUID ( self ) :
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " a123456789abcdef " } )
try :
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " a123456789abcdef " } )
self . fail ( " Should have failed adding dupliate GUID " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
def test_duplicate_name_dup_GUID2 ( self ) :
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " abc3456789abcdef " } )
try :
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " aaa3456789abcdef " } )
self . fail ( " Should have failed adding dupliate DN " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_ENTRY_ALREADY_EXISTS )
# Checking the GUID didn't stick in the index
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " aaa3456789abcdef " } )
def test_add_dup_guid_add ( self ) :
self . l . add ( { " dn " : " OU=DUP,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
try :
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . fail ( " Should have failed on duplicate GUID " )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_CONSTRAINT_VIOLATION )
self . l . add ( { " dn " : " OU=DUP2,DC=SAMBA,DC=ORG " ,
" name " : b " Admins " ,
" x " : " z " , " y " : " a " ,
" objectUUID " : b " 0123456789abcde2 " } )
2018-07-30 09:20:39 +03:00
2017-09-13 02:28:23 +03:00
class GUIDIndexedAddModifyTests ( IndexedAddModifyTests ) :
""" Test searches using the index, to ensure the index doesn ' t
break things """
2018-07-30 09:19:59 +03:00
2017-09-13 02:28:23 +03:00
def setUp ( self ) :
2018-03-06 05:30:10 +03:00
self . index = { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXONE " : [ b " 1 " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ] }
2017-09-13 02:28:23 +03:00
super ( GUIDIndexedAddModifyTests , self ) . setUp ( )
class GUIDTransIndexedAddModifyTests ( GUIDIndexedAddModifyTests ) :
""" Test GUID index behaviour insdie the transaction """
2018-07-30 09:19:59 +03:00
2017-09-13 02:28:23 +03:00
def setUp ( self ) :
super ( GUIDTransIndexedAddModifyTests , self ) . setUp ( )
self . l . transaction_start ( )
def tearDown ( self ) :
self . l . transaction_commit ( )
super ( GUIDTransIndexedAddModifyTests , self ) . tearDown ( )
2018-07-30 09:20:39 +03:00
2017-09-13 02:28:23 +03:00
class TransIndexedAddModifyTests ( IndexedAddModifyTests ) :
""" Test index behaviour insdie the transaction """
2018-07-30 09:19:59 +03:00
2017-09-13 02:28:23 +03:00
def setUp ( self ) :
super ( TransIndexedAddModifyTests , self ) . setUp ( )
self . l . transaction_start ( )
def tearDown ( self ) :
self . l . transaction_commit ( )
super ( TransIndexedAddModifyTests , self ) . tearDown ( )
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class GuidIndexedAddModifyTestsLmdb ( GUIDIndexedAddModifyTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
super ( GuidIndexedAddModifyTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( GuidIndexedAddModifyTestsLmdb , self ) . tearDown ( )
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class GuidTransIndexedAddModifyTestsLmdb ( GUIDTransIndexedAddModifyTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
super ( GuidTransIndexedAddModifyTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( GuidTransIndexedAddModifyTestsLmdb , self ) . tearDown ( )
2017-08-24 08:01:14 +03:00
2018-07-30 09:20:39 +03:00
2018-03-26 06:07:45 +03:00
class BadIndexTests ( LdbBaseTest ) :
def setUp ( self ) :
super ( BadIndexTests , self ) . setUp ( )
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " test.ldb " )
self . ldb = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
if hasattr ( self , ' IDXGUID ' ) :
self . ldb . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] ,
" @IDXGUID " : [ b " objectUUID " ] ,
" @IDX_DN_GUID " : [ b " GUID " ] } )
else :
self . ldb . add ( { " dn " : " @INDEXLIST " ,
" @IDXATTR " : [ b " x " , b " y " , b " ou " ] } )
super ( BadIndexTests , self ) . setUp ( )
def test_unique ( self ) :
self . ldb . add ( { " dn " : " x=x,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde1 " ,
" y " : " 1 " } )
self . ldb . add ( { " dn " : " x=y,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde2 " ,
" y " : " 1 " } )
self . ldb . add ( { " dn " : " x=z,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde3 " ,
" y " : " 1 " } )
res = self . ldb . search ( expression = " (y=1) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 3 )
# Now set this to unique index, but forget to check the result
try :
self . ldb . add ( { " dn " : " @ATTRIBUTES " ,
2018-07-30 09:16:12 +03:00
" y " : " UNIQUE_INDEX " } )
2018-03-26 06:07:45 +03:00
self . fail ( )
except ldb . LdbError :
pass
# We must still have a working index
res = self . ldb . search ( expression = " (y=1) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 3 )
def test_unique_transaction ( self ) :
self . ldb . add ( { " dn " : " x=x,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde1 " ,
" y " : " 1 " } )
self . ldb . add ( { " dn " : " x=y,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde2 " ,
" y " : " 1 " } )
self . ldb . add ( { " dn " : " x=z,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde3 " ,
" y " : " 1 " } )
res = self . ldb . search ( expression = " (y=1) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 3 )
self . ldb . transaction_start ( )
# Now set this to unique index, but forget to check the result
try :
self . ldb . add ( { " dn " : " @ATTRIBUTES " ,
2018-07-30 09:16:12 +03:00
" y " : " UNIQUE_INDEX " } )
2018-03-26 06:07:45 +03:00
except ldb . LdbError :
pass
try :
self . ldb . transaction_commit ( )
self . fail ( )
except ldb . LdbError as err :
enum = err . args [ 0 ]
self . assertEqual ( enum , ldb . ERR_OPERATIONS_ERROR )
# We must still have a working index
res = self . ldb . search ( expression = " (y=1) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 3 )
def test_casefold ( self ) :
self . ldb . add ( { " dn " : " x=x,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde1 " ,
" y " : " a " } )
self . ldb . add ( { " dn " : " x=y,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde2 " ,
" y " : " A " } )
self . ldb . add ( { " dn " : " x=z,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde3 " ,
" y " : [ " a " , " A " ] } )
res = self . ldb . search ( expression = " (y=a) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 2 )
self . ldb . add ( { " dn " : " @ATTRIBUTES " ,
" y " : " CASE_INSENSITIVE " } )
# We must still have a working index
res = self . ldb . search ( expression = " (y=a) " ,
base = " dc=samba,dc=org " )
if hasattr ( self , ' IDXGUID ' ) :
self . assertEquals ( len ( res ) , 3 )
else :
# We should not return this entry twice, but sadly
# we have not yet fixed
# https://bugzilla.samba.org/show_bug.cgi?id=13361
self . assertEquals ( len ( res ) , 4 )
def test_casefold_transaction ( self ) :
self . ldb . add ( { " dn " : " x=x,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde1 " ,
" y " : " a " } )
self . ldb . add ( { " dn " : " x=y,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde2 " ,
" y " : " A " } )
self . ldb . add ( { " dn " : " x=z,dc=samba,dc=org " ,
" objectUUID " : b " 0123456789abcde3 " ,
" y " : [ " a " , " A " ] } )
res = self . ldb . search ( expression = " (y=a) " ,
base = " dc=samba,dc=org " )
self . assertEquals ( len ( res ) , 2 )
self . ldb . transaction_start ( )
self . ldb . add ( { " dn " : " @ATTRIBUTES " ,
" y " : " CASE_INSENSITIVE " } )
self . ldb . transaction_commit ( )
# We must still have a working index
res = self . ldb . search ( expression = " (y=a) " ,
base = " dc=samba,dc=org " )
if hasattr ( self , ' IDXGUID ' ) :
self . assertEquals ( len ( res ) , 3 )
else :
# We should not return this entry twice, but sadly
# we have not yet fixed
# https://bugzilla.samba.org/show_bug.cgi?id=13361
self . assertEquals ( len ( res ) , 4 )
def tearDown ( self ) :
super ( BadIndexTests , self ) . tearDown ( )
class GUIDBadIndexTests ( BadIndexTests ) :
""" Test Bad index things with GUID index mode """
2018-07-30 09:19:59 +03:00
2018-03-26 06:07:45 +03:00
def setUp ( self ) :
self . IDXGUID = True
super ( GUIDBadIndexTests , self ) . setUp ( )
2018-07-30 09:20:39 +03:00
2011-11-10 23:20:00 +04:00
class DnTests ( TestCase ) :
2009-06-17 20:25:21 +04:00
2007-11-21 13:47:55 +03:00
def setUp ( self ) :
2011-11-10 23:20:00 +04:00
super ( DnTests , self ) . setUp ( )
2017-09-21 04:47:49 +03:00
self . ldb = ldb . Ldb ( )
2007-11-21 13:47:55 +03:00
2017-09-21 05:15:32 +03:00
def tearDown ( self ) :
super ( DnTests , self ) . tearDown ( )
del ( self . ldb )
2009-08-03 20:15:16 +04:00
def test_set_dn_invalid ( self ) :
x = ldb . Message ( )
2018-07-30 09:21:32 +03:00
2009-08-03 20:15:16 +04:00
def assign ( ) :
x . dn = " astring "
self . assertRaises ( TypeError , assign )
2008-12-21 00:21:39 +03:00
def test_eq ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo11,bar=bloe " )
y = ldb . Dn ( self . ldb , " dc=foo11,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( x , y )
2010-11-18 11:22:07 +03:00
y = ldb . Dn ( self . ldb , " dc=foo11,bar=blie " )
2015-06-10 11:21:24 +03:00
self . assertNotEqual ( x , y )
2008-01-11 06:05:41 +03:00
2007-11-21 13:47:55 +03:00
def test_str ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo12,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( x . __str__ ( ) , " dc=foo12,bar=bloe " )
2007-11-21 13:47:55 +03:00
2008-05-20 01:07:04 +04:00
def test_repr ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo13,bla=blie " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( x . __repr__ ( ) , " Dn( ' dc=foo13,bla=blie ' ) " )
2008-05-20 01:07:04 +04:00
2018-10-11 06:47:44 +03:00
def test_get_casefold_2 ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo14,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( x . get_casefold ( ) , " DC=FOO14,BAR=bloe " )
2007-11-21 13:47:55 +03:00
def test_validate ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo15,bar=bloe " )
2007-11-21 13:47:55 +03:00
self . assertTrue ( x . validate ( ) )
def test_parent ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo16,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " bar=bloe " , x . parent ( ) . __str__ ( ) )
2007-11-21 13:47:55 +03:00
2012-06-11 19:36:38 +04:00
def test_parent_nonexistent ( self ) :
2009-06-17 20:25:21 +04:00
x = ldb . Dn ( self . ldb , " @BLA " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , x . parent ( ) )
2007-11-21 13:47:55 +03:00
def test_is_valid ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo18,dc=bloe " )
2007-11-21 13:47:55 +03:00
self . assertTrue ( x . is_valid ( ) )
x = ldb . Dn ( self . ldb , " " )
2010-11-18 11:22:07 +03:00
self . assertTrue ( x . is_valid ( ) )
2007-11-21 13:47:55 +03:00
def test_is_special ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo19,bar=bloe " )
2007-11-21 13:47:55 +03:00
self . assertFalse ( x . is_special ( ) )
x = ldb . Dn ( self . ldb , " @FOOBAR " )
self . assertTrue ( x . is_special ( ) )
def test_check_special ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo20,bar=bloe " )
2007-11-21 13:47:55 +03:00
self . assertFalse ( x . check_special ( " FOOBAR " ) )
x = ldb . Dn ( self . ldb , " @FOOBAR " )
self . assertTrue ( x . check_special ( " @FOOBAR " ) )
def test_len ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo21,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 2 , len ( x ) )
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo21 " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( x ) )
2007-11-21 13:47:55 +03:00
def test_add_child ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo22,bar=bloe " )
2007-11-21 13:47:55 +03:00
self . assertTrue ( x . add_child ( ldb . Dn ( self . ldb , " bla=bloe " ) ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " bla=bloe,dc=foo22,bar=bloe " , x . __str__ ( ) )
2007-11-21 13:47:55 +03:00
def test_add_base ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo23,bar=bloe " )
2008-12-21 00:21:39 +03:00
base = ldb . Dn ( self . ldb , " bla=bloe " )
self . assertTrue ( x . add_base ( base ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " dc=foo23,bar=bloe,bla=bloe " , x . __str__ ( ) )
2007-11-21 13:47:55 +03:00
2015-10-14 03:49:01 +03:00
def test_add_child_str ( self ) :
x = ldb . Dn ( self . ldb , " dc=foo22,bar=bloe " )
self . assertTrue ( x . add_child ( " bla=bloe " ) )
2016-02-14 18:50:38 +03:00
self . assertEqual ( " bla=bloe,dc=foo22,bar=bloe " , x . __str__ ( ) )
2015-10-14 03:49:01 +03:00
def test_add_base_str ( self ) :
x = ldb . Dn ( self . ldb , " dc=foo23,bar=bloe " )
base = " bla=bloe "
self . assertTrue ( x . add_base ( base ) )
2016-02-14 18:50:38 +03:00
self . assertEqual ( " dc=foo23,bar=bloe,bla=bloe " , x . __str__ ( ) )
2015-10-14 03:49:01 +03:00
2007-11-21 13:47:55 +03:00
def test_add ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo24 " )
2007-11-21 13:47:55 +03:00
y = ldb . Dn ( self . ldb , " bar=bla " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " dc=foo24,bar=bla " , str ( x + y ) )
2007-11-21 13:47:55 +03:00
2012-07-06 13:57:10 +04:00
def test_remove_base_components ( self ) :
x = ldb . Dn ( self . ldb , " dc=foo24,dc=samba,dc=org " )
2018-07-30 09:18:25 +03:00
x . remove_base_components ( len ( x ) - 1 )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " dc=foo24 " , str ( x ) )
2012-07-06 13:57:10 +04:00
2007-11-21 13:47:55 +03:00
def test_parse_ldif ( self ) :
msgs = self . ldb . parse_ldif ( " dn: foo=bar \n " )
2015-06-09 11:36:26 +03:00
msg = next ( msgs )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " foo=bar " , str ( msg [ 1 ] . dn ) )
2007-11-21 13:47:55 +03:00
self . assertTrue ( isinstance ( msg [ 1 ] , ldb . Message ) )
2009-08-26 09:59:00 +04:00
ldif = self . ldb . write_ldif ( msg [ 1 ] , ldb . CHANGETYPE_NONE )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " dn: foo=bar \n \n " , ldif )
2010-12-21 16:05:18 +03:00
2007-11-21 13:47:55 +03:00
def test_parse_ldif_more ( self ) :
msgs = self . ldb . parse_ldif ( " dn: foo=bar \n \n \n dn: bar=bar " )
2015-06-09 11:36:26 +03:00
msg = next ( msgs )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " foo=bar " , str ( msg [ 1 ] . dn ) )
2015-06-09 11:36:26 +03:00
msg = next ( msgs )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " bar=bar " , str ( msg [ 1 ] . dn ) )
2007-11-21 13:47:55 +03:00
def test_canonical_string ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo25,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " /bloe/foo25 " , x . canonical_str ( ) )
2007-11-21 13:47:55 +03:00
def test_canonical_ex_string ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo26,bar=bloe " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " /bloe \n foo26 " , x . canonical_ex_str ( ) )
2010-11-18 18:37:10 +03:00
2011-07-14 07:17:49 +04:00
def test_ldb_is_child_of ( self ) :
2011-06-11 18:31:28 +04:00
""" Testing ldb_dn_compare_dn """
dn1 = ldb . Dn ( self . ldb , " dc=base " )
dn2 = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
dn3 = ldb . Dn ( self . ldb , " cn=bar,dc=base " )
dn4 = ldb . Dn ( self . ldb , " cn=baz,cn=bar,dc=base " )
2017-12-20 17:55:50 +03:00
self . assertTrue ( dn1 . is_child_of ( dn1 ) )
2011-07-14 07:17:49 +04:00
self . assertTrue ( dn2 . is_child_of ( dn1 ) )
self . assertTrue ( dn4 . is_child_of ( dn1 ) )
self . assertTrue ( dn4 . is_child_of ( dn3 ) )
2017-12-20 17:55:50 +03:00
self . assertTrue ( dn4 . is_child_of ( dn4 ) )
2011-07-14 07:17:49 +04:00
self . assertFalse ( dn3 . is_child_of ( dn2 ) )
self . assertFalse ( dn1 . is_child_of ( dn4 ) )
2007-11-21 13:47:55 +03:00
2015-10-14 03:49:01 +03:00
def test_ldb_is_child_of_str ( self ) :
""" Testing ldb_dn_compare_dn """
dn1_str = " dc=base "
dn2_str = " cn=foo,dc=base "
dn3_str = " cn=bar,dc=base "
dn4_str = " cn=baz,cn=bar,dc=base "
dn1 = ldb . Dn ( self . ldb , dn1_str )
dn2 = ldb . Dn ( self . ldb , dn2_str )
dn3 = ldb . Dn ( self . ldb , dn3_str )
dn4 = ldb . Dn ( self . ldb , dn4_str )
2017-12-20 17:55:50 +03:00
self . assertTrue ( dn1 . is_child_of ( dn1_str ) )
2015-10-14 03:49:01 +03:00
self . assertTrue ( dn2 . is_child_of ( dn1_str ) )
self . assertTrue ( dn4 . is_child_of ( dn1_str ) )
self . assertTrue ( dn4 . is_child_of ( dn3_str ) )
2017-12-20 17:55:50 +03:00
self . assertTrue ( dn4 . is_child_of ( dn4_str ) )
2015-10-14 03:49:01 +03:00
self . assertFalse ( dn3 . is_child_of ( dn2_str ) )
self . assertFalse ( dn1 . is_child_of ( dn4_str ) )
2015-08-21 11:22:22 +03:00
def test_get_component_name ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_component_name ( 0 ) , ' cn ' )
self . assertEqual ( dn . get_component_name ( 1 ) , ' dc ' )
self . assertEqual ( dn . get_component_name ( 2 ) , None )
self . assertEqual ( dn . get_component_name ( - 1 ) , None )
def test_get_component_value ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_component_value ( 0 ) , ' foo ' )
self . assertEqual ( dn . get_component_value ( 1 ) , ' base ' )
self . assertEqual ( dn . get_component_name ( 2 ) , None )
self . assertEqual ( dn . get_component_name ( - 1 ) , None )
def test_set_component ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
dn . set_component ( 0 , ' cn ' , ' bar ' )
self . assertEqual ( str ( dn ) , " cn=bar,dc=base " )
dn . set_component ( 1 , ' o ' , ' asep ' )
self . assertEqual ( str ( dn ) , " cn=bar,o=asep " )
self . assertRaises ( TypeError , dn . set_component , 2 , ' dc ' , ' base ' )
self . assertEqual ( str ( dn ) , " cn=bar,o=asep " )
dn . set_component ( 1 , ' o ' , ' a,b+c ' )
self . assertEqual ( str ( dn ) , r " cn=bar,o=a \ ,b \ +c " )
def test_set_component_bytes ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
dn . set_component ( 0 , ' cn ' , b ' bar ' )
self . assertEqual ( str ( dn ) , " cn=bar,dc=base " )
dn . set_component ( 1 , ' o ' , b ' asep ' )
self . assertEqual ( str ( dn ) , " cn=bar,o=asep " )
def test_set_component_none ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,cn=bar,dc=base " )
self . assertRaises ( TypeError , dn . set_component , 1 , ' cn ' , None )
def test_get_extended_component_null ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,cn=bar,dc=base " )
self . assertEqual ( dn . get_extended_component ( " TEST " ) , None )
def test_get_extended_component ( self ) :
self . ldb . _register_test_extensions ( )
dn = ldb . Dn ( self . ldb , " <TEST=foo>;cn=bar,dc=base " )
self . assertEqual ( dn . get_extended_component ( " TEST " ) , b " foo " )
def test_set_extended_component ( self ) :
self . ldb . _register_test_extensions ( )
dn = ldb . Dn ( self . ldb , " dc=base " )
dn . set_extended_component ( " TEST " , " foo " )
self . assertEqual ( dn . get_extended_component ( " TEST " ) , b " foo " )
dn . set_extended_component ( " TEST " , b " bar " )
self . assertEqual ( dn . get_extended_component ( " TEST " ) , b " bar " )
def test_extended_str ( self ) :
self . ldb . _register_test_extensions ( )
dn = ldb . Dn ( self . ldb , " <TEST=foo>;cn=bar,dc=base " )
self . assertEqual ( dn . extended_str ( ) , " <TEST=foo>;cn=bar,dc=base " )
def test_get_rdn_name ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_rdn_name ( ) , ' cn ' )
def test_get_rdn_value ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_rdn_value ( ) , ' foo ' )
def test_get_casefold ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_casefold ( ) , ' CN=FOO,DC=BASE ' )
def test_get_linearized ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertEqual ( dn . get_linearized ( ) , ' cn=foo,dc=base ' )
def test_is_null ( self ) :
dn = ldb . Dn ( self . ldb , " cn=foo,dc=base " )
self . assertFalse ( dn . is_null ( ) )
dn = ldb . Dn ( self . ldb , ' ' )
self . assertTrue ( dn . is_null ( ) )
2018-07-30 09:20:39 +03:00
2011-11-10 23:20:00 +04:00
class LdbMsgTests ( TestCase ) :
2009-06-17 20:25:21 +04:00
2007-11-21 13:47:55 +03:00
def setUp ( self ) :
2011-11-10 23:20:00 +04:00
super ( LdbMsgTests , self ) . setUp ( )
2007-11-21 13:47:55 +03:00
self . msg = ldb . Message ( )
def test_init_dn ( self ) :
2009-06-17 22:17:56 +04:00
self . msg = ldb . Message ( ldb . Dn ( ldb . Ldb ( ) , " dc=foo27 " ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " dc=foo27 " , str ( self . msg . dn ) )
2007-11-21 13:47:55 +03:00
2008-05-20 01:07:04 +04:00
def test_iter_items ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , len ( self . msg . items ( ) ) )
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " dc=foo28 " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( self . msg . items ( ) ) )
2008-05-20 01:07:04 +04:00
def test_repr ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " dc=foo29 " )
2015-07-29 13:45:43 +03:00
self . msg [ " dc " ] = b " foo "
if PY3 :
self . assertIn ( repr ( self . msg ) , [
" Message( { ' dn ' : Dn( ' dc=foo29 ' ), ' dc ' : MessageElement([b ' foo ' ])}) " ,
" Message( { ' dc ' : MessageElement([b ' foo ' ]), ' dn ' : Dn( ' dc=foo29 ' )}) " ,
] )
self . assertIn ( repr ( self . msg . text ) , [
" Message( { ' dn ' : Dn( ' dc=foo29 ' ), ' dc ' : MessageElement([b ' foo ' ])}).text " ,
" Message( { ' dc ' : MessageElement([b ' foo ' ]), ' dn ' : Dn( ' dc=foo29 ' )}).text " ,
] )
else :
2016-02-14 18:50:38 +03:00
self . assertEqual (
2015-07-29 13:45:43 +03:00
repr ( self . msg ) ,
" Message( { ' dn ' : Dn( ' dc=foo29 ' ), ' dc ' : MessageElement([ ' foo ' ])}) " )
2016-02-14 18:50:38 +03:00
self . assertEqual (
2015-07-29 13:45:43 +03:00
repr ( self . msg . text ) ,
" Message( { ' dn ' : Dn( ' dc=foo29 ' ), ' dc ' : MessageElement([ ' foo ' ])}).text " )
2008-05-20 01:07:04 +04:00
2007-11-21 13:47:55 +03:00
def test_len ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , len ( self . msg ) )
2007-11-21 13:47:55 +03:00
def test_notpresent ( self ) :
self . assertRaises ( KeyError , lambda : self . msg [ " foo " ] )
def test_del ( self ) :
del self . msg [ " foo " ]
2010-12-21 16:05:18 +03:00
def test_add ( self ) :
2015-07-29 13:45:43 +03:00
self . msg . add ( ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " ) )
def test_add_text ( self ) :
2010-12-21 16:05:18 +03:00
self . msg . add ( ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " ) )
def test_elements_empty ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( [ ] , self . msg . elements ( ) )
2010-12-21 16:05:18 +03:00
def test_elements ( self ) :
2015-07-29 13:45:43 +03:00
el = ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
2010-12-21 16:05:18 +03:00
self . msg . add ( el )
2015-06-10 11:21:24 +03:00
self . assertEqual ( [ el ] , self . msg . elements ( ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ el . text ] , self . msg . text . elements ( ) )
2010-12-21 16:05:18 +03:00
2007-11-21 13:47:55 +03:00
def test_add_value ( self ) :
2015-07-29 13:45:43 +03:00
self . assertEqual ( 0 , len ( self . msg ) )
self . msg [ " foo " ] = [ b " foo " ]
self . assertEqual ( 1 , len ( self . msg ) )
def test_add_value_text ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , len ( self . msg ) )
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " foo " ]
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( self . msg ) )
2007-11-21 13:47:55 +03:00
def test_add_value_multiple ( self ) :
2015-07-29 13:45:43 +03:00
self . assertEqual ( 0 , len ( self . msg ) )
self . msg [ " foo " ] = [ b " foo " , b " bla " ]
self . assertEqual ( 1 , len ( self . msg ) )
self . assertEqual ( [ b " foo " , b " bla " ] , list ( self . msg [ " foo " ] ) )
def test_add_value_multiple_text ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( 0 , len ( self . msg ) )
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " foo " , " bla " ]
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( self . msg ) )
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " foo " , " bla " ] , list ( self . msg . text [ " foo " ] ) )
2007-11-21 13:47:55 +03:00
def test_set_value ( self ) :
2015-07-29 13:45:43 +03:00
self . msg [ " foo " ] = [ b " fool " ]
self . assertEqual ( [ b " fool " ] , list ( self . msg [ " foo " ] ) )
self . msg [ " foo " ] = [ b " bar " ]
self . assertEqual ( [ b " bar " ] , list ( self . msg [ " foo " ] ) )
def test_set_value_text ( self ) :
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " fool " ]
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " fool " ] , list ( self . msg . text [ " foo " ] ) )
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " bar " ]
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " bar " ] , list ( self . msg . text [ " foo " ] ) )
2007-11-21 13:47:55 +03:00
def test_keys ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2015-07-29 13:45:43 +03:00
self . msg [ " foo " ] = [ b " bla " ]
self . msg [ " bar " ] = [ b " bla " ]
2018-05-04 15:33:03 +03:00
self . assertEqual ( [ " dn " , " foo " , " bar " ] , list ( self . msg . keys ( ) ) )
2015-07-29 13:45:43 +03:00
def test_keys_text ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " bla " ]
self . msg [ " bar " ] = [ " bla " ]
2018-05-04 15:33:03 +03:00
self . assertEqual ( [ " dn " , " foo " , " bar " ] , list ( self . msg . text . keys ( ) ) )
2007-11-21 13:47:55 +03:00
def test_dn ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " @BASEINFO " , self . msg . dn . __str__ ( ) )
2007-11-21 13:47:55 +03:00
2008-05-20 01:07:04 +04:00
def test_get_dn ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " @BASEINFO " , self . msg . get ( " dn " ) . __str__ ( ) )
2008-05-20 01:07:04 +04:00
2015-07-29 13:45:43 +03:00
def test_dn_text ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . text . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2015-07-29 13:45:43 +03:00
self . assertEqual ( " @BASEINFO " , str ( self . msg . dn ) )
self . assertEqual ( " @BASEINFO " , str ( self . msg . text . dn ) )
def test_get_dn_text ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2015-07-29 13:45:43 +03:00
self . assertEqual ( " @BASEINFO " , str ( self . msg . get ( " dn " ) ) )
self . assertEqual ( " @BASEINFO " , str ( self . msg . text . get ( " dn " ) ) )
2009-09-14 19:03:30 +04:00
def test_get_invalid ( self ) :
2017-09-21 04:47:49 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( ) , " @BASEINFO " )
2009-09-14 19:03:30 +04:00
self . assertRaises ( TypeError , self . msg . get , 42 )
2008-05-20 01:07:04 +04:00
def test_get_other ( self ) :
2015-07-29 13:45:43 +03:00
self . msg [ " foo " ] = [ b " bar " ]
self . assertEqual ( b " bar " , self . msg . get ( " foo " ) [ 0 ] )
self . assertEqual ( b " bar " , self . msg . get ( " foo " , idx = 0 ) )
self . assertEqual ( None , self . msg . get ( " foo " , idx = 1 ) )
self . assertEqual ( " " , self . msg . get ( " foo " , default = ' ' , idx = 1 ) )
def test_get_other_text ( self ) :
2008-05-20 01:07:04 +04:00
self . msg [ " foo " ] = [ " bar " ]
2015-07-29 13:45:43 +03:00
self . assertEqual ( [ " bar " ] , list ( self . msg . text . get ( " foo " ) ) )
self . assertEqual ( " bar " , self . msg . text . get ( " foo " ) [ 0 ] )
self . assertEqual ( " bar " , self . msg . text . get ( " foo " , idx = 0 ) )
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , self . msg . get ( " foo " , idx = 1 ) )
self . assertEqual ( " " , self . msg . get ( " foo " , default = ' ' , idx = 1 ) )
2008-05-20 01:07:04 +04:00
2011-06-11 18:09:40 +04:00
def test_get_default ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , self . msg . get ( " tatayoyo " , idx = 0 ) )
self . assertEqual ( " anniecordie " , self . msg . get ( " tatayoyo " , " anniecordie " ) )
2011-06-11 18:09:40 +04:00
2015-07-29 13:45:43 +03:00
def test_get_default_text ( self ) :
self . assertEqual ( None , self . msg . text . get ( " tatayoyo " , idx = 0 ) )
self . assertEqual ( " anniecordie " , self . msg . text . get ( " tatayoyo " , " anniecordie " ) )
2008-05-20 01:07:04 +04:00
def test_get_unknown ( self ) :
2015-06-10 11:21:24 +03:00
self . assertEqual ( None , self . msg . get ( " lalalala " ) )
2008-05-20 01:07:04 +04:00
2015-07-29 13:45:43 +03:00
def test_get_unknown_text ( self ) :
self . assertEqual ( None , self . msg . text . get ( " lalalala " ) )
2009-08-24 14:11:43 +04:00
def test_msg_diff ( self ) :
l = ldb . Ldb ( )
msgs = l . parse_ldif ( " dn: foo=bar \n foo: bar \n baz: do \n \n dn: foo=bar \n foo: bar \n baz: dont \n " )
2015-06-09 11:36:26 +03:00
msg1 = next ( msgs ) [ 1 ]
msg2 = next ( msgs ) [ 1 ]
2009-08-24 14:11:43 +04:00
msgdiff = l . msg_diff ( msg1 , msg2 )
2015-06-10 11:21:24 +03:00
self . assertEqual ( " foo=bar " , msgdiff . get ( " dn " ) . __str__ ( ) )
2009-08-24 14:11:43 +04:00
self . assertRaises ( KeyError , lambda : msgdiff [ " foo " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 1 , len ( msgdiff ) )
2009-08-24 14:11:43 +04:00
2010-05-07 04:16:11 +04:00
def test_equal_empty ( self ) :
msg1 = ldb . Message ( )
msg2 = ldb . Message ( )
2015-06-10 11:21:24 +03:00
self . assertEqual ( msg1 , msg2 )
2010-05-07 04:16:11 +04:00
def test_equal_simplel ( self ) :
2017-09-21 04:47:49 +03:00
db = ldb . Ldb ( )
2010-05-07 04:16:11 +04:00
msg1 = ldb . Message ( )
msg1 . dn = ldb . Dn ( db , " foo=bar " )
msg2 = ldb . Message ( )
msg2 . dn = ldb . Dn ( db , " foo=bar " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( msg1 , msg2 )
2015-07-29 13:45:43 +03:00
msg1 [ ' foo ' ] = b ' bar '
msg2 [ ' foo ' ] = b ' bar '
2015-06-10 11:21:24 +03:00
self . assertEqual ( msg1 , msg2 )
2015-07-29 13:45:43 +03:00
msg2 [ ' foo ' ] = b ' blie '
2015-06-10 11:21:24 +03:00
self . assertNotEqual ( msg1 , msg2 )
2015-07-29 13:45:43 +03:00
msg2 [ ' foo ' ] = b ' blie '
2010-05-07 04:16:11 +04:00
2010-11-18 18:46:33 +03:00
def test_from_dict ( self ) :
rec = { " dn " : " dc=fromdict " ,
2015-07-29 13:45:43 +03:00
" a1 " : [ b " a1-val1 " , b " a1-val1 " ] }
2010-11-18 18:46:33 +03:00
l = ldb . Ldb ( )
# check different types of input Flags
for flags in [ ldb . FLAG_MOD_ADD , ldb . FLAG_MOD_REPLACE , ldb . FLAG_MOD_DELETE ] :
m = ldb . Message . from_dict ( l , rec , flags )
2015-06-10 11:21:24 +03:00
self . assertEqual ( rec [ " a1 " ] , list ( m [ " a1 " ] ) )
self . assertEqual ( flags , m [ " a1 " ] . flags ( ) )
2010-11-18 18:46:33 +03:00
# check input params
self . assertRaises ( TypeError , ldb . Message . from_dict , dict ( ) , rec , ldb . FLAG_MOD_REPLACE )
self . assertRaises ( TypeError , ldb . Message . from_dict , l , list ( ) , ldb . FLAG_MOD_REPLACE )
self . assertRaises ( ValueError , ldb . Message . from_dict , l , rec , 0 )
# Message.from_dict expects dictionary with 'dn'
2015-07-29 13:45:43 +03:00
err_rec = { " a1 " : [ b " a1-val1 " , b " a1-val1 " ] }
self . assertRaises ( TypeError , ldb . Message . from_dict , l , err_rec , ldb . FLAG_MOD_REPLACE )
def test_from_dict_text ( self ) :
rec = { " dn " : " dc=fromdict " ,
" a1 " : [ " a1-val1 " , " a1-val1 " ] }
l = ldb . Ldb ( )
# check different types of input Flags
for flags in [ ldb . FLAG_MOD_ADD , ldb . FLAG_MOD_REPLACE , ldb . FLAG_MOD_DELETE ] :
m = ldb . Message . from_dict ( l , rec , flags )
self . assertEqual ( rec [ " a1 " ] , list ( m . text [ " a1 " ] ) )
self . assertEqual ( flags , m . text [ " a1 " ] . flags ( ) )
# check input params
self . assertRaises ( TypeError , ldb . Message . from_dict , dict ( ) , rec , ldb . FLAG_MOD_REPLACE )
self . assertRaises ( TypeError , ldb . Message . from_dict , l , list ( ) , ldb . FLAG_MOD_REPLACE )
self . assertRaises ( ValueError , ldb . Message . from_dict , l , rec , 0 )
# Message.from_dict expects dictionary with 'dn'
2010-11-18 18:46:33 +03:00
err_rec = { " a1 " : [ " a1-val1 " , " a1-val1 " ] }
self . assertRaises ( TypeError , ldb . Message . from_dict , l , err_rec , ldb . FLAG_MOD_REPLACE )
2014-11-25 05:50:52 +03:00
def test_copy_add_message_element ( self ) :
m = ldb . Message ( )
2015-07-29 13:45:43 +03:00
m [ " 1 " ] = ldb . MessageElement ( [ b " val 111 " ] , ldb . FLAG_MOD_ADD , " 1 " )
m [ " 2 " ] = ldb . MessageElement ( [ b " val 222 " ] , ldb . FLAG_MOD_ADD , " 2 " )
2014-11-25 05:50:52 +03:00
mto = ldb . Message ( )
mto [ " 1 " ] = m [ " 1 " ]
mto [ " 2 " ] = m [ " 2 " ]
self . assertEqual ( mto [ " 1 " ] , m [ " 1 " ] )
self . assertEqual ( mto [ " 2 " ] , m [ " 2 " ] )
mto = ldb . Message ( )
mto . add ( m [ " 1 " ] )
mto . add ( m [ " 2 " ] )
self . assertEqual ( mto [ " 1 " ] , m [ " 1 " ] )
self . assertEqual ( mto [ " 2 " ] , m [ " 2 " ] )
2009-08-24 14:11:43 +04:00
2015-07-29 13:45:43 +03:00
def test_copy_add_message_element_text ( self ) :
m = ldb . Message ( )
m [ " 1 " ] = ldb . MessageElement ( [ " val 111 " ] , ldb . FLAG_MOD_ADD , " 1 " )
m [ " 2 " ] = ldb . MessageElement ( [ " val 222 " ] , ldb . FLAG_MOD_ADD , " 2 " )
mto = ldb . Message ( )
mto [ " 1 " ] = m [ " 1 " ]
mto [ " 2 " ] = m [ " 2 " ]
self . assertEqual ( mto [ " 1 " ] , m . text [ " 1 " ] )
self . assertEqual ( mto [ " 2 " ] , m . text [ " 2 " ] )
mto = ldb . Message ( )
mto . add ( m [ " 1 " ] )
mto . add ( m [ " 2 " ] )
self . assertEqual ( mto . text [ " 1 " ] , m . text [ " 1 " ] )
self . assertEqual ( mto . text [ " 2 " ] , m . text [ " 2 " ] )
self . assertEqual ( mto [ " 1 " ] , m [ " 1 " ] )
self . assertEqual ( mto [ " 2 " ] , m [ " 2 " ] )
2007-11-21 13:47:55 +03:00
2011-11-10 23:20:00 +04:00
class MessageElementTests ( TestCase ) :
2009-06-17 20:25:21 +04:00
2007-11-21 13:47:55 +03:00
def test_cmp_element ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " ] )
y = ldb . MessageElement ( [ b " foo " ] )
z = ldb . MessageElement ( [ b " bzr " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( x , y )
self . assertNotEqual ( x , z )
2007-11-21 13:47:55 +03:00
2015-07-29 13:45:43 +03:00
def test_cmp_element_text ( self ) :
x = ldb . MessageElement ( [ b " foo " ] )
y = ldb . MessageElement ( [ " foo " ] )
self . assertEqual ( x , y )
2007-11-21 13:47:55 +03:00
def test_create_iterable ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " ] )
self . assertEqual ( [ b " foo " ] , list ( x ) )
self . assertEqual ( [ " foo " ] , list ( x . text ) )
2007-12-22 23:28:45 +03:00
2008-05-20 01:07:04 +04:00
def test_repr ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " ] )
if PY3 :
self . assertEqual ( " MessageElement([b ' foo ' ]) " , repr ( x ) )
self . assertEqual ( " MessageElement([b ' foo ' ]).text " , repr ( x . text ) )
else :
self . assertEqual ( " MessageElement([ ' foo ' ]) " , repr ( x ) )
self . assertEqual ( " MessageElement([ ' foo ' ]).text " , repr ( x . text ) )
x = ldb . MessageElement ( [ b " foo " , b " bla " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 2 , len ( x ) )
2015-07-29 13:45:43 +03:00
if PY3 :
self . assertEqual ( " MessageElement([b ' foo ' ,b ' bla ' ]) " , repr ( x ) )
self . assertEqual ( " MessageElement([b ' foo ' ,b ' bla ' ]).text " , repr ( x . text ) )
else :
self . assertEqual ( " MessageElement([ ' foo ' , ' bla ' ]) " , repr ( x ) )
self . assertEqual ( " MessageElement([ ' foo ' , ' bla ' ]).text " , repr ( x . text ) )
2008-05-20 01:07:04 +04:00
2008-01-11 02:23:33 +03:00
def test_get_item ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " , b " bar " ] )
self . assertEqual ( b " foo " , x [ 0 ] )
self . assertEqual ( b " bar " , x [ 1 ] )
self . assertEqual ( b " bar " , x [ - 1 ] )
self . assertRaises ( IndexError , lambda : x [ 45 ] )
def test_get_item_text ( self ) :
2008-01-11 02:23:33 +03:00
x = ldb . MessageElement ( [ " foo " , " bar " ] )
2015-07-29 13:45:43 +03:00
self . assertEqual ( " foo " , x . text [ 0 ] )
self . assertEqual ( " bar " , x . text [ 1 ] )
self . assertEqual ( " bar " , x . text [ - 1 ] )
2008-12-21 00:21:39 +03:00
self . assertRaises ( IndexError , lambda : x [ 45 ] )
2008-01-11 02:23:33 +03:00
2008-01-11 03:02:58 +03:00
def test_len ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " , b " bar " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( 2 , len ( x ) )
2008-01-11 03:02:58 +03:00
2008-01-11 03:55:56 +03:00
def test_eq ( self ) :
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " , b " bar " ] )
y = ldb . MessageElement ( [ b " foo " , b " bar " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( y , x )
2015-07-29 13:45:43 +03:00
x = ldb . MessageElement ( [ b " foo " ] )
2015-06-10 11:21:24 +03:00
self . assertNotEqual ( y , x )
2015-07-29 13:45:43 +03:00
y = ldb . MessageElement ( [ b " foo " ] )
2015-06-10 11:21:24 +03:00
self . assertEqual ( y , x )
2008-01-11 03:55:56 +03:00
2011-01-03 03:43:51 +03:00
def test_extended ( self ) :
2015-07-29 13:45:43 +03:00
el = ldb . MessageElement ( [ b " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
if PY3 :
self . assertEqual ( " MessageElement([b ' 456 ' ]) " , repr ( el ) )
self . assertEqual ( " MessageElement([b ' 456 ' ]).text " , repr ( el . text ) )
else :
self . assertEqual ( " MessageElement([ ' 456 ' ]) " , repr ( el ) )
self . assertEqual ( " MessageElement([ ' 456 ' ]).text " , repr ( el . text ) )
def test_bad_text ( self ) :
el = ldb . MessageElement ( b ' \xba \xdd ' )
self . assertRaises ( UnicodeDecodeError , el . text . __getitem__ , 0 )
2011-01-03 03:43:51 +03:00
2008-12-19 19:08:35 +03:00
2011-11-10 23:20:00 +04:00
class ModuleTests ( TestCase ) :
2009-06-17 20:25:21 +04:00
2017-04-25 11:14:33 +03:00
def setUp ( self ) :
super ( ModuleTests , self ) . setUp ( )
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " test.ldb " )
self . ldb = ldb . Ldb ( self . filename )
def tearDown ( self ) :
shutil . rmtree ( self . testdir )
super ( ModuleTests , self ) . setUp ( )
2007-12-22 23:28:45 +03:00
def test_register_module ( self ) :
2008-09-19 18:17:52 +04:00
class ExampleModule :
name = " example "
ldb . register_module ( ExampleModule )
def test_use_module ( self ) :
ops = [ ]
2018-07-30 09:21:32 +03:00
2008-09-19 18:17:52 +04:00
class ExampleModule :
name = " bla "
def __init__ ( self , ldb , next ) :
ops . append ( " init " )
2008-09-20 17:06:26 +04:00
self . next = next
def search ( self , * args , * * kwargs ) :
return self . next . search ( * args , * * kwargs )
2008-09-19 18:17:52 +04:00
2011-11-10 23:20:00 +04:00
def request ( self , * args , * * kwargs ) :
pass
2011-02-09 10:18:02 +03:00
2008-09-19 18:17:52 +04:00
ldb . register_module ( ExampleModule )
2017-04-25 11:14:33 +03:00
l = ldb . Ldb ( self . filename )
2008-09-20 17:06:26 +04:00
l . add ( { " dn " : " @MODULES " , " @LIST " : " bla " } )
2015-06-10 11:21:24 +03:00
self . assertEqual ( [ ] , ops )
2017-04-25 11:14:33 +03:00
l = ldb . Ldb ( self . filename )
2015-06-10 11:21:24 +03:00
self . assertEqual ( [ " init " ] , ops )
2007-12-22 23:28:45 +03:00
2018-07-30 09:20:39 +03:00
2018-03-05 23:13:31 +03:00
class LdbResultTests ( LdbBaseTest ) :
2011-02-09 11:22:46 +03:00
def setUp ( self ) :
2011-11-10 23:20:00 +04:00
super ( LdbResultTests , self ) . setUp ( )
2017-04-25 11:14:33 +03:00
self . testdir = tempdir ( )
self . filename = os . path . join ( self . testdir , " test.ldb " )
2018-03-05 23:13:31 +03:00
self . l = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2018-03-06 05:30:10 +03:00
try :
self . l . add ( self . index )
except AttributeError :
pass
self . l . add ( { " dn " : " DC=SAMBA,DC=ORG " , " name " : b " samba.org " ,
" objectUUID " : b " 0123456789abcde0 " } )
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " , " name " : b " Admins " ,
" objectUUID " : b " 0123456789abcde1 " } )
self . l . add ( { " dn " : " OU=USERS,DC=SAMBA,DC=ORG " , " name " : b " Users " ,
" objectUUID " : b " 0123456789abcde2 " } )
self . l . add ( { " dn " : " OU=OU1,DC=SAMBA,DC=ORG " , " name " : b " OU #1 " ,
" objectUUID " : b " 0123456789abcde3 " } )
self . l . add ( { " dn " : " OU=OU2,DC=SAMBA,DC=ORG " , " name " : b " OU #2 " ,
" objectUUID " : b " 0123456789abcde4 " } )
self . l . add ( { " dn " : " OU=OU3,DC=SAMBA,DC=ORG " , " name " : b " OU #3 " ,
" objectUUID " : b " 0123456789abcde5 " } )
self . l . add ( { " dn " : " OU=OU4,DC=SAMBA,DC=ORG " , " name " : b " OU #4 " ,
" objectUUID " : b " 0123456789abcde6 " } )
self . l . add ( { " dn " : " OU=OU5,DC=SAMBA,DC=ORG " , " name " : b " OU #5 " ,
" objectUUID " : b " 0123456789abcde7 " } )
self . l . add ( { " dn " : " OU=OU6,DC=SAMBA,DC=ORG " , " name " : b " OU #6 " ,
" objectUUID " : b " 0123456789abcde8 " } )
self . l . add ( { " dn " : " OU=OU7,DC=SAMBA,DC=ORG " , " name " : b " OU #7 " ,
" objectUUID " : b " 0123456789abcde9 " } )
self . l . add ( { " dn " : " OU=OU8,DC=SAMBA,DC=ORG " , " name " : b " OU #8 " ,
" objectUUID " : b " 0123456789abcdea " } )
self . l . add ( { " dn " : " OU=OU9,DC=SAMBA,DC=ORG " , " name " : b " OU #9 " ,
" objectUUID " : b " 0123456789abcdeb " } )
self . l . add ( { " dn " : " OU=OU10,DC=SAMBA,DC=ORG " , " name " : b " OU #10 " ,
" objectUUID " : b " 0123456789abcdec " } )
2011-02-09 11:22:46 +03:00
def tearDown ( self ) :
2017-04-25 11:14:33 +03:00
shutil . rmtree ( self . testdir )
2011-11-10 23:20:00 +04:00
super ( LdbResultTests , self ) . tearDown ( )
2017-09-21 05:15:32 +03:00
# Ensure the LDB is closed now, so we close the FD
del ( self . l )
2011-02-09 11:22:46 +03:00
def test_return_type ( self ) :
res = self . l . search ( )
2015-06-10 11:21:24 +03:00
self . assertEqual ( str ( res ) , " <ldb result> " )
2011-02-09 11:22:46 +03:00
def test_get_msgs ( self ) :
res = self . l . search ( )
list = res . msgs
def test_get_controls ( self ) :
res = self . l . search ( )
list = res . controls
def test_get_referals ( self ) :
res = self . l . search ( )
list = res . referals
def test_iter_msgs ( self ) :
found = False
for l in self . l . search ( ) . msgs :
if str ( l . dn ) == " OU=OU10,DC=SAMBA,DC=ORG " :
found = True
self . assertTrue ( found )
def test_iter_msgs_count ( self ) :
self . assertTrue ( self . l . search ( ) . count > 0 )
# 13 objects has been added to the DC=SAMBA, DC=ORG
self . assertEqual ( self . l . search ( base = " DC=SAMBA,DC=ORG " ) . count , 13 )
def test_iter_controls ( self ) :
res = self . l . search ( ) . controls
it = iter ( res )
def test_create_control ( self ) :
self . assertRaises ( ValueError , ldb . Control , self . l , " tatayoyo:0 " )
c = ldb . Control ( self . l , " relax:1 " )
2015-06-10 11:21:24 +03:00
self . assertEqual ( c . critical , True )
self . assertEqual ( c . oid , " 1.3.6.1.4.1.4203.666.5.12 " )
2011-02-09 11:22:46 +03:00
def test_iter_refs ( self ) :
res = self . l . search ( ) . referals
it = iter ( res )
2017-04-07 00:32:05 +03:00
def test_search_sequence_msgs ( self ) :
2011-02-09 11:22:46 +03:00
found = False
res = self . l . search ( ) . msgs
for i in range ( 0 , len ( res ) ) :
l = res [ i ]
if str ( l . dn ) == " OU=OU10,DC=SAMBA,DC=ORG " :
found = True
self . assertTrue ( found )
2017-04-07 00:32:05 +03:00
def test_search_as_iter ( self ) :
2011-02-09 11:22:46 +03:00
found = False
res = self . l . search ( )
2017-04-07 00:32:05 +03:00
for l in res :
if str ( l . dn ) == " OU=OU10,DC=SAMBA,DC=ORG " :
found = True
self . assertTrue ( found )
def test_search_iter ( self ) :
found = False
res = self . l . search_iterator ( )
for l in res :
2011-02-09 11:22:46 +03:00
if str ( l . dn ) == " OU=OU10,DC=SAMBA,DC=ORG " :
found = True
self . assertTrue ( found )
2009-06-17 20:25:21 +04:00
2017-06-16 06:44:46 +03:00
# Show that search results can't see into a transaction
2018-07-30 09:19:59 +03:00
2017-06-16 06:44:46 +03:00
def test_search_against_trans ( self ) :
found11 = False
( r1 , w1 ) = os . pipe ( )
( r2 , w2 ) = os . pipe ( )
# For the first element, fork a child that will
# write to the DB
pid = os . fork ( )
if pid == 0 :
# In the child, re-open
del ( self . l )
gc . collect ( )
2018-03-05 23:13:31 +03:00
child_ldb = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2017-06-16 06:44:46 +03:00
# start a transaction
child_ldb . transaction_start ( )
# write to it
child_ldb . add ( { " dn " : " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-03-06 05:30:10 +03:00
" name " : b " samba.org " ,
" objectUUID " : b " o123456789acbdef " } )
2017-06-16 06:44:46 +03:00
os . write ( w1 , b " added " )
# Now wait for the search to be done
os . read ( r2 , 6 )
# and commit
try :
child_ldb . transaction_commit ( )
2018-10-11 06:48:12 +03:00
except ldb . LdbError as err :
2017-06-16 06:44:46 +03:00
# We print this here to see what went wrong in the child
print ( err )
os . _exit ( 1 )
os . write ( w1 , b " transaction " )
os . _exit ( 0 )
self . assertEqual ( os . read ( r1 , 5 ) , b " added " )
# This should not turn up until the transaction is concluded
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-07-30 09:16:12 +03:00
scope = ldb . SCOPE_BASE )
2017-06-16 06:44:46 +03:00
self . assertEqual ( len ( res11 ) , 0 )
os . write ( w2 , b " search " )
# Now wait for the transaction to be done. This should
# deadlock, but the search doesn't hold a read lock for the
# iterator lifetime currently.
self . assertEqual ( os . read ( r1 , 11 ) , b " transaction " )
# This should now turn up, as the transaction is over
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-07-30 09:16:12 +03:00
scope = ldb . SCOPE_BASE )
2017-06-16 06:44:46 +03:00
self . assertEqual ( len ( res11 ) , 1 )
self . assertFalse ( found11 )
( got_pid , status ) = os . waitpid ( pid , 0 )
self . assertEqual ( got_pid , pid )
2017-04-07 00:32:05 +03:00
def test_search_iter_against_trans ( self ) :
found = False
found11 = False
# We need to hold this iterator open to hold the all-record
# lock
res = self . l . search_iterator ( )
( r1 , w1 ) = os . pipe ( )
( r2 , w2 ) = os . pipe ( )
# For the first element, with the sequence open (which
# means with ldb locks held), fork a child that will
# write to the DB
pid = os . fork ( )
if pid == 0 :
# In the child, re-open
2017-06-16 06:49:16 +03:00
del ( res )
2017-04-07 00:32:05 +03:00
del ( self . l )
gc . collect ( )
2018-03-05 23:13:31 +03:00
child_ldb = ldb . Ldb ( self . url ( ) , flags = self . flags ( ) )
2017-04-07 00:32:05 +03:00
# start a transaction
child_ldb . transaction_start ( )
# write to it
child_ldb . add ( { " dn " : " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-03-06 05:30:10 +03:00
" name " : b " samba.org " ,
" objectUUID " : b " o123456789acbdef " } )
2017-04-07 00:32:05 +03:00
os . write ( w1 , b " added " )
# Now wait for the search to be done
os . read ( r2 , 6 )
# and commit
try :
child_ldb . transaction_commit ( )
2018-10-11 06:48:12 +03:00
except ldb . LdbError as err :
2017-04-07 00:32:05 +03:00
# We print this here to see what went wrong in the child
print ( err )
os . _exit ( 1 )
os . write ( w1 , b " transaction " )
os . _exit ( 0 )
self . assertEqual ( os . read ( r1 , 5 ) , b " added " )
# This should not turn up until the transaction is concluded
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-07-30 09:16:12 +03:00
scope = ldb . SCOPE_BASE )
2017-04-07 00:32:05 +03:00
self . assertEqual ( len ( res11 ) , 0 )
os . write ( w2 , b " search " )
2017-06-16 06:49:16 +03:00
# allow the transaction to start
time . sleep ( 1 )
2017-04-07 00:32:05 +03:00
# This should not turn up until the search finishes and
# removed the read lock, but for ldb_tdb that happened as soon
# as we called the first res.next()
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-07-30 09:16:12 +03:00
scope = ldb . SCOPE_BASE )
2017-06-16 06:49:16 +03:00
self . assertEqual ( len ( res11 ) , 0 )
2017-04-07 00:32:05 +03:00
2017-06-16 06:49:16 +03:00
# These results are all collected at the first next(res) call
2017-04-07 00:32:05 +03:00
for l in res :
if str ( l . dn ) == " OU=OU10,DC=SAMBA,DC=ORG " :
found = True
if str ( l . dn ) == " OU=OU11,DC=SAMBA,DC=ORG " :
found11 = True
2017-06-16 06:49:16 +03:00
# Now wait for the transaction to be done.
self . assertEqual ( os . read ( r1 , 11 ) , b " transaction " )
2017-04-07 00:32:05 +03:00
# This should now turn up, as the transaction is over and all
# read locks are gone
res11 = self . l . search ( base = " OU=OU11,DC=SAMBA,DC=ORG " ,
2018-07-30 09:16:12 +03:00
scope = ldb . SCOPE_BASE )
2017-04-07 00:32:05 +03:00
self . assertEqual ( len ( res11 ) , 1 )
self . assertTrue ( found )
self . assertFalse ( found11 )
( got_pid , status ) = os . waitpid ( pid , 0 )
self . assertEqual ( got_pid , pid )
2011-11-10 23:20:00 +04:00
2018-03-05 23:13:31 +03:00
class LdbResultTestsLmdb ( LdbResultTests ) :
def setUp ( self ) :
self . prefix = MDB_PREFIX
self . index = MDB_INDEX_OBJ
super ( LdbResultTestsLmdb , self ) . setUp ( )
def tearDown ( self ) :
super ( LdbResultTestsLmdb , self ) . tearDown ( )
2015-03-04 00:29:14 +03:00
class BadTypeTests ( TestCase ) :
def test_control ( self ) :
l = ldb . Ldb ( )
self . assertRaises ( TypeError , ldb . Control , ' <bad type> ' , ' relax:1 ' )
self . assertRaises ( TypeError , ldb . Control , ldb , 1234 )
def test_modify ( self ) :
l = ldb . Ldb ( )
dn = ldb . Dn ( l , ' a=b ' )
m = ldb . Message ( dn )
self . assertRaises ( TypeError , l . modify , ' <bad type> ' )
self . assertRaises ( TypeError , l . modify , m , ' <bad type> ' )
def test_add ( self ) :
l = ldb . Ldb ( )
dn = ldb . Dn ( l , ' a=b ' )
m = ldb . Message ( dn )
self . assertRaises ( TypeError , l . add , ' <bad type> ' )
self . assertRaises ( TypeError , l . add , m , ' <bad type> ' )
def test_delete ( self ) :
l = ldb . Ldb ( )
dn = ldb . Dn ( l , ' a=b ' )
self . assertRaises ( TypeError , l . add , ' <bad type> ' )
self . assertRaises ( TypeError , l . add , dn , ' <bad type> ' )
def test_rename ( self ) :
l = ldb . Ldb ( )
dn = ldb . Dn ( l , ' a=b ' )
self . assertRaises ( TypeError , l . add , ' <bad type> ' , dn )
self . assertRaises ( TypeError , l . add , dn , ' <bad type> ' )
self . assertRaises ( TypeError , l . add , dn , dn , ' <bad type> ' )
def test_search ( self ) :
l = ldb . Ldb ( )
self . assertRaises ( TypeError , l . search , base = 1234 )
self . assertRaises ( TypeError , l . search , scope = ' <bad type> ' )
self . assertRaises ( TypeError , l . search , expression = 1234 )
self . assertRaises ( TypeError , l . search , attrs = ' <bad type> ' )
self . assertRaises ( TypeError , l . search , controls = ' <bad type> ' )
2011-11-10 23:20:00 +04:00
class VersionTests ( TestCase ) :
2010-10-05 02:36:21 +04:00
def test_version ( self ) :
self . assertTrue ( isinstance ( ldb . __version__ , str ) )
2011-11-10 16:40:03 +04:00
if __name__ == ' __main__ ' :
import unittest
unittest . TestProgram ( )