def setUp(self): """ Set up the TestCase by calling the homeserver constructor, optionally hijacking the authentication system to return a fixed user, and then calling the prepare function. """ self.reactor, self.clock = get_clock() self._hs_args = {"clock": self.clock, "reactor": self.reactor} self.hs = self.make_homeserver(self.reactor, self.clock) if self.hs is None: raise Exception("No homeserver returned from make_homeserver.") if not isinstance(self.hs, HomeServer): raise Exception("A homeserver wasn't returned, but %r" % (self.hs, )) # Register the resources self.resource = self.create_test_json_resource() from tests.rest.client.v1.utils import RestHelper self.helper = RestHelper(self.hs, self.resource, getattr(self, "user_id", None)) if hasattr(self, "user_id"): if self.hijack_auth: def get_user_by_access_token(token=None, allow_guest=False): return succeed({ "user": UserID.from_string(self.helper.auth_user_id), "token_id": 1, "is_guest": False, }) def get_user_by_req(request, allow_guest=False, rights="access"): return succeed( create_requester( UserID.from_string(self.helper.auth_user_id), 1, False, None)) self.hs.get_auth().get_user_by_req = get_user_by_req self.hs.get_auth( ).get_user_by_access_token = get_user_by_access_token self.hs.get_auth().get_access_token_from_request = Mock( return_value="1234") if self.needs_threadpool: self.reactor.threadpool = ThreadPool() self.addCleanup(self.reactor.threadpool.stop) self.reactor.threadpool.start() if hasattr(self, "prepare"): self.prepare(self.reactor, self.clock, self.hs)
def test_ratelimit(self): """A simple test with the default values""" reactor, clock = get_clock() rc_config = build_rc_config() ratelimiter = FederationRateLimiter(clock, rc_config) with ratelimiter.ratelimit("testhost") as d1: # shouldn't block self.successResultOf(d1)
def setUp(self): self.clock, hs_clock = get_clock() # We ensure that we remove any existing metrics for "test_queue". try: number_queued.remove("test_queue") number_of_keys.remove("test_queue") number_in_flight.remove("test_queue") except KeyError: pass self._pending_calls = [] self.queue = BatchingQueue("test_queue", hs_clock, self._process_queue)
def test_sleep(self): reactor, _ = get_clock() sleeper = AwakenableSleeper(reactor) d = defer.ensureDeferred(sleeper.sleep("name", 1000)) reactor.pump([0.0]) self.assertFalse(d.called) reactor.advance(0.5) self.assertFalse(d.called) reactor.advance(0.6) self.assertTrue(d.called)
def setUp(self): """ Set up the TestCase by calling the homeserver constructor, optionally hijacking the authentication system to return a fixed user, and then calling the prepare function. """ self.reactor, self.clock = get_clock() self._hs_args = {"clock": self.clock, "reactor": self.reactor} self.hs = self.make_homeserver(self.reactor, self.clock) if self.hs is None: raise Exception("No homeserver returned from make_homeserver.") if not isinstance(self.hs, HomeServer): raise Exception("A homeserver wasn't returned, but %r" % (self.hs,)) # Register the resources self.resource = JsonResource(self.hs) for servlet in self.servlets: servlet(self.hs, self.resource) from tests.rest.client.v1.utils import RestHelper self.helper = RestHelper(self.hs, self.resource, getattr(self, "user_id", None)) if hasattr(self, "user_id"): if self.hijack_auth: def get_user_by_access_token(token=None, allow_guest=False): return { "user": UserID.from_string(self.helper.auth_user_id), "token_id": 1, "is_guest": False, } def get_user_by_req(request, allow_guest=False, rights="access"): return create_requester( UserID.from_string(self.helper.auth_user_id), 1, False, None ) self.hs.get_auth().get_user_by_req = get_user_by_req self.hs.get_auth().get_user_by_access_token = get_user_by_access_token self.hs.get_auth().get_access_token_from_request = Mock( return_value="1234" ) if hasattr(self, "prepare"): self.prepare(self.reactor, self.clock, self.hs)
def setUp(self): self.reactor, self.clock = get_clock() self.safe_domain, self.safe_ip = b"safe.test", b"1.2.3.4" self.unsafe_domain, self.unsafe_ip = b"danger.test", b"5.6.7.8" self.allowed_domain, self.allowed_ip = b"allowed.test", b"5.1.1.1" # Configure the reactor's DNS resolver. for (domain, ip) in ( (self.safe_domain, self.safe_ip), (self.unsafe_domain, self.unsafe_ip), (self.allowed_domain, self.allowed_ip), ): self.reactor.lookups[domain.decode()] = ip.decode() self.reactor.lookups[ip.decode()] = ip.decode() self.ip_whitelist = IPSet([self.allowed_ip.decode()]) self.ip_blacklist = IPSet(["5.0.0.0/8"])
def test_multiple_sleepers_timeout(self): reactor, _ = get_clock() sleeper = AwakenableSleeper(reactor) d1 = defer.ensureDeferred(sleeper.sleep("name", 1000)) reactor.advance(0.6) self.assertFalse(d1.called) # Add another sleeper d2 = defer.ensureDeferred(sleeper.sleep("name", 1000)) # Only the first sleep should time out now. reactor.advance(0.6) self.assertTrue(d1.called) self.assertFalse(d2.called) reactor.advance(0.6) self.assertTrue(d2.called)
def test_sleep_limit(self): """Test what happens when we hit the sleep limit""" reactor, clock = get_clock() rc_config = build_rc_config( {"rc_federation": {"sleep_limit": 2, "sleep_delay": 500}} ) ratelimiter = FederationRateLimiter(clock, rc_config) with ratelimiter.ratelimit("testhost") as d1: # shouldn't block self.successResultOf(d1) with ratelimiter.ratelimit("testhost") as d2: # nor this self.successResultOf(d2) with ratelimiter.ratelimit("testhost") as d3: # this one should block, though ... self.assertNoResult(d3) sleep_time = _await_resolution(reactor, d3) self.assertAlmostEqual(sleep_time, 500, places=3)
def test_multiple_sleepers_wake(self): reactor, _ = get_clock() sleeper = AwakenableSleeper(reactor) d1 = defer.ensureDeferred(sleeper.sleep("name", 1000)) reactor.advance(0.5) self.assertFalse(d1.called) # Add another sleeper d2 = defer.ensureDeferred(sleeper.sleep("name", 1000)) # Neither should fire yet reactor.advance(0.3) self.assertFalse(d1.called) self.assertFalse(d2.called) # Explicitly waking both up works sleeper.wake("name") self.assertTrue(d1.called) self.assertTrue(d2.called)
def test_concurrent_limit(self): """Test what happens when we hit the concurrent limit""" reactor, clock = get_clock() rc_config = build_rc_config({"rc_federation": {"concurrent": 2}}) ratelimiter = FederationRateLimiter(clock, rc_config) with ratelimiter.ratelimit("testhost") as d1: # shouldn't block self.successResultOf(d1) cm2 = ratelimiter.ratelimit("testhost") d2 = cm2.__enter__() # also shouldn't block self.successResultOf(d2) cm3 = ratelimiter.ratelimit("testhost") d3 = cm3.__enter__() # this one should block, though ... self.assertNoResult(d3) # ... until we complete an earlier request cm2.__exit__(None, None, None) self.successResultOf(d3)
def setUp(self): """ Set up the TestCase by calling the homeserver constructor, optionally hijacking the authentication system to return a fixed user, and then calling the prepare function. """ self.reactor, self.clock = get_clock() self._hs_args = {"clock": self.clock, "reactor": self.reactor} self.hs = self.make_homeserver(self.reactor, self.clock) # Honour the `use_frozen_dicts` config option. We have to do this # manually because this is taken care of in the app `start` code, which # we don't run. Plus we want to reset it on tearDown. events.USE_FROZEN_DICTS = self.hs.config.server.use_frozen_dicts if self.hs is None: raise Exception("No homeserver returned from make_homeserver.") if not isinstance(self.hs, HomeServer): raise Exception("A homeserver wasn't returned, but %r" % (self.hs, )) # create the root resource, and a site to wrap it. self.resource = self.create_test_resource() self.site = SynapseSite( logger_name="synapse.access.http.fake", site_tag=self.hs.config.server.server_name, config=self.hs.config.server.listeners[0], resource=self.resource, server_version_string="1", max_request_body_size=1234, reactor=self.reactor, ) from tests.rest.client.utils import RestHelper self.helper = RestHelper(self.hs, self.site, getattr(self, "user_id", None)) if hasattr(self, "user_id"): if self.hijack_auth: assert self.helper.auth_user_id is not None # We need a valid token ID to satisfy foreign key constraints. token_id = self.get_success( self.hs.get_datastores().main.add_access_token_to_user( self.helper.auth_user_id, "some_fake_token", None, None, )) async def get_user_by_access_token(token=None, allow_guest=False): assert self.helper.auth_user_id is not None return { "user": UserID.from_string(self.helper.auth_user_id), "token_id": token_id, "is_guest": False, } async def get_user_by_req(request, allow_guest=False): assert self.helper.auth_user_id is not None return create_requester( UserID.from_string(self.helper.auth_user_id), token_id, False, False, None, ) # Type ignore: mypy doesn't like us assigning to methods. self.hs.get_auth( ).get_user_by_req = get_user_by_req # type: ignore[assignment] self.hs.get_auth( ).get_user_by_access_token = get_user_by_access_token # type: ignore[assignment] self.hs.get_auth( ).get_access_token_from_request = Mock( # type: ignore[assignment] return_value="1234") if self.needs_threadpool: self.reactor.threadpool = ThreadPool() # type: ignore[assignment] self.addCleanup(self.reactor.threadpool.stop) self.reactor.threadpool.start() if hasattr(self, "prepare"): self.prepare(self.reactor, self.clock, self.hs)
def setUp(self): self.reactor, self.clock = get_clock()
def setUp(self): """ Set up the TestCase by calling the homeserver constructor, optionally hijacking the authentication system to return a fixed user, and then calling the prepare function. """ self.reactor, self.clock = get_clock() self._hs_args = {"clock": self.clock, "reactor": self.reactor} self.hs = self.make_homeserver(self.reactor, self.clock) if self.hs is None: raise Exception("No homeserver returned from make_homeserver.") if not isinstance(self.hs, HomeServer): raise Exception("A homeserver wasn't returned, but %r" % (self.hs,)) # create the root resource, and a site to wrap it. self.resource = self.create_test_resource() self.site = SynapseSite( logger_name="synapse.access.http.fake", site_tag=self.hs.config.server.server_name, config=self.hs.config.server.listeners[0], resource=self.resource, server_version_string="1", ) from tests.rest.client.v1.utils import RestHelper self.helper = RestHelper(self.hs, self.site, getattr(self, "user_id", None)) if hasattr(self, "user_id"): if self.hijack_auth: # We need a valid token ID to satisfy foreign key constraints. token_id = self.get_success( self.hs.get_datastore().add_access_token_to_user( self.helper.auth_user_id, "some_fake_token", None, None, ) ) async def get_user_by_access_token(token=None, allow_guest=False): return { "user": UserID.from_string(self.helper.auth_user_id), "token_id": token_id, "is_guest": False, } async def get_user_by_req(request, allow_guest=False, rights="access"): return create_requester( UserID.from_string(self.helper.auth_user_id), token_id, False, False, None, ) self.hs.get_auth().get_user_by_req = get_user_by_req self.hs.get_auth().get_user_by_access_token = get_user_by_access_token self.hs.get_auth().get_access_token_from_request = Mock( return_value="1234" ) if self.needs_threadpool: self.reactor.threadpool = ThreadPool() self.addCleanup(self.reactor.threadpool.stop) self.reactor.threadpool.start() if hasattr(self, "prepare"): self.prepare(self.reactor, self.clock, self.hs)
def setUp(self): self.reactor, _ = get_clock()
def setUp(self): self.clock, hs_clock = get_clock() self._pending_calls = [] self.queue = BatchingQueue("test_queue", hs_clock, self._process_queue)
def setUp(self): self.reactor, hs_clock = get_clock() self.macaroon_generator = MacaroonGenerator(hs_clock, "tesths", b"verysecret") self.other_macaroon_generator = MacaroonGenerator( hs_clock, "tesths", b"anothersecretkey")