test/host: checks for invalid fd handling
Check that we properly handle `dispatch` returning invalid fds and that all fds are properly closed.
This commit is contained in:
parent
28dcd0ee9a
commit
662fe0feb9
1 changed files with 38 additions and 0 deletions
|
|
@ -4,6 +4,7 @@
|
|||
# Runtime Tests for Host Services
|
||||
#
|
||||
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
|
@ -17,6 +18,13 @@ from osbuild.util.jsoncomm import FdSet
|
|||
|
||||
class ServiceTest(host.Service):
|
||||
|
||||
def __init__(self, args):
|
||||
super().__init__(args)
|
||||
self.fds = []
|
||||
|
||||
def register_fds(self, fds):
|
||||
self.fds.extend(fds)
|
||||
|
||||
def dispatch(self, method: str, args: Any, fds: FdSet):
|
||||
ret = None
|
||||
|
||||
|
|
@ -31,9 +39,28 @@ class ServiceTest(host.Service):
|
|||
f.write(d.read())
|
||||
f.seek(0)
|
||||
fds = [os.dup(f.fileno())]
|
||||
self.register_fds(fds)
|
||||
|
||||
elif method == "identify":
|
||||
ret = self.id
|
||||
elif method == "invalid-fd":
|
||||
ret = []
|
||||
with tempfile.TemporaryFile("w+") as f:
|
||||
valid_fd = os.dup(f.fileno())
|
||||
invalid_fd = valid_fd + 10
|
||||
fds = [valid_fd, invalid_fd]
|
||||
self.register_fds([valid_fd])
|
||||
elif method == "check-fds-are-closed":
|
||||
while self.fds:
|
||||
fd = self.fds.pop()
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError as e:
|
||||
if e.errno == errno.EBADF:
|
||||
print(f"fd '{fd}' was closed")
|
||||
continue
|
||||
raise
|
||||
raise ValueError(f"fd '{fd}' was not closed")
|
||||
else:
|
||||
raise host.ProtocolError("unknown method:", method)
|
||||
|
||||
|
|
@ -79,6 +106,8 @@ def test_pass_fd():
|
|||
with os.fdopen(fds.steal(0)) as d:
|
||||
assert data == d.read()
|
||||
|
||||
client.call_with_fds("check-fds-are-closed")
|
||||
|
||||
remote_id = client.call("identify")
|
||||
assert remote_id == str(i)
|
||||
|
||||
|
|
@ -90,6 +119,15 @@ def test_pass_fd():
|
|||
client.stop()
|
||||
|
||||
|
||||
def test_pass_fd_invalid():
|
||||
with host.ServiceManager() as mgr:
|
||||
|
||||
client = mgr.start(str("test-invalid-fd"), __file__)
|
||||
with pytest.raises(host.RemoteError):
|
||||
client.call_with_fds("invalid-fd")
|
||||
client.call_with_fds("check-fds-are-closed")
|
||||
|
||||
|
||||
def test_exception():
|
||||
with host.ServiceManager() as mgr:
|
||||
client = mgr.start("exception", __file__)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue