blob: 08b59ec26df3862ddb3c4ad42504ed8e0882704b [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Brett Cannon1f5182b2008-03-13 21:09:28 +00002
3OS/2+EMX doesn't support the file locking operations.
4
Roger E. Massefb01d4b1996-12-17 17:41:09 +00005"""
6import struct
7import fcntl
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +00008import os
9import struct
10import sys
Brett Cannon1f5182b2008-03-13 21:09:28 +000011import unittest
12from test.test_support import verbose, TESTFN, unlink, run_unittest
Roger E. Massefb01d4b1996-12-17 17:41:09 +000013
Brett Cannon1f5182b2008-03-13 21:09:28 +000014# TODO - Write tests for ioctl(), flock() and lockf().
Roger E. Massefb01d4b1996-12-17 17:41:09 +000015
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +000016try:
17 import termios
18except ImportError:
19 termios = None
Martin v. Löwisa660a342001-10-18 22:07:48 +000020
Brett Cannon1f5182b2008-03-13 21:09:28 +000021def get_lockdata():
22 if sys.platform.startswith('atheos'):
23 start_len = "qq"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000024 else:
Brett Cannon1f5182b2008-03-13 21:09:28 +000025 try:
26 os.O_LARGEFILE
27 except AttributeError:
28 start_len = "ll"
29 else:
30 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000031
Brett Cannon1f5182b2008-03-13 21:09:28 +000032 if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
33 'Darwin1.2', 'darwin',
34 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
35 'freebsd6', 'freebsd7', 'freebsd8',
36 'bsdos2', 'bsdos3', 'bsdos4',
37 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
38 if struct.calcsize('l') == 8:
39 off_t = 'l'
40 pid_t = 'i'
41 else:
42 off_t = 'lxxxx'
43 pid_t = 'l'
44 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
45 fcntl.F_WRLCK, 0)
46 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
47 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
48 elif sys.platform in ['os2emx']:
49 lockdata = None
50 else:
51 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
52 if lockdata:
53 if verbose:
54 print 'struct.pack: ', repr(lockdata)
55 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000056
Brett Cannon1f5182b2008-03-13 21:09:28 +000057lockdata = get_lockdata()
Fred Drakebc7809b2001-05-09 21:11:59 +000058
59
Brett Cannon1f5182b2008-03-13 21:09:28 +000060class TestFcntl(unittest.TestCase):
Fred Drakebc7809b2001-05-09 21:11:59 +000061
Brett Cannon1f5182b2008-03-13 21:09:28 +000062 def setUp(self):
63 self.f = None
Fred Drakebc7809b2001-05-09 21:11:59 +000064
Brett Cannon1f5182b2008-03-13 21:09:28 +000065 def tearDown(self):
66 if not self.f.closed:
67 self.f.close()
68 unlink(TESTFN)
69
70 def test_fcntl_fileno(self):
71 # the example from the library docs
72 self.f = open(TESTFN, 'w')
73 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
74 if verbose:
75 print 'Status from fcntl with O_NONBLOCK: ', rv
76 if sys.platform not in ['os2emx']:
77 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
78 if verbose:
79 print 'String from fcntl with F_SETLKW: ', repr(rv)
80 self.f.close()
81
82 def test_fcntl_file_descriptor(self):
83 # again, but pass the file rather than numeric descriptor
84 self.f = open(TESTFN, 'w')
85 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
86 if sys.platform not in ['os2emx']:
87 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
88 self.f.close()
89
90
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +000091class TestIoctl(unittest.TestCase):
92 if termios:
93 def test_ioctl_signed_unsigned_code_param(self):
94 if termios.TIOCSWINSZ < 0:
95 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
96 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffffL
97 else:
98 set_winsz_opcode_pos = termios.TIOCSWINSZ
99 set_winsz_opcode_maybe_neg, = struct.unpack("i",
100 struct.pack("I", termios.TIOCSWINSZ))
101
102 # We're just testing that these calls do not raise exceptions.
103 saved_winsz = fcntl.ioctl(0, termios.TIOCGWINSZ, "\0"*8)
104 our_winsz = struct.pack("HHHH",80,25,0,0)
105 # test both with a positive and potentially negative ioctl code
106 new_winsz = fcntl.ioctl(0, set_winsz_opcode_pos, our_winsz)
107 new_winsz = fcntl.ioctl(0, set_winsz_opcode_maybe_neg, our_winsz)
108 fcntl.ioctl(0, set_winsz_opcode_maybe_neg, saved_winsz)
109
110
Brett Cannon1f5182b2008-03-13 21:09:28 +0000111def test_main():
112 run_unittest(TestFcntl)
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +0000113 run_unittest(TestIoctl)
Brett Cannon1f5182b2008-03-13 21:09:28 +0000114
115if __name__ == '__main__':
116 test_main()