|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- coding: utf-8 -*
|
3 | 3 |
|
| 4 | +from functools import partial |
| 5 | +import socket |
4 | 6 | import sys
|
5 | 7 | import unittest
|
6 | 8 |
|
| 9 | +from mock import patch |
| 10 | + |
7 | 11 | from Xlib.support import unix_connect
|
8 |
| -from Xlib.error import DisplayNameError |
| 12 | +from Xlib.error import DisplayConnectionError, DisplayNameError |
9 | 13 |
|
10 | 14 |
|
11 | 15 | @unittest.skipUnless(sys.platform.startswith('linux'), 'Linux specific tests')
|
12 | 16 | class TestUnixConnect(unittest.TestCase):
|
13 | 17 |
|
14 |
| - def test_get_display(self): |
15 |
| - # Valid cases. |
16 |
| - for display, expected in ( |
17 |
| - # Implicit Unix socket connections. |
18 |
| - (':0.1', ('', 0, 1)), |
19 |
| - (':4', ('', 4, 0)), |
20 |
| - # Implicit TCP connections. |
21 |
| - ('foo:1.2', ('foo', 1, 2)), |
22 |
| - ('bar:5', ('bar', 5, 0)), |
23 |
| - # Explicit Unix socket connections. |
24 |
| - ('unix/foo:4.3', ('', 4, 3)), |
25 |
| - ('unix/:66', ('', 66, 0)), |
26 |
| - # Explicit TCP connections. |
27 |
| - ('tcp/foo:11.1', ('foo', 11, 1)), |
28 |
| - ('tcp/bar:66.6', ('bar', 66, 6)), |
29 |
| - ('tcp/unix:54.3', ('unix', 54, 3)), |
30 |
| - # Special case: `unix:0.0` is equivalent to `:0.0`. |
31 |
| - ('unix:99.5', ('', 99, 5)), |
32 |
| - ('unix:42', ('', 42, 0)), |
33 |
| - ): |
34 |
| - result = unix_connect.get_display(display) |
35 |
| - self.assertEqual(result, (display,) + expected) |
36 |
| - # Invalid cases. |
37 |
| - for display in ( |
38 |
| - # No display number. |
39 |
| - '', |
40 |
| - ':', |
41 |
| - 'foo', |
42 |
| - 'bar:', |
43 |
| - # Bad screen number. |
44 |
| - ':48.', |
45 |
| - ':47.f', |
46 |
| - # Bad hostname. |
47 |
| - u'fòó:0', |
48 |
| - u'tcp/bàr:1', |
49 |
| - u'unix/fòóbàr:2', |
50 |
| - # Bad protocol. |
51 |
| - 'udp/foo:0' |
52 |
| - # With explicit TCP connections, hostname must be set. |
53 |
| - 'tcp/:0', |
54 |
| - ): |
55 |
| - with self.assertRaises(DisplayNameError): |
56 |
| - unix_connect.get_display(display) |
| 18 | + def test_get_display(self): |
| 19 | + # Valid cases. |
| 20 | + for display, expected in ( |
| 21 | + # Implicit Unix socket connections. |
| 22 | + (':0.1', (None, '', 0, 1)), |
| 23 | + (':4', (None, '', 4, 0)), |
| 24 | + # Implicit TCP connections. |
| 25 | + ('foo:1.2', (None, 'foo', 1, 2)), |
| 26 | + ('bar:5', (None, 'bar', 5, 0)), |
| 27 | + # Explicit Unix socket connections. |
| 28 | + ('unix/foo:4.3', ('unix', 'foo', 4, 3)), |
| 29 | + ('unix/:66', ('unix', '', 66, 0)), |
| 30 | + # Explicit TCP connections. |
| 31 | + ('tcp/foo:11.1', ('tcp', 'foo', 11, 1)), |
| 32 | + ('tcp/bar:66.6', ('tcp', 'bar', 66, 6)), |
| 33 | + ('tcp/unix:54.3', ('tcp', 'unix', 54, 3)), |
| 34 | + # Special case: `unix:0.0` is equivalent to `:0.0`. |
| 35 | + ('unix:99.5', (None, 'unix', 99, 5)), |
| 36 | + ('unix:42', (None, 'unix', 42, 0)), |
| 37 | + ): |
| 38 | + result = unix_connect.get_display(display) |
| 39 | + self.assertEqual(result, (display,) + expected) |
| 40 | + # Invalid cases. |
| 41 | + for display in ( |
| 42 | + # No display number. |
| 43 | + '', |
| 44 | + ':', |
| 45 | + 'foo', |
| 46 | + 'bar:', |
| 47 | + # Bad screen number. |
| 48 | + ':48.', |
| 49 | + ':47.f', |
| 50 | + # Bad hostname. |
| 51 | + u'fòó:0', |
| 52 | + u'tcp/bàr:1', |
| 53 | + u'unix/fòóbàr:2', |
| 54 | + # Bad protocol. |
| 55 | + 'udp/foo:0' |
| 56 | + # With explicit TCP connections, hostname must be set. |
| 57 | + 'tcp/:0', |
| 58 | + ): |
| 59 | + with self.assertRaises(DisplayNameError): |
| 60 | + unix_connect.get_display(display) |
| 61 | + |
| 62 | + def test_get_socket(self): |
| 63 | + class FakeSocket(object): |
| 64 | + def fileno(self): |
| 65 | + return 42 |
| 66 | + calls = [] |
| 67 | + def _get_socket(socket_type, raises, *params): |
| 68 | + calls.append(('_get_%s_socket' % socket_type,) + params) |
| 69 | + if raises: |
| 70 | + raise socket.error() |
| 71 | + return FakeSocket() |
| 72 | + def path_exists(returns, path): |
| 73 | + calls.append(('os.path.exists', path)) |
| 74 | + return returns |
| 75 | + def fcntl(*args): |
| 76 | + calls.append(('fcntl',) + args) |
| 77 | + for params, allow_unix, unix_addr_exists, allow_tcp, expect_connection_error, expected_calls in ( |
| 78 | + # Successful explicit TCP socket connection. |
| 79 | + (('tcp/host:6', None, 'host', 6), False, False, True, False, [ |
| 80 | + ('_get_tcp_socket', 'host', 6), |
| 81 | + ]), |
| 82 | + # Failed explicit TCP socket connection. |
| 83 | + (('tcp/host:6', None, 'host', 6), False, False, False, True, [ |
| 84 | + ('_get_tcp_socket', 'host', 6), |
| 85 | + ]), |
| 86 | + # Successful implicit TCP socket connection. |
| 87 | + (('host:5', None, 'host', 5), False, False, True, False, [ |
| 88 | + ('_get_tcp_socket', 'host', 5), |
| 89 | + ]), |
| 90 | + # Failed implicit TCP socket connection. |
| 91 | + (('host:5', None, 'host', 5), False, False, False, True, [ |
| 92 | + ('_get_tcp_socket', 'host', 5), |
| 93 | + ]), |
| 94 | + # Successful explicit Unix socket connection. |
| 95 | + (('unix/name:0', 'unix', 'name', 0), True, True, False, False, [ |
| 96 | + ('os.path.exists', '/tmp/.X11-unix/X0'), |
| 97 | + ('_get_unix_socket', '/tmp/.X11-unix/X0'), |
| 98 | + ]), |
| 99 | + # Failed explicit Unix socket connection. |
| 100 | + (('unix/name:0', 'unix', 'name', 0), False, True, False, True, [ |
| 101 | + ('os.path.exists', '/tmp/.X11-unix/X0'), |
| 102 | + ('_get_unix_socket', '/tmp/.X11-unix/X0'), |
| 103 | + ]), |
| 104 | + # Successful explicit Unix socket connection, variant. |
| 105 | + (('unix:0', None, 'unix', 0), True, True, False, False, [ |
| 106 | + ('os.path.exists', '/tmp/.X11-unix/X0'), |
| 107 | + ('_get_unix_socket', '/tmp/.X11-unix/X0'), |
| 108 | + ]), |
| 109 | + # Failed explicit Unix socket connection, variant. |
| 110 | + (('unix:0', None, 'unix', 0), False, True, False, True, [ |
| 111 | + ('os.path.exists', '/tmp/.X11-unix/X0'), |
| 112 | + ('_get_unix_socket', '/tmp/.X11-unix/X0'), |
| 113 | + ]), |
| 114 | + # Successful implicit Unix socket connection. |
| 115 | + ((':4', None, '', 4), True, True, False, False, [ |
| 116 | + ('os.path.exists', '/tmp/.X11-unix/X4'), |
| 117 | + ('_get_unix_socket', '/tmp/.X11-unix/X4'), |
| 118 | + ]), |
| 119 | + # Successful implicit Unix socket connection, abstract address. |
| 120 | + ((':3', None, '', 3), True, False, False, False, [ |
| 121 | + ('os.path.exists', '/tmp/.X11-unix/X3'), |
| 122 | + ('_get_unix_socket', '\0/tmp/.X11-unix/X3'), |
| 123 | + ]), |
| 124 | + # Failed implicit Unix socket connection, successful fallback on TCP. |
| 125 | + ((':2', None, '', 2), False, False, True, False, [ |
| 126 | + ('os.path.exists', '/tmp/.X11-unix/X2'), |
| 127 | + ('_get_unix_socket', '\0/tmp/.X11-unix/X2'), |
| 128 | + ('_get_tcp_socket', '', 2), |
| 129 | + ]), |
| 130 | + # Failed implicit Unix socket connection, failed fallback on TCP. |
| 131 | + ((':1', None, '', 1), False, False, False, True, [ |
| 132 | + ('os.path.exists', '/tmp/.X11-unix/X1'), |
| 133 | + ('_get_unix_socket', '\0/tmp/.X11-unix/X1'), |
| 134 | + ('_get_tcp_socket', '', 1), |
| 135 | + ]), |
| 136 | + ): |
| 137 | + with \ |
| 138 | + patch('Xlib.support.unix_connect._get_unix_socket', |
| 139 | + partial(_get_socket, 'unix', not allow_unix)), \ |
| 140 | + patch('Xlib.support.unix_connect._get_tcp_socket', |
| 141 | + partial(_get_socket, 'tcp', not allow_tcp)), \ |
| 142 | + patch('os.path.exists', |
| 143 | + partial(path_exists, unix_addr_exists)), \ |
| 144 | + patch('fcntl.fcntl', fcntl): |
| 145 | + del calls[:] |
| 146 | + if expect_connection_error: |
| 147 | + with self.assertRaises(DisplayConnectionError): |
| 148 | + unix_connect.get_socket(*params) |
| 149 | + else: |
| 150 | + s = unix_connect.get_socket(*params) |
| 151 | + self.assertIsInstance(s, FakeSocket) |
| 152 | + expected_calls.append(('fcntl', 42, |
| 153 | + unix_connect.F_SETFD, |
| 154 | + unix_connect.FD_CLOEXEC)) |
| 155 | + self.assertEqual(calls, expected_calls) |
57 | 156 |
|
58 | 157 |
|
59 | 158 | if __name__ == '__main__':
|
|
0 commit comments