Example #1
0
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()
 def test_unix_socket_connection_connect_send_str_read(self):
     connection = UnixSocketConnection(
         path=self.socketname, timeout=DEFAULT_TIMEOUT
     )
     connection.connect()
     req = connection.send("<gmp/>")
     self.assertIsNone(req)
     resp = connection.read()
     self.assertEqual(resp, '<gmp_response status="200" status_text="OK"/>')
     connection.disconnect()
Example #3
0
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_send_unconnected_socket(self):
     connection = UnixSocketConnection(
         path=self.socketname, timeout=DEFAULT_TIMEOUT
     )
     with self.assertRaises(GvmError, msg="Socket is not connected"):
         connection.send("<gmp>/")