Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 1 | """Test program for the fcntl C module. |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 2 | """ |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 3 | import platform |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 4 | import os |
| 5 | import struct |
| 6 | import sys |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7 | import unittest |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 8 | from test.support import (verbose, TESTFN, unlink, run_unittest, import_module, |
| 9 | cpython_only) |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 10 | |
Ezio Melotti | 78ede7c | 2013-08-23 23:06:31 +0300 | [diff] [blame] | 11 | # Skip test if no fcntl module. |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 12 | fcntl = import_module('fcntl') |
| 13 | |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 14 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 15 | # TODO - Write tests for flock() and lockf(). |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 16 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 17 | def get_lockdata(): |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 18 | try: |
| 19 | os.O_LARGEFILE |
| 20 | except AttributeError: |
| 21 | start_len = "ll" |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 22 | else: |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 23 | start_len = "qq" |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 24 | |
Victor Stinner | e674747 | 2011-08-21 00:39:18 +0200 | [diff] [blame] | 25 | if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos')) |
| 26 | or sys.platform == 'darwin'): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 27 | if struct.calcsize('l') == 8: |
| 28 | off_t = 'l' |
| 29 | pid_t = 'i' |
| 30 | else: |
| 31 | off_t = 'lxxxx' |
| 32 | pid_t = 'l' |
| 33 | lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0, |
| 34 | fcntl.F_WRLCK, 0) |
doko@ubuntu.com | 1dfb918 | 2013-08-03 16:12:33 +0200 | [diff] [blame] | 35 | elif sys.platform.startswith('gnukfreebsd'): |
| 36 | lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 37 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']: |
| 38 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 39 | else: |
| 40 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) |
| 41 | if lockdata: |
| 42 | if verbose: |
| 43 | print('struct.pack: ', repr(lockdata)) |
| 44 | return lockdata |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 45 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 46 | lockdata = get_lockdata() |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 47 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 48 | class BadFile: |
| 49 | def __init__(self, fn): |
| 50 | self.fn = fn |
| 51 | def fileno(self): |
| 52 | return self.fn |
| 53 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 54 | class TestFcntl(unittest.TestCase): |
| 55 | |
| 56 | def setUp(self): |
| 57 | self.f = None |
| 58 | |
| 59 | def tearDown(self): |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 60 | if self.f and not self.f.closed: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 61 | self.f.close() |
| 62 | unlink(TESTFN) |
| 63 | |
| 64 | def test_fcntl_fileno(self): |
| 65 | # the example from the library docs |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 66 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 67 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 68 | if verbose: |
| 69 | print('Status from fcntl with O_NONBLOCK: ', rv) |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 70 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata) |
| 71 | if verbose: |
| 72 | print('String from fcntl with F_SETLKW: ', repr(rv)) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 73 | self.f.close() |
| 74 | |
| 75 | def test_fcntl_file_descriptor(self): |
| 76 | # again, but pass the file rather than numeric descriptor |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 77 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 78 | rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK) |
Ezio Melotti | 892584e | 2013-08-23 23:09:32 +0300 | [diff] [blame] | 79 | if verbose: |
| 80 | print('Status from fcntl with O_NONBLOCK: ', rv) |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 81 | rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata) |
Ezio Melotti | 892584e | 2013-08-23 23:09:32 +0300 | [diff] [blame] | 82 | if verbose: |
| 83 | print('String from fcntl with F_SETLKW: ', repr(rv)) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 84 | self.f.close() |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 85 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 86 | def test_fcntl_bad_file(self): |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 87 | with self.assertRaises(ValueError): |
| 88 | fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 89 | with self.assertRaises(ValueError): |
| 90 | fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK) |
| 91 | with self.assertRaises(TypeError): |
| 92 | fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK) |
| 93 | with self.assertRaises(TypeError): |
| 94 | fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK) |
| 95 | |
| 96 | @cpython_only |
| 97 | def test_fcntl_bad_file_overflow(self): |
| 98 | from _testcapi import INT_MAX, INT_MIN |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 99 | # Issue 15989 |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 100 | with self.assertRaises(OverflowError): |
| 101 | fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 102 | with self.assertRaises(OverflowError): |
| 103 | fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK) |
| 104 | with self.assertRaises(OverflowError): |
| 105 | fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 106 | with self.assertRaises(OverflowError): |
| 107 | fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK) |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 108 | |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 109 | @unittest.skipIf( |
| 110 | platform.machine().startswith('arm') and platform.system() == 'Linux', |
| 111 | "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT") |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 112 | def test_fcntl_64_bit(self): |
| 113 | # Issue #1309352: fcntl shouldn't fail when the third arg fits in a |
| 114 | # C 'long' but not in a C 'int'. |
| 115 | try: |
| 116 | cmd = fcntl.F_NOTIFY |
| 117 | # This flag is larger than 2**31 in 64-bit builds |
| 118 | flags = fcntl.DN_MULTISHOT |
| 119 | except AttributeError: |
| 120 | self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable") |
| 121 | fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY) |
| 122 | try: |
| 123 | fcntl.fcntl(fd, cmd, flags) |
| 124 | finally: |
| 125 | os.close(fd) |
| 126 | |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 127 | def test_flock(self): |
Christian Heimes | 2e7d4f0 | 2013-12-07 18:19:21 +0100 | [diff] [blame] | 128 | # Solaris needs readable file for shared lock |
| 129 | self.f = open(TESTFN, 'wb+') |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 130 | fileno = self.f.fileno() |
| 131 | fcntl.flock(fileno, fcntl.LOCK_SH) |
| 132 | fcntl.flock(fileno, fcntl.LOCK_UN) |
| 133 | fcntl.flock(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB) |
| 134 | fcntl.flock(self.f, fcntl.LOCK_UN) |
| 135 | fcntl.flock(fileno, fcntl.LOCK_EX) |
| 136 | fcntl.flock(fileno, fcntl.LOCK_UN) |
| 137 | |
| 138 | self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH) |
| 139 | self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH) |
Serhiy Storchaka | f28ba36 | 2014-02-07 10:10:55 +0200 | [diff] [blame] | 140 | |
| 141 | @cpython_only |
| 142 | def test_flock_overflow(self): |
| 143 | import _testcapi |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 144 | self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1, |
| 145 | fcntl.LOCK_SH) |
| 146 | |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 147 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 148 | def test_main(): |
| 149 | run_unittest(TestFcntl) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 150 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 151 | if __name__ == '__main__': |
| 152 | test_main() |