2010-03-24 08:50:50 +03:00
#!/usr/bin/env python
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
2007-12-17 05:25:28 +03:00
2007-11-21 13:47:55 +03:00
import ldb
2010-10-04 22:56:20 +04:00
2008-12-21 06:36:16 +03:00
def filename ( ) :
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
return tempfile . mktemp ( 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 ) :
self . assertEquals ( " 19700101000000.0Z " , ldb . timestring ( 0 ) )
self . assertEquals ( " 20071119191012.0Z " , ldb . timestring ( 1195499412 ) )
def test_string_to_time ( self ) :
self . assertEquals ( 0 , ldb . string_to_time ( " 19700101000000.0Z " ) )
self . assertEquals ( 1195499412 , ldb . string_to_time ( " 20071119191012.0Z " ) )
2011-07-28 11:03:06 +04:00
def test_binary_encode ( self ) :
2011-11-10 23:20:00 +04:00
encoded = ldb . binary_encode ( ' test \\ x ' )
decoded = ldb . binary_decode ( encoded )
2011-07-28 11:03:06 +04:00
self . assertEquals ( decoded , ' test \\ x ' )
2007-11-21 13:47:55 +03:00
2011-11-10 23:20:00 +04:00
class SimpleLdb ( TestCase ) :
2009-06-17 20:25:21 +04:00
2007-11-21 13:47:55 +03:00
def test_connect ( self ) :
2008-12-21 06:36:16 +03:00
ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
def test_connect_none ( self ) :
ldb . Ldb ( )
def test_connect_later ( self ) :
x = ldb . Ldb ( )
2008-12-21 06:36:16 +03:00
x . connect ( filename ( ) )
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 ( )
x . set_create_perms ( 0600 )
2008-09-19 18:17:52 +04:00
def test_modules_none ( self ) :
x = ldb . Ldb ( )
self . assertEquals ( [ ] , x . modules ( ) )
def test_modules_tdb ( self ) :
2010-12-24 13:11:45 +03:00
x = ldb . Ldb ( filename ( ) )
2008-09-19 18:17:52 +04:00
self . assertEquals ( " [<ldb module ' tdb ' >] " , repr ( x . modules ( ) ) )
2007-11-21 13:47:55 +03:00
def test_search ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( controls = [ " paged_results:0:5 " ] ) ) , 0 )
2008-01-11 05:25:22 +03:00
2007-11-21 13:47:55 +03:00
def test_search_attrs ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
self . assertEquals ( len ( l . search ( ldb . Dn ( l , " " ) , ldb . SCOPE_SUBTREE , " (dc=*) " , [ " dc " ] ) ) , 0 )
2007-12-24 04:19:41 +03:00
def test_search_string_dn ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-12-24 04:19:41 +03:00
self . assertEquals ( len ( l . search ( " " , ldb . SCOPE_SUBTREE , " (dc=*) " , [ " dc " ] ) ) , 0 )
2008-05-20 01:07:04 +04:00
def test_search_attr_string ( self ) :
2010-12-24 13:11:45 +03:00
l = ldb . Ldb ( filename ( ) )
2008-05-20 01:07:04 +04:00
self . assertRaises ( TypeError , l . search , attrs = " dc " )
2007-11-21 13:47:55 +03:00
def test_opaque ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
l . set_opaque ( " my_opaque " , l )
self . assertTrue ( l . get_opaque ( " my_opaque " ) is not None )
self . assertEquals ( None , l . get_opaque ( " unknown " ) )
def test_search_scope_base ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2009-06-17 22:17:56 +04:00
self . assertEquals ( len ( l . search ( ldb . Dn ( l , " dc=foo1 " ) ,
2007-11-21 13:47:55 +03:00
ldb . SCOPE_ONELEVEL ) ) , 0 )
def test_delete ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 ) :
l = ldb . Ldb ( filename ( ) )
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo1 " )
m [ " b " ] = [ " a " ]
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 ) :
2010-12-24 13:40:02 +03:00
name = filename ( )
l = ldb . Ldb ( name )
2009-06-17 22:17:56 +04:00
self . assertFalse ( ldb . Dn ( l , " dc=foo3 " ) in l )
2010-12-24 13:40:02 +03:00
l = ldb . Ldb ( name )
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 " ]
l . add ( m )
try :
2009-06-17 22:17:56 +04:00
self . assertTrue ( ldb . Dn ( l , " dc=foo3 " ) in l )
2007-11-21 13:47:55 +03:00
finally :
l . delete ( m . dn )
def test_get_config_basedn ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
self . assertEquals ( None , l . get_config_basedn ( ) )
def test_get_root_basedn ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
self . assertEquals ( None , l . get_root_basedn ( ) )
def test_get_schema_basedn ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
self . assertEquals ( None , l . get_schema_basedn ( ) )
def test_get_default_basedn ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
self . assertEquals ( None , l . get_default_basedn ( ) )
def test_add ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 "
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 ) :
l = ldb . Ldb ( filename ( ) )
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=foo4 " )
m [ " bla " ] = " bla "
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 0 )
2009-09-23 00:51:25 +04:00
self . assertRaises ( ldb . LdbError , lambda : l . add ( m , [ " search_options:1:2 " ] ) )
2007-11-21 13:47:55 +03:00
def test_add_dict ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2009-06-17 22:17:56 +04:00
m = { " dn " : ldb . Dn ( l , " dc=foo5 " ) ,
2007-11-21 13:47:55 +03:00
" bla " : " bla " }
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 0 )
2007-11-21 13:47:55 +03:00
l . add ( m )
try :
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
2009-06-17 22:17:56 +04:00
m = { " dn " : " dc=foo6 " , " bla " : " bla " }
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 0 )
2007-12-20 01:27:34 +03:00
l . add ( m )
try :
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " )
2007-11-21 13:47:55 +03:00
m [ " bla " ] = " bla "
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 " ) )
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " )
2007-12-24 04:19:41 +03:00
m [ " bla " ] = " bla "
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 0 )
2011-11-10 16:40:03 +04:00
l . add ( m )
2011-11-10 23:20:00 +04:00
self . assertEquals ( 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 " )
2011-11-10 23:20:00 +04:00
self . assertEquals ( len ( l . search ( ) ) , 1 )
2007-12-24 04:19:41 +03:00
finally :
l . delete ( ldb . Dn ( l , " dc=bar " ) )
2007-11-21 13:47:55 +03:00
def test_modify_delete ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " )
2007-11-21 13:47:55 +03:00
m [ " bla " ] = [ " 1234 " ]
l . add ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEquals ( [ " 1234 " ] , list ( rm [ " bla " ] ) )
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 " )
self . assertEquals ( ldb . FLAG_MOD_DELETE , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEquals ( 1 , len ( rm ) )
2010-11-23 23:39:34 +03:00
rm = l . search ( m . dn , attrs = [ " bla " ] )
2009-08-26 09:01:12 +04:00
self . assertEquals ( 0 , 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=modifydelete " ) )
2007-11-21 13:47:55 +03:00
def test_modify_add ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " )
2007-11-21 13:47:55 +03:00
m [ " bla " ] = [ " 1234 " ]
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 " )
self . assertEquals ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEquals ( 2 , len ( rm ) )
self . assertEquals ( [ " 1234 " , " 456 " ] , list ( rm [ " bla " ] ) )
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 ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " )
2007-11-21 13:47:55 +03:00
m [ " bla " ] = [ " 1234 " , " 456 " ]
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 " )
self . assertEquals ( ldb . FLAG_MOD_REPLACE , m [ " bla " ] . flags ( ) )
2007-11-21 13:47:55 +03:00
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEquals ( 2 , len ( rm ) )
2009-08-26 09:01:12 +04:00
self . assertEquals ( [ " 789 " ] , list ( rm [ " bla " ] ) )
rm = l . search ( m . dn , attrs = [ " bla " ] ) [ 0 ]
self . assertEquals ( 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 ) :
l = ldb . Ldb ( filename ( ) )
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m [ " bla " ] = [ " 1234 " ]
l . add ( m )
try :
m = ldb . Message ( )
m . dn = ldb . Dn ( l , " dc=add " )
m [ " bla " ] = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
self . assertEquals ( ldb . FLAG_MOD_ADD , m [ " bla " ] . flags ( ) )
l . modify ( m )
rm = l . search ( m . dn ) [ 0 ]
self . assertEquals ( 2 , len ( rm ) )
self . assertEquals ( [ " 1234 " , " 456 " ] , list ( rm [ " 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 ]
self . assertEquals ( 1 , len ( rm ) )
self . assertEquals ( [ " 1234 " ] , list ( rm [ " bla " ] ) )
finally :
l . delete ( ldb . Dn ( l , " dc=add " ) )
2007-11-21 13:47:55 +03:00
def test_transaction_commit ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " ) )
2007-11-21 13:47:55 +03:00
m [ " foo " ] = [ " bar " ]
l . add ( m )
l . transaction_commit ( )
l . delete ( m . dn )
def test_transaction_cancel ( self ) :
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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 " ) )
2007-11-21 13:47:55 +03:00
m [ " foo " ] = [ " bar " ]
l . add ( m )
l . transaction_cancel ( )
2009-06-17 22:17:56 +04:00
self . assertEquals ( 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
2008-12-21 06:36:16 +03:00
l = ldb . Ldb ( filename ( ) )
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. """
l = ldb . Ldb ( filename ( ) )
l . add ( {
" dn " : " dc=somedn " ,
" objectclass " : " user " ,
" cN " : " LDAPtestUSER " ,
" givenname " : " ldap " ,
" displayname " : " foo \0 bar " ,
} )
res = l . search ( expression = " (dn=dc=somedn) " )
self . assertEquals ( " foo \0 bar " , res [ 0 ] [ " displayname " ] [ 0 ] )
2010-10-04 22:56:20 +04:00
def test_no_crash_broken_expr ( self ) :
l = ldb . Ldb ( filename ( ) )
self . assertRaises ( ldb . LdbError , lambda : l . search ( " " , ldb . SCOPE_SUBTREE , " &(dc=*)(dn=*) " , [ " dc " ] ) )
2007-11-21 13:47:55 +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 ( )
2008-12-21 06:36:16 +03:00
self . ldb = ldb . Ldb ( filename ( ) )
2007-11-21 13:47:55 +03:00
2009-08-03 20:15:16 +04:00
def test_set_dn_invalid ( self ) :
x = ldb . Message ( )
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 " )
2008-12-21 00:21:39 +03:00
self . assertEquals ( x , y )
2010-11-18 11:22:07 +03:00
y = ldb . Dn ( self . ldb , " dc=foo11,bar=blie " )
self . assertNotEquals ( 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 " )
self . assertEquals ( 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 " )
self . assertEquals ( x . __repr__ ( ) , " Dn( ' dc=foo13,bla=blie ' ) " )
2008-05-20 01:07:04 +04:00
2007-11-21 13:47:55 +03:00
def test_get_casefold ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo14,bar=bloe " )
self . assertEquals ( 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 " )
2007-11-21 13:47:55 +03:00
self . assertEquals ( " bar=bloe " , x . parent ( ) . __str__ ( ) )
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 " )
self . assertEquals ( 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 " )
2007-11-21 13:47:55 +03:00
self . assertEquals ( 2 , len ( x ) )
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo21 " )
2007-11-21 13:47:55 +03:00
self . assertEquals ( 1 , len ( x ) )
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 " ) ) )
2009-06-17 22:17:56 +04:00
self . assertEquals ( " 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 ) )
2009-06-17 22:17:56 +04:00
self . assertEquals ( " dc=foo23,bar=bloe,bla=bloe " , x . __str__ ( ) )
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 " )
2012-07-06 14:41:10 +04:00
self . assertEquals ( " 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 " )
x . remove_base_components ( len ( x ) - 1 )
self . assertEquals ( " dc=foo24 " , str ( x ) )
2007-11-21 13:47:55 +03:00
def test_parse_ldif ( self ) :
msgs = self . ldb . parse_ldif ( " dn: foo=bar \n " )
msg = msgs . next ( )
self . assertEquals ( " foo=bar " , str ( msg [ 1 ] . dn ) )
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 )
self . assertEquals ( " 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 " )
msg = msgs . next ( )
self . assertEquals ( " foo=bar " , str ( msg [ 1 ] . dn ) )
msg = msgs . next ( )
self . assertEquals ( " bar=bar " , str ( msg [ 1 ] . dn ) )
def test_canonical_string ( self ) :
2009-06-17 22:17:56 +04:00
x = ldb . Dn ( self . ldb , " dc=foo25,bar=bloe " )
2009-06-17 22:43:25 +04:00
self . assertEquals ( " /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 " )
2009-06-17 22:43:25 +04:00
self . assertEquals ( " /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 " )
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 ) )
self . assertFalse ( dn3 . is_child_of ( dn2 ) )
self . assertFalse ( dn1 . is_child_of ( dn4 ) )
2007-11-21 13:47:55 +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 " ) )
self . assertEquals ( " 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 ) :
self . assertEquals ( 0 , len ( self . msg . items ( ) ) )
2010-12-27 16:07:50 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " dc=foo28 " )
2008-05-20 01:07:04 +04:00
self . assertEquals ( 1 , len ( self . msg . items ( ) ) )
def test_repr ( self ) :
2010-12-27 16:07:50 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " dc=foo29 " )
2008-05-20 01:07:04 +04:00
self . msg [ " dc " ] = " foo "
2009-06-17 22:17:56 +04:00
self . assertEquals ( " Message( { ' dn ' : Dn( ' dc=foo29 ' ), ' dc ' : MessageElement([ ' foo ' ])}) " , repr ( self . msg ) )
2008-05-20 01:07:04 +04:00
2007-11-21 13:47:55 +03:00
def test_len ( self ) :
self . assertEquals ( 0 , len ( self . msg ) )
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 ) :
self . msg . add ( ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " ) )
def test_elements_empty ( self ) :
self . assertEquals ( [ ] , self . msg . elements ( ) )
def test_elements ( self ) :
el = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
self . msg . add ( el )
self . assertEquals ( [ el ] , self . msg . elements ( ) )
2007-11-21 13:47:55 +03:00
def test_add_value ( self ) :
self . assertEquals ( 0 , len ( self . msg ) )
self . msg [ " foo " ] = [ " foo " ]
self . assertEquals ( 1 , len ( self . msg ) )
def test_add_value_multiple ( self ) :
self . assertEquals ( 0 , len ( self . msg ) )
self . msg [ " foo " ] = [ " foo " , " bla " ]
self . assertEquals ( 1 , len ( self . msg ) )
self . assertEquals ( [ " foo " , " bla " ] , list ( self . msg [ " foo " ] ) )
def test_set_value ( self ) :
self . msg [ " foo " ] = [ " fool " ]
self . assertEquals ( [ " fool " ] , list ( self . msg [ " foo " ] ) )
self . msg [ " foo " ] = [ " bar " ]
self . assertEquals ( [ " bar " ] , list ( self . msg [ " foo " ] ) )
def test_keys ( self ) :
2010-12-27 16:07:50 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " @BASEINFO " )
2007-11-21 13:47:55 +03:00
self . msg [ " foo " ] = [ " bla " ]
self . msg [ " bar " ] = [ " bla " ]
2008-05-20 01:07:04 +04:00
self . assertEquals ( [ " dn " , " foo " , " bar " ] , self . msg . keys ( ) )
2007-11-21 13:47:55 +03:00
def test_dn ( self ) :
2008-12-21 06:36:16 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " @BASEINFO " )
2007-11-21 13:47:55 +03:00
self . assertEquals ( " @BASEINFO " , self . msg . dn . __str__ ( ) )
2008-05-20 01:07:04 +04:00
def test_get_dn ( self ) :
2010-12-27 16:07:50 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " @BASEINFO " )
2008-05-20 01:07:04 +04:00
self . assertEquals ( " @BASEINFO " , self . msg . get ( " dn " ) . __str__ ( ) )
2009-09-14 19:03:30 +04:00
def test_get_invalid ( self ) :
2010-12-27 16:07:50 +03:00
self . msg . dn = ldb . Dn ( ldb . Ldb ( filename ( ) ) , " @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 ) :
self . msg [ " foo " ] = [ " bar " ]
self . assertEquals ( " bar " , self . msg . get ( " foo " ) [ 0 ] )
2011-11-10 23:20:00 +04:00
self . assertEquals ( " bar " , self . msg . get ( " foo " , idx = 0 ) )
self . assertEquals ( None , self . msg . get ( " foo " , idx = 1 ) )
self . assertEquals ( " " , 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 ) :
2011-11-10 23:20:00 +04:00
self . assertEquals ( None , self . msg . get ( " tatayoyo " , idx = 0 ) )
2011-06-11 18:09:40 +04:00
self . assertEquals ( " anniecordie " , self . msg . get ( " tatayoyo " , " anniecordie " ) )
2008-05-20 01:07:04 +04:00
def test_get_unknown ( self ) :
2008-12-19 19:08:35 +03:00
self . assertEquals ( None , self . msg . get ( " lalalala " ) )
2008-05-20 01:07:04 +04:00
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 " )
msg1 = msgs . next ( ) [ 1 ]
msg2 = msgs . next ( ) [ 1 ]
msgdiff = l . msg_diff ( msg1 , msg2 )
self . assertEquals ( " foo=bar " , msgdiff . get ( " dn " ) . __str__ ( ) )
self . assertRaises ( KeyError , lambda : msgdiff [ " foo " ] )
self . assertEquals ( 1 , len ( msgdiff ) )
2010-05-07 04:16:11 +04:00
def test_equal_empty ( self ) :
msg1 = ldb . Message ( )
msg2 = ldb . Message ( )
self . assertEquals ( msg1 , msg2 )
def test_equal_simplel ( self ) :
2010-12-27 16:07:50 +03:00
db = ldb . Ldb ( filename ( ) )
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 " )
self . assertEquals ( msg1 , msg2 )
msg1 [ ' foo ' ] = ' bar '
msg2 [ ' foo ' ] = ' bar '
self . assertEquals ( msg1 , msg2 )
msg2 [ ' foo ' ] = ' blie '
self . assertNotEquals ( msg1 , msg2 )
msg2 [ ' foo ' ] = ' blie '
2010-11-18 18:46:33 +03:00
def test_from_dict ( 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 . assertEquals ( rec [ " a1 " ] , list ( m [ " a1 " ] ) )
self . assertEquals ( flags , m [ " 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'
err_rec = { " a1 " : [ " a1-val1 " , " a1-val1 " ] }
self . assertRaises ( TypeError , ldb . Message . from_dict , l , err_rec , ldb . FLAG_MOD_REPLACE )
2009-08-24 14:11:43 +04:00
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 ) :
x = ldb . MessageElement ( [ " foo " ] )
y = ldb . MessageElement ( [ " foo " ] )
z = ldb . MessageElement ( [ " bzr " ] )
self . assertEquals ( x , y )
self . assertNotEquals ( x , z )
def test_create_iterable ( self ) :
x = ldb . MessageElement ( [ " foo " ] )
self . assertEquals ( [ " foo " ] , list ( x ) )
2007-12-22 23:28:45 +03:00
2008-05-20 01:07:04 +04:00
def test_repr ( self ) :
x = ldb . MessageElement ( [ " foo " ] )
self . assertEquals ( " MessageElement([ ' foo ' ]) " , repr ( x ) )
x = ldb . MessageElement ( [ " foo " , " bla " ] )
2008-12-21 00:21:39 +03:00
self . assertEquals ( 2 , len ( x ) )
2008-05-20 01:07:04 +04:00
self . assertEquals ( " MessageElement([ ' foo ' , ' bla ' ]) " , repr ( x ) )
2008-01-11 02:23:33 +03:00
def test_get_item ( self ) :
x = ldb . MessageElement ( [ " foo " , " bar " ] )
self . assertEquals ( " foo " , x [ 0 ] )
self . assertEquals ( " bar " , x [ 1 ] )
2008-12-21 00:21:39 +03:00
self . assertEquals ( " bar " , x [ - 1 ] )
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 ) :
x = ldb . MessageElement ( [ " foo " , " bar " ] )
self . assertEquals ( 2 , len ( x ) )
2008-01-11 03:55:56 +03:00
def test_eq ( self ) :
x = ldb . MessageElement ( [ " foo " , " bar " ] )
2008-12-21 00:21:39 +03:00
y = ldb . MessageElement ( [ " foo " , " bar " ] )
self . assertEquals ( y , x )
2008-01-11 03:55:56 +03:00
x = ldb . MessageElement ( [ " foo " ] )
2008-12-21 00:21:39 +03:00
self . assertNotEquals ( y , x )
y = ldb . MessageElement ( [ " foo " ] )
self . assertEquals ( y , x )
2008-01-11 03:55:56 +03:00
2011-01-03 03:43:51 +03:00
def test_extended ( self ) :
el = ldb . MessageElement ( [ " 456 " ] , ldb . FLAG_MOD_ADD , " bla " )
self . assertEquals ( " MessageElement([ ' 456 ' ]) " , repr ( el ) )
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
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 = [ ]
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
2010-12-24 13:11:45 +03:00
name = filename ( )
2008-09-19 18:17:52 +04:00
ldb . register_module ( ExampleModule )
2010-12-24 13:11:45 +03:00
if os . path . exists ( name ) :
os . unlink ( name )
l = ldb . Ldb ( name )
2008-09-20 17:06:26 +04:00
l . add ( { " dn " : " @MODULES " , " @LIST " : " bla " } )
2008-09-19 18:17:52 +04:00
self . assertEquals ( [ ] , ops )
2010-12-24 13:11:45 +03:00
l = ldb . Ldb ( name )
2008-09-19 18:17:52 +04:00
self . assertEquals ( [ " init " ] , ops )
2007-12-22 23:28:45 +03:00
2011-11-10 23:20:00 +04:00
class LdbResultTests ( TestCase ) :
2011-02-09 11:22:46 +03:00
def setUp ( self ) :
2011-11-10 23:20:00 +04:00
super ( LdbResultTests , self ) . setUp ( )
2011-02-09 11:22:46 +03:00
name = filename ( )
self . name = name
if os . path . exists ( name ) :
os . unlink ( name )
self . l = ldb . Ldb ( name )
self . l . add ( { " dn " : " DC=SAMBA,DC=ORG " , " name " : " samba.org " } )
self . l . add ( { " dn " : " OU=ADMIN,DC=SAMBA,DC=ORG " , " name " : " Admins " } )
self . l . add ( { " dn " : " OU=USERS,DC=SAMBA,DC=ORG " , " name " : " Users " } )
self . l . add ( { " dn " : " OU=OU1,DC=SAMBA,DC=ORG " , " name " : " OU #1 " } )
self . l . add ( { " dn " : " OU=OU2,DC=SAMBA,DC=ORG " , " name " : " OU #2 " } )
self . l . add ( { " dn " : " OU=OU3,DC=SAMBA,DC=ORG " , " name " : " OU #3 " } )
self . l . add ( { " dn " : " OU=OU4,DC=SAMBA,DC=ORG " , " name " : " OU #4 " } )
self . l . add ( { " dn " : " OU=OU5,DC=SAMBA,DC=ORG " , " name " : " OU #5 " } )
self . l . add ( { " dn " : " OU=OU6,DC=SAMBA,DC=ORG " , " name " : " OU #6 " } )
self . l . add ( { " dn " : " OU=OU7,DC=SAMBA,DC=ORG " , " name " : " OU #7 " } )
self . l . add ( { " dn " : " OU=OU8,DC=SAMBA,DC=ORG " , " name " : " OU #8 " } )
self . l . add ( { " dn " : " OU=OU9,DC=SAMBA,DC=ORG " , " name " : " OU #9 " } )
self . l . add ( { " dn " : " OU=OU10,DC=SAMBA,DC=ORG " , " name " : " OU #10 " } )
def tearDown ( self ) :
2011-11-10 23:20:00 +04:00
super ( LdbResultTests , self ) . tearDown ( )
2011-02-09 11:22:46 +03:00
if os . path . exists ( self . name ) :
os . unlink ( self . name )
def test_return_type ( self ) :
res = self . l . search ( )
self . assertEquals ( str ( res ) , " <ldb result> " )
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 " )
self . assertEquals ( c . critical , True )
self . assertEquals ( c . oid , " 1.3.6.1.4.1.4203.666.5.12 " )
def test_iter_refs ( self ) :
res = self . l . search ( ) . referals
it = iter ( res )
def test_iter_as_sequence_msgs ( self ) :
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 )
def test_iter_as_sequence ( self ) :
found = False
res = self . l . search ( )
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 )
2009-06-17 20:25:21 +04:00
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 ( )