Ejemplo n.º 1
0
  def test_get_server_descriptor(self):
    """
    Basic checks for get_server_descriptor().
    """

    runner = test.runner.get_runner()

    if test.runner.require_control(self):
      return
    elif runner.get_tor_version() >= Requirement.MICRODESCRIPTOR_IS_DEFAULT:
      test.runner.skip(self, "(requires server 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_relay = self._get_router_status_entry(controller)

      desc_by_fingerprint = controller.get_server_descriptor(test_relay.fingerprint)
      desc_by_nickname = controller.get_server_descriptor(test_relay.nickname)

      self.assertEqual(desc_by_fingerprint, desc_by_nickname)
Ejemplo n.º 2
0
def _can_authenticate(auth_type):
    """
  Checks if a given authentication method can authenticate to our control
  socket.

  :param stem.connection.AuthMethod auth_type: authentication method to check

  :returns: bool that's True if we should be able to authenticate and False otherwise
  """

    runner = test.runner.get_runner()
    tor_options = runner.get_options()
    password_auth = test.runner.Torrc.PASSWORD in tor_options
    cookie_auth = test.runner.Torrc.COOKIE in tor_options
    safecookie_auth = cookie_auth and runner.get_tor_version(
    ).meets_requirements(stem.version.Requirement.AUTH_SAFECOOKIE)

    if not password_auth and not cookie_auth:
        # open socket, anything but safecookie will work
        return auth_type != stem.connection.AuthMethod.SAFECOOKIE
    elif auth_type == stem.connection.AuthMethod.PASSWORD:
        return password_auth
    elif auth_type == stem.connection.AuthMethod.COOKIE:
        return cookie_auth
    elif auth_type == stem.connection.AuthMethod.SAFECOOKIE:
        return safecookie_auth
    else:
        return False
Ejemplo n.º 3
0
    def test_get_server_descriptors(self):
        """
    Fetches a few descriptors via the get_server_descriptors() method.
    """

        runner = test.runner.get_runner()

        if test.runner.require_control(self):
            return
        elif runner.get_tor_version(
        ) >= Requirement.MICRODESCRIPTOR_IS_DEFAULT:
            test.runner.skip(self, '(requires server descriptors)')
            return

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

            for desc in controller.get_server_descriptors():
                self.assertTrue(desc.fingerprint is not None)
                self.assertTrue(desc.nickname is not None)

                # Se don't want to take the time to read the whole thing. We already
                # have another test that reads the full cached descriptors (and takes a
                # while to do so).

                count += 1
                if count > 10:
                    break
Ejemplo n.º 4
0
    def assert_matches_test_config(self, protocolinfo_response):
        """
    Makes assertions that the protocolinfo response's attributes match those of
    the test configuration.
    """

        runner = test.runner.get_runner()
        tor_options = runner.get_options()
        tor_version = runner.get_tor_version()
        auth_methods, auth_cookie_path = [], None

        if test.runner.Torrc.COOKIE in tor_options:
            auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)

            if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
                auth_methods.append(
                    stem.response.protocolinfo.AuthMethod.SAFECOOKIE)

            chroot_path = runner.get_chroot()
            auth_cookie_path = runner.get_auth_cookie_path()

            if chroot_path and auth_cookie_path.startswith(chroot_path):
                auth_cookie_path = auth_cookie_path[len(chroot_path):]

        if test.runner.Torrc.PASSWORD in tor_options:
            auth_methods.append(stem.response.protocolinfo.AuthMethod.PASSWORD)

        if not auth_methods:
            auth_methods.append(stem.response.protocolinfo.AuthMethod.NONE)

        self.assertEqual((), protocolinfo_response.unknown_auth_methods)
        self.assertEqual(tuple(auth_methods),
                         protocolinfo_response.auth_methods)
        self.assertEqual(auth_cookie_path, protocolinfo_response.cookie_path)
Ejemplo n.º 5
0
  def test_protocolinfo(self):
    """
    Test that the convenient method protocolinfo() works.
    """

    if test.runner.require_control(self):
      return

    runner = test.runner.get_runner()

    with runner.get_tor_controller(False) as controller:
      protocolinfo = controller.get_protocolinfo()
      self.assertTrue(isinstance(protocolinfo, stem.response.protocolinfo.ProtocolInfoResponse))

      # Doing a sanity test on the ProtocolInfoResponse instance returned.
      tor_options = runner.get_options()
      tor_version = runner.get_tor_version()
      auth_methods = []

      if test.runner.Torrc.COOKIE in tor_options:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)

        if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
          auth_methods.append(stem.response.protocolinfo.AuthMethod.SAFECOOKIE)

      if test.runner.Torrc.PASSWORD in tor_options:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.PASSWORD)

      if not auth_methods:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.NONE)

      self.assertEqual(tuple(auth_methods), protocolinfo.auth_methods)
Ejemplo n.º 6
0
  def assert_matches_test_config(self, protocolinfo_response):
    """
    Makes assertions that the protocolinfo response's attributes match those of
    the test configuration.
    """

    runner = test.runner.get_runner()
    tor_options = runner.get_options()
    tor_version = runner.get_tor_version()
    auth_methods, auth_cookie_path = [], None

    if test.runner.Torrc.COOKIE in tor_options:
      auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)

      if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.SAFECOOKIE)

      chroot_path = runner.get_chroot()
      auth_cookie_path = runner.get_auth_cookie_path()

      if chroot_path and auth_cookie_path.startswith(chroot_path):
        auth_cookie_path = auth_cookie_path[len(chroot_path):]

    if test.runner.Torrc.PASSWORD in tor_options:
      auth_methods.append(stem.response.protocolinfo.AuthMethod.PASSWORD)

    if not auth_methods:
      auth_methods.append(stem.response.protocolinfo.AuthMethod.NONE)

    self.assertEqual((), protocolinfo_response.unknown_auth_methods)
    self.assertEqual(tuple(auth_methods), protocolinfo_response.auth_methods)
    self.assertEqual(auth_cookie_path, protocolinfo_response.cookie_path)
Ejemplo n.º 7
0
def _can_authenticate(auth_type):
  """
  Checks if a given authentication method can authenticate to our control
  socket.

  :param stem.connection.AuthMethod auth_type: authentication method to check

  :returns: bool that's True if we should be able to authenticate and False otherwise
  """

  runner = test.runner.get_runner()
  tor_options = runner.get_options()
  password_auth = test.runner.Torrc.PASSWORD in tor_options
  cookie_auth = test.runner.Torrc.COOKIE in tor_options
  safecookie_auth = cookie_auth and runner.get_tor_version() >= stem.version.Requirement.AUTH_SAFECOOKIE

  if not password_auth and not cookie_auth:
    # open socket, anything but safecookie will work
    return auth_type != stem.connection.AuthMethod.SAFECOOKIE
  elif auth_type == stem.connection.AuthMethod.PASSWORD:
    return password_auth
  elif auth_type == stem.connection.AuthMethod.COOKIE:
    return cookie_auth
  elif auth_type == stem.connection.AuthMethod.SAFECOOKIE:
    return safecookie_auth
  else:
    return False
Ejemplo n.º 8
0
  def test_get_server_descriptors(self):
    """
    Fetches a few descriptors via the get_server_descriptors() method.
    """

    runner = test.runner.get_runner()

    if test.runner.require_control(self):
      return
    elif runner.get_tor_version() >= Requirement.MICRODESCRIPTOR_IS_DEFAULT:
      test.runner.skip(self, "(requires server descriptors)")
      return

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

      for desc in controller.get_server_descriptors():
        self.assertTrue(desc.fingerprint is not None)
        self.assertTrue(desc.nickname is not None)

        # Se don't want to take the time to read the whole thing. We already
        # have another test that reads the full cached descriptors (and takes a
        # while to do so).

        count += 1
        if count > 10:
          break
Ejemplo n.º 9
0
  def test_get_server_descriptor(self):
    """
    Basic checks for get_server_descriptor().
    """

    runner = test.runner.get_runner()

    if test.runner.require_control(self):
      return
    elif runner.get_tor_version() >= Requirement.MICRODESCRIPTOR_IS_DEFAULT:
      test.runner.skip(self, "(requires server 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_relay = self._get_router_status_entry(controller)

      desc_by_fingerprint = controller.get_server_descriptor(test_relay.fingerprint)
      desc_by_nickname = controller.get_server_descriptor(test_relay.nickname)

      self.assertEqual(desc_by_fingerprint, desc_by_nickname)
Ejemplo n.º 10
0
  def test_protocolinfo(self):
    """
    Test that the convenient method protocolinfo() works.
    """

    if test.runner.require_control(self):
      return

    runner = test.runner.get_runner()

    with runner.get_tor_controller(False) as controller:
      protocolinfo = controller.get_protocolinfo()
      self.assertTrue(isinstance(protocolinfo, stem.response.protocolinfo.ProtocolInfoResponse))

      # Doing a sanity test on the ProtocolInfoResponse instance returned.
      tor_options = runner.get_options()
      tor_version = runner.get_tor_version()
      auth_methods = []

      if test.runner.Torrc.COOKIE in tor_options:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)

        if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
          auth_methods.append(stem.response.protocolinfo.AuthMethod.SAFECOOKIE)

      if test.runner.Torrc.PASSWORD in tor_options:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.PASSWORD)

      if not auth_methods:
        auth_methods.append(stem.response.protocolinfo.AuthMethod.NONE)

      self.assertEqual(tuple(auth_methods), protocolinfo.auth_methods)
Ejemplo n.º 11
0
  def test_get_system_tor_version_value(self):
    """
    Checks that the get_system_tor_version() provides the same value as our
    test instance provides.
    """

    runner = test.runner.get_runner()
    system_tor_version = stem.version.get_system_tor_version(runner.get_tor_command())
    self.assertEqual(runner.get_tor_version(), system_tor_version)
Ejemplo n.º 12
0
 def test_get_version(self):
   """
   Test that the convenient method get_version() works.
   """
   
   if test.runner.require_control(self): return
   
   runner = test.runner.get_runner()
   with runner.get_tor_controller() as controller:
     version = controller.get_version()
     self.assertTrue(isinstance(version, stem.version.Version))
     self.assertEqual(version, runner.get_tor_version())
Ejemplo n.º 13
0
    def test_get_version(self):
        """
    Test that the convenient method get_version() works.
    """

        if test.runner.require_control(self):
            return

        runner = test.runner.get_runner()

        with runner.get_tor_controller() as controller:
            version = controller.get_version()
            self.assertTrue(isinstance(version, stem.version.Version))
            self.assertEqual(version, runner.get_tor_version())
Ejemplo n.º 14
0
  def test_send_buffered(self):
    """
    Sends multiple requests before receiving back any of the replies.
    """

    runner = test.runner.get_runner()
    tor_version = runner.get_tor_version()

    with runner.get_tor_socket() as control_socket:
      for _ in range(100):
        control_socket.send('GETINFO version')

      for _ in range(100):
        response = control_socket.recv()
        self.assertTrue(str(response).startswith('version=%s' % tor_version))
        self.assertTrue(str(response).endswith('\nOK'))
Ejemplo n.º 15
0
    def test_send_buffered(self):
        """
    Sends multiple requests before receiving back any of the replies.
    """

        runner = test.runner.get_runner()
        tor_version = runner.get_tor_version()

        with runner.get_tor_socket() as control_socket:
            for _ in range(100):
                control_socket.send('GETINFO version')

            for _ in range(100):
                response = control_socket.recv()
                self.assertTrue(
                    str(response).startswith('version=%s' % tor_version))
                self.assertTrue(str(response).endswith('\nOK'))