blob: 1810c4e95eb25bff54cb94e9514cec42d9462f4d [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002
3OS/2+EMX doesn't support the file locking operations.
4
Roger E. Massefb01d4b1996-12-17 17:41:09 +00005"""
Gregory P. Smithe5aefa42013-03-31 10:10:50 -07006import platform
Christian Heimese25f35e2008-03-20 10:49:03 +00007import os
8import struct
9import sys
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010import unittest
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020011from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
12 cpython_only)
R. David Murraya21e4ca2009-03-31 23:16:50 +000013
Ezio Melotti78ede7c2013-08-23 23:06:31 +030014# Skip test if no fcntl module.
R. David Murraya21e4ca2009-03-31 23:16:50 +000015fcntl = import_module('fcntl')
16
Roger E. Massefb01d4b1996-12-17 17:41:09 +000017
Christian Heimese25f35e2008-03-20 10:49:03 +000018# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000019
Christian Heimesdd15f6c2008-03-16 00:07:10 +000020def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000021 try:
22 os.O_LARGEFILE
23 except AttributeError:
24 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000025 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000026 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000027
Charles-François Natali6cea35a2011-08-23 19:46:46 +020028 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
29 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000030 if struct.calcsize('l') == 8:
31 off_t = 'l'
32 pid_t = 'i'
33 else:
34 off_t = 'lxxxx'
35 pid_t = 'l'
36 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
37 fcntl.F_WRLCK, 0)
doko@ubuntu.com1dfb9182013-08-03 16:12:33 +020038 elif sys.platform.startswith('gnukfreebsd'):
39 lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000040 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
41 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
42 elif sys.platform in ['os2emx']:
43 lockdata = None
44 else:
45 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
46 if lockdata:
47 if verbose:
48 print('struct.pack: ', repr(lockdata))
49 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000050
Christian Heimesdd15f6c2008-03-16 00:07:10 +000051lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000052
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020053class BadFile:
54 def __init__(self, fn):
55 self.fn = fn
56 def fileno(self):
57 return self.fn
58
Christian Heimesdd15f6c2008-03-16 00:07:10 +000059class TestFcntl(unittest.TestCase):
60
61 def setUp(self):
62 self.f = None
63
64 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000065 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000066 self.f.close()
67 unlink(TESTFN)
68
69 def test_fcntl_fileno(self):
70 # the example from the library docs
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.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
73 if verbose:
74 print('Status from fcntl with O_NONBLOCK: ', rv)
75 if sys.platform not in ['os2emx']:
76 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
77 if verbose:
78 print('String from fcntl with F_SETLKW: ', repr(rv))
79 self.f.close()
80
81 def test_fcntl_file_descriptor(self):
82 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020083 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000084 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
85 if sys.platform not in ['os2emx']:
86 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
87 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000088
Serhiy Storchaka441d30f2013-01-19 12:26:26 +020089 def test_fcntl_bad_file(self):
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020090 with self.assertRaises(ValueError):
91 fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
92 with self.assertRaises(ValueError):
93 fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
94 with self.assertRaises(TypeError):
95 fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
96 with self.assertRaises(TypeError):
97 fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
98
99 @cpython_only
100 def test_fcntl_bad_file_overflow(self):
101 from _testcapi import INT_MAX, INT_MIN
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200102 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200103 with self.assertRaises(OverflowError):
104 fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
105 with self.assertRaises(OverflowError):
106 fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
107 with self.assertRaises(OverflowError):
108 fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
109 with self.assertRaises(OverflowError):
110 fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200111
Gregory P. Smithe5aefa42013-03-31 10:10:50 -0700112 @unittest.skipIf(
113 platform.machine().startswith('arm') and platform.system() == 'Linux',
114 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000115 def test_fcntl_64_bit(self):
116 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
117 # C 'long' but not in a C 'int'.
118 try:
119 cmd = fcntl.F_NOTIFY
120 # This flag is larger than 2**31 in 64-bit builds
121 flags = fcntl.DN_MULTISHOT
122 except AttributeError:
123 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
124 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
125 try:
126 fcntl.fcntl(fd, cmd, flags)
127 finally:
128 os.close(fd)
129
Fred Drakebc7809b2001-05-09 21:11:59 +0000130
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000131def test_main():
132 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000133
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000134if __name__ == '__main__':
135 test_main()