blob: e3b7ed20a08d24733605b2f54aa72c7b9bd596da [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Roger E. Massefb01d4b1996-12-17 17:41:09 +00002"""
Gregory P. Smithe5aefa42013-03-31 10:10:50 -07003import platform
Christian Heimese25f35e2008-03-20 10:49:03 +00004import os
5import struct
6import sys
Christian Heimesdd15f6c2008-03-16 00:07:10 +00007import unittest
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02008from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
9 cpython_only)
R. David Murraya21e4ca2009-03-31 23:16:50 +000010
Ezio Melotti78ede7c2013-08-23 23:06:31 +030011# Skip test if no fcntl module.
R. David Murraya21e4ca2009-03-31 23:16:50 +000012fcntl = import_module('fcntl')
13
Roger E. Massefb01d4b1996-12-17 17:41:09 +000014
Christian Heimese25f35e2008-03-20 10:49:03 +000015# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000016
Christian Heimesdd15f6c2008-03-16 00:07:10 +000017def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000018 try:
19 os.O_LARGEFILE
20 except AttributeError:
21 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000022 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000023 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000024
Victor Stinnere6747472011-08-21 00:39:18 +020025 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
26 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000027 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.com1dfb9182013-08-03 16:12:33 +020035 elif sys.platform.startswith('gnukfreebsd'):
36 lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000037 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
38 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000039 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 Drakebc7809b2001-05-09 21:11:59 +000045
Christian Heimesdd15f6c2008-03-16 00:07:10 +000046lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000047
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020048class BadFile:
49 def __init__(self, fn):
50 self.fn = fn
51 def fileno(self):
52 return self.fn
53
Christian Heimesdd15f6c2008-03-16 00:07:10 +000054class TestFcntl(unittest.TestCase):
55
56 def setUp(self):
57 self.f = None
58
59 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000060 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000061 self.f.close()
62 unlink(TESTFN)
63
64 def test_fcntl_fileno(self):
65 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020066 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000067 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 Ceaf1af7052012-10-05 02:48:46 +020070 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
71 if verbose:
72 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000073 self.f.close()
74
75 def test_fcntl_file_descriptor(self):
76 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020077 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000078 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
Ezio Melotti892584e2013-08-23 23:09:32 +030079 if verbose:
80 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020081 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
Ezio Melotti892584e2013-08-23 23:09:32 +030082 if verbose:
83 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000084 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000085
Serhiy Storchaka78980432013-01-15 01:12:17 +020086 def test_fcntl_bad_file(self):
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020087 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 Storchaka78980432013-01-15 01:12:17 +020099 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200100 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 Storchaka78980432013-01-15 01:12:17 +0200108
Gregory P. Smithe5aefa42013-03-31 10:10:50 -0700109 @unittest.skipIf(
110 platform.machine().startswith('arm') and platform.system() == 'Linux',
111 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000112 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 Heimes0a956f12013-12-05 16:13:03 +0100127 def test_flock(self):
Christian Heimes2e7d4f02013-12-07 18:19:21 +0100128 # Solaris needs readable file for shared lock
129 self.f = open(TESTFN, 'wb+')
Christian Heimes0a956f12013-12-05 16:13:03 +0100130 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 Storchakaf28ba362014-02-07 10:10:55 +0200140
141 @cpython_only
142 def test_flock_overflow(self):
143 import _testcapi
Christian Heimes0a956f12013-12-05 16:13:03 +0100144 self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
145 fcntl.LOCK_SH)
146
Fred Drakebc7809b2001-05-09 21:11:59 +0000147
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000148def test_main():
149 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000150
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000151if __name__ == '__main__':
152 test_main()