Exemplo n.º 1
0
 def test_gaierror(self):
     """
     Test a connection that fails on socket.connect() because of a name
     resolution error.
     """
     # See RFC 6761 for the .test TLD -- unless the DNS server is doing
     # something weird, this name should not resolve.
     op = TCPConnectOperation(self.test_op, ("example.test", 666))
     op.callback = op_callback()
     op.start()
     op.callback.assert_called_once_with(op)
     self.assertIsNone(op.socket)
     self.assertTrue(self.test_op.is_done())
Exemplo n.º 2
0
 def test_timeout(self):
     """
     Test a connection that times out and fails.
     """
     with drop_connection(port=self.sock.getsockname()[1]):
         op = TCPConnectOperation(self.test_op, self.sock.getsockname(), 0.01)
         op.callback = op_callback()
         op.start()
         time.sleep(0.01)
         self.test_op.run_selector()
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.updated_with("Timed out"))
         self.assertTrue(self.test_op.is_done())
Exemplo n.º 3
0
 def test_connection_refused(self):
     """
     Test a connection that fails because the server refuses the connection.
     """
     self.sock.shutdown(socket.SHUT_RD)
     self.shutdown = False
     op = TCPConnectOperation(self.test_op, self.sock.getsockname())
     op.callback = op_callback()
     op.start()
     self.test_op.run_selector()
     op.callback.assert_called_once_with(op)
     self.assertIsNone(op.socket)
     self.assertTrue(self.test_op.updated_with("Connection refused"))
     self.assertTrue(self.test_op.is_done())
Exemplo n.º 4
0
 def test_missing_cafile(self):
     """
     Test a handshake that fails because the CA file doesn't exist.
     """
     self.start_server()
     sock = self.connect_client()
     op = SSLHandshakeOperation(self.test_op, sock, 'localhost',
                                tempfile.mktemp())
     op.callback = op_callback()
     op.start()
     op.callback.assert_called_once_with(op)
     self.assertIsNone(op.socket)
     self.assertTrue(self.test_op.updated_with('No such file'))
     self.assertTrue(self.test_op.is_done())
Exemplo n.º 5
0
 def test_timeout(self):
     """
     Test a handshake that fails because it times out.
     """
     self.start_server()
     sock = self.connect_client()
     with drop_connection(port=self.sock.getsockname()[1]):
         op = SSLHandshakeOperation(self.test_op, sock, 'localhost',
                                    self.certfile, 0.01)
         op.callback = op_callback()
         op.start()
         time.sleep(0.01)
         self.test_op.run_selector()
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.updated_with('Timed out'))
         self.assertTrue(self.test_op.is_done())
Exemplo n.º 6
0
 def test_success(self):
     """
     Test a successful connection.
     """
     op = TCPConnectOperation(self.test_op, self.sock.getsockname())
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     while not op.callback.called:
         self.test_op.run_selector()
     accept.thread.join()
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNotNone(op.socket)
         op.socket.close()
     finally:
         accept.sock.close()
     self.assertTrue(self.test_op.is_done())
Exemplo n.º 7
0
 def test_timeout_and_connect(self):
     """
     Test the same case as test_connect_and_timeout but where the timerfd
     event comes before the socket event.
     """
     op = TCPConnectOperation(self.test_op, self.sock.getsockname(), 0.01)
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     time.sleep(0.01)
     while not op.callback.called:
         self.test_op.run_selector(priority="timer")
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.updated_with("Timed out"))
         self.assertTrue(self.test_op.is_done())
     finally:
         accept.sock.close()
Exemplo n.º 8
0
 def test_connect_and_timeout(self):
     """
     Test the case where the connection succeeds but the timerfd still fires
     before we call select. This is the case where the socket comes before
     the timerfd in the list of events, so the connection should succeed.
     """
     op = TCPConnectOperation(self.test_op, self.sock.getsockname(), 0.01)
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     time.sleep(0.01)
     while not op.callback.called:
         self.test_op.run_selector(priority="socket")
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNotNone(op.socket)
         op.socket.close()
         self.assertTrue(self.test_op.is_done())
     finally:
         accept.sock.close()
Exemplo n.º 9
0
 def test_success(self):
     """
     Test a successful handshake.
     """
     self.start_server()
     sock = self.connect_client()
     op = SSLHandshakeOperation(self.test_op, sock, 'localhost',
                                self.certfile)
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     while not op.callback.called:
         self.test_op.run_selector()
     accept.thread.join()
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNotNone(op.socket)
         op.socket.close()
         self.assertTrue(self.test_op.is_done())
     finally:
         accept.sock.close()
Exemplo n.º 10
0
 def test_invalid_handshake(self):
     """
     Test a handshake that fails because the server does something invalid
     (e.g., the server is not actually using SSL).
     """
     self.start_server(wrap_ssl=False)
     sock = self.connect_client()
     op = SSLHandshakeOperation(self.test_op, sock, 'localhost',
                                self.certfile)
     op.callback = op_callback()
     op.start()
     accept = self.accept(send=b'* OK Hello\r\n')
     while not op.callback.called:
         self.test_op.run_selector()
     accept.thread.join()
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.is_done())
     finally:
         if accept.sock:
             accept.sock.close()
Exemplo n.º 11
0
 def test_certificate_verify(self):
     """
     Test a handshake that fails because the certificate is not trusted.
     """
     self.start_server()
     sock = self.connect_client()
     # Notice that we're using ca2.pem as the CA file here.
     op = SSLHandshakeOperation(self.test_op, sock, 'localhost',
                                self.certfile2)
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     while not op.callback.called:
         self.test_op.run_selector()
     accept.thread.join()
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.updated_with('CERTIFICATE_VERIFY_FAILED'))
         self.assertTrue(self.test_op.is_done())
     finally:
         if accept.sock:
             accept.sock.close()
Exemplo n.º 12
0
 def test_hostname_verify(self):
     """
     Test a handshake that fails because the server hostname does not match.
     """
     self.start_server()
     sock = self.connect_client()
     # We're passing 'localghost' instead of 'localhost' as the server
     # hostname.
     op = SSLHandshakeOperation(self.test_op, sock, 'localghost',
                                self.certfile)
     op.callback = op_callback()
     op.start()
     accept = self.accept()
     while not op.callback.called:
         self.test_op.run_selector()
     accept.thread.join()
     try:
         op.callback.assert_called_once_with(op)
         self.assertIsNone(op.socket)
         self.assertTrue(self.test_op.updated_with('hostname'))
         self.assertTrue(self.test_op.is_done())
     finally:
         if accept.sock:
             accept.sock.close()
Exemplo n.º 13
0
 def setUp(self):
     self.test_op = TestIMAPOperation()
     self.op = IMAPGreetingOperation(self.test_op)
     self.op.callback = op_callback()
     self.test_op.start()