def test_annotations(self):
        """
    Checks that content before a descriptor are parsed as annotations.
    """

        desc_text = "@pepperjack very tasty\n@mushrooms not so much\n"
        desc_text += get_relay_server_descriptor(content=True)
        desc_text = sign_descriptor_content(desc_text)
        desc_text += "\ntrailing text that should be ignored, ho hum"

        # running parse_file should provide an iterator with a single descriptor
        desc_iter = stem.descriptor.server_descriptor.parse_file(
            StringIO.StringIO(desc_text))
        desc_entries = list(desc_iter)
        self.assertEquals(1, len(desc_entries))
        desc = desc_entries[0]

        self.assertEquals("caerSidi", desc.nickname)
        self.assertEquals("@pepperjack very tasty",
                          desc.get_annotation_lines()[0])
        self.assertEquals("@mushrooms not so much",
                          desc.get_annotation_lines()[1])
        self.assertEquals(
            {
                "@pepperjack": "very tasty",
                "@mushrooms": "not so much"
            }, desc.get_annotations())
        self.assertEquals([], desc.get_unrecognized_lines())
Esempio n. 2
0
  def test_mirror_mirror_on_the_wall_4(self):
    def tutorial_example():
      from stem.control import Controller
      from stem.util import str_tools

      # provides a mapping of observed bandwidth to the relay nicknames
      def get_bw_to_relay():
        bw_to_relay = {}

        with Controller.from_port(control_port = 9051) as controller:
          controller.authenticate()

          for desc in controller.get_server_descriptors():
            if desc.exit_policy.is_exiting_allowed():
              bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)

        return bw_to_relay

      # prints the top fifteen relays

      bw_to_relay = get_bw_to_relay()
      count = 1

      for bw_value in sorted(bw_to_relay.keys(), reverse = True):
        for nickname in bw_to_relay[bw_value]:
          print "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2))
          count += 1

          if count > 15:
            return

    exit_descriptor = mocking.get_relay_server_descriptor({
      'router': 'speedyexit 149.255.97.109 9001 0 0'
    }, content = True).replace(b'reject *:*', b'accept *:*')

    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
    exit_descriptor = RelayDescriptor(exit_descriptor)

    controller = mocking.get_object(Controller, {
      'authenticate': mocking.no_op(),
      'close': mocking.no_op(),
      'get_server_descriptors': mocking.return_value([
        exit_descriptor,
        mocking.get_relay_server_descriptor(),  # non-exit
        exit_descriptor,
        exit_descriptor,
      ])
    })

    mocking.mock(
      Controller.from_port, mocking.return_value(controller),
      target_module = Controller,
      is_static = True,
    )

    tutorial_example()
    self.assertEqual(MIRROR_MIRROR_OUTPUT, self.stdout.getvalue())
Esempio n. 3
0
    def test_mirror_mirror_on_the_wall_5(self, downloader_mock, stdout_mock):
        def tutorial_example():
            from stem.descriptor.remote import DescriptorDownloader
            from stem.util import str_tools

            # provides a mapping of observed bandwidth to the relay nicknames
            def get_bw_to_relay():
                bw_to_relay = {}

                downloader = DescriptorDownloader()

                try:
                    for desc in downloader.get_server_descriptors().run():
                        if desc.exit_policy.is_exiting_allowed():
                            bw_to_relay.setdefault(desc.observed_bandwidth,
                                                   []).append(desc.nickname)
                except Exception as exc:
                    print('Unable to retrieve the server descriptors: %s' %
                          exc)

                return bw_to_relay

            # prints the top fifteen relays

            bw_to_relay = get_bw_to_relay()
            count = 1

            for bw_value in sorted(bw_to_relay.keys(), reverse=True):
                for nickname in bw_to_relay[bw_value]:
                    print('%i. %s (%s/s)' %
                          (count, nickname, str_tools.size_label(bw_value, 2)))
                    count += 1

                    if count > 15:
                        return

        exit_descriptor = mocking.get_relay_server_descriptor(
            {
                'router': 'speedyexit 149.255.97.109 9001 0 0'
            }, content=True).replace(b'reject *:*', b'accept *:*')

        exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
        exit_descriptor = RelayDescriptor(exit_descriptor)

        downloader_mock().get_server_descriptors().run.return_value = [
            exit_descriptor,
            mocking.get_relay_server_descriptor(),  # non-exit
            exit_descriptor,
            exit_descriptor,
        ]

        tutorial_example()
        self.assertEqual(MIRROR_MIRROR_OUTPUT, stdout_mock.getvalue())
Esempio n. 4
0
 def test_published_leap_year(self):
   """
   Constructs with a published entry for a leap year, and when the date is
   invalid.
   """
   
   desc_text = get_relay_server_descriptor({"published": "2011-02-29 04:03:19"}, content = True)
   self._expect_invalid_attr(desc_text, "published")
   
   desc_text = get_relay_server_descriptor({"published": "2012-02-29 04:03:19"}, content = True)
   desc_text = sign_descriptor_content(desc_text)
   expected_published = datetime.datetime(2012, 2, 29, 4, 3, 19)
   self.assertEquals(expected_published, RelayDescriptor(desc_text).published)
Esempio n. 5
0
    def test_mirror_mirror_on_the_wall_4(self, from_port_mock, stdout_mock):
        def tutorial_example():
            from stem.control import Controller
            from stem.util import str_tools

            # provides a mapping of observed bandwidth to the relay nicknames
            def get_bw_to_relay():
                bw_to_relay = {}

                with Controller.from_port(control_port=9051) as controller:
                    controller.authenticate()

                    for desc in controller.get_server_descriptors():
                        if desc.exit_policy.is_exiting_allowed():
                            bw_to_relay.setdefault(desc.observed_bandwidth,
                                                   []).append(desc.nickname)

                return bw_to_relay

            # prints the top fifteen relays

            bw_to_relay = get_bw_to_relay()
            count = 1

            for bw_value in sorted(bw_to_relay.keys(), reverse=True):
                for nickname in bw_to_relay[bw_value]:
                    print "%i. %s (%s/s)" % (
                        count, nickname, str_tools.get_size_label(bw_value, 2))
                    count += 1

                    if count > 15:
                        return

        exit_descriptor = mocking.get_relay_server_descriptor(
            {
                'router': 'speedyexit 149.255.97.109 9001 0 0'
            }, content=True).replace(b'reject *:*', b'accept *:*')

        exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
        exit_descriptor = RelayDescriptor(exit_descriptor)

        controller = from_port_mock().__enter__()
        controller.get_server_descriptors.return_value = [
            exit_descriptor,
            mocking.get_relay_server_descriptor(),  # non-exit
            exit_descriptor,
            exit_descriptor,
        ]

        tutorial_example()
        self.assertEqual(MIRROR_MIRROR_OUTPUT, stdout_mock.getvalue())
Esempio n. 6
0
  def test_mirror_mirror_on_the_wall(self):
    from stem.descriptor.server_descriptor import RelayDescriptor
    from stem.descriptor.reader import DescriptorReader
    from stem.util import str_tools

    exit_descriptor = mocking.get_relay_server_descriptor({
     'router': 'speedyexit 149.255.97.109 9001 0 0'
    }, content = True).replace('reject *:*', 'accept *:*')
    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
    exit_descriptor = RelayDescriptor(exit_descriptor)

    reader_wrapper = mocking.get_object(DescriptorReader, {
      '__enter__': lambda x: x,
      '__exit__': mocking.no_op(),
      '__iter__': mocking.return_value(iter((
        exit_descriptor,
        mocking.get_relay_server_descriptor(),  # non-exit
        exit_descriptor,
        exit_descriptor,
      )))
    })

    # provides a mapping of observed bandwidth to the relay nicknames
    def get_bw_to_relay():
      bw_to_relay = {}

      with reader_wrapper as reader:
        for desc in reader:
          if desc.exit_policy.is_exiting_allowed():
            bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)

      return bw_to_relay

    # prints the top fifteen relays

    bw_to_relay = get_bw_to_relay()
    count = 1

    for bw_value in sorted(bw_to_relay.keys(), reverse = True):
      for nickname in bw_to_relay[bw_value]:
        expected_line = "%i. speedyexit (102.13 KB/s)" % count
        printed_line = "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2))
        self.assertEqual(expected_line, printed_line)

        count += 1

        if count > 15:
          return

    self.assertEqual(4, count)
Esempio n. 7
0
  def test_mirror_mirror_on_the_wall_5(self, downloader_mock, stdout_mock):
    def tutorial_example():
      from stem.descriptor.remote import DescriptorDownloader
      from stem.util import str_tools

      # provides a mapping of observed bandwidth to the relay nicknames
      def get_bw_to_relay():
        bw_to_relay = {}

        downloader = DescriptorDownloader()

        try:
          for desc in downloader.get_server_descriptors().run():
            if desc.exit_policy.is_exiting_allowed():
              bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
        except Exception as exc:
          print('Unable to retrieve the server descriptors: %s' % exc)

        return bw_to_relay

      # prints the top fifteen relays

      bw_to_relay = get_bw_to_relay()
      count = 1

      for bw_value in sorted(bw_to_relay.keys(), reverse = True):
        for nickname in bw_to_relay[bw_value]:
          print('%i. %s (%s/s)' % (count, nickname, str_tools.size_label(bw_value, 2)))
          count += 1

          if count > 15:
            return

    exit_descriptor = mocking.get_relay_server_descriptor({
      'router': 'speedyexit 149.255.97.109 9001 0 0'
    }, content = True).replace(b'reject *:*', b'accept *:*')

    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
    exit_descriptor = RelayDescriptor(exit_descriptor)

    downloader_mock().get_server_descriptors().run.return_value = [
      exit_descriptor,
      mocking.get_relay_server_descriptor(),  # non-exit
      exit_descriptor,
      exit_descriptor,
    ]

    tutorial_example()
    self.assertEqual(MIRROR_MIRROR_OUTPUT, stdout_mock.getvalue())
    def test_published_leap_year(self):
        """
    Constructs with a published entry for a leap year, and when the date is
    invalid.
    """

        desc_text = get_relay_server_descriptor(
            {"published": "2011-02-29 04:03:19"}, content=True)
        self._expect_invalid_attr(desc_text, "published")

        desc_text = get_relay_server_descriptor(
            {"published": "2012-02-29 04:03:19"}, content=True)
        desc_text = sign_descriptor_content(desc_text)
        expected_published = datetime.datetime(2012, 2, 29, 4, 3, 19)
        self.assertEquals(expected_published,
                          RelayDescriptor(desc_text).published)
Esempio n. 9
0
 def test_annotations(self):
   """
   Checks that content before a descriptor are parsed as annotations.
   """
   
   desc_text = "@pepperjack very tasty\n@mushrooms not so much\n"
   desc_text += get_relay_server_descriptor(content = True)
   desc_text = sign_descriptor_content(desc_text)
   desc_text += "\ntrailing text that should be ignored, ho hum"
   
   # running parse_file should provide an iterator with a single descriptor
   desc_iter = stem.descriptor.server_descriptor.parse_file(StringIO.StringIO(desc_text))
   desc_entries = list(desc_iter)
   self.assertEquals(1, len(desc_entries))
   desc = desc_entries[0]
   
   self.assertEquals("caerSidi", desc.nickname)
   self.assertEquals("@pepperjack very tasty", desc.get_annotation_lines()[0])
   self.assertEquals("@mushrooms not so much", desc.get_annotation_lines()[1])
   self.assertEquals({"@pepperjack": "very tasty", "@mushrooms": "not so much"}, desc.get_annotations())
   self.assertEquals([], desc.get_unrecognized_lines())