2017-03-31 09:38:42 +03:00
|
|
|
#!/usr/bin/env python3
|
2019-06-20 02:58:25 +03:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2017-03-31 09:38:42 +03:00
|
|
|
#
|
2019-06-20 02:58:25 +03:00
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2.
|
2017-03-31 09:38:42 +03:00
|
|
|
|
|
|
|
import os
|
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import stat
|
|
|
|
import threading
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
from .lib import testcase
|
|
|
|
|
|
|
|
|
|
|
|
PAYLOAD = b"W00t\n"
|
2017-03-31 09:38:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
@testcase.eden_repo_test
|
2018-04-05 03:31:25 +03:00
|
|
|
class UnixSocketTest(testcase.EdenRepoTest):
|
2018-04-05 03:31:28 +03:00
|
|
|
def populate_repo(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
self.repo.write_file("hello", "hola\n")
|
|
|
|
self.repo.commit("Initial commit.")
|
2017-03-31 09:38:42 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_unixsock(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
"""Create a UNIX domain socket in EdenFS and verify that a client
|
2017-03-31 09:38:42 +03:00
|
|
|
can connect and write, and that the server side can accept
|
2018-05-10 07:33:49 +03:00
|
|
|
and read data from it."""
|
2017-03-31 09:38:42 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
filename = os.path.join(self.mount, "example.sock")
|
2017-03-31 09:38:42 +03:00
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2018-04-05 03:31:28 +03:00
|
|
|
sock.setblocking(False) # ensure that we don't block unexpectedly
|
2017-03-31 09:38:42 +03:00
|
|
|
try:
|
|
|
|
sock.bind(filename)
|
|
|
|
st = os.lstat(filename)
|
|
|
|
self.assertTrue(stat.S_ISSOCK(st.st_mode))
|
|
|
|
sock.listen(1)
|
|
|
|
|
|
|
|
class Client(threading.Thread):
|
2018-04-05 03:31:28 +03:00
|
|
|
def run(self) -> None:
|
2017-03-31 09:38:42 +03:00
|
|
|
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
try:
|
2018-04-05 03:31:28 +03:00
|
|
|
s.setblocking(False) # don't block here either
|
2017-03-31 09:38:42 +03:00
|
|
|
s.connect(filename)
|
|
|
|
s.send(PAYLOAD)
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
# spin up a thread to connect back to us and prove that
|
|
|
|
# the socket actually functions
|
|
|
|
client_thread = Client()
|
|
|
|
client_thread.start()
|
|
|
|
|
|
|
|
readable, _, _ = select.select([sock], [], [], 2)
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertTrue(sock in readable, msg="sock should be ready for accept")
|
2017-03-31 09:38:42 +03:00
|
|
|
conn, addr = sock.accept()
|
|
|
|
data = conn.recv(len(PAYLOAD))
|
|
|
|
self.assertEqual(PAYLOAD, data)
|
|
|
|
conn.close()
|
|
|
|
finally:
|
|
|
|
sock.close()
|