def _start_edge(self, name, options=None): if not options: options = getattr(self.cfg.edge, name) new_edges = [] queue_name = options.queue queue = self._start_queue(queue_name) if options.type == 'smtp': from slimta.edge.smtp import SmtpEdge from .helpers import build_smtpedge_validators from .helpers import fill_hostname_template kwargs = {} kwargs['context'] = self._get_server_ssl_context(options.tls) kwargs['tls_immediately'] = options.tls_immediately kwargs['validator_class'] = build_smtpedge_validators(options) kwargs['auth'] = [b'PLAIN', b'LOGIN'] kwargs['command_timeout'] = 20.0 kwargs['data_timeout'] = 30.0 kwargs['max_size'] = int(options.get('max_size', 10485760)) kwargs['hostname'] = fill_hostname_template(options.hostname) for listener in Listeners(options, 25): new_edge = SmtpEdge(listener, queue, **kwargs) if options.proxyprotocol: ProxyProtocol.mixin(new_edge) new_edge.start() self.edges.append(new_edge) elif options.type == 'http': from slimta.edge.wsgi import WsgiEdge from .helpers import build_wsgiedge_validators from .helpers import fill_hostname_template kwargs = {} kwargs['hostname'] = fill_hostname_template(options.hostname) kwargs['validator_class'] = build_wsgiedge_validators(options) kwargs['uri_pattern'] = options.uri kwargs['context'] = self._get_server_ssl_context(options.tls) listener = self._get_listener(options, 8025) for listener in Listeners(options, 8025): new_edge = WsgiEdge(queue, listener=listener, **kwargs) if options.proxyprotocol: ProxyProtocol.mixin(new_edge) new_edge.start() self.edges.append(new_edge) elif options.type == 'custom': new_edge = custom_factory(options, queue) self.edges.append(new_edge) else: msg = 'edge type does not exist: '+options.type raise ConfigValidationError(msg)
class TestProxyProtocol(MoxTestBase): def setUp(self): super(TestProxyProtocol, self).setUp() self.pp = ProxyProtocol() def test_read_pp_initial(self): def _get_side_effect(data): def _side_effect(view, length): view[0:len(data)] = data return _side_effect sock = self.mox.CreateMock(socket.socket) sock.recv_into(IsA(memoryview), 8).WithSideEffects( _get_side_effect(b'abcdefg')).AndReturn(7) sock.recv_into(IsA(memoryview), 1).WithSideEffects(_get_side_effect(b'h')).AndReturn(1) self.mox.ReplayAll() line = self.pp._ProxyProtocol__read_pp_initial(sock) self.assertEqual(b'abcdefgh', line) def test_handle_pp_v1(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, 13) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') self.mox.StubOutWithMock(ProxyProtocolV1, 'process_pp_v1') pp._ProxyProtocol__read_pp_initial(sock).AndReturn(b'PROXY ') ProxyProtocolV1.process_pp_v1(sock, b'PROXY ').AndReturn((13, 14)) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called) def test_handle_pp_v2(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, 13) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') self.mox.StubOutWithMock(ProxyProtocolV2, 'process_pp_v2') pp._ProxyProtocol__read_pp_initial(sock).AndReturn( b'\r\n\r\n\x00\r\nQ') ProxyProtocolV2.process_pp_v2(sock, b'\r\n\r\n\x00\r\nQ').AndReturn( (13, 14)) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called) def test_handle_error(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, (None, None)) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') pp._ProxyProtocol__read_pp_initial(sock).AndRaise(AssertionError) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called)
class TestProxyProtocol(MoxTestBase): def setUp(self): super(TestProxyProtocol, self).setUp() self.pp = ProxyProtocol() def test_read_pp_initial(self): def _get_side_effect(data): def _side_effect(view, length): view[0:len(data)] = data return _side_effect sock = self.mox.CreateMock(socket.socket) sock.recv_into(IsA(memoryview), 8).WithSideEffects(_get_side_effect(b'abcdefg')).AndReturn(7) sock.recv_into(IsA(memoryview), 1).WithSideEffects(_get_side_effect(b'h')).AndReturn(1) self.mox.ReplayAll() line = self.pp._ProxyProtocol__read_pp_initial(sock) self.assertEqual(b'abcdefgh', line) def test_handle_pp_v1(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, 13) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') self.mox.StubOutWithMock(ProxyProtocolV1, 'process_pp_v1') pp._ProxyProtocol__read_pp_initial(sock).AndReturn(b'PROXY ') ProxyProtocolV1.process_pp_v1(sock, b'PROXY ').AndReturn((13, 14)) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called) def test_handle_pp_v2(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, 13) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') self.mox.StubOutWithMock(ProxyProtocolV2, 'process_pp_v2') pp._ProxyProtocol__read_pp_initial(sock).AndReturn(b'\r\n\r\n\x00\r\nQ') ProxyProtocolV2.process_pp_v2(sock, b'\r\n\r\n\x00\r\nQ').AndReturn((13, 14)) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called) def test_handle_error(self): sock = self.mox.CreateMock(socket.socket) sock.fileno = lambda: -1 pp = PPEdge(self, sock, (None, None)) self.mox.StubOutWithMock(ProxyProtocol, '_ProxyProtocol__read_pp_initial') pp._ProxyProtocol__read_pp_initial(sock).AndRaise(AssertionError) self.mox.ReplayAll() pp.handle(sock, None) self.assertTrue(pp.handle_called)
def setUp(self): super(TestProxyProtocol, self).setUp() self.pp = ProxyProtocol()