Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
test.test_posix.PosixTester Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def testNoArgFunctions
def test_getresuid
def test_getresgid
def test_setresuid
def test_setresuid_exception
def test_setresgid
def test_setresgid_exception
def test_initgroups
def test_statvfs
def test_fstatvfs
def test_ftruncate
def test_dup
def test_confstr
def test_dup2
def test_osexlock
def test_osshlock
def test_fstat
def test_stat
def test_mkfifo
def test_mknod
def test_chown
def test_fchown
def test_lchown
def test_chdir
def test_listdir
def test_listdir_default
def test_access
def test_umask
def test_strerror
def test_pipe
def test_utime
def test_chflags
def test_lchflags_regular_file
def test_lchflags_symlink
def test_environ
def test_getcwd_long_pathnames
def test_getgroups

Public Attributes

 teardown_files

Private Member Functions

def _test_all_chown_common
def _test_chflags_regular_file

Private Attributes

 _warnings_manager

Detailed Description

Definition at line 22 of file test_posix.py.


Member Function Documentation

def test.test_posix.PosixTester._test_all_chown_common (   self,
  chown_func,
  first_param 
) [private]
Common code for chown, fchown and lchown tests.

Definition at line 230 of file test_posix.py.

00230 
00231     def _test_all_chown_common(self, chown_func, first_param):
00232         """Common code for chown, fchown and lchown tests."""
00233         if os.getuid() == 0:
00234             try:
00235                 # Many linux distros have a nfsnobody user as MAX_UID-2
00236                 # that makes a good test case for signedness issues.
00237                 #   http://bugs.python.org/issue1747858
00238                 # This part of the test only runs when run as root.
00239                 # Only scary people run their tests as root.
00240                 ent = pwd.getpwnam('nfsnobody')
00241                 chown_func(first_param, ent.pw_uid, ent.pw_gid)
00242             except KeyError:
00243                 pass
00244         else:
00245             # non-root cannot chown to root, raises OSError
00246             self.assertRaises(OSError, chown_func,
00247                               first_param, 0, 0)
00248         # test a successful chown call
00249         chown_func(first_param, os.getuid(), os.getgid())

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_posix.PosixTester._test_chflags_regular_file (   self,
  chflags_func,
  target_file 
) [private]

Definition at line 323 of file test_posix.py.

00323 
00324     def _test_chflags_regular_file(self, chflags_func, target_file):
00325         st = os.stat(target_file)
00326         self.assertTrue(hasattr(st, 'st_flags'))
00327         chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE)
00328         try:
00329             new_st = os.stat(target_file)
00330             self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
00331             try:
00332                 fd = open(target_file, 'w+')
00333             except IOError as e:
00334                 self.assertEqual(e.errno, errno.EPERM)
00335         finally:
00336             posix.chflags(target_file, st.st_flags)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 24 of file test_posix.py.

00024 
00025     def setUp(self):
00026         # create empty file
00027         fp = open(support.TESTFN, 'w+')
00028         fp.close()
00029         self.teardown_files = [ support.TESTFN ]
00030         self._warnings_manager = support.check_warnings()
00031         self._warnings_manager.__enter__()
00032         warnings.filterwarnings('ignore', '.* potential security risk .*',
00033                                 RuntimeWarning)

Here is the caller graph for this function:

Definition at line 34 of file test_posix.py.

00034 
00035     def tearDown(self):
00036         for teardown_file in self.teardown_files:
00037             support.unlink(teardown_file)
00038         self._warnings_manager.__exit__(None, None, None)

Here is the caller graph for this function:

Definition at line 293 of file test_posix.py.

00293 
00294     def test_access(self):
00295         if hasattr(posix, 'access'):
00296             self.assertTrue(posix.access(support.TESTFN, os.R_OK))

Here is the call graph for this function:

Definition at line 279 of file test_posix.py.

00279 
00280     def test_chdir(self):
00281         if hasattr(posix, 'chdir'):
00282             posix.chdir(os.curdir)
00283             self.assertRaises(OSError, posix.chdir, support.TESTFN)

Here is the call graph for this function:

Definition at line 338 of file test_posix.py.

00338 
00339     def test_chflags(self):
00340         self._test_chflags_regular_file(posix.chflags, support.TESTFN)

Here is the call graph for this function:

Definition at line 251 of file test_posix.py.

00251 
00252     def test_chown(self):
00253         # raise an OSError if the file does not exist
00254         os.unlink(support.TESTFN)
00255         self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
00256 
00257         # re-create the file
00258         open(support.TESTFN, 'w').close()
00259         self._test_all_chown_common(posix.chown, support.TESTFN)

Here is the call graph for this function:

Definition at line 150 of file test_posix.py.

00150 
00151     def test_confstr(self):
00152         if hasattr(posix, 'confstr'):
00153             self.assertRaises(ValueError, posix.confstr, "CS_garbage")
00154             self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)

Here is the call graph for this function:

Definition at line 140 of file test_posix.py.

00140 
00141     def test_dup(self):
00142         if hasattr(posix, 'dup'):
00143             fp = open(support.TESTFN)
00144             try:
00145                 fd = posix.dup(fp.fileno())
00146                 self.assertIsInstance(fd, int)
00147                 os.close(fd)
00148             finally:
00149                 fp.close()

Here is the call graph for this function:

Definition at line 155 of file test_posix.py.

00155 
00156     def test_dup2(self):
00157         if hasattr(posix, 'dup2'):
00158             fp1 = open(support.TESTFN)
00159             fp2 = open(support.TESTFN)
00160             try:
00161                 posix.dup2(fp1.fileno(), fp2.fileno())
00162             finally:
00163                 fp1.close()
00164                 fp2.close()

Definition at line 367 of file test_posix.py.

00367 
00368     def test_environ(self):
00369         if os.name == "nt":
00370             item_type = str
00371         else:
00372             item_type = bytes
00373         for k, v in posix.environ.items():
00374             self.assertEqual(type(k), item_type)
00375             self.assertEqual(type(v), item_type)

Here is the call graph for this function:

Definition at line 261 of file test_posix.py.

00261 
00262     def test_fchown(self):
00263         os.unlink(support.TESTFN)
00264 
00265         # re-create the file
00266         test_file = open(support.TESTFN, 'w')
00267         try:
00268             fd = test_file.fileno()
00269             self._test_all_chown_common(posix.fchown, fd)
00270         finally:
00271             test_file.close()

Here is the call graph for this function:

Definition at line 196 of file test_posix.py.

00196 
00197     def test_fstat(self):
00198         if hasattr(posix, 'fstat'):
00199             fp = open(support.TESTFN)
00200             try:
00201                 self.assertTrue(posix.fstat(fp.fileno()))
00202             finally:
00203                 fp.close()

Here is the call graph for this function:

Definition at line 121 of file test_posix.py.

00121 
00122     def test_fstatvfs(self):
00123         if hasattr(posix, 'fstatvfs'):
00124             fp = open(support.TESTFN)
00125             try:
00126                 self.assertTrue(posix.fstatvfs(fp.fileno()))
00127             finally:
00128                 fp.close()

Here is the call graph for this function:

Definition at line 129 of file test_posix.py.

00129 
00130     def test_ftruncate(self):
00131         if hasattr(posix, 'ftruncate'):
00132             fp = open(support.TESTFN, 'w+')
00133             try:
00134                 # we need to have some data to truncate
00135                 fp.write('test')
00136                 fp.flush()
00137                 posix.ftruncate(fp.fileno(), 0)
00138             finally:
00139                 fp.close()

Definition at line 376 of file test_posix.py.

00376 
00377     def test_getcwd_long_pathnames(self):
00378         if hasattr(posix, 'getcwd'):
00379             dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
00380             curdir = os.getcwd()
00381             base_path = os.path.abspath(support.TESTFN) + '.getcwd'
00382 
00383             try:
00384                 os.mkdir(base_path)
00385                 os.chdir(base_path)
00386             except:
00387 #               Just returning nothing instead of the SkipTest exception,
00388 #               because the test results in Error in that case.
00389 #               Is that ok?
00390 #                raise unittest.SkipTest("cannot create directory for testing")
00391                 return
00392 
00393                 def _create_and_do_getcwd(dirname, current_path_length = 0):
00394                     try:
00395                         os.mkdir(dirname)
00396                     except:
00397                         raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
00398 
00399                     os.chdir(dirname)
00400                     try:
00401                         os.getcwd()
00402                         if current_path_length < 1027:
00403                             _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
00404                     finally:
00405                         os.chdir('..')
00406                         os.rmdir(dirname)
00407 
00408                 _create_and_do_getcwd(dirname)
00409 
00410             finally:
00411                 os.chdir(curdir)
00412                 support.rmtree(base_path)

Definition at line 414 of file test_posix.py.

00414 
00415     def test_getgroups(self):
00416         with os.popen('id -G') as idg:
00417             groups = idg.read().strip()
00418 
00419         if not groups:
00420             raise unittest.SkipTest("need working 'id -G'")
00421 
00422         # 'id -G' and 'os.getgroups()' should return the same
00423         # groups, ignoring order and duplicates.
00424         # #10822 - it is implementation defined whether posix.getgroups()
00425         # includes the effective gid so we include it anyway, since id -G does
00426         self.assertEqual(
00427                 set([int(x) for x in groups.split()]),
00428                 set(posix.getgroups() + [posix.getegid()]))

Here is the call graph for this function:

Definition at line 62 of file test_posix.py.

00062 
00063         def test_getresgid(self):
00064             group_ids = posix.getresgid()
00065             self.assertEqual(len(group_ids), 3)
00066             for val in group_ids:
00067                 self.assertGreaterEqual(val, 0)

Here is the call graph for this function:

Definition at line 55 of file test_posix.py.

00055 
00056         def test_getresuid(self):
00057             user_ids = posix.getresuid()
00058             self.assertEqual(len(user_ids), 3)
00059             for val in user_ids:
00060                 self.assertGreaterEqual(val, 0)

Here is the call graph for this function:

Definition at line 98 of file test_posix.py.

00098 
00099     def test_initgroups(self):
00100         # It takes a string and an integer; check that it raises a TypeError
00101         # for other argument lists.
00102         self.assertRaises(TypeError, posix.initgroups)
00103         self.assertRaises(TypeError, posix.initgroups, None)
00104         self.assertRaises(TypeError, posix.initgroups, 3, "foo")
00105         self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
00106 
00107         # If a non-privileged user invokes it, it should fail with OSError
00108         # EPERM.
00109         if os.getuid() != 0:
00110             name = pwd.getpwuid(posix.getuid()).pw_name
00111             try:
00112                 posix.initgroups(name, 13)
00113             except OSError as e:
00114                 self.assertEqual(e.errno, errno.EPERM)
00115             else:
00116                 self.fail("Expected OSError to be raised by initgroups")

Here is the call graph for this function:

Definition at line 342 of file test_posix.py.

00342 
00343     def test_lchflags_regular_file(self):
00344         self._test_chflags_regular_file(posix.lchflags, support.TESTFN)

Here is the call graph for this function:

Definition at line 346 of file test_posix.py.

00346 
00347     def test_lchflags_symlink(self):
00348         testfn_st = os.stat(support.TESTFN)
00349 
00350         self.assertTrue(hasattr(testfn_st, 'st_flags'))
00351 
00352         os.symlink(support.TESTFN, _DUMMY_SYMLINK)
00353         self.teardown_files.append(_DUMMY_SYMLINK)
00354         dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
00355 
00356         posix.lchflags(_DUMMY_SYMLINK,
00357                        dummy_symlink_st.st_flags | stat.UF_IMMUTABLE)
00358         try:
00359             new_testfn_st = os.stat(support.TESTFN)
00360             new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
00361 
00362             self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
00363             self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
00364                              new_dummy_symlink_st.st_flags)
00365         finally:
00366             posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)

Here is the call graph for this function:

Definition at line 273 of file test_posix.py.

00273 
00274     def test_lchown(self):
00275         os.unlink(support.TESTFN)
00276         # create a symlink
00277         os.symlink(_DUMMY_SYMLINK, support.TESTFN)
00278         self._test_all_chown_common(posix.lchown, support.TESTFN)

Here is the call graph for this function:

Definition at line 284 of file test_posix.py.

00284 
00285     def test_listdir(self):
00286         if hasattr(posix, 'listdir'):
00287             self.assertTrue(support.TESTFN in posix.listdir(os.curdir))

Here is the call graph for this function:

Definition at line 288 of file test_posix.py.

00288 
00289     def test_listdir_default(self):
00290         # When listdir is called without argument, it's the same as listdir(os.curdir)
00291         if hasattr(posix, 'listdir'):
00292             self.assertTrue(support.TESTFN in posix.listdir())

Here is the call graph for this function:

Definition at line 209 of file test_posix.py.

00209 
00210     def test_mkfifo(self):
00211         support.unlink(support.TESTFN)
00212         posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
00213         self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))

Here is the call graph for this function:

Definition at line 216 of file test_posix.py.

00216 
00217     def test_mknod(self):
00218         # Test using mknod() to create a FIFO (the only use specified
00219         # by POSIX).
00220         support.unlink(support.TESTFN)
00221         mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
00222         try:
00223             posix.mknod(support.TESTFN, mode, 0)
00224         except OSError as e:
00225             # Some old systems don't allow unprivileged users to use
00226             # mknod(), or only support creating device nodes.
00227             self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
00228         else:
00229             self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))

Here is the call graph for this function:

Definition at line 165 of file test_posix.py.

00165 
00166     def test_osexlock(self):
00167         if hasattr(posix, "O_EXLOCK"):
00168             fd = os.open(support.TESTFN,
00169                          os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
00170             self.assertRaises(OSError, os.open, support.TESTFN,
00171                               os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
00172             os.close(fd)
00173 
00174             if hasattr(posix, "O_SHLOCK"):
00175                 fd = os.open(support.TESTFN,
00176                              os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
00177                 self.assertRaises(OSError, os.open, support.TESTFN,
00178                                   os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
00179                 os.close(fd)

Here is the call graph for this function:

Definition at line 180 of file test_posix.py.

00180 
00181     def test_osshlock(self):
00182         if hasattr(posix, "O_SHLOCK"):
00183             fd1 = os.open(support.TESTFN,
00184                          os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
00185             fd2 = os.open(support.TESTFN,
00186                           os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
00187             os.close(fd2)
00188             os.close(fd1)
00189 
00190             if hasattr(posix, "O_EXLOCK"):
00191                 fd = os.open(support.TESTFN,
00192                              os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
00193                 self.assertRaises(OSError, os.open, support.TESTFN,
00194                                   os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
00195                 os.close(fd)

Here is the call graph for this function:

Definition at line 307 of file test_posix.py.

00307 
00308     def test_pipe(self):
00309         if hasattr(posix, 'pipe'):
00310             reader, writer = posix.pipe()
00311             os.close(reader)
00312             os.close(writer)

Definition at line 83 of file test_posix.py.

00083 
00084         def test_setresgid(self):
00085             current_group_ids = posix.getresgid()
00086             self.assertIsNone(posix.setresgid(*current_group_ids))
00087             # -1 means don't change that value.
00088             self.assertIsNone(posix.setresgid(-1, -1, -1))

Here is the call graph for this function:

Definition at line 89 of file test_posix.py.

00089 
00090         def test_setresgid_exception(self):
00091             # Don't do this test if someone is silly enough to run us as root.
00092             current_group_ids = posix.getresgid()
00093             if 0 not in current_group_ids:
00094                 new_group_ids = (current_group_ids[0]+1, -1, -1)
00095                 self.assertRaises(OSError, posix.setresgid, *new_group_ids)

Here is the call graph for this function:

Definition at line 69 of file test_posix.py.

00069 
00070         def test_setresuid(self):
00071             current_user_ids = posix.getresuid()
00072             self.assertIsNone(posix.setresuid(*current_user_ids))
00073             # -1 means don't change that value.
00074             self.assertIsNone(posix.setresuid(-1, -1, -1))

Here is the call graph for this function:

Definition at line 75 of file test_posix.py.

00075 
00076         def test_setresuid_exception(self):
00077             # Don't do this test if someone is silly enough to run us as root.
00078             current_user_ids = posix.getresuid()
00079             if 0 not in current_user_ids:
00080                 new_user_ids = (current_user_ids[0]+1, -1, -1)
00081                 self.assertRaises(OSError, posix.setresuid, *new_user_ids)

Here is the call graph for this function:

Definition at line 204 of file test_posix.py.

00204 
00205     def test_stat(self):
00206         if hasattr(posix, 'stat'):
00207             self.assertTrue(posix.stat(support.TESTFN))

Here is the call graph for this function:

Definition at line 117 of file test_posix.py.

00117 
00118     def test_statvfs(self):
00119         if hasattr(posix, 'statvfs'):
00120             self.assertTrue(posix.statvfs(os.curdir))

Here is the call graph for this function:

Definition at line 303 of file test_posix.py.

00303 
00304     def test_strerror(self):
00305         if hasattr(posix, 'strerror'):
00306             self.assertTrue(posix.strerror(0))

Here is the call graph for this function:

Definition at line 297 of file test_posix.py.

00297 
00298     def test_umask(self):
00299         if hasattr(posix, 'umask'):
00300             old_mask = posix.umask(0)
00301             self.assertIsInstance(old_mask, int)
00302             posix.umask(old_mask)

Here is the call graph for this function:

Definition at line 313 of file test_posix.py.

00313 
00314     def test_utime(self):
00315         if hasattr(posix, 'utime'):
00316             now = time.time()
00317             posix.utime(support.TESTFN, None)
00318             self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
00319             self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
00320             self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
00321             posix.utime(support.TESTFN, (int(now), int(now)))
00322             posix.utime(support.TESTFN, (now, now))

Here is the call graph for this function:

Definition at line 39 of file test_posix.py.

00039 
00040     def testNoArgFunctions(self):
00041         # test posix functions which take no arguments and have
00042         # no side-effects which we need to cleanup (e.g., fork, wait, abort)
00043         NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
00044                              "times", "getloadavg",
00045                              "getegid", "geteuid", "getgid", "getgroups",
00046                              "getpid", "getpgrp", "getppid", "getuid",
00047                            ]
00048 
00049         for name in NO_ARG_FUNCTIONS:
00050             posix_func = getattr(posix, name, None)
00051             if posix_func is not None:
00052                 posix_func()
00053                 self.assertRaises(TypeError, posix_func, 1)

Here is the call graph for this function:


Member Data Documentation

Definition at line 29 of file test_posix.py.

Definition at line 28 of file test_posix.py.


The documentation for this class was generated from the following file: