<s> """<STR_LIT>""" <EOL> import os <EOL> import ldb <EOL> import samba <EOL> import samba . auth <EOL> from samba import param <EOL> from samba . samdb import SamDB <EOL> import subprocess <EOL> import tempfile <EOL> samba . ensure_external_module ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> from testtools . testcase import ( <EOL> TestCase as TesttoolsTestCase , <EOL> TestSkipped , <EOL> ) <EOL> class TestCase ( TesttoolsTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestCase , self ) . setUp ( ) <EOL> test_debug_level = os . getenv ( "<STR_LIT>" ) <EOL> if test_debug_level is not None : <EOL> test_debug_level = int ( test_debug_level ) <EOL> self . _old_debug_level = samba . get_debug_level ( ) <EOL> samba . set_debug_level ( test_debug_level ) <EOL> self . addCleanup ( samba . set_debug_level , test_debug_level ) <EOL> def get_loadparm ( self ) : <EOL> return env_loadparm ( ) <EOL> def get_credentials ( self ) : <EOL> return cmdline_credentials <EOL> class LdbTestCase ( TesttoolsTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( LdbTestCase , self ) . setUp ( ) <EOL> self . filename = os . tempnam ( ) <EOL> self . ldb = samba . Ldb ( self . filename ) <EOL> def set_modules ( self , modules = [ ] ) : <EOL> """<STR_LIT>""" <EOL> m = ldb . Message ( ) <EOL> m . dn = ldb . Dn ( self . ldb , "<STR_LIT>" ) <EOL> m [ "<STR_LIT>" ] = "<STR_LIT:U+002C>" . join ( modules ) <EOL> self . ldb . add ( m ) <EOL> self . ldb = samba . Ldb ( self . filename ) <EOL> class TestCaseInTempDir ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestCaseInTempDir , self ) . setUp ( ) <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . addCleanup ( self . _remove_tempdir ) <EOL> def _remove_tempdir ( self ) : <EOL> self . assertEquals ( [ ] , os . listdir ( self . tempdir ) ) <EOL> os . rmdir ( self . tempdir ) <EOL> self . tempdir = None <EOL> def env_loadparm ( ) : <EOL> lp = param . LoadParm ( ) <EOL> try : <EOL> lp . load ( os . environ [ "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return lp <EOL> def env_get_var_value ( var_name ) : <EOL> """<STR_LIT>""" <EOL> assert var_name in os . environ . keys ( ) , "<STR_LIT>" % var_name <EOL> return os . environ [ var_name ] <EOL> cmdline_credentials = None <EOL> class RpcInterfaceTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> class ValidNetbiosNameTests ( TestCase ) : <EOL> def test_valid ( self ) : <EOL> self . assertTrue ( samba . valid_netbios_name ( "<STR_LIT>" ) ) <EOL> def test_too_long ( self ) : <EOL> self . assertFalse ( samba . valid_netbios_name ( "<STR_LIT>" * <NUM_LIT:10> ) ) <EOL> def test_invalid_characters ( self ) : <EOL> self . assertFalse ( samba . valid_netbios_name ( "<STR_LIT>" ) ) <EOL> class BlackboxProcessError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , returncode , cmd , stdout , stderr ) : <EOL> self . returncode = returncode <EOL> self . cmd = cmd <EOL> self . stdout = stdout <EOL> self . stderr = stderr <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . cmd , self . returncode , <EOL> self . stdout , self . stderr ) <EOL> class BlackboxTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _make_cmdline ( self , line ) : <EOL> bindir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) ) <EOL> parts = line . split ( "<STR_LIT:U+0020>" ) <EOL> if os . path . exists ( os . path . join ( bindir , parts [ <NUM_LIT:0> ] ) ) : <EOL> parts [ <NUM_LIT:0> ] = os . path . join ( bindir , parts [ <NUM_LIT:0> ] ) <EOL> line = "<STR_LIT:U+0020>" . join ( parts ) <EOL> return line <EOL> def check_run ( self , line ) : <EOL> line = self . _make_cmdline ( line ) <EOL> p = subprocess . Popen ( line , stdout = subprocess . PIPE , stderr = subprocess . PIPE , shell = True ) <EOL> retcode = p . wait ( ) <EOL> if retcode : <EOL> raise BlackboxProcessError ( retcode , line , p . stdout . read ( ) , p . stderr . read ( ) ) <EOL> def check_output ( self , line ) : <EOL> line = self . _make_cmdline ( line ) <EOL> p = subprocess . Popen ( line , stdout = subprocess . PIPE , stderr = subprocess . PIPE , shell = True , close_fds = True ) <EOL> retcode = p . wait ( ) <EOL> if retcode : <EOL> raise BlackboxProcessError ( retcode , line , p . stdout . read ( ) , p . stderr . read ( ) ) <EOL> return p . stdout . read ( ) <EOL> def connect_samdb ( samdb_url , lp = None , session_info = None , credentials = None , <EOL> flags = <NUM_LIT:0> , ldb_options = None , ldap_only = False ) : <EOL> """<STR_LIT>""" <EOL> samdb_url = samdb_url . lower ( ) <EOL> if not "<STR_LIT>" in samdb_url : <EOL> if not ldap_only and os . path . isfile ( samdb_url ) : <EOL> samdb_url = "<STR_LIT>" % samdb_url <EOL> else : <EOL> samdb_url = "<STR_LIT>" % samdb_url <EOL> if samdb_url . startswith ( "<STR_LIT>" ) : <EOL> ldb_options = [ "<STR_LIT>" ] <EOL> elif ldap_only : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" % samdb_url ) <EOL> if lp is None : <EOL> lp = env_loadparm ( ) <EOL> if session_info is None : <EOL> session_info = samba . auth . system_session ( lp ) <EOL> if credentials is None : <EOL> credentials = cmdline_credentials <EOL> return SamDB ( url = samdb_url , <EOL> lp = lp , <EOL> session_info = session_info , <EOL> credentials = credentials , <EOL> flags = flags , <EOL> options = ldb_options ) <EOL> def connect_samdb_ex ( samdb_url , lp = None , session_info = None , credentials = None , <EOL> flags = <NUM_LIT:0> , ldb_options = None , ldap_only = False ) : <EOL> """<STR_LIT>""" <EOL> sam_db = connect_samdb ( samdb_url , lp , session_info , credentials , <EOL> flags , ldb_options , ldap_only ) <EOL> res = sam_db . search ( base = "<STR_LIT>" , expression = "<STR_LIT>" , scope = ldb . SCOPE_BASE , <EOL> attrs = [ "<STR_LIT:*>" ] ) <EOL> return ( sam_db , res [ <NUM_LIT:0> ] ) <EOL> def delete_force ( samdb , dn ) : <EOL> try : <EOL> samdb . delete ( dn ) <EOL> except ldb . LdbError , ( num , _ ) : <EOL> assert ( num == ldb . ERR_NO_SUCH_OBJECT ) </s>
