/Lib/test/test_posix.py

http://unladen-swallow.googlecode.com/ · Python · 278 lines · 216 code · 45 blank · 17 comment · 54 complexity · 7a416e5caa461b8a917d56ce3009c57d MD5 · raw file

  1. "Test posix functions"
  2. from test import test_support
  3. try:
  4. import posix
  5. except ImportError:
  6. raise test_support.TestSkipped, "posix is not available"
  7. import time
  8. import os
  9. import pwd
  10. import shutil
  11. import unittest
  12. import warnings
  13. warnings.filterwarnings('ignore', '.* potential security risk .*',
  14. RuntimeWarning)
  15. class PosixTester(unittest.TestCase):
  16. def setUp(self):
  17. # create empty file
  18. fp = open(test_support.TESTFN, 'w+')
  19. fp.close()
  20. def tearDown(self):
  21. os.unlink(test_support.TESTFN)
  22. def testNoArgFunctions(self):
  23. # test posix functions which take no arguments and have
  24. # no side-effects which we need to cleanup (e.g., fork, wait, abort)
  25. NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
  26. "times", "getloadavg", "tmpnam",
  27. "getegid", "geteuid", "getgid", "getgroups",
  28. "getpid", "getpgrp", "getppid", "getuid",
  29. ]
  30. for name in NO_ARG_FUNCTIONS:
  31. posix_func = getattr(posix, name, None)
  32. if posix_func is not None:
  33. posix_func()
  34. self.assertRaises(TypeError, posix_func, 1)
  35. def test_statvfs(self):
  36. if hasattr(posix, 'statvfs'):
  37. self.assert_(posix.statvfs(os.curdir))
  38. def test_fstatvfs(self):
  39. if hasattr(posix, 'fstatvfs'):
  40. fp = open(test_support.TESTFN)
  41. try:
  42. self.assert_(posix.fstatvfs(fp.fileno()))
  43. finally:
  44. fp.close()
  45. def test_ftruncate(self):
  46. if hasattr(posix, 'ftruncate'):
  47. fp = open(test_support.TESTFN, 'w+')
  48. try:
  49. # we need to have some data to truncate
  50. fp.write('test')
  51. fp.flush()
  52. posix.ftruncate(fp.fileno(), 0)
  53. finally:
  54. fp.close()
  55. def test_dup(self):
  56. if hasattr(posix, 'dup'):
  57. fp = open(test_support.TESTFN)
  58. try:
  59. fd = posix.dup(fp.fileno())
  60. self.assert_(isinstance(fd, int))
  61. os.close(fd)
  62. finally:
  63. fp.close()
  64. def test_confstr(self):
  65. if hasattr(posix, 'confstr'):
  66. self.assertRaises(ValueError, posix.confstr, "CS_garbage")
  67. self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
  68. def test_dup2(self):
  69. if hasattr(posix, 'dup2'):
  70. fp1 = open(test_support.TESTFN)
  71. fp2 = open(test_support.TESTFN)
  72. try:
  73. posix.dup2(fp1.fileno(), fp2.fileno())
  74. finally:
  75. fp1.close()
  76. fp2.close()
  77. def fdopen_helper(self, *args):
  78. fd = os.open(test_support.TESTFN, os.O_RDONLY)
  79. fp2 = posix.fdopen(fd, *args)
  80. fp2.close()
  81. def test_fdopen(self):
  82. if hasattr(posix, 'fdopen'):
  83. self.fdopen_helper()
  84. self.fdopen_helper('r')
  85. self.fdopen_helper('r', 100)
  86. def test_osexlock(self):
  87. if hasattr(posix, "O_EXLOCK"):
  88. fd = os.open(test_support.TESTFN,
  89. os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
  90. self.assertRaises(OSError, os.open, test_support.TESTFN,
  91. os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
  92. os.close(fd)
  93. if hasattr(posix, "O_SHLOCK"):
  94. fd = os.open(test_support.TESTFN,
  95. os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
  96. self.assertRaises(OSError, os.open, test_support.TESTFN,
  97. os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
  98. os.close(fd)
  99. def test_osshlock(self):
  100. if hasattr(posix, "O_SHLOCK"):
  101. fd1 = os.open(test_support.TESTFN,
  102. os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
  103. fd2 = os.open(test_support.TESTFN,
  104. os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
  105. os.close(fd2)
  106. os.close(fd1)
  107. if hasattr(posix, "O_EXLOCK"):
  108. fd = os.open(test_support.TESTFN,
  109. os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
  110. self.assertRaises(OSError, os.open, test_support.TESTFN,
  111. os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
  112. os.close(fd)
  113. def test_fstat(self):
  114. if hasattr(posix, 'fstat'):
  115. fp = open(test_support.TESTFN)
  116. try:
  117. self.assert_(posix.fstat(fp.fileno()))
  118. finally:
  119. fp.close()
  120. def test_stat(self):
  121. if hasattr(posix, 'stat'):
  122. self.assert_(posix.stat(test_support.TESTFN))
  123. if hasattr(posix, 'chown'):
  124. def test_chown(self):
  125. # raise an OSError if the file does not exist
  126. os.unlink(test_support.TESTFN)
  127. self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
  128. # re-create the file
  129. open(test_support.TESTFN, 'w').close()
  130. if os.getuid() == 0:
  131. try:
  132. # Many linux distros have a nfsnobody user as MAX_UID-2
  133. # that makes a good test case for signedness issues.
  134. # http://bugs.python.org/issue1747858
  135. # This part of the test only runs when run as root.
  136. # Only scary people run their tests as root.
  137. ent = pwd.getpwnam('nfsnobody')
  138. posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
  139. except KeyError:
  140. pass
  141. else:
  142. # non-root cannot chown to root, raises OSError
  143. self.assertRaises(OSError, posix.chown,
  144. test_support.TESTFN, 0, 0)
  145. # test a successful chown call
  146. posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
  147. def test_chdir(self):
  148. if hasattr(posix, 'chdir'):
  149. posix.chdir(os.curdir)
  150. self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
  151. def test_lsdir(self):
  152. if hasattr(posix, 'lsdir'):
  153. self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
  154. def test_access(self):
  155. if hasattr(posix, 'access'):
  156. self.assert_(posix.access(test_support.TESTFN, os.R_OK))
  157. def test_umask(self):
  158. if hasattr(posix, 'umask'):
  159. old_mask = posix.umask(0)
  160. self.assert_(isinstance(old_mask, int))
  161. posix.umask(old_mask)
  162. def test_strerror(self):
  163. if hasattr(posix, 'strerror'):
  164. self.assert_(posix.strerror(0))
  165. def test_pipe(self):
  166. if hasattr(posix, 'pipe'):
  167. reader, writer = posix.pipe()
  168. os.close(reader)
  169. os.close(writer)
  170. def test_tempnam(self):
  171. if hasattr(posix, 'tempnam'):
  172. self.assert_(posix.tempnam())
  173. self.assert_(posix.tempnam(os.curdir))
  174. self.assert_(posix.tempnam(os.curdir, 'blah'))
  175. def test_tmpfile(self):
  176. if hasattr(posix, 'tmpfile'):
  177. fp = posix.tmpfile()
  178. fp.close()
  179. def test_utime(self):
  180. if hasattr(posix, 'utime'):
  181. now = time.time()
  182. posix.utime(test_support.TESTFN, None)
  183. self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
  184. self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
  185. self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
  186. posix.utime(test_support.TESTFN, (int(now), int(now)))
  187. posix.utime(test_support.TESTFN, (now, now))
  188. def test_chflags(self):
  189. if hasattr(posix, 'chflags'):
  190. st = os.stat(test_support.TESTFN)
  191. if hasattr(st, 'st_flags'):
  192. posix.chflags(test_support.TESTFN, st.st_flags)
  193. def test_lchflags(self):
  194. if hasattr(posix, 'lchflags'):
  195. st = os.stat(test_support.TESTFN)
  196. if hasattr(st, 'st_flags'):
  197. posix.lchflags(test_support.TESTFN, st.st_flags)
  198. def test_getcwd_long_pathnames(self):
  199. if hasattr(posix, 'getcwd'):
  200. dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
  201. curdir = os.getcwd()
  202. base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
  203. try:
  204. os.mkdir(base_path)
  205. os.chdir(base_path)
  206. except:
  207. # Just returning nothing instead of the TestSkipped exception,
  208. # because the test results in Error in that case.
  209. # Is that ok?
  210. # raise test_support.TestSkipped, "cannot create directory for testing"
  211. return
  212. try:
  213. def _create_and_do_getcwd(dirname, current_path_length = 0):
  214. try:
  215. os.mkdir(dirname)
  216. except:
  217. raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"
  218. os.chdir(dirname)
  219. try:
  220. os.getcwd()
  221. if current_path_length < 1027:
  222. _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
  223. finally:
  224. os.chdir('..')
  225. os.rmdir(dirname)
  226. _create_and_do_getcwd(dirname)
  227. finally:
  228. os.chdir(curdir)
  229. shutil.rmtree(base_path)
  230. def test_main():
  231. test_support.run_unittest(PosixTester)
  232. if __name__ == '__main__':
  233. test_main()