2009-12-07 19:13:00 +03:00
# Unix SMB/CIFS implementation. Tests for xattr manipulation
# Copyright (C) Matthieu Patou <mat@matws.net> 2009
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2010-12-05 16:56:27 +01:00
""" Tests for samba.xattr_native and samba.xattr_tdb. """
2018-07-30 18:21:38 +12:00
import samba . xattr_native
import samba . xattr_tdb
import samba . posix_eadb
2012-02-27 02:46:31 +01:00
from samba . xattr import copytree_with_xattrs
2009-12-07 19:13:00 +03:00
from samba . dcerpc import xattr
2010-01-21 16:56:23 +13:00
from samba . ndr import ndr_pack
2012-02-27 02:46:31 +01:00
from samba . tests import (
2014-12-14 20:03:28 +00:00
SkipTest ,
2012-02-27 02:46:31 +01:00
TestCase ,
TestCaseInTempDir ,
2018-07-30 18:14:37 +12:00
)
2009-12-07 19:13:00 +03:00
import random
2012-02-27 02:46:31 +01:00
import shutil
2009-12-07 19:13:00 +03:00
import os
2018-07-30 18:20:39 +12:00
2009-12-07 19:13:00 +03:00
class XattrTests ( TestCase ) :
2010-01-21 17:17:02 +13:00
def _tmpfilename ( self ) :
random . seed ( )
path = os . environ [ ' SELFTEST_PREFIX ' ]
2018-07-30 18:18:25 +12:00
return os . path . join ( path , " pytests " + str ( int ( 100000 * random . random ( ) ) ) )
2010-01-21 17:17:02 +13:00
def _eadbpath ( self ) :
return os . path . join ( os . environ [ ' SELFTEST_PREFIX ' ] , " eadb.tdb " )
2010-01-08 13:10:30 +03:00
def test_set_xattr_native ( self ) :
2010-01-21 16:56:23 +13:00
if not samba . xattr_native . is_xattr_supported ( ) :
2014-12-14 20:03:28 +00:00
raise SkipTest ( )
2010-01-21 16:56:23 +13:00
ntacl = xattr . NTACL ( )
ntacl . version = 1
2010-01-21 17:17:02 +13:00
tempf = self . _tmpfilename ( )
2010-01-21 16:56:23 +13:00
open ( tempf , ' w ' ) . write ( " empty " )
try :
2012-09-27 09:30:47 -07:00
samba . xattr_native . wrap_setxattr ( tempf , " user.unittests " ,
2018-07-30 18:16:12 +12:00
ndr_pack ( ntacl ) )
2010-01-21 16:56:23 +13:00
except IOError :
2023-06-06 13:28:13 +02:00
raise SkipTest ( " the filesystem where the tests are run does not "
" support XATTR " )
2010-01-21 16:56:23 +13:00
os . unlink ( tempf )
2010-01-08 13:13:02 +03:00
def test_set_and_get_native ( self ) :
2010-01-21 16:56:23 +13:00
if not samba . xattr_native . is_xattr_supported ( ) :
2014-12-14 20:03:28 +00:00
raise SkipTest ( )
2010-01-21 17:17:02 +13:00
tempf = self . _tmpfilename ( )
2018-04-05 14:49:55 +12:00
reftxt = b " this is a test "
2010-01-21 16:56:23 +13:00
open ( tempf , ' w ' ) . write ( " empty " )
try :
samba . xattr_native . wrap_setxattr ( tempf , " user.unittests " , reftxt )
text = samba . xattr_native . wrap_getxattr ( tempf , " user.unittests " )
2020-02-07 11:02:38 +13:00
self . assertEqual ( text , reftxt )
2010-01-21 16:56:23 +13:00
except IOError :
2023-06-06 13:28:13 +02:00
raise SkipTest ( " the filesystem where the tests are run does not "
" support XATTR " )
2010-01-21 16:56:23 +13:00
os . unlink ( tempf )
2010-01-08 13:13:02 +03:00
def test_set_xattr_tdb ( self ) :
2010-01-21 17:17:02 +13:00
tempf = self . _tmpfilename ( )
eadb_path = self . _eadbpath ( )
2010-01-21 16:56:23 +13:00
ntacl = xattr . NTACL ( )
2010-01-21 16:41:30 +13:00
ntacl . version = 1
open ( tempf , ' w ' ) . write ( " empty " )
2010-01-21 16:56:23 +13:00
try :
2010-01-21 17:17:02 +13:00
samba . xattr_tdb . wrap_setxattr ( eadb_path ,
2018-07-30 18:16:12 +12:00
tempf , " user.unittests " , ndr_pack ( ntacl ) )
2010-01-21 16:56:23 +13:00
finally :
os . unlink ( tempf )
2010-01-21 17:17:02 +13:00
os . unlink ( eadb_path )
2009-12-07 19:13:00 +03:00
2010-01-08 13:13:02 +03:00
def test_set_tdb_not_open ( self ) :
2010-01-21 17:17:02 +13:00
tempf = self . _tmpfilename ( )
2010-01-21 16:56:23 +13:00
ntacl = xattr . NTACL ( )
2010-01-21 16:41:30 +13:00
ntacl . version = 1
open ( tempf , ' w ' ) . write ( " empty " )
2010-01-21 16:56:23 +13:00
try :
2012-02-18 23:59:48 +01:00
self . assertRaises ( IOError , samba . xattr_tdb . wrap_setxattr ,
2018-07-30 18:16:12 +12:00
os . path . join ( " nonexistent " , " eadb.tdb " ) , tempf ,
" user.unittests " , ndr_pack ( ntacl ) )
2010-01-21 16:56:23 +13:00
finally :
os . unlink ( tempf )
2010-01-08 13:13:02 +03:00
def test_set_and_get_tdb ( self ) :
2010-01-21 17:17:02 +13:00
tempf = self . _tmpfilename ( )
eadb_path = self . _eadbpath ( )
2018-04-05 14:49:55 +12:00
reftxt = b " this is a test "
2010-01-21 16:41:30 +13:00
open ( tempf , ' w ' ) . write ( " empty " )
2010-01-21 16:56:23 +13:00
try :
2010-01-21 17:17:02 +13:00
samba . xattr_tdb . wrap_setxattr ( eadb_path , tempf , " user.unittests " ,
2018-07-30 18:16:12 +12:00
reftxt )
2010-01-21 17:17:02 +13:00
text = samba . xattr_tdb . wrap_getxattr ( eadb_path , tempf ,
2018-07-30 18:16:12 +12:00
" user.unittests " )
2020-02-07 11:02:38 +13:00
self . assertEqual ( text , reftxt )
2010-01-21 16:56:23 +13:00
finally :
os . unlink ( tempf )
2010-01-21 17:17:02 +13:00
os . unlink ( eadb_path )
2012-02-27 02:46:31 +01:00
2017-10-24 09:01:16 +02:00
def test_set_posix_eadb ( self ) :
tempf = self . _tmpfilename ( )
eadb_path = self . _eadbpath ( )
ntacl = xattr . NTACL ( )
ntacl . version = 1
open ( tempf , ' w ' ) . write ( " empty " )
try :
samba . posix_eadb . wrap_setxattr ( eadb_path ,
2018-07-30 18:16:12 +12:00
tempf , " user.unittests " , ndr_pack ( ntacl ) )
2017-10-24 09:01:16 +02:00
finally :
os . unlink ( tempf )
os . unlink ( eadb_path )
def test_set_and_get_posix_eadb ( self ) :
tempf = self . _tmpfilename ( )
eadb_path = self . _eadbpath ( )
2018-04-05 14:49:55 +12:00
reftxt = b " this is a test "
2017-10-24 09:01:16 +02:00
open ( tempf , ' w ' ) . write ( " empty " )
try :
samba . posix_eadb . wrap_setxattr ( eadb_path , tempf , " user.unittests " ,
2018-07-30 18:16:12 +12:00
reftxt )
2017-10-24 09:01:16 +02:00
text = samba . posix_eadb . wrap_getxattr ( eadb_path , tempf ,
2018-07-30 18:16:12 +12:00
" user.unittests " )
2020-02-07 11:02:38 +13:00
self . assertEqual ( text , reftxt )
2017-10-24 09:01:16 +02:00
finally :
os . unlink ( tempf )
os . unlink ( eadb_path )
2012-02-27 02:46:31 +01:00
class TestCopyTreeWithXattrs ( TestCaseInTempDir ) :
def test_simple ( self ) :
os . chdir ( self . tempdir )
os . mkdir ( " a " )
os . mkdir ( " a/b " )
os . mkdir ( " a/b/c " )
f = open ( ' a/b/c/d ' , ' w ' )
try :
f . write ( " foo " )
finally :
f . close ( )
copytree_with_xattrs ( " a " , " b " )
shutil . rmtree ( " a " )
shutil . rmtree ( " b " )