def test_unix_socket_connection_connect_read(self): connection = UnixSocketConnection( path=self.socketname, timeout=DEFAULT_TIMEOUT ) connection.connect() resp = connection.read() self.assertEqual(resp, '<gmp_response status="200" status_text="OK"/>') connection.disconnect()
def test_unix_socket_connection_connect_send_bytes_read(self): connection = UnixSocketConnection( path=self.socketname, timeout=DEFAULT_TIMEOUT ) connection.connect() req = connection.send(bytes("<gmp/>", "utf-8")) self.assertIsNone(req) resp = connection.read() self.assertEqual(resp, '<gmp_response status="200" status_text="OK"/>') connection.disconnect()
def test_unix_socket_connect_could_not_connect(self): connection = UnixSocketConnection( path=self.socketname, timeout=DEFAULT_TIMEOUT ) with patch("socket.socket.connect") as ConnectMock: connect_mock = ConnectMock connect_mock.side_effect = ConnectionError with self.assertRaises( GvmError, msg=f"Could not connect to socket {self.socketname}" ): connection.connect() connection.disconnect()
class UnixSocketConnectionTestCase(unittest.TestCase): def setUp(self): self.socketname = "%s/%s.sock" % (tempfile.gettempdir(), str(uuid.uuid4())) self.sockserv = ThreadedUnixStreamServer(self.socketname, DummyRequestHandler) self.server_thread = threading.Thread(target=self.sockserv.serve_forever) self.server_thread.daemon = True self.server_thread.start() def tearDown(self): self.sockserv.shutdown() self.sockserv.server_close() os.unlink(self.socketname) def test_unix_socket_connection_connect_read(self): self.connection = UnixSocketConnection(self.socketname, DEFAULT_TIMEOUT) self.connection.connect() self.connection.read() self.connection.disconnect() def test_unix_socket_connection_connect_send_bytes_read(self): self.connection = UnixSocketConnection(self.socketname, DEFAULT_TIMEOUT) self.connection.connect() self.connection.send(bytes("<gmp/>", 'utf-8')) self.connection.read() self.connection.disconnect() def test_unix_socket_connection_connect_send_str_read(self): self.connection = UnixSocketConnection(self.socketname, DEFAULT_TIMEOUT) self.connection.connect() self.connection.send("<gmp/>") self.connection.read() self.connection.disconnect()
class UnixSocketConnectionTestCase(unittest.TestCase): # pylint: disable=protected-access def setUp(self): self.socketname = "%s/%s.sock" % ( tempfile.gettempdir(), str(uuid.uuid4()), ) self.sockserv = ThreadedUnixStreamServer(self.socketname, DummyRequestHandler) self.server_thread = threading.Thread( target=self.sockserv.serve_forever) self.server_thread.daemon = True self.server_thread.start() def tearDown(self): self.sockserv.shutdown() self.sockserv.server_close() os.unlink(self.socketname) def test_unix_socket_connection_connect_read(self): self.connection = UnixSocketConnection(path=self.socketname, timeout=DEFAULT_TIMEOUT) self.connection.connect() self.connection.read() self.connection.disconnect() def test_unix_socket_connection_connect_send_bytes_read(self): self.connection = UnixSocketConnection(path=self.socketname, timeout=DEFAULT_TIMEOUT) self.connection.connect() self.connection.send(bytes("<gmp/>", 'utf-8')) self.connection.read() self.connection.disconnect() def test_unix_socket_connection_connect_send_str_read(self): self.connection = UnixSocketConnection(path=self.socketname, timeout=DEFAULT_TIMEOUT) self.connection.connect() self.connection.send("<gmp/>") self.connection.read() self.connection.disconnect() def test_unix_socket_send_unconnected_socket(self): self.connection = UnixSocketConnection(path=self.socketname, timeout=DEFAULT_TIMEOUT) with self.assertRaises(GvmError): self.connection.send("<gmp>/") def test_init_no_args(self): connection = UnixSocketConnection() self.check_default_values(connection) def test_init_with_none(self): connection = UnixSocketConnection(path=None, timeout=None) self.check_default_values(connection) def check_default_values(self, connection: UnixSocketConnection): self.assertEqual(connection._timeout, DEFAULT_TIMEOUT) self.assertEqual(connection.path, DEFAULT_UNIX_SOCKET_PATH)
def test_unix_socket_connect_file_not_found(self): connection = UnixSocketConnection(path="foo", timeout=DEFAULT_TIMEOUT) with self.assertRaises(GvmError, msg="Socket foo does not exist"): connection.connect() connection.disconnect()