def test_single_failed_upload(self): eph = torconfig.EphemeralHiddenService(["80 127.0.0.1:80"]) proto = Mock() proto.queue_command = Mock(return_value=defer.succeed("PrivateKey=seekrit\nServiceID=42\n")) d = eph.add_to_tor(proto) # get the event-listener callback that torconfig code added; # the first call [0] was to add_event_listener; we want the # [1] arg of that cb = proto.method_calls[0][1][1] # Tor leads with UPLOAD events for each attempt; we queue 2 of # these... cb('UPLOAD 42 UNKNOWN hsdir0') cb('UPLOAD 42 UNKNOWN hsdir1') # ...then fail one cb('FAILED 42 UNKNOWN hsdir1 REASON=UPLOAD_REJECTED') # ...and succeed on the last. cb('UPLOADED 42 UNKNOWN hsdir0') self.assertEqual("seekrit", eph.private_key) self.assertEqual("42.onion", eph.hostname) self.assertTrue(d.called)
def test_remove(self): eph = torconfig.EphemeralHiddenService(["80 127.0.0.1:80"]) eph.hostname = 'foo.onion' proto = Mock() proto.queue_command = Mock(return_value="OK") eph.remove_from_tor(proto)
def test_add_keyblob(self): eph = torconfig.EphemeralHiddenService(["80 127.0.0.1:80"], "alg:blam") proto = Mock() proto.queue_command = Mock(return_value="ServiceID=ohai") eph.add_to_tor(proto) self.assertEqual("alg:blam", eph.private_key) self.assertEqual("ohai.onion", eph.hostname)
def test_wrong_blob(self): wrong_blobs = ["", " ", "foo", ":", " : ", "foo:", ":foo", 0] for b in wrong_blobs: try: torconfig.EphemeralHiddenService(["80 localhost:80"], b) self.fail("should get exception") except ValueError: pass
def test_remove_error(self): eph = torconfig.EphemeralHiddenService(["80 127.0.0.1:80"]) eph.hostname = 'foo.onion' proto = Mock() proto.queue_command = Mock(return_value="it's not ok") try: yield eph.remove_from_tor(proto) self.fail("should have gotten exception") except RuntimeError: pass
def test_descriptor_wait(self): eph = torconfig.EphemeralHiddenService(["80 127.0.0.1:80"]) proto = Mock() proto.queue_command = Mock(return_value=defer.succeed("PrivateKey=blam\nServiceID=ohai\n")) eph.add_to_tor(proto) # get the event-listener callback that torconfig code added; # the first call [0] was to add_event_listener; we want the # [1] arg of that cb = proto.method_calls[0][1][1] # Tor doesn't actually provide the .onion, but we can test it anyway cb('UPLOADED ohai UNKNOWN somehsdir') cb('UPLOADED UNKNOWN UNKNOWN somehsdir') self.assertEqual("blam", eph.private_key) self.assertEqual("ohai.onion", eph.hostname)
def test_defaults(self): eph = torconfig.EphemeralHiddenService(["80 localhost:80"]) self.assertEqual(eph._ports, ["80,localhost:80"])
def test_wrong_blob(self): try: torconfig.EphemeralHiddenService("80 localhost:80", "foo") self.fail("should get exception") except RuntimeError: pass