def getSessId(self): """Return a hash of the current session id. We concatenate with a timestamp to get a unique session Id. In the final countermeasure we can hash the URL to set particular padding strategies for individual pages. """ return gu.hash_text(str(self._sessId) + str(gu.timestamp()))
class SessionPrimitiveTestCase(WFPadPrimitiveTestCase): """Provide methods for test cases that do require a session.""" sess_id = gu.hash_text(str(randint(1, 10)) + str(gu.timestamp())) def setUp(self): """Start a session.""" WFPadPrimitiveTestCase.setUp(self) self.pt_client.onSessionStarts(self.sess_id) def send_message(self, endpoint, msg): """Send message to the specified endpoint.""" endpoint.receivedUpstream(msg) def send_str(self, endpoint, txt): """Send a text string to the endpoint.""" b = Buffer() b.write(txt) self.send_message(endpoint, b) def send_timestamp(self, endpoint): """Send a timestamp to the endpoint.""" self.send_str(endpoint, str(gu.timestamp())) def extract_ts(self, history, selector=gu.iden): """Extract the list of timestamps from the history of messages.""" return [ts for ts, msgs in history if len(msgs) > 0 and len(self.extract_msgs(msgs, selector)) > 0] def extract_msgs(self, msgs, selector=gu.iden): """Extract messages satisfying a condition specified by `selector`.""" return [msg for msg in msgs if selector(msg) and not isControl(msg)] def run_primitive(self, *args, **kwargs): """Run the primitive with the specified arguments.""" endpoint = kwargs.get('endpoint', self.pt_client) getattr(self.pt_client, self.primitive)(*args) for _ in xrange(N_SAMPLES): self.send_timestamp(endpoint) self.advance_delayed_calls() # call flush buffer self.advance_delayed_calls() # call timeouts set in flushbuffer self.advance_delayed_calls() def tearDown(self): """Close session and tear down the setting.""" if self.pt_client.isVisiting(): self.pt_client.onSessionEnds(self.sess_id) self.pt_client.session.is_peer_padding = False WFPadPrimitiveTestCase.tearDown(self)
def test_session_status_is_visiting(self): sess_id = gu.hash_text(str(randint(1, 10)) + str(gu.timestamp())) status = True self.pt_client.relayAppHint(sess_id, status) self.pt_client.session.is_peer_padding = False self.assertTrue(self.pt_server._visiting)