| 1 |
import sys |
| 2 |
import os |
| 3 |
import posix |
| 4 |
import socket |
| 5 |
import contextlib |
| 6 |
import errno |
| 7 |
from systemd.daemon import (booted, |
| 8 |
is_fifo, _is_fifo, |
| 9 |
is_socket, _is_socket, |
| 10 |
is_socket_inet, _is_socket_inet, |
| 11 |
is_socket_unix, _is_socket_unix, |
| 12 |
is_socket_sockaddr, _is_socket_sockaddr, |
| 13 |
is_mq, _is_mq, |
| 14 |
listen_fds, listen_fds_with_names, |
| 15 |
notify) |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
@contextlib.contextmanager |
| 20 |
def skip_enosys(): |
| 21 |
try: |
| 22 |
yield |
| 23 |
except OSError as e: |
| 24 |
if e.errno == errno.ENOSYS: |
| 25 |
pytest.skip() |
| 26 |
raise |
| 27 |
|
| 28 |
@contextlib.contextmanager |
| 29 |
def closing_socketpair(family): |
| 30 |
pair = socket.socketpair(family) |
| 31 |
try: |
| 32 |
yield pair |
| 33 |
finally: |
| 34 |
pair[0].close() |
| 35 |
pair[1].close() |
| 36 |
|
| 37 |
|
| 38 |
def test_booted(): |
| 39 |
if os.path.exists('/run/systemd/system'): |
| 40 |
|
| 41 |
assert booted() |
| 42 |
else: |
| 43 |
|
| 44 |
assert booted() in {False, True} |
| 45 |
|
| 46 |
def test__is_fifo(tmpdir): |
| 47 |
path = tmpdir.join('test.fifo').strpath |
| 48 |
posix.mkfifo(path) |
| 49 |
fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) |
| 50 |
|
| 51 |
assert _is_fifo(fd, None) |
| 52 |
assert _is_fifo(fd, path) |
| 53 |
|
| 54 |
def test__is_fifo_file(tmpdir): |
| 55 |
file = tmpdir.join('test.fifo') |
| 56 |
file.write('boo') |
| 57 |
path = file.strpath |
| 58 |
fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) |
| 59 |
|
| 60 |
assert not _is_fifo(fd, None) |
| 61 |
assert not _is_fifo(fd, path) |
| 62 |
|
| 63 |
def test__is_fifo_bad_fd(tmpdir): |
| 64 |
path = tmpdir.join('test.fifo').strpath |
| 65 |
|
| 66 |
with pytest.raises(OSError): |
| 67 |
assert not _is_fifo(-1, None) |
| 68 |
|
| 69 |
with pytest.raises(OSError): |
| 70 |
assert not _is_fifo(-1, path) |
| 71 |
|
| 72 |
def test_is_fifo(tmpdir): |
| 73 |
path = tmpdir.join('test.fifo').strpath |
| 74 |
posix.mkfifo(path) |
| 75 |
fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) |
| 76 |
file = os.fdopen(fd, 'r') |
| 77 |
|
| 78 |
assert is_fifo(file, None) |
| 79 |
assert is_fifo(file, path) |
| 80 |
assert is_fifo(fd, None) |
| 81 |
assert is_fifo(fd, path) |
| 82 |
|
| 83 |
def test_is_fifo_file(tmpdir): |
| 84 |
file = tmpdir.join('test.fifo') |
| 85 |
file.write('boo') |
| 86 |
path = file.strpath |
| 87 |
fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) |
| 88 |
file = os.fdopen(fd, 'r') |
| 89 |
|
| 90 |
assert not is_fifo(file, None) |
| 91 |
assert not is_fifo(file, path) |
| 92 |
assert not is_fifo(fd, None) |
| 93 |
assert not is_fifo(fd, path) |
| 94 |
|
| 95 |
def test_is_fifo_bad_fd(tmpdir): |
| 96 |
path = tmpdir.join('test.fifo').strpath |
| 97 |
|
| 98 |
with pytest.raises(OSError): |
| 99 |
assert not is_fifo(-1, None) |
| 100 |
|
| 101 |
with pytest.raises(OSError): |
| 102 |
assert not is_fifo(-1, path) |
| 103 |
|
| 104 |
def is_mq_wrapper(arg): |
| 105 |
try: |
| 106 |
return is_mq(arg) |
| 107 |
except OSError as error: |
| 108 |
|
| 109 |
assert error.errno == errno.EBADF |
| 110 |
return False |
| 111 |
|
| 112 |
def _is_mq_wrapper(arg): |
| 113 |
try: |
| 114 |
return _is_mq(arg) |
| 115 |
except OSError as error: |
| 116 |
|
| 117 |
assert error.errno == errno.EBADF |
| 118 |
return False |
| 119 |
|
| 120 |
def test_no_mismatch(): |
| 121 |
with closing_socketpair(socket.AF_UNIX) as pair: |
| 122 |
for sock in pair: |
| 123 |
assert not is_fifo(sock) |
| 124 |
assert not is_mq_wrapper(sock) |
| 125 |
assert not is_socket_inet(sock) |
| 126 |
with skip_enosys(): |
| 127 |
assert not is_socket_sockaddr(sock, '127.0.0.1:2000') |
| 128 |
|
| 129 |
fd = sock.fileno() |
| 130 |
assert not is_fifo(fd) |
| 131 |
assert not is_mq_wrapper(fd) |
| 132 |
assert not is_socket_inet(fd) |
| 133 |
with skip_enosys(): |
| 134 |
assert not is_socket_sockaddr(fd, '127.0.0.1:2000') |
| 135 |
|
| 136 |
assert not _is_fifo(fd) |
| 137 |
assert not _is_mq_wrapper(fd) |
| 138 |
assert not _is_socket_inet(fd) |
| 139 |
with skip_enosys(): |
| 140 |
assert not _is_socket_sockaddr(fd, '127.0.0.1:2000') |
| 141 |
|
| 142 |
def test_is_socket(): |
| 143 |
with closing_socketpair(socket.AF_UNIX) as pair: |
| 144 |
for sock in pair: |
| 145 |
for arg in (sock, sock.fileno()): |
| 146 |
assert is_socket(arg) |
| 147 |
assert is_socket(arg, socket.AF_UNIX) |
| 148 |
assert not is_socket(arg, socket.AF_INET) |
| 149 |
assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM) |
| 150 |
assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM) |
| 151 |
with skip_enosys(): |
| 152 |
assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) |
| 153 |
|
| 154 |
assert _is_socket(arg) |
| 155 |
assert _is_socket(arg, socket.AF_UNIX) |
| 156 |
assert not _is_socket(arg, socket.AF_INET) |
| 157 |
assert _is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM) |
| 158 |
assert not _is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM) |
| 159 |
with skip_enosys(): |
| 160 |
assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) |
| 161 |
|
| 162 |
def test_is_socket_sockaddr(): |
| 163 |
with contextlib.closing(socket.socket(socket.AF_INET)) as sock: |
| 164 |
sock.bind(('127.0.0.1', 0)) |
| 165 |
addr, port = sock.getsockname() |
| 166 |
port = ':{}'.format(port) |
| 167 |
|
| 168 |
for listening in (0, 1): |
| 169 |
for arg in (sock, sock.fileno()): |
| 170 |
with skip_enosys(): |
| 171 |
assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM) |
| 172 |
with skip_enosys(): |
| 173 |
assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM) |
| 174 |
|
| 175 |
with skip_enosys(): |
| 176 |
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening) |
| 177 |
with skip_enosys(): |
| 178 |
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1) |
| 179 |
with skip_enosys(): |
| 180 |
assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening) |
| 181 |
|
| 182 |
with pytest.raises(ValueError): |
| 183 |
is_socket_sockaddr(arg, '127.0.0.1', flowinfo=123456) |
| 184 |
|
| 185 |
with skip_enosys(): |
| 186 |
assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) |
| 187 |
with skip_enosys(): |
| 188 |
assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) |
| 189 |
|
| 190 |
with pytest.raises(ValueError): |
| 191 |
_is_socket_sockaddr(arg, '127.0.0.1', 0, 123456) |
| 192 |
|
| 193 |
with skip_enosys(): |
| 194 |
assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) |
| 195 |
with skip_enosys(): |
| 196 |
assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) |
| 197 |
|
| 198 |
sock.listen(11) |
| 199 |
|
| 200 |
def test__is_socket(): |
| 201 |
with closing_socketpair(socket.AF_UNIX) as pair: |
| 202 |
for sock in pair: |
| 203 |
fd = sock.fileno() |
| 204 |
assert _is_socket(fd) |
| 205 |
assert _is_socket(fd, socket.AF_UNIX) |
| 206 |
assert not _is_socket(fd, socket.AF_INET) |
| 207 |
assert _is_socket(fd, socket.AF_UNIX, socket.SOCK_STREAM) |
| 208 |
assert not _is_socket(fd, socket.AF_INET, socket.SOCK_DGRAM) |
| 209 |
|
| 210 |
assert _is_socket(fd) |
| 211 |
assert _is_socket(fd, socket.AF_UNIX) |
| 212 |
assert not _is_socket(fd, socket.AF_INET) |
| 213 |
assert _is_socket(fd, socket.AF_UNIX, socket.SOCK_STREAM) |
| 214 |
assert not _is_socket(fd, socket.AF_INET, socket.SOCK_DGRAM) |
| 215 |
|
| 216 |
def test_is_socket_unix(): |
| 217 |
with closing_socketpair(socket.AF_UNIX) as pair: |
| 218 |
for sock in pair: |
| 219 |
for arg in (sock, sock.fileno()): |
| 220 |
assert is_socket_unix(arg) |
| 221 |
assert not is_socket_unix(arg, path="/no/such/path") |
| 222 |
assert is_socket_unix(arg, socket.SOCK_STREAM) |
| 223 |
assert not is_socket_unix(arg, socket.SOCK_DGRAM) |
| 224 |
|
| 225 |
def test__is_socket_unix(): |
| 226 |
with closing_socketpair(socket.AF_UNIX) as pair: |
| 227 |
for sock in pair: |
| 228 |
fd = sock.fileno() |
| 229 |
assert _is_socket_unix(fd) |
| 230 |
assert not _is_socket_unix(fd, 0, -1, "/no/such/path") |
| 231 |
assert _is_socket_unix(fd, socket.SOCK_STREAM) |
| 232 |
assert not _is_socket_unix(fd, socket.SOCK_DGRAM) |
| 233 |
|
| 234 |
def test_listen_fds_no_fds(): |
| 235 |
|
| 236 |
os.unsetenv('LISTEN_FDS') |
| 237 |
os.unsetenv('LISTEN_PID') |
| 238 |
|
| 239 |
assert listen_fds() == [] |
| 240 |
assert listen_fds(True) == [] |
| 241 |
assert listen_fds(False) == [] |
| 242 |
|
| 243 |
def test_listen_fds(): |
| 244 |
os.environ['LISTEN_FDS'] = '3' |
| 245 |
os.environ['LISTEN_PID'] = str(os.getpid()) |
| 246 |
|
| 247 |
assert listen_fds(False) == [3, 4, 5] |
| 248 |
assert listen_fds(True) == [3, 4, 5] |
| 249 |
assert listen_fds() == [] |
| 250 |
|
| 251 |
def test_listen_fds_default_unset(): |
| 252 |
os.environ['LISTEN_FDS'] = '1' |
| 253 |
os.environ['LISTEN_PID'] = str(os.getpid()) |
| 254 |
|
| 255 |
assert listen_fds(False) == [3] |
| 256 |
assert listen_fds() == [3] |
| 257 |
assert listen_fds() == [] |
| 258 |
|
| 259 |
def test_listen_fds_with_names_nothing(): |
| 260 |
|
| 261 |
os.unsetenv('LISTEN_FDS') |
| 262 |
os.unsetenv('LISTEN_PID') |
| 263 |
os.unsetenv('LISTEN_FDNAMES') |
| 264 |
|
| 265 |
assert listen_fds_with_names() == {} |
| 266 |
assert listen_fds_with_names(True) == {} |
| 267 |
assert listen_fds_with_names(False) == {} |
| 268 |
|
| 269 |
def test_listen_fds_with_names_no_names(): |
| 270 |
|
| 271 |
os.environ['LISTEN_FDS'] = '1' |
| 272 |
os.environ['LISTEN_PID'] = str(os.getpid()) |
| 273 |
os.unsetenv('LISTEN_FDNAMES') |
| 274 |
|
| 275 |
assert listen_fds_with_names(False) == {3: 'unknown'} |
| 276 |
assert listen_fds_with_names(True) == {3: 'unknown'} |
| 277 |
assert listen_fds_with_names() == {} |
| 278 |
|
| 279 |
def test_listen_fds_with_names_single(): |
| 280 |
|
| 281 |
os.environ['LISTEN_FDS'] = '1' |
| 282 |
os.environ['LISTEN_PID'] = str(os.getpid()) |
| 283 |
os.environ['LISTEN_FDNAMES'] = 'cmds' |
| 284 |
|
| 285 |
assert listen_fds_with_names(False) == {3: 'cmds'} |
| 286 |
assert listen_fds_with_names() == {3: 'cmds'} |
| 287 |
assert listen_fds_with_names(True) == {} |
| 288 |
|
| 289 |
def test_listen_fds_with_names_multiple(): |
| 290 |
|
| 291 |
os.environ['LISTEN_FDS'] = '3' |
| 292 |
os.environ['LISTEN_PID'] = str(os.getpid()) |
| 293 |
os.environ['LISTEN_FDNAMES'] = 'cmds:data:errs' |
| 294 |
|
| 295 |
assert listen_fds_with_names(False) == {3: 'cmds', 4: 'data', 5: 'errs'} |
| 296 |
assert listen_fds_with_names(True) == {3: 'cmds', 4: 'data', 5: 'errs'} |
| 297 |
assert listen_fds_with_names() == {} |
| 298 |
|
| 299 |
def test_notify_no_socket(): |
| 300 |
os.environ.pop('NOTIFY_SOCKET', None) |
| 301 |
|
| 302 |
assert notify('READY=1') is False |
| 303 |
with skip_enosys(): |
| 304 |
assert notify('FDSTORE=1', fds=[]) is False |
| 305 |
assert notify('FDSTORE=1', fds=[1, 2]) is False |
| 306 |
assert notify('FDSTORE=1', pid=os.getpid()) is False |
| 307 |
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) is False |
| 308 |
|
| 309 |
if sys.version_info >= (3,): |
| 310 |
connection_error = ConnectionRefusedError |
| 311 |
else: |
| 312 |
connection_error = OSError |
| 313 |
|
| 314 |
def test_notify_bad_socket(): |
| 315 |
os.environ['NOTIFY_SOCKET'] = '/dev/null' |
| 316 |
|
| 317 |
with pytest.raises(connection_error): |
| 318 |
notify('READY=1') |
| 319 |
with pytest.raises(connection_error): |
| 320 |
with skip_enosys(): |
| 321 |
notify('FDSTORE=1', fds=[]) |
| 322 |
with pytest.raises(connection_error): |
| 323 |
notify('FDSTORE=1', fds=[1, 2]) |
| 324 |
with pytest.raises(connection_error): |
| 325 |
notify('FDSTORE=1', pid=os.getpid()) |
| 326 |
with pytest.raises(connection_error): |
| 327 |
notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) |
| 328 |
|
| 329 |
def test_notify_with_socket(tmpdir): |
| 330 |
path = tmpdir.join('socket').strpath |
| 331 |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) |
| 332 |
try: |
| 333 |
sock.bind(path) |
| 334 |
except socket.error as e: |
| 335 |
pytest.xfail('failed to bind socket (%s)' % e) |
| 336 |
|
| 337 |
SO_PASSCRED = getattr(socket, 'SO_PASSCRED', 16) |
| 338 |
sock.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1) |
| 339 |
os.environ['NOTIFY_SOCKET'] = path |
| 340 |
|
| 341 |
assert notify('READY=1') |
| 342 |
with skip_enosys(): |
| 343 |
assert notify('FDSTORE=1', fds=[]) |
| 344 |
assert notify('FDSTORE=1', fds=[1, 2]) |
| 345 |
assert notify('FDSTORE=1', pid=os.getpid()) |
| 346 |
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) |
| 347 |
|
| 348 |
def test_daemon_notify_memleak(): |
| 349 |
|
| 350 |
fd = 1 |
| 351 |
fds = [fd] |
| 352 |
ref_cnt = sys.getrefcount(fd) |
| 353 |
|
| 354 |
try: |
| 355 |
notify('', True, 0, fds) |
| 356 |
except connection_error: |
| 357 |
pass |
| 358 |
|
| 359 |
assert sys.getrefcount(fd) <= ref_cnt, 'leak' |