def test_mail_name_file(self, exists_mock): try: with patch('__builtin__.open', return_value=StringIO(self.check_value)): mn = get_mailname() self.assertEqual(mn, self.check_value) except ImportError: with patch('builtins.open', return_value=StringIO(self.check_value)): mn = get_mailname() self.assertEqual(mn, self.check_value)
def connection_ready(sock, fd, events): """ Accepts the socket connections and passes them off to be handled. 'sock' is an instance of 'socket'. 'fd' is an open file descriptor for the current connection. 'events' is an integer of the number of events on the socket. """ while True: try: connection, address = sock.accept() except socket.error as e: if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN): raise return log.debug("Connection from '%s'" % address[0]) connection.setblocking(0) stream = connection_stream(connection) # No stream, bail out if not stream: return mail_state = MailState() mail_state.email_id = email_id() mail_state.stream = stream # Sadly there is nothing I can do about the handle and loop # fuctions. They have to exist within connection_ready def handle(line): """ Handle a line of socket data, figure out if it's a valid SMTP keyword and handle it accordingly. """ log.debug("[%s] RECV: %s" % (mail_state.email_id, line.rstrip())) resp, close = handle_command(line, mail_state) if resp: # Multiple responses, i.e. EHLO if isinstance(resp, list): for r in resp: write_response(mail_state, r) else: # Otherwise it's a single response write_response(mail_state, resp) # Switch to SSL connection if starttls is called # and we have an SSL library if line.lower().startswith("starttls") and ssl and options.ssl: fileno = mail_state.stream.socket.fileno() IOLoop.current().remove_handler(fileno) mail_state.stream = ssl_connection(connection) # Close connection if close is True: log.debug("Closing") mail_state.stream.close() del mail_state.stream return else: loop() def loop(): """ Loop over the socket data until we receive a newline character (\n) """ # Protection against stream already reading exceptions if not mail_state.stream.reading(): mail_state.stream.read_until("\n", handle) hm = "220 %s [%s]\r\n" % (get_mailname(), __fullname__) mail_state.stream.write(hm) loop()
def test_mail_name_socket(self, exists_mock, socket_mock): mn = get_mailname() self.assertEqual(mn, self.check_value)