def listenUNIX(self, address, factory, backlog=50, mode=0o666, wantPID=0): """ Fake L{reactor.listenUNIX}, that logs the call and returns an L{IListeningPort}. """ self.unixServers.append((address, factory, backlog, mode, wantPID)) return _FakePort(UNIXAddress(address))
def buildAddress(self): """ Create an arbitrary new L{UNIXAddress} instance. A new instance is created for each call, but always for the same address. This builds it with a fixed address of L{None}. """ return UNIXAddress(None)
def test_whois_protocol_access_list_denied_unknown_client_address( self, config_override): config_override({ 'sources': { 'TEST1': {} }, 'server': { 'whois': { 'access_list': 'test-access-list', }, }, 'access_lists': { 'test-access-list': ['192.0.2.0/25'], }, }) mock_transport = Mock() mock_transport.getPeer = lambda: UNIXAddress(b'not-supported') mock_factory = Mock() mock_factory.current_connections = 10 receiver = WhoisQueryReceiver() receiver.transport = mock_transport receiver.factory = mock_factory receiver.connectionMade() assert mock_transport.mock_calls[0][0] == 'write' expected_output_start = b'%% Access denied' assert mock_transport.mock_calls[0][1][ 0][:len(expected_output_start)] == expected_output_start assert mock_transport.mock_calls[1][0] == 'loseConnection' assert len(mock_transport.mock_calls) == 2
def connectUNIX(self, address, factory, timeout=30, checkPID=0): """ Fake L{reactor.connectUNIX}, that logs the call and returns an L{IConnector}. """ self.unixClients.append((address, factory, timeout, checkPID)) conn = _FakeConnector(UNIXAddress(address)) factory.startedConnecting(conn) return conn
def test_connectToLinuxAbstractNamespace(self): """ L{IReactorUNIX.connectUNIX} also accepts a Linux abstract namespace path. """ path = _abstractPath(self) reactor = self.buildReactor() connector = reactor.connectUNIX('\0' + path, ClientFactory()) self.assertEqual(connector.getDestination(), UNIXAddress('\0' + path))
def test_bwHackDeprecation(self): """ If a value is passed for the C{_bwHack} parameter to L{UNIXAddress}, a deprecation warning is emitted. """ message = ( "twisted.internet.address.UNIXAddress._bwHack is deprecated " "since Twisted 11.0") address = UNIXAddress(self.mktemp(), _bwHack='UNIX') return self.assertDeprecations(self.test_bwHackDeprecation, message)
def test_listenOnLinuxAbstractNamespace(self): """ On Linux, a UNIX socket path may begin with C{'\0'} to indicate a socket in the abstract namespace. L{IReactorUNIX.listenUNIXDatagram} accepts such a path. """ path = _abstractPath(self) reactor = self.buildReactor() port = reactor.listenUNIXDatagram('\0' + path, DatagramProtocol()) self.assertEqual(port.getHost(), UNIXAddress('\0' + path))
def test_listenOnLinuxAbstractNamespace(self): """ On Linux, a UNIX socket path may begin with C{'\0'} to indicate a socket in the abstract namespace. L{IReactorUNIX.listenUNIX} accepts such a path. """ # Don't listen on a path longer than the maximum allowed. path = _abstractPath(self) reactor = self.buildReactor() port = reactor.listenUNIX('\0' + path, ServerFactory()) self.assertEqual(port.getHost(), UNIXAddress('\0' + path))
def test_bwHackDeprecation(self): """ If a value is passed for the C{_bwHack} parameter to L{UNIXAddress}, a deprecation warning is emitted. """ # Construct this for warning side-effects, disregard the actual object. UNIXAddress(None, _bwHack='UNIX') message = ( "twisted.internet.address.UNIXAddress._bwHack is deprecated " "since Twisted 11.0") return self.assertDeprecations(self.test_bwHackDeprecation, message)
def test_database_status_get_denied_unknown_client_address( self, prepare_resource_mocks): mock_database_status_request, mock_http_request = prepare_resource_mocks mock_http_request.getClientAddress = lambda: UNIXAddress( 'not-supported') resource = DatabaseStatusResource() mock_database_status_request.generate_status = lambda: 'test 🦄' response = resource.render_GET(mock_http_request) assert response == b'Access denied' assert flatten_mock_calls(mock_http_request) == [[ 'setResponseCode', (403, ), {} ]]
def createClientEndpoint(self, reactor, clientFactory, **connectArgs): """ Create an L{UNIXClientEndpoint} and return the values needed to verify its behaviour. @param reactor: A fake L{IReactorUNIX} that L{UNIXClientEndpoint} can call L{IReactorUNIX.connectUNIX} on. @param clientFactory: The thing that we expect to be passed to our L{IStreamClientEndpoint.connect} implementation. @param connectArgs: Optional dictionary of arguments to L{IReactorUNIX.connectUNIX} """ address = UNIXAddress(self.mktemp()) return (endpoints.UNIXClientEndpoint(reactor, address.name, **connectArgs), (address.name, clientFactory, connectArgs.get('timeout', 30), connectArgs.get('checkPID', 0)), address)
def test_unix_domain_socket(self): """Test that the whitelist is skipped for Unix domain sockets. """ factory = WhitelistingFactory(TestFactory(), []) # Should be blocking IP addresses self.assertEqual( factory.buildProtocol(IPv4Address('TCP', '127.0.0.1', 0)), None ) # But Unix domain sockets are allowed self.assertIsInstance( factory.buildProtocol(UNIXAddress('/test/address')), Protocol ) # With a warning logged self.assertTrue("Bypassing" in self.observer.messages[1][0])
def createServerEndpoint(self, reactor, factory, **listenArgs): """ Create an L{UNIXServerEndpoint} and return the tools to verify its behaviour. @param reactor: A fake L{IReactorUNIX} that L{UNIXServerEndpoint} can call L{IReactorUNIX.listenUNIX} on. @param factory: The thing that we expect to be passed to our L{IStreamServerEndpoint.listen} implementation. @param listenArgs: Optional dictionary of arguments to L{IReactorUNIX.listenUNIX}. """ address = UNIXAddress(self.mktemp()) return (endpoints.UNIXServerEndpoint(reactor, address.name, **listenArgs), (address.name, factory, listenArgs.get('backlog', 50), listenArgs.get('mode', 0666), listenArgs.get('wantPID', 0)), address)
def test_match_ip_address_ipv4_unix(self): matcher = NetfilterMatchIPAddress('192.168.0.1/32') context = NetfilterContext(addr=UNIXAddress('/unix.sock')) self.assertFalse(matcher.match(context))
def test_match_ip_address_ipv6_unix(self): matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::1/128') context = NetfilterContext(addr=UNIXAddress('/unix.sock')) self.assertFalse(matcher.match(context))
def buildAddress(self): return UNIXAddress(self._socketAddress)
def buildDifferentAddress(self): """ Like L{buildAddress}, but with a different fixed address. """ return UNIXAddress(self._otherAddress)
def buildAddress(self): """ Create an arbitrary new L{UNIXAddress} instance. A new instance is created for each call, but always for the same address. """ return UNIXAddress(self._socketAddress)
Fake L{reactor.connectSSL}, that logs the call and returns an L{IConnector}. """ self.sslClients.append((host, port, factory, contextFactory, timeout, bindAddress)) return _FakeConnector(IPv4Address('TCP', host, port)) def listenUNIX(self, address, factory, backlog=50, mode=0666, wantPID=0): """ Fake L{reactor.listenUNIX}, that logs the call and returns an L{IListeningPort}. """ self.unixServers.append((address, factory, backlog, mode, wantPID)) return _FakePort(UNIXAddress(address)) def connectUNIX(self, address, factory, timeout=30, checkPID=0): """ Fake L{reactor.connectUNIX}, that logs the call and returns an L{IConnector}. """ self.unixClients.append((address, factory, timeout, checkPID)) return _FakeConnector(UNIXAddress(address)) class RaisingMemoryReactor(object): """ A fake reactor to be used in tests. It accepts TCP connection setup
def buildDifferentAddress(self): """ Like L{buildAddress}, but with a random temporary directory. """ return UNIXAddress(self._socketAddress)
def buildDifferentAddress(self): """ Like L{buildAddress}, but with a fixed address of C{None}. """ return UNIXAddress(None)
def test_access_list_denied_unknown_client_address(self): unix_peer = UNIXAddress(b'not-supported') assert not is_client_permitted( unix_peer, 'test.access_list', default_deny=False) assert not is_client_permitted( unix_peer, 'test.access_list', default_deny=True)