blob: 8e5508207141dcd7ee151f5d90b0a4c30084326e [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
Serhiy Storchaka78980432013-01-15 01:12:17 +02007import _testcapi
Christian Heimesdd15f6c2008-03-16 00:07:10 +00008import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +00009from test.support import verbose, TESTFN, unlink, run_unittest, import_module
10
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
Christian Heimesdd15f6c2008-03-16 00:07:10 +000048class TestFcntl(unittest.TestCase):
49
50 def setUp(self):
51 self.f = None
52
53 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000054 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000055 self.f.close()
56 unlink(TESTFN)
57
58 def test_fcntl_fileno(self):
59 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020060 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000061 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
62 if verbose:
63 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020064 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
65 if verbose:
66 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000067 self.f.close()
68
69 def test_fcntl_file_descriptor(self):
70 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020071 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000072 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
Ezio Melotti892584e2013-08-23 23:09:32 +030073 if verbose:
74 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020075 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
Ezio Melotti892584e2013-08-23 23:09:32 +030076 if verbose:
77 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000078 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000079
Serhiy Storchaka78980432013-01-15 01:12:17 +020080 def test_fcntl_bad_file(self):
81 class F:
82 def __init__(self, fn):
83 self.fn = fn
84 def fileno(self):
85 return self.fn
86 self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
87 self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
88 self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
89 self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
90 # Issue 15989
91 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1,
92 fcntl.F_SETFL, os.O_NONBLOCK)
93 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
94 fcntl.F_SETFL, os.O_NONBLOCK)
95 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1,
96 fcntl.F_SETFL, os.O_NONBLOCK)
97 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
98 fcntl.F_SETFL, os.O_NONBLOCK)
99
Gregory P. Smithe5aefa42013-03-31 10:10:50 -0700100 @unittest.skipIf(
101 platform.machine().startswith('arm') and platform.system() == 'Linux',
102 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000103 def test_fcntl_64_bit(self):
104 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
105 # C 'long' but not in a C 'int'.
106 try:
107 cmd = fcntl.F_NOTIFY
108 # This flag is larger than 2**31 in 64-bit builds
109 flags = fcntl.DN_MULTISHOT
110 except AttributeError:
111 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
112 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
113 try:
114 fcntl.fcntl(fd, cmd, flags)
115 finally:
116 os.close(fd)
117
Christian Heimes0a956f12013-12-05 16:13:03 +0100118 def test_flock(self):
Christian Heimes2e7d4f02013-12-07 18:19:21 +0100119 # Solaris needs readable file for shared lock
120 self.f = open(TESTFN, 'wb+')
Christian Heimes0a956f12013-12-05 16:13:03 +0100121 fileno = self.f.fileno()
122 fcntl.flock(fileno, fcntl.LOCK_SH)
123 fcntl.flock(fileno, fcntl.LOCK_UN)
124 fcntl.flock(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
125 fcntl.flock(self.f, fcntl.LOCK_UN)
126 fcntl.flock(fileno, fcntl.LOCK_EX)
127 fcntl.flock(fileno, fcntl.LOCK_UN)
128
129 self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
130 self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
131 self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
132 fcntl.LOCK_SH)
133
Fred Drakebc7809b2001-05-09 21:11:59 +0000134
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000135def test_main():
136 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000137
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000138if __name__ == '__main__':
139 test_main()