Beispiel #1
0
 def test_get_server_descriptor(self):
   """
   Compares get_server_descriptor() against our cached descriptors.
   """
   
   runner = test.runner.get_runner()
   descriptor_path = runner.get_test_dir("cached-descriptors")
   
   if test.runner.require_control(self): return
   elif not os.path.exists(descriptor_path):
     test.runner.skip(self, "(no cached descriptors)")
     return
   
   with runner.get_tor_controller() as controller:
     # we should balk at invalid content
     self.assertRaises(ValueError, controller.get_server_descriptor, None)
     self.assertRaises(ValueError, controller.get_server_descriptor, "")
     self.assertRaises(ValueError, controller.get_server_descriptor, 5)
     self.assertRaises(ValueError, controller.get_server_descriptor, "z" * 30)
     
     # try with a relay that doesn't exist
     self.assertRaises(stem.socket.ControllerError, controller.get_server_descriptor, "blargg")
     self.assertRaises(stem.socket.ControllerError, controller.get_server_descriptor, "5" * 40)
     
     first_descriptor = None
     with stem.descriptor.reader.DescriptorReader([descriptor_path]) as reader:
       for desc in reader:
         if desc.nickname != "Unnamed":
           first_descriptor = desc
           break
     
     self.assertEqual(first_descriptor, controller.get_server_descriptor(first_descriptor.fingerprint))
     self.assertEqual(first_descriptor, controller.get_server_descriptor(first_descriptor.nickname))
Beispiel #2
0
    def test_get_network_status(self):
        """
    Compares get_network_status() against our cached descriptors.
    """

        runner = test.runner.get_runner()
        descriptor_path = runner.get_test_dir("cached-consensus")

        if test.runner.require_control(self):
            return
        elif not os.path.exists(descriptor_path):
            test.runner.skip(self, "(no cached descriptors)")
            return

        with runner.get_tor_controller() as controller:
            # we should balk at invalid content
            self.assertRaises(ValueError, controller.get_network_status, None)
            self.assertRaises(ValueError, controller.get_network_status, "")
            self.assertRaises(ValueError, controller.get_network_status, 5)
            self.assertRaises(ValueError, controller.get_network_status,
                              "z" * 30)

            # try with a relay that doesn't exist
            self.assertRaises(stem.ControllerError,
                              controller.get_network_status, "blargg")
            self.assertRaises(stem.ControllerError,
                              controller.get_network_status, "5" * 40)

            # our cached consensus is v3 but the control port can only be queried for
            # v2 or v1 network status information

            test.runner.skip(self, "(https://trac.torproject.org/7163)")
            return

            first_descriptor = None
            with stem.descriptor.reader.DescriptorReader([descriptor_path
                                                          ]) as reader:
                for desc in reader:
                    if desc.nickname != "Unnamed":
                        # truncate to just the first couple lines and reconstruct as a v2 entry
                        truncated_content = "\n".join(
                            str(desc).split("\n")[:2])

                        first_descriptor = stem.descriptor.router_status_entry.RouterStatusEntryV2(
                            truncated_content)
                        break

            self.assertEqual(
                first_descriptor,
                controller.get_network_status(first_descriptor.fingerprint))
            self.assertEqual(
                first_descriptor,
                controller.get_network_status(first_descriptor.nickname))
Beispiel #3
0
    def test_get_server_descriptor(self):
        """
    Compares get_server_descriptor() against our cached descriptors.
    """

        runner = test.runner.get_runner()
        descriptor_path = runner.get_test_dir("cached-descriptors")

        if test.runner.require_control(self):
            return
        elif not os.path.exists(descriptor_path):
            test.runner.skip(self, "(no cached descriptors)")
            return

        with runner.get_tor_controller() as controller:
            # we should balk at invalid content
            self.assertRaises(ValueError, controller.get_server_descriptor,
                              None)
            self.assertRaises(ValueError, controller.get_server_descriptor, "")
            self.assertRaises(ValueError, controller.get_server_descriptor, 5)
            self.assertRaises(ValueError, controller.get_server_descriptor,
                              "z" * 30)

            # try with a relay that doesn't exist
            self.assertRaises(stem.ControllerError,
                              controller.get_server_descriptor, "blargg")
            self.assertRaises(stem.ControllerError,
                              controller.get_server_descriptor, "5" * 40)

            test.runner.skip(self, "(https://trac.torproject.org/7163)")
            return

            first_descriptor = None
            with stem.descriptor.reader.DescriptorReader([descriptor_path
                                                          ]) as reader:
                for desc in reader:
                    if desc.nickname != "Unnamed":
                        first_descriptor = desc
                        break

            self.assertEqual(
                first_descriptor,
                controller.get_server_descriptor(first_descriptor.fingerprint))
            self.assertEqual(
                first_descriptor,
                controller.get_server_descriptor(first_descriptor.nickname))
Beispiel #4
0
    def test_get_network_status(self):
        """
    Compares get_network_status() against our cached descriptors.
    """

        runner = test.runner.get_runner()
        descriptor_path = runner.get_test_dir("cached-consensus")

        if test.runner.require_control(self):
            return
        elif not os.path.exists(descriptor_path):
            test.runner.skip(self, "(no cached descriptors)")
            return

        with runner.get_tor_controller() as controller:
            # we should balk at invalid content
            self.assertRaises(ValueError, controller.get_network_status, None)
            self.assertRaises(ValueError, controller.get_network_status, "")
            self.assertRaises(ValueError, controller.get_network_status, 5)
            self.assertRaises(ValueError, controller.get_network_status, "z" * 30)

            # try with a relay that doesn't exist
            self.assertRaises(stem.ControllerError, controller.get_network_status, "blargg")
            self.assertRaises(stem.ControllerError, controller.get_network_status, "5" * 40)

            # our cached consensus is v3 but the control port can only be queried for
            # v2 or v1 network status information

            test.runner.skip(self, "(https://trac.torproject.org/7163)")
            return

            first_descriptor = None
            with stem.descriptor.reader.DescriptorReader([descriptor_path]) as reader:
                for desc in reader:
                    if desc.nickname != "Unnamed":
                        # truncate to just the first couple lines and reconstruct as a v2 entry
                        truncated_content = "\n".join(str(desc).split("\n")[:2])

                        first_descriptor = stem.descriptor.router_status_entry.RouterStatusEntryV2(truncated_content)
                        break

            self.assertEqual(first_descriptor, controller.get_network_status(first_descriptor.fingerprint))
            self.assertEqual(first_descriptor, controller.get_network_status(first_descriptor.nickname))
Beispiel #5
0
  def test_get_microdescriptors(self):
    """
    Fetches a few descriptors via the get_microdescriptors() method.
    """

    runner = test.runner.get_runner()

    if not os.path.exists(runner.get_test_dir('cached-descriptors')):
      self.skipTest('(no cached microdescriptors)')
      return

    with runner.get_tor_controller() as controller:
      count = 0

      for desc in controller.get_microdescriptors():
        self.assertTrue(desc.onion_key is not None)

        count += 1
        if count > 10:
          break
Beispiel #6
0
    def test_get_microdescriptors(self):
        """
    Fetches a few descriptors via the get_microdescriptors() method.
    """

        runner = test.runner.get_runner()

        if test.runner.require_control(self):
            return
        elif not os.path.exists(runner.get_test_dir('cached-descriptors')):
            test.runner.skip(self, '(no cached microdescriptors)')
            return

        with runner.get_tor_controller() as controller:
            count = 0

            for desc in controller.get_microdescriptors():
                self.assertTrue(desc.onion_key is not None)

                count += 1
                if count > 10:
                    break
Beispiel #7
0
  def test_hidden_services_conf(self):
    """
    Exercises the hidden service family of methods (get_hidden_service_conf,
    set_hidden_service_conf, create_hidden_service, and remove_hidden_service).
    """

    runner = test.runner.get_runner()

    test_dir = runner.get_test_dir()
    service1_path = os.path.join(test_dir, 'test_hidden_service1')
    service2_path = os.path.join(test_dir, 'test_hidden_service2')
    service3_path = os.path.join(test_dir, 'test_hidden_service3')
    service4_path = os.path.join(test_dir, 'test_hidden_service4')

    with runner.get_tor_controller() as controller:
      try:
        # initially we shouldn't be running any hidden services

        self.assertEqual({}, controller.get_hidden_service_conf())

        # try setting a blank config, shouldn't have any impact

        controller.set_hidden_service_conf({})
        self.assertEqual({}, controller.get_hidden_service_conf())

        # create a hidden service

        initialconf = {
          service1_path: {
            'HiddenServicePort': [
              (8020, '127.0.0.1', 8020),
              (8021, '127.0.0.1', 8021),
            ],
            'HiddenServiceVersion': '2',
          },
          service2_path: {
            'HiddenServiceAuthorizeClient': 'stealth a, b',
            'HiddenServicePort': [
              (8030, '127.0.0.1', 8030),
              (8031, '127.0.0.1', 8031),
              (8032, '127.0.0.1', 8032),
            ]
          },
        }

        controller.set_hidden_service_conf(initialconf)
        self.assertEqual(initialconf, controller.get_hidden_service_conf())

        # add already existing services, with/without explicit target

        self.assertEqual(None, controller.create_hidden_service(service1_path, 8020))
        self.assertEqual(None, controller.create_hidden_service(service1_path, 8021, target_port = 8021))
        self.assertEqual(initialconf, controller.get_hidden_service_conf())

        # add a new service, with/without explicit target

        hs_path = os.path.join(os.getcwd(), service3_path)
        hs_address1 = controller.create_hidden_service(hs_path, 8888).hostname
        hs_address2 = controller.create_hidden_service(hs_path, 8989, target_port = 8021).hostname

        self.assertEqual(hs_address1, hs_address2)
        self.assertTrue(hs_address1.endswith('.onion'))

        conf = controller.get_hidden_service_conf()
        self.assertEqual(3, len(conf))
        self.assertEqual(2, len(conf[hs_path]['HiddenServicePort']))

        # remove a hidden service, the service dir should still be there

        controller.remove_hidden_service(hs_path, 8888)
        self.assertEqual(3, len(controller.get_hidden_service_conf()))

        # remove a service completely, it should now be gone

        controller.remove_hidden_service(hs_path, 8989)
        self.assertEqual(2, len(controller.get_hidden_service_conf()))

        # add a new service, this time with client authentication

        hs_path = os.path.join(os.getcwd(), service4_path)
        hs_attributes = controller.create_hidden_service(hs_path, 8888, auth_type = 'basic', client_names = ['c1', 'c2'])

        self.assertEqual(2, len(hs_attributes.hostname.splitlines()))
        self.assertEqual(2, len(hs_attributes.hostname_for_client))
        self.assertTrue(hs_attributes.hostname_for_client['c1'].endswith('.onion'))
        self.assertTrue(hs_attributes.hostname_for_client['c2'].endswith('.onion'))

        conf = controller.get_hidden_service_conf()
        self.assertEqual(3, len(conf))
        self.assertEqual(1, len(conf[hs_path]['HiddenServicePort']))

        # remove a hidden service

        controller.remove_hidden_service(hs_path, 8888)
        self.assertEqual(2, len(controller.get_hidden_service_conf()))
      finally:
        controller.set_hidden_service_conf({})  # drop hidden services created during the test

        # clean up the hidden service directories created as part of this test

        for path in (service1_path, service2_path, service3_path, service4_path):
          try:
            shutil.rmtree(path)
          except:
            pass