Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_listener_attach_no_remap(self):
"Attachment is via SENTCONNECT on .onion addresses (for example)"
self.circuits[186] = FakeCircuit(186)
listener = Listener([('new', {'target_host': 'www.yahoo.com',
'target_port': 80}),
('attach', {})])
stream = Stream(self)
stream.listen(listener)
stream.update("316 NEW 0 www.yahoo.com:80 SOURCE_ADDR=127.0.0.1:55877 PURPOSE=USER".split())
stream.update("316 SENTCONNECT 186 1.2.3.4:80 SOURCE=EXIT".split())
self.assertEqual(self.circuits[186].streams[0], stream)
def test_circuit_already_valid_in_new(self):
stream = Stream(self)
stream.circuit = FakeCircuit(1)
stream.update("1 NEW 0 94.23.164.42.$43ED8310EB968746970896E8835C2F1991E50B69.exit:9001 SOURCE_ADDR=(Tor_internal):0 PURPOSE=DIR_FETCH".split())
errs = self.flushLoggedErrors()
self.assertEqual(len(errs), 1)
self.assertTrue('Weird' in errs[0].getErrorMessage())
def test_listen_unlisten(self):
self.circuits[186] = FakeCircuit(186)
listener = Listener([])
stream = Stream(self)
stream.listen(listener)
stream.listen(listener)
self.assertEqual(len(stream.listeners), 1)
stream.unlisten(listener)
self.assertEqual(len(stream.listeners), 0)
def test_listener_new(self):
listener = Listener([('new', {'target_port': 9001})])
stream = Stream(self)
stream.listen(listener)
stream.update("1 NEW 0 94.23.164.42.$43ED8310EB968746970896E8835C2F1991E50B69.exit:9001 SOURCE_ADDR=(Tor_internal):0 PURPOSE=DIR_FETCH".split())
def test_magic_circuit_detach(self):
stream = Stream(self)
stream.circuit = FakeCircuit(1)
stream.circuit.streams = [stream]
stream.update("1 SENTCONNECT 0 94.23.164.42.$43ED8310EB968746970896E8835C2F1991E50B69.exit:9001 SOURCE_ADDR=(Tor_internal):0 PURPOSE=DIR_FETCH".split())
self.assertTrue(stream.circuit is None)
def test_stream_changed_with_detach(self):
"Change a stream-id mid-stream, but with a DETACHED message"
self.circuits[123] = FakeCircuit(123)
self.circuits[456] = FakeCircuit(456)
listener = Listener(
[
('new', {'target_host': 'www.yahoo.com', 'target_port': 80}),
('attach', {}),
('detach', {'kwargs': dict(reason='END', remote_reason='MISC')}),
('attach', {})
]
)
stream = Stream(self)
stream.listen(listener)
stream.update("999 NEW 0 www.yahoo.com:80 SOURCE_ADDR=127.0.0.1:55877 PURPOSE=USER".split())
stream.update("999 SENTCONNECT 123 1.2.3.4:80".split())
self.assertEqual(len(self.circuits[123].streams), 1)
self.assertEqual(self.circuits[123].streams[0], stream)
stream.update("999 DETACHED 123 1.2.3.4:80 REASON=END REMOTE_REASON=MISC".split())
self.assertEqual(len(self.circuits[123].streams), 0)
stream.update("999 SENTCONNECT 456 1.2.3.4:80 SOURCE=EXIT".split())
self.assertEqual(len(self.circuits[456].streams), 1)
self.assertEqual(self.circuits[456].streams[0], stream)
def test_listener_close(self):
self.circuits[186] = FakeCircuit(186)
listener = Listener(
[
('new', {'target_host': 'www.yahoo.com', 'target_port': 80}),
('attach', {'target_addr': maybe_ip_addr('1.2.3.4')}),
('closed', {'kwargs': dict(REASON='END', REMOTE_REASON='DONE')})
]
)
stream = Stream(self)
stream.listen(listener)
stream.update("316 NEW 0 www.yahoo.com:80 SOURCE_ADDR=127.0.0.1:55877 PURPOSE=USER".split())
stream.update("316 REMAP 186 1.2.3.4:80 SOURCE=EXIT".split())
stream.update("316 CLOSED 186 1.2.3.4:80 REASON=END REMOTE_REASON=DONE".split())
self.assertEqual(len(self.circuits[186].streams), 0)
def test_str(self):
stream = Stream(self)
stream.update("316 NEW 0 www.yahoo.com:80 SOURCE_ADDR=127.0.0.1:55877 PURPOSE=USER".split())
stream.circuit = FakeCircuit(1)
str(stream)
def test_ipv6_source(self):
listener = Listener(
[
('new', {'source_addr': maybe_ip_addr('::1'),
'source_port': 12345})
]
)
stream = Stream(self)
stream.listen(listener)
stream.update("1234 NEW 0 127.0.0.1:80 SOURCE_ADDR=::1:12345 PURPOSE=USER".split())
def test_lowercase_flags(self):
# testing an internal method, maybe a no-no?
stream = Stream(self)
kw = dict(FOO='bar', BAR='baz')
flags = stream._create_flags(kw)
self.assertTrue('FOO' in flags)
self.assertTrue('foo' in flags)
self.assertTrue(flags['foo'] is flags['FOO'])
self.assertTrue('BAR' in flags)
self.assertTrue('bar' in flags)
self.assertTrue(flags['bar'] is flags['BAR'])