def test_ephemeral_remove_not_ok(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) eph_d = EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION NEW:BEST Port=80,127.0.0.1:80", cmd) d.callback("PrivateKey={}\nServiceID={}".format(_test_private_key_blob, _test_onion_id)) cb = protocol.events['HS_DESC'] for x in range(6): cb('UPLOAD {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) for x in range(6): cb('UPLOADED {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) hs = yield eph_d remove_d = hs.remove() cmd, d = protocol.commands[-1] self.assertEqual(u"DEL_ONION {}".format(_test_onion_id), cmd) d.callback('bad stuff') with self.assertRaises(RuntimeError): yield remove_d
def test_ephemeral_v3_no_key(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) # returns a Deferred we're ignoring EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], detach=True, version=3, ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION NEW:ED25519-V3 Port=80,127.0.0.1:80 Flags=Detach", cmd) d.callback("PrivateKey={}\nServiceID={}".format(_test_private_key_blob, _test_onion_id))
def test_ephemeral_given_key(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) # returns a Deferred we're ignoring EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], private_key=_test_private_key_blob, detach=True, ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION RSA1024:{} Port=80,127.0.0.1:80 Flags=Detach".format(_test_private_key_blob), cmd) d.callback("PrivateKey={}\nServiceID={}".format(_test_private_key_blob, _test_onion_id))
def test_ephemeral_v3_ip_addr_tuple(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) # returns a Deferred we're ignoring EphemeralOnionService.create( Mock(), config, ports=[(80, "192.168.1.2:80")], detach=True, version=3, ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION NEW:ED25519-V3 Port=80,192.168.1.2:80 Flags=Detach", cmd) d.callback("PrivateKey={}\nServiceID={}".format(_test_private_key_blob, _test_onion_id))
def test_ephemeral_bad_return_value(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) progress_messages = [] def progress(*args): progress_messages.append(args) eph_d = EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], progress=progress, private_key=DISCARD, ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION NEW:BEST Port=80,127.0.0.1:80 Flags=DiscardPK", cmd) d.callback("BadKey=nothing") def check(f): self.assertIn("Expected ADD_ONION to return ServiceID", str(f.value)) return None eph_d.addCallbacks(self.fail, check) return eph_d
def test_ephemeral_ver_option(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) hs = EphemeralOnionService( config, ports=["80 127.0.0.1:80"], ver=2, ) self.assertEqual(2, hs.version)
def test_ephemeral_v3_non_anonymous(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) # returns a Deferred we're ignoring EphemeralOnionService.create( Mock(), config, ports=[(80, "192.168.1.2:80")], version=3, detach=True, single_hop=True, ) cmd, d = protocol.commands[0] self.assertEqual( u"ADD_ONION NEW:ED25519-V3 Port=80,192.168.1.2:80 Flags=Detach,NonAnonymous", cmd) d.callback("PrivateKey={}\nServiceID={}".format( _test_private_key_blob, _test_onion_id))
def test_ephemeral_key_whitespace1(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) d = EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], private_key=_test_private_key_blob + '\n', detach=True, ) return self.assertFailure(d, ValueError)
def test_ephemeral_v3_ip_addr_tuple_non_local(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) # returns a Deferred we're ignoring with self.assertRaises(ValueError): yield EphemeralOnionService.create( Mock(), config, ports=[(80, "hostname:80")], detach=True, version=3, )
def test_ephemeral_ports_not_a_list(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports="80 127.0.0.1:80", private_key=privkey, ) self.assertIn("'ports' must be a list of strings", str(ctx.exception))
def test_ephemeral_ports_no_spaces(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["80:127.0.0.1:80"], private_key=privkey, ) self.assertIn("exactly one space", str(ctx.exception))
def test_ephemeral_ports_non_local(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["80 8.8.8.8:80"], private_key=privkey, ) self.assertIn("should be a local address", str(ctx.exception))
def test_ephemeral_ports_not_an_int(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["web 127.0.0.1:80"], private_key=privkey, ) self.assertIn("external port isn't an int", str(ctx.exception))
def test_ephemeral_extra_kwargs(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) with self.assertRaises(ValueError) as ctx: EphemeralOnionService( config, ports=["80 127.0.0.1:80"], ver=2, something_funny="foo", ) self.assertIn( "Unknown kwarg", str(ctx.exception), )
def test_ephemeral_ports_not_an_int(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["web 127.0.0.1:80"], private_key=privkey, ) self.assertIn( "external port isn't an int", str(ctx.exception) )
def test_ephemeral_ports_non_local(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["80 8.8.8.8:80"], private_key=privkey, ) self.assertIn( "should be a local address", str(ctx.exception) )
def test_ephemeral_ports_no_spaces(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["80:127.0.0.1:80"], private_key=privkey, ) self.assertIn( "exactly one space", str(ctx.exception) )
def test_ephemeral_ports_not_a_list(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'a' * 32 with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports="80 127.0.0.1:80", private_key=privkey, ) self.assertIn( "'ports' must be a list of strings", str(ctx.exception) )
def test_ephemeral_v3_wrong_key_type(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) privkey = 'RSA1024:{}'.format('a' * 32) with self.assertRaises(ValueError) as ctx: yield EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], detach=True, version=3, private_key=privkey, ) self.assertIn( "but private key isn't", str(ctx.exception), )
def test_descriptor_all_uploads_fail(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) progress_messages = [] def progress(*args): progress_messages.append(args) eph_d = EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], progress=progress, private_key=DISCARD, ) cmd, d = protocol.commands[0] self.assertEqual( u"ADD_ONION NEW:BEST Port=80,127.0.0.1:80 Flags=DiscardPK", cmd) d.callback("PrivateKey={}\nServiceID={}".format( _test_private_key_blob, _test_onion_id)) # get the event-listener callback that torconfig code added cb = protocol.events['HS_DESC'] for x in range(6): cb('UPLOAD {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) for x in range(6): cb('FAILED {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) # now when we wait for our onion, it should already be failed # because all 6 uploads failed. with self.assertRaises(RuntimeError) as ctx: yield eph_d self.assertIn("Failed to upload", str(ctx.exception)) for x in range(6): self.assertIn("hsdir_{}".format(x), str(ctx.exception))
def test_descriptor_all_uploads_fail(self): protocol = FakeControlProtocol([]) config = TorConfig(protocol) progress_messages = [] def progress(*args): progress_messages.append(args) eph_d = EphemeralOnionService.create( Mock(), config, ports=["80 127.0.0.1:80"], progress=progress, private_key=DISCARD, ) cmd, d = protocol.commands[0] self.assertEqual(u"ADD_ONION NEW:BEST Port=80,127.0.0.1:80 Flags=DiscardPK", cmd) d.callback("PrivateKey={}\nServiceID={}".format(_test_private_key_blob, _test_onion_id)) # get the event-listener callback that torconfig code added cb = protocol.events['HS_DESC'] for x in range(6): cb('UPLOAD {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) for x in range(6): cb('FAILED {} UNKNOWN hsdir_{}'.format(_test_onion_id, x)) # now when we wait for our onion, it should already be failed # because all 6 uploads failed. with self.assertRaises(RuntimeError) as ctx: yield eph_d self.assertIn("Failed to upload", str(ctx.exception)) for x in range(6): self.assertIn("hsdir_{}".format(x), str(ctx.exception))