예제 #1
0
파일: system.py 프로젝트: rl1987/stem
    def test_is_running(self, call_mock):
        """
    Exercises multiple use cases for the is_running function.
    """

        # mock response with a linux and bsd resolver
        running_commands = [
            str_type('irssi'),
            str_type('moc'),
            str_type('tor'),
            str_type('ps'),
            str_type('  firefox  ')
        ]

        for ps_cmd in (system.IS_RUNNING_PS_LINUX, system.IS_RUNNING_PS_BSD):
            call_mock.side_effect = mock_call(ps_cmd, running_commands)

            self.assertTrue(system.is_running('irssi'))
            self.assertTrue(system.is_running('moc'))
            self.assertTrue(system.is_running('tor'))
            self.assertTrue(system.is_running('ps'))
            self.assertTrue(system.is_running('firefox'))
            self.assertEqual(False, system.is_running('something_else'))

        # mock both calls failing

        call_mock.return_value = None
        call_mock.side_effect = None
        self.assertFalse(system.is_running('irssi'))
        self.assertEqual(None, system.is_running('irssi'))
예제 #2
0
파일: system.py 프로젝트: patrickod/stem
  def test_is_running(self, call_mock):
    """
    Exercises multiple use cases for the is_running function.
    """

    # mock response with a linux and bsd resolver
    running_commands = [str_type('irssi'), str_type('moc'), str_type('tor'),
                        str_type('ps'), str_type('  firefox  ')]

    for ps_cmd in (system.IS_RUNNING_PS_LINUX, system.IS_RUNNING_PS_BSD):
      call_mock.side_effect = mock_call(ps_cmd, running_commands)

      self.assertTrue(system.is_running('irssi'))
      self.assertTrue(system.is_running('moc'))
      self.assertTrue(system.is_running('tor'))
      self.assertTrue(system.is_running('ps'))
      self.assertTrue(system.is_running('firefox'))
      self.assertEqual(False, system.is_running('something_else'))

    # mock both calls failing

    call_mock.return_value = None
    call_mock.side_effect = None
    self.assertFalse(system.is_running('irssi'))
    self.assertEqual(None, system.is_running('irssi'))
예제 #3
0
    def test_local_microdescriptors(self):
        """
    Checks a small microdescriptor file with known contents.
    """

        descriptor_path = get_resource('cached-microdescs')

        with open(descriptor_path, 'rb') as descriptor_file:
            descriptors = stem.descriptor.parse_file(descriptor_file,
                                                     'microdescriptor 1.0')

            router = next(descriptors)
            self.assertEqual(FIRST_ONION_KEY, router.onion_key)
            self.assertEqual(None, router.ntor_onion_key)
            self.assertEqual([], router.or_addresses)
            self.assertEqual([], router.family)
            self.assertEqual(
                stem.exit_policy.MicroExitPolicy('reject 1-65535'),
                router.exit_policy)
            self.assertEqual({b'@last-listed': b'2013-02-24 00:18:36'},
                             router.get_annotations())
            self.assertEqual([b'@last-listed 2013-02-24 00:18:36'],
                             router.get_annotation_lines())

            router = next(descriptors)
            self.assertEqual(SECOND_ONION_KEY, router.onion_key)
            self.assertEqual(
                str_type('r5572HzD+PMPBbXlZwBhsm6YEbxnYgis8vhZ1jmdI2k='),
                router.ntor_onion_key)
            self.assertEqual([], router.or_addresses)
            self.assertEqual(['$6141629FA0D15A6AEAEF3A1BEB76E64C767B3174'],
                             router.family)
            self.assertEqual(
                stem.exit_policy.MicroExitPolicy('reject 1-65535'),
                router.exit_policy)
            self.assertEqual({b'@last-listed': b'2013-02-24 00:18:37'},
                             router.get_annotations())
            self.assertEqual([b'@last-listed 2013-02-24 00:18:37'],
                             router.get_annotation_lines())

            router = next(descriptors)
            self.assertEqual(THIRD_ONION_KEY, router.onion_key)
            self.assertEqual(None, router.ntor_onion_key)
            self.assertEqual([(str_type('2001:6b0:7:125::242'), 9001, True)],
                             router.or_addresses)
            self.assertEqual([], router.family)
            self.assertEqual(stem.exit_policy.MicroExitPolicy('accept 80,443'),
                             router.exit_policy)
            self.assertEqual({b'@last-listed': b'2013-02-24 00:18:36'},
                             router.get_annotations())
            self.assertEqual([b'@last-listed 2013-02-24 00:18:36'],
                             router.get_annotation_lines())
예제 #4
0
파일: reader.py 프로젝트: neelchauhan/stem
    def test_load_processed_files(self, open_mock):
        """
    Successful load of content.
    """

        test_lines = (
            str_type('/dir/ 0'),
            str_type('/dir/file 12345'),
            str_type('/dir/file with spaces 7138743'),
            str_type('  /dir/with extra space 12345   '),
            str_type('   \t   '),
            str_type(''),
            str_type('/dir/after empty line 12345'),
        )

        expected_value = {
            '/dir/': 0,
            '/dir/file': 12345,
            '/dir/file with spaces': 7138743,
            '/dir/with extra space': 12345,
            '/dir/after empty line': 12345,
        }

        open_mock.return_value = io.StringIO(str_type('\n'.join(test_lines)))
        self.assertEqual(expected_value,
                         stem.descriptor.reader.load_processed_files(''))
예제 #5
0
파일: reader.py 프로젝트: patrickod/stem
  def test_load_processed_files(self, open_mock):
    """
    Successful load of content.
    """

    test_lines = (
      str_type('/dir/ 0'),
      str_type('/dir/file 12345'),
      str_type('/dir/file with spaces 7138743'),
      str_type('  /dir/with extra space 12345   '),
      str_type('   \t   '),
      str_type(''),
      str_type('/dir/after empty line 12345'),
    )

    expected_value = {
      '/dir/': 0,
      '/dir/file': 12345,
      '/dir/file with spaces': 7138743,
      '/dir/with extra space': 12345,
      '/dir/after empty line': 12345,
    }

    open_mock.return_value = io.StringIO(str_type('\n'.join(test_lines)))
    self.assertEqual(expected_value, stem.descriptor.reader.load_processed_files(''))
예제 #6
0
파일: reader.py 프로젝트: neelchauhan/stem
    def test_load_processed_files_empty(self, open_mock):
        """
    Tests the load_processed_files() function with an empty file.
    """

        open_mock.return_value = io.StringIO(str_type(''))
        self.assertEqual({}, stem.descriptor.reader.load_processed_files(''))
예제 #7
0
파일: reader.py 프로젝트: patrickod/stem
  def test_load_processed_files_empty(self, open_mock):
    """
    Tests the load_processed_files() function with an empty file.
    """

    open_mock.return_value = io.StringIO(str_type(''))
    self.assertEqual({}, stem.descriptor.reader.load_processed_files(''))
예제 #8
0
  def test_load_processed_files_malformed_timestamp(self, open_mock):
    """
    Tests the load_processed_files() function content that is malformed because
    it has a non-numeric timestamp.
    """

    open_mock.return_value = io.StringIO(str_type('/dir/file 123a'))
    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
예제 #9
0
  def test_load_processed_files_malformed_file(self, open_mock):
    """
    Tests the load_processed_files() function content that is malformed because
    it has an invalid file path.
    """

    open_mock.return_value = io.StringIO(str_type('not_an_absolute_file 12345'))
    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
예제 #10
0
def _parse_exit_policy(descriptor, entries):
  if hasattr(descriptor, '_unparsed_exit_policy'):
    if descriptor._unparsed_exit_policy == [str_type('reject *:*')]:
      descriptor.exit_policy = REJECT_ALL_POLICY
    else:
      descriptor.exit_policy = stem.exit_policy.ExitPolicy(*descriptor._unparsed_exit_policy)

    del descriptor._unparsed_exit_policy
예제 #11
0
def _parse_exit_policy(descriptor, entries):
    if hasattr(descriptor, '_unparsed_exit_policy'):
        if descriptor._unparsed_exit_policy == [str_type('reject *:*')]:
            descriptor.exit_policy = REJECT_ALL_POLICY
        else:
            descriptor.exit_policy = stem.exit_policy.ExitPolicy(
                *descriptor._unparsed_exit_policy)

        del descriptor._unparsed_exit_policy
예제 #12
0
  def test_local_microdescriptors(self):
    """
    Checks a small microdescriptor file with known contents.
    """

    descriptor_path = get_resource('cached-microdescs')

    with open(descriptor_path, 'rb') as descriptor_file:
      descriptors = stem.descriptor.parse_file(descriptor_file, 'microdescriptor 1.0')

      router = next(descriptors)
      self.assertEqual(FIRST_ONION_KEY, router.onion_key)
      self.assertEqual(None, router.ntor_onion_key)
      self.assertEqual([], router.or_addresses)
      self.assertEqual([], router.family)
      self.assertEqual(stem.exit_policy.MicroExitPolicy('reject 1-65535'), router.exit_policy)
      self.assertEqual({b'@last-listed': b'2013-02-24 00:18:36'}, router.get_annotations())
      self.assertEqual([b'@last-listed 2013-02-24 00:18:36'], router.get_annotation_lines())

      router = next(descriptors)
      self.assertEqual(SECOND_ONION_KEY, router.onion_key)
      self.assertEqual(str_type('r5572HzD+PMPBbXlZwBhsm6YEbxnYgis8vhZ1jmdI2k='), router.ntor_onion_key)
      self.assertEqual([], router.or_addresses)
      self.assertEqual(['$6141629FA0D15A6AEAEF3A1BEB76E64C767B3174'], router.family)
      self.assertEqual(stem.exit_policy.MicroExitPolicy('reject 1-65535'), router.exit_policy)
      self.assertEqual({b'@last-listed': b'2013-02-24 00:18:37'}, router.get_annotations())
      self.assertEqual([b'@last-listed 2013-02-24 00:18:37'], router.get_annotation_lines())

      router = next(descriptors)
      self.assertEqual(THIRD_ONION_KEY, router.onion_key)
      self.assertEqual(None, router.ntor_onion_key)
      self.assertEqual([(str_type('2001:6b0:7:125::242'), 9001, True)], router.or_addresses)
      self.assertEqual([], router.family)
      self.assertEqual(stem.exit_policy.MicroExitPolicy('accept 80,443'), router.exit_policy)
      self.assertEqual({b'@last-listed': b'2013-02-24 00:18:36'}, router.get_annotations())
      self.assertEqual([b'@last-listed 2013-02-24 00:18:36'], router.get_annotation_lines())
예제 #13
0
from test.mocking import (
    get_relay_server_descriptor,
    get_bridge_server_descriptor,
    CRYPTO_BLOB,
)

from test.unit.descriptor import get_resource

try:
    # added in python 3.3
    from unittest.mock import Mock, patch
except ImportError:
    from mock import Mock, patch

TARFILE_FINGERPRINTS = set([
    str_type('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'),
    str_type('E0BD57A11F00041A9789577C53A1B784473669E4'),
    str_type('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'),
])


class TestServerDescriptor(unittest.TestCase):
    def test_with_tarfile_path(self):
        """
    Fetch server descriptors via parse_file() for a tarfile path.
    """

        descriptors = list(
            stem.descriptor.parse_file(get_resource('descriptor_archive.tar')))
        self.assertEqual(3, len(descriptors))
예제 #14
0
from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor

from test.unit.descriptor import (
  get_resource,
  base_expect_invalid_attr,
  base_expect_invalid_attr_for_text,
)

try:
  # added in python 3.3
  from unittest.mock import Mock, patch
except ImportError:
  from mock import Mock, patch

TARFILE_FINGERPRINTS = set([
  str_type('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'),
  str_type('E0BD57A11F00041A9789577C53A1B784473669E4'),
  str_type('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'),
])

expect_invalid_attr = functools.partial(base_expect_invalid_attr, RelayDescriptor, 'nickname', 'Unnamed')
expect_invalid_attr_for_text = functools.partial(base_expect_invalid_attr_for_text, RelayDescriptor, 'nickname', 'Unnamed')


class TestServerDescriptor(unittest.TestCase):
  def test_with_tarfile_path(self):
    """
    Fetch server descriptors via parse_file() for a tarfile path.
    """

    descriptors = list(stem.descriptor.parse_file(get_resource('descriptor_archive.tar')))
예제 #15
0
CIRC_CONTENT = '650 CIRC %d %s \
%s \
PURPOSE=%s'

PATH_CONTENT = '$%s=%s,$%s=%s,$%s=%s'

LIST_CIRCUITS_OUTPUT = str_type("""\

Circuit 4 (GENERAL)
 |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61)
 |- 0DD9935C5E939CFA1E07B8DDA6D91C1A2A9D9338 (afo02, 87.238.194.176)
 +- DB3B1CFBD3E4D97B84B548ADD5B9A31451EEC4CC (edwardsnowden3, 109.163.234.10)

Circuit 6 (GENERAL)
 |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61)
 |- EC01CB4766BADC1611678555CE793F2A7EB2D723 (sprockets, 46.165.197.96)
 +- 9EA317EECA56BDF30CAEB208A253FB456EDAB1A0 (bolobolo1, 96.47.226.20)

Circuit 10 (GENERAL)
 |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61)
 |- 00C2C2A16AEDB51D5E5FB7D6168FC66B343D822F (ph3x, 86.59.119.83)
 +- 65242C91BFF30F165DA4D132C81A9EBA94B71D62 (torexit16, 176.67.169.171)
""")

EXIT_USED_OUTPUT = str_type("""\
Tracking requests for tor exits. Press 'enter' to end.

Exit relay for our connection to 64.15.112.44:80
  address: 31.172.30.2:443
  fingerprint: A59E1E7C7EAEE083D756EE1FF6EC31CA3D8651D7