Esempio n. 1
0
    def test_bad_port(self):
        port = 'bogus'
        with self.assertRaises(ValueError):
            with TSocket.TServerSocket(port=port):
                pass

        with self.assertRaises(ValueError):
            with TSocket.TSocket(port=port):
                pass
Esempio n. 2
0
 def test_unix_socket(self):
     text = b"hi"  # sample text to send over the wire
     with tempfile.NamedTemporaryFile(delete=True) as fh:
         unix_socket = fh.name
         with TSocket.TServerSocket(unix_socket=unix_socket) as server:
             with TSocket.TSocket(unix_socket=unix_socket) as conn:
                 conn.write(text)
             with server.accept() as client:
                 read = client.read(len(text))
             self.assertEquals(read, text)
         # The socket will not be cleaned up when the server has been shutdown.
         self.assertTrue(os.path.exists(unix_socket))
Esempio n. 3
0
 def test_deprecated_str_form_of_port(self):
     # Make sure that the deprecated form of the `port` parameter is
     # accepted in TServerSocket and TSocket.
     port = "0"
     text = b"hi"  # sample text to send over the wire
     # NB: unfortunately unittest.TestCase.assertWarns isn't available until
     # py3.
     with TSocket.TServerSocket(port=port,
                                family=socket.AF_INET6) as server:
         addr = server.getSocketNames()[0]
         with TSocket.TSocket(host=addr[0], port=str(addr[1])) as conn:
             conn.write(text)
         with server.accept() as client:
             read = client.read(len(text))
         self.assertEquals(read, text)
Esempio n. 4
0
 def _get_client(self, options):
     socket = TSocket.TSocket(unix_socket=options.path)
     if options.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(options, transport, socket=socket)
Esempio n. 5
0
    def test_open_failure(self):
        # Bind a server socket to an address, but don't actually listen on it.
        server_socket = socket.socket(socket.AF_INET6)
        try:
            server_socket.bind(('::', 0))
            server_port = server_socket.getsockname()[1]

            # Explicitly use "localhost" as the hostname, so that the
            # connect code will try both IPv6 and IPv4.  We want to
            # exercise the failure behavior when trying multiple addresses.
            sock = TSocket.TSocket(host='localhost', port=server_port)
            sock.setTimeout(50)  # ms
            try:
                sock.open()
                self.fail('unexpectedly succeeded to connect to closed socket')
            except TTransport.TTransportException:
                # sock.open() should not leave the file descriptor open
                # when it fails
                self.assertEquals(None, sock.handle)
                self.assertEquals({}, sock.handles)

                # Calling close() again on the socket should be a no-op,
                # and shouldn't throw an error
                sock.close()
        finally:
            server_socket.close()
Esempio n. 6
0
 def test_usage_as_context_manager(self):
     """
     Asserts that both TSocket and TServerSocket can be used with `with` and
     that their resources are disposed of at the close of the `with`.
     """
     text = b"hi"  # sample text to send over the wire
     with TSocket.TServerSocket(port=0, family=socket.AF_INET6) as server:
         addr = server.getSocketNames()[0]
         with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
             conn.write(text)
         self.assertFalse(conn.isOpen())
         with server.accept() as client:
             read = client.read(len(text))
         self.assertFalse(conn.isOpen())
     self.assertFalse(server.isListening())
     self.assertEquals(read, text)
Esempio n. 7
0
    def test_throw_populates_headers(self):
        handler = self.Handler({7: "hello"})
        processor = TestService.Processor(handler)
        server = TCppServerTestManager.make_server(processor)
        with TCppServerTestManager(server) as server:
            host, port = server.addr()
            with TSocket.TSocket(host=host, port=port) as sock:
                transport = THeaderTransport.THeaderTransport(sock)
                protocol = THeaderProtocol.THeaderProtocol(transport)
                client = TestService.Client(protocol, protocol)

                try:
                    client.throwUserException()
                    self.fail('Expect to throw UserException2')
                except UserException2:
                    pass

                self.assertEquals("UserException2",
                                  transport.get_headers()["uex"])
                self.assertIn("Some message", transport.get_headers()["uexw"])

                try:
                    client.throwUncaughtException("a message!")
                    self.fail('Expect to throw TApplicationException')
                except TApplicationException:
                    pass

                self.assertEquals("TApplicationException",
                                  transport.get_headers()["uex"])
                self.assertIn("a message!", transport.get_headers()["uexw"])
Esempio n. 8
0
 def _get_client(self, options):
     host, port = self._parse_host_port(options.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port)
               if options.ssl else TSocket.TSocket(host, port))
     if options.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(options, transport, socket=socket)
Esempio n. 9
0
 def _get_client_by_host(self):
     config = self.config
     host, port = self._parse_host_port(config.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port)
               if config.ssl else TSocket.TSocket(host, port))
     if config.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(config, transport, socket=socket)
Esempio n. 10
0
 def open(self):
     try:
         self.close()
         s = TSocket.TSocket(self._address.host, self._address.port)
         if self._timeout > 0:
             s.setTimeout(self._timeout)
         transport = TTransport.TBufferedTransport(s)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         transport.open()
         self._connection = GraphStorageService.Client(protocol)
     except Exception:
         raise
Esempio n. 11
0
 def _expiring_rpc(self, server, service, method, tm, headers, *args,
                   **kwargs):
     host, port = server.addr()
     with TSocket.TSocket(host=host, port=port) as sock:
         sock.setTimeout(tm)
         transport = THeaderTransport.THeaderTransport(sock)
         if headers:
             for key, val in headers.items():
                 transport.set_header(key, val)
         protocol = THeaderProtocol.THeaderProtocol(transport)
         client = service.Client(protocol, protocol)
         return getattr(client, method)(*args, **kwargs)
Esempio n. 12
0
    def test_poller_process(self):
        # Make sure that pollers do not fail when they're given None as timeout
        text = "hi"  # sample text to send over the wire
        with TSocket.TServerSocket(port=0, family=socket.AF_INET6) as server:
            addr = server.getSocketNames()[0]

            def write_data():
                # delay writing to verify that poller.process is waiting
                time.sleep(1)
                with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
                    conn.write(text)

            poller = TSocket.ConnectionSelect()
            thread = threading.Thread(target=write_data)
            thread.start()
            for filenos in server.handles.keys():
                poller.read(filenos)

            r, _, x = poller.process(timeout=None)

            thread.join()
            # Verify that r is non-empty
            self.assertTrue(r)
Esempio n. 13
0
    def open(self):
        """open the connection to connect meta service

        :eturn: void
        """
        try:
            self.close()
            s = TSocket.TSocket(self._leader[0], self._leader[1])
            if self._timeout > 0:
                s.setTimeout(self._timeout)
            transport = TTransport.TBufferedTransport(s)
            protocol = TBinaryProtocol.TBinaryProtocol(transport)
            transport.open()
            self._connection = MetaService.Client(protocol)
        except Exception:
            raise
Esempio n. 14
0
    def open_SSL(self, ip, port, timeout, ssl_config=None):
        """open the SSL connection

        :param ip: the server ip
        :param port: the server port
        :param timeout: the timeout for connect and execute
        :ssl_config: configs for SSL
        :return: void
        """
        self._ip = ip
        self._port = port
        self._timeout = timeout
        self._ssl_conf = ssl_config
        try:
            if ssl_config is not None:
                s = TSSLSocket.TSSLSocket(
                    self._ip,
                    self._port,
                    ssl_config.unix_socket,
                    ssl_config.ssl_version,
                    ssl_config.cert_reqs,
                    ssl_config.ca_certs,
                    ssl_config.verify_name,
                    ssl_config.keyfile,
                    ssl_config.certfile,
                    ssl_config.allow_weak_ssl_versions,
                )
            else:
                s = TSocket.TSocket(self._ip, self._port)
            if timeout > 0:
                s.setTimeout(timeout)
            transport = TTransport.TBufferedTransport(s)
            protocol = TBinaryProtocol.TBinaryProtocol(transport)
            transport.open()
            self._connection = GraphService.Client(protocol)
            resp = self._connection.verifyClientVersion(
                VerifyClientVersionReq())
            if resp.error_code != ErrorCode.SUCCEEDED:
                self._connection._iprot.trans.close()
                raise ClientServerIncompatibleException(resp.error_msg)
        except Exception:
            raise
Esempio n. 15
0
 def write_data():
     # delay writing to verify that poller.process is waiting
     time.sleep(1)
     with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
         conn.write(text)
Esempio n. 16
0
 def do_test():
     with TSocket.TServerSocket(port=0, family=socket.AF_INET6):
         raise Exception('test_error')