2010-09-23 06:21:58 +04:00
# Unix SMB/CIFS implementation.
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
2011-09-13 03:10:37 +04:00
#
2010-09-23 06:21:58 +04:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
2011-09-13 03:10:37 +04:00
#
2010-09-23 06:21:58 +04:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2011-09-13 03:10:37 +04:00
#
2010-09-23 06:21:58 +04:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
""" Samba Python tests. """
import ldb
import os
import samba
2016-01-28 17:10:00 +03:00
from samba import arcfour_encrypt , string_to_byte_array
2010-09-23 06:21:58 +04:00
from samba . tests import TestCase , TestCaseInTempDir
2018-07-30 09:20:39 +03:00
2010-09-23 06:21:58 +04:00
class SubstituteVarTestCase ( TestCase ) :
def test_empty ( self ) :
2020-02-07 01:02:38 +03:00
self . assertEqual ( " " , samba . substitute_var ( " " , { } ) )
2010-09-23 06:21:58 +04:00
def test_nothing ( self ) :
2020-02-07 01:02:38 +03:00
self . assertEqual ( " foo bar " ,
2018-07-30 09:16:12 +03:00
samba . substitute_var ( " foo bar " , { " bar " : " bla " } ) )
2010-09-23 06:21:58 +04:00
def test_replace ( self ) :
2020-02-07 01:02:38 +03:00
self . assertEqual ( " foo bla " ,
2018-07-30 09:16:12 +03:00
samba . substitute_var ( " foo $ {bar} " , { " bar " : " bla " } ) )
2010-09-23 06:21:58 +04:00
def test_broken ( self ) :
2020-02-07 01:02:38 +03:00
self . assertEqual ( " foo $ { bdkjfhsdkfh sdkfh " ,
2018-07-30 09:16:12 +03:00
samba . substitute_var ( " foo $ { bdkjfhsdkfh sdkfh " , { " bar " : " bla " } ) )
2010-09-23 06:21:58 +04:00
def test_unknown_var ( self ) :
2020-02-07 01:02:38 +03:00
self . assertEqual ( " foo $ {bla} gsff " ,
2018-07-30 09:16:12 +03:00
samba . substitute_var ( " foo $ {bla} gsff " , { " bar " : " bla " } ) )
2010-09-23 06:21:58 +04:00
def test_check_all_substituted ( self ) :
samba . check_all_substituted ( " nothing to see here " )
self . assertRaises ( Exception , samba . check_all_substituted ,
2023-06-06 14:28:13 +03:00
" Not substituted: $ {FOOBAR} " )
2010-09-23 06:21:58 +04:00
2018-07-30 09:20:39 +03:00
2016-01-28 17:10:00 +03:00
class ArcfourTestCase ( TestCase ) :
def test_arcfour_direct ( self ) :
2017-05-03 20:11:03 +03:00
key = b ' 12345678 '
plain = b ' abcdefghi '
2017-01-02 16:10:29 +03:00
crypt_expected = b ' \xda \x91 Z \xb0 l \xd7 \xb9 \xcf \x99 '
2016-01-28 17:10:00 +03:00
crypt_calculated = arcfour_encrypt ( key , plain )
2020-02-07 01:02:38 +03:00
self . assertEqual ( crypt_expected , crypt_calculated )
2016-01-28 17:10:00 +03:00
2018-07-30 09:20:39 +03:00
2016-01-28 17:10:00 +03:00
class StringToByteArrayTestCase ( TestCase ) :
def test_byte_array ( self ) :
expected = [ 218 , 145 , 90 , 176 , 108 , 215 , 185 , 207 , 153 ]
calculated = string_to_byte_array ( ' \xda \x91 Z \xb0 l \xd7 \xb9 \xcf \x99 ' )
2020-02-07 01:02:38 +03:00
self . assertEqual ( expected , calculated )
2010-09-23 06:21:58 +04:00
2018-07-30 09:20:39 +03:00
2010-09-23 06:21:58 +04:00
class LdbExtensionTests ( TestCaseInTempDir ) :
def test_searchone ( self ) :
path = self . tempdir + " /searchone.ldb "
l = samba . Ldb ( path )
try :
l . add ( { " dn " : " foo=dc " , " bar " : " bla " } )
2020-02-07 01:02:38 +03:00
self . assertEqual ( b " bla " ,
2018-07-30 09:16:12 +03:00
l . searchone ( basedn = ldb . Dn ( l , " foo=dc " ) , attribute = " bar " ) )
2010-09-23 06:21:58 +04:00
finally :
del l
os . unlink ( path )