1 # Blackbox tests for smbcacls
3 # Copyright (C) Noel Power noel.power@suse.com
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from samba
.tests
.blackbox
.smbcacls
import SmbCaclsBlockboxTestBase
19 from samba
.tests
import BlackboxProcessError
22 class SaveRestoreSmbCaclsTests(SmbCaclsBlockboxTestBase
):
27 # create toplevel testdir structure with desired ACL(s)
29 # +-tar_test_dir/ (OI)(CI)(I)(F)
30 # +-oi_dir/ (OI)(CI)(I)(F)
32 # | +-nested/ (OI)(CI)(I)(F)
34 # | +-nested_again/ (OI)(CI)(I)(F)
37 self
.toplevel
= self
.create_remote_test_file("tar_test_dir/file-0")
38 self
.f1
= self
.create_remote_test_file("tar_test_dir/oi_dir/file-1")
39 self
.f2
= self
.create_remote_test_file("tar_test_dir/oi_dir/nested/file-2")
40 self
.f3
= self
.create_remote_test_file("tar_test_dir/oi_dir/nested/nested_again/file-3")
41 self
.tar_dir
= os
.path
.split(self
.toplevel
)[0]
42 self
.oi_dir
= os
.path
.split(self
.f1
)[0]
43 self
.nested_dir
= os
.path
.split(self
.f2
)[0]
44 self
.nested_again_dir
= os
.path
.split(self
.f3
)[0]
46 dir_acl_str
= "ACL:%s:ALLOWED/OI|CI/FULL" % self
.user
47 inherited_dir_acl_str
= "ACL:%s:ALLOWED/OI|CI|I/FULL" % self
.user
48 file_acl_str
= "ACL:%s:ALLOWED/I/FULL" % self
.user
50 self
.smb_cacls(["--modify", dir_acl_str
, self
.tar_dir
])
51 self
.smb_cacls(["--modify", inherited_dir_acl_str
, self
.oi_dir
])
52 self
.smb_cacls(["--modify", inherited_dir_acl_str
, self
.nested_dir
])
53 self
.smb_cacls(["--modify", inherited_dir_acl_str
, self
.nested_again_dir
])
54 self
.smb_cacls(["--modify", file_acl_str
, self
.f1
])
55 self
.smb_cacls(["--modify", file_acl_str
, self
.f2
])
56 self
.smb_cacls(["--modify", file_acl_str
, self
.f3
])
59 # tmp is the default share which has an existing testdir smbcacls
60 # we need to be prepared to deal with a 'custom' share (which also
61 # would have an existing testdir)
62 if self
.share
!= "tmp":
63 self
.dirpath
= os
.path
.join(os
.environ
["LOCAL_PATH"],self
.share
)
64 self
.dirpath
= os
.path
.join(self
.dirpath
,self
.testdir
)
67 def test_simple_save_dir(self
):
69 # simple test to just store dacl of directory
70 with self
.mktemp() as tmpfile
:
71 out
= self
.smb_cacls(["--save", tmpfile
,
73 with
open(tmpfile
, 'rb') as infile
:
74 contents
= infile
.read().decode('utf16')
75 lines
= contents
.splitlines()
77 self
.assertEqual(len(lines
), 2)
78 # first line should be the path
79 self
.assertEqual(self
.oi_dir
.replace('/','\\'), lines
[0])
81 except BlackboxProcessError
as e
:
84 def test_simple_save_dir_r(self
):
86 # simple test to just store dacl of directory (recursively)
87 with self
.mktemp() as tmpfile
:
88 out
= self
.smb_cacls(["--recurse", "--save", tmpfile
,
90 with
open(tmpfile
, 'rb') as infile
:
91 contents
= infile
.read().decode('utf16')
92 print("contents = %s" % contents
)
93 lines
= contents
.splitlines()
95 self
.assertEqual(len(lines
), 12)
97 self
.oi_dir
.replace('/','\\'),
98 self
.f1
.replace('/','\\'),
99 self
.nested_dir
.replace('/','\\'),
100 self
.f2
.replace('/','\\'),
101 self
.nested_again_dir
.replace('/','\\'),
102 self
.f3
.replace('/','\\')
109 self
.assertEqual(0, len(paths
))
111 except BlackboxProcessError
as e
:
114 def test_simple_restore_dir(self
):
116 # simple test to just store dacl of directory
120 with self
.mktemp() as tmpfile
:
121 self
.smb_cacls(["--save", tmpfile
,
123 with
open(tmpfile
, 'rb') as infile
:
124 orig_saved
= infile
.read()
126 # modify directory structure
127 dir_add_acl_str
= "ACL:%s:ALLOWED/OI|CI/READ" % self
.user
128 self
.smb_cacls(["--propagate-inheritance", "--add",
129 dir_add_acl_str
, self
.oi_dir
])
131 # save modified directory dacls to file
132 with self
.mktemp() as tmpfile
:
133 self
.smb_cacls(["--save", tmpfile
,
135 with
open(tmpfile
, 'rb') as infile
:
136 modified
= infile
.read()
138 # compare orig and unmodified dacls
139 # they shouldn't match
140 self
.assertNotEqual(orig_saved
.decode('utf16'), modified
.decode('utf16'))
141 # restore original dacls from file
142 with self
.mktemp() as tmpfile
:
143 with
open(tmpfile
, 'wb') as outfile
:
144 outfile
.write(orig_saved
)
146 out
= self
.smb_cacls([".", "--restore", tmpfile
])
148 # save newly restored dacls to file
149 with self
.mktemp() as tmpfile
:
150 self
.smb_cacls(["--save", tmpfile
,
152 with
open(tmpfile
, 'rb') as infile
:
153 restored
= infile
.read()
155 # after restoring the dalcs, orig unmodified dacls should match
157 self
.assertEqual(orig_saved
.decode('utf16'), restored
.decode('utf16'))
159 except BlackboxProcessError
as e
:
162 def test_simple_restore_dir_r(self
):
164 # simple test to just store dacl(s) of directory recursively
168 with self
.mktemp() as tmpfile
:
169 self
.smb_cacls(["--recurse", "--save", tmpfile
,
171 with
open(tmpfile
, 'rb') as infile
:
172 orig_saved
= infile
.read()
174 # modify directory's dacls recursively
175 dir_add_acl_str
= "ACL:%s:ALLOWED/OI|CI/READ" % self
.user
176 self
.smb_cacls(["--propagate-inheritance", "--add",
177 dir_add_acl_str
, self
.oi_dir
])
179 # save modified directories dacls recursively
180 with self
.mktemp() as tmpfile
:
181 self
.smb_cacls(["--recurse", "--save", tmpfile
,
183 with
open(tmpfile
, 'rb') as infile
:
184 modified
= infile
.read()
186 # the unmodified stringified dacls shouldn't match
188 self
.assertNotEqual(orig_saved
.decode('utf16'), modified
.decode('utf16'))
189 # restore original dacls from file
190 with self
.mktemp() as tmpfile
:
191 with
open(tmpfile
, 'wb') as outfile
:
192 outfile
.write(orig_saved
)
194 out
= self
.smb_cacls([".", "--restore", tmpfile
])
196 with self
.mktemp() as tmpfile
:
197 out
= self
.smb_cacls(["--recurse", "--save", tmpfile
,
199 with
open(tmpfile
, 'rb') as infile
:
200 restored
= infile
.read()
201 # after restoring the dalcs orig unmodified dacls should match
203 self
.assertEqual(orig_saved
.decode('utf16'), restored
.decode('utf16'))
204 except BlackboxProcessError
as e
: