FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding base snippets tests for os.chown · RustPython/RustPython@12dee0d · GitHub

Repository navigation

Commit 12dee0d

Browse files
Adding base snippets tests for os.chown
1 parent dfffca4 commit 12dee0d

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

‎tests/snippets/stdlib_os.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,71 @@ def __exit__(self, exc_type, exc_val, exc_tb):
262262
os.chmod(fname, 0o666)
263263
assert oct(os.stat(fname).st_mode) == '0o100666'
264264

265+
# os.chown
266+
if os.name != "nt":
267+
# setup
268+
root_in_posix = False
269+
if hasattr(os, 'geteuid'):
270+
root_in_posix = (os.geteuid() == 0)
271+
try:
272+
import pwd
273+
all_users = [u.pw_uid for u in pwd.getpwall()]
274+
except (ImportError, AttributeError):
275+
all_users = []
276+
277+
fname1 = os.path.join(tmpdir, FILE_NAME)
278+
fname2 = os.path.join(tmpdir, FILE_NAME2)
279+
fd = os.open(fname2, os.O_RDONLY)
280+
281+
# test chown without root permissions
282+
if not root_in_posix and len(all_users) > 1:
283+
uid_1, uid_2 = all_users[:2]
284+
gid = os.stat(fname1).st_gid
285+
assert_raises(PermissionError,
286+
lambda: os.chown(fname1, uid_1, gid))
287+
assert_raises(PermissionError,
288+
lambda: os.chown(fname1, uid_2, gid))
289+
290+
# test chown with root perm and file name
291+
if root_in_posix and len(all_users) > 1:
292+
uid_1, uid_2 = all_users[:2]
293+
gid = os.stat(fname1).st_gid
294+
os.chown(fname1, uid_1, gid)
295+
uid = os.stat(fname1).st_uid
296+
assert uid == uid_1
297+
os.chown(fname1, uid_2, gid)
298+
uid = os.stat(fname1).st_uid
299+
assert uid == uid_2
300+
301+
# test chown with root perm and file descriptor
302+
if root_in_posix and len(all_users) > 1:
303+
uid_1, uid_2 = all_users[:2]
304+
gid = os.stat(fd).st_gid
305+
os.chown(fd, uid_1, gid)
306+
uid = os.stat(fd).st_uid
307+
assert uid == uid_1
308+
os.chown(fd, uid_2, gid)
309+
uid = os.stat(fd).st_uid
310+
assert uid == uid_2
311+
312+
# test gid change
313+
if hasattr(os, 'getgroups'):
314+
groups = os.getgroups()
315+
if len(groups) > 1:
316+
gid_1, gid_2 = groups[:2]
317+
uid = os.stat(fname1).st_uid
318+
319+
os.chown(fname1, uid, gid_1)
320+
gid = os.stat(fname1).st_gid
321+
assert gid == gid_1
322+
323+
os.chown(fname1, uid, gid_2)
324+
gid = os.stat(fname1).st_gid
325+
assert gid == gid_2
326+
327+
# teardown
328+
os.close(fd)
329+
265330
# os.path
266331
assert os.path.exists(fname) is True
267332
assert os.path.exists("NO_SUCH_FILE") is False

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL