Exemple #1
0
    def test_exit_policy(self):
        """
    Handles a variety of 'p' lines.
    """

        test_values = {
            'reject 1-65535': MicroExitPolicy('reject 1-65535'),
            'accept 80,110,143,443': MicroExitPolicy('accept 80,110,143,443'),
        }

        for p_line, expected in test_values.items():
            entry = get_router_status_entry_v3({'p': p_line})
            self.assertEquals(expected, entry.exit_policy)

        # tries some invalid inputs
        test_values = (
            '',
            'blarg',
            'reject -50',
            'accept 80,',
        )

        for p_line in test_values:
            content = get_router_status_entry_v3({'p': p_line}, content=True)
            self._expect_invalid_attr(content, 'exit_policy')
Exemple #2
0
    def test_microdescriptor_can_exit_to(self):
        test_inputs = {
            'accept 443': {
                442: False,
                443: True,
                444: False
            },
            'reject 443': {
                442: True,
                443: False,
                444: True
            },
            'accept 80,443': {
                80: True,
                443: True,
                10: False
            },
            'reject 1-1024': {
                1: False,
                1024: False,
                1025: True
            },
        }

        for policy_arg, attr in test_inputs.items():
            policy = MicroExitPolicy(policy_arg)

            for port, expected_value in attr.items():
                self.assertEqual(expected_value, policy.can_exit_to(port=port))

        # address argument should be ignored
        policy = MicroExitPolicy('accept 80,443')

        self.assertFalse(policy.can_exit_to('127.0.0.1', 79))
        self.assertTrue(policy.can_exit_to('127.0.0.1', 80))
Exemple #3
0
    def test_exit_policy(self):
        """
    Handles a variety of 'p' lines.
    """

        test_values = {
            "reject 1-65535": MicroExitPolicy("reject 1-65535"),
            "accept 80,110,143,443": MicroExitPolicy("accept 80,110,143,443"),
        }

        for p_line, expected in test_values.items():
            entry = get_router_status_entry_v3({'p': p_line})
            self.assertEquals(expected, entry.exit_policy)

        # tries some invalid inputs
        test_values = (
            "",
            "blarg",
            "reject -50",
            "accept 80,",
        )

        for p_line in test_values:
            content = get_router_status_entry_v3({'p': p_line}, content=True)
            self._expect_invalid_attr(content, "exit_policy")
Exemple #4
0
    def test_microdescriptor_attributes(self):
        # checks that its is_accept attribute is properly set

        # single port
        policy = MicroExitPolicy('accept 443')
        self.assertTrue(policy.is_accept)

        # multiple ports
        policy = MicroExitPolicy('accept 80,443')
        self.assertTrue(policy.is_accept)

        # port range
        policy = MicroExitPolicy('reject 1-1024')
        self.assertFalse(policy.is_accept)
Exemple #5
0
    def test_microdescriptor_parsing(self):
        # mapping between inputs and if they should succeed or not
        test_inputs = {
            'accept 80': True,
            'accept 80,443': True,
            '': False,
            'accept': False,
            'accept ': False,
            'accept\t80,443': False,
            'accept 80, 443': False,
            'accept 80,\t443': False,
            '80,443': False,
            'accept 80,-443': False,
            'accept 80,+443': False,
            'accept 80,66666': False,
            'reject 80,foo': False,
            'bar 80,443': False,
        }

        for policy_arg, expect_success in test_inputs.items():
            try:
                policy = MicroExitPolicy(policy_arg)

                if expect_success:
                    self.assertEqual(policy_arg, str(policy))
                else:
                    self.fail()
            except ValueError:
                if expect_success:
                    self.fail()
Exemple #6
0
  def test_with_ed25519(self):
    """
    Parses a router status entry with a ed25519 value.
    """

    microdescriptor_hashes = [
      ([13], {'sha256': 'PTSHzE7RKnRGZMRmBddSzDiZio254FUhv9+V4F5zq8s'}),
      ([14, 15], {'sha256': '0wsEwBbxJ8RtPmGYwilHQTVEw2pWzUBEVlSgEO77OyU'}),
      ([16, 17], {'sha256': 'JK2xhYr/VsCF60px+LsT990BCpfKfQTeMxRbD63o2vE'}),
      ([18, 19, 20], {'sha256': 'AkZH3gIvz3wunsroqh5izBJizdYuR7kn2oVbsvqgML8'}),
      ([21], {'sha256': 'AVp41YVxKEJCaoEf0+77Cdvyw5YgpyDXdob0+LSv/pE'}),
    ]

    entry = RouterStatusEntryV3(ENTRY_WITH_ED25519, document = vote_document(), validate = True)
    self.assertEqual('PDrelay1', entry.nickname)
    self.assertEqual('000149E6EF7102AACA9690D6E8DD2932124B94AB', entry.fingerprint)
    self.assertEqual(datetime.datetime(2015, 8, 23, 16, 52, 37), entry.published)
    self.assertEqual('95.215.44.189', entry.address)
    self.assertEqual(8080, entry.or_port)
    self.assertEqual(None, entry.dir_port)
    self.assertEqual(set([Flag.FAST, Flag.RUNNING, Flag.STABLE, Flag.VALID]), set(entry.flags))
    self.assertEqual('Tor 0.2.7.2-alpha-dev', entry.version_line)
    self.assertEqual(Version('0.2.7.2-alpha-dev'), entry.version)
    self.assertEqual(608, entry.bandwidth)
    self.assertEqual(472, entry.measured)
    self.assertEqual(False, entry.is_unmeasured)
    self.assertEqual([], entry.unrecognized_bandwidth_entries)
    self.assertEqual(MicroExitPolicy('reject 1-65535'), entry.exit_policy)
    self.assertEqual(microdescriptor_hashes, entry.microdescriptor_hashes)
    self.assertEqual('ed25519', entry.identifier_type)
    self.assertEqual('8RH34kO07Pp+XYwzdoATVyCibIvmbslUjRkAm7J4IA8', entry.identifier)
    self.assertEqual('CAB27A6FFEF7A661C18B0B11120C3E8A77FC585C', entry.digest)
    self.assertEqual([], entry.get_unrecognized_lines())
Exemple #7
0
  def test_without_ed25519(self):
    """
    Parses a router status entry without a ed25519 value.
    """

    microdescriptor_hashes = [
      ([13, 14, 15], {'sha256': 'uaAYTOVuYRqUwJpNfP2WizjzO0FiNQB4U97xSQu+vMc'}),
      ([16, 17], {'sha256': 'G6FmPe/ehgfb6tsRzFKDCwvvae+RICeP1MaP0vWDGyI'}),
      ([18, 19, 20, 21], {'sha256': '/XhIMOnhElo2UiKjL2S10uRka/fhg1CFfNd+9wgUwEE'}),
    ]

    entry = RouterStatusEntryV3(ENTRY_WITHOUT_ED25519, document = vote_document(), validate = True)
    self.assertEqual('seele', entry.nickname)
    self.assertEqual('000A10D43011EA4928A35F610405F92B4433B4DC', entry.fingerprint)
    self.assertEqual(datetime.datetime(2015, 8, 23, 0, 26, 35), entry.published)
    self.assertEqual('73.15.150.172', entry.address)
    self.assertEqual(9001, entry.or_port)
    self.assertEqual(None, entry.dir_port)
    self.assertEqual(set([Flag.RUNNING, Flag.STABLE, Flag.VALID]), set(entry.flags))
    self.assertEqual('Tor 0.2.6.10', entry.version_line)
    self.assertEqual(Version('0.2.6.10'), entry.version)
    self.assertEqual(102, entry.bandwidth)
    self.assertEqual(31, entry.measured)
    self.assertEqual(False, entry.is_unmeasured)
    self.assertEqual([], entry.unrecognized_bandwidth_entries)
    self.assertEqual(MicroExitPolicy('reject 1-65535'), entry.exit_policy)
    self.assertEqual(microdescriptor_hashes, entry.microdescriptor_hashes)
    self.assertEqual('ed25519', entry.identifier_type)
    self.assertEqual('none', entry.identifier)
    self.assertEqual('9B4CA73EEC3349EC6DCEC897609600D0771EF82B', entry.digest)
    self.assertEqual([], entry.get_unrecognized_lines())
Exemple #8
0
    def test_example(self):
        # tests the ExitPolicy and MicroExitPolicy pydoc examples
        policy = ExitPolicy('accept *:80', 'accept *:443', 'reject *:*')
        self.assertEqual('accept *:80, accept *:443, reject *:*', str(policy))
        self.assertEqual('accept 80, 443', policy.summary())
        self.assertTrue(policy.can_exit_to('75.119.206.243', 80))

        policy = MicroExitPolicy('accept 80,443')
        self.assertTrue(policy.can_exit_to('75.119.206.243', 80))
Exemple #9
0
    def test_example(self):
        # tests the ExitPolicy and MicroExitPolicy pydoc examples
        policy = ExitPolicy("accept *:80", "accept *:443", "reject *:*")
        self.assertEquals("accept *:80, accept *:443, reject *:*", str(policy))
        self.assertEquals("accept 80, 443", policy.summary())
        self.assertTrue(policy.can_exit_to("75.119.206.243", 80))

        policy = MicroExitPolicy("accept 80,443")
        self.assertTrue(policy.can_exit_to("75.119.206.243", 80))
Exemple #10
0
    def test_exit_policy(self):
        """
    Handles a variety of 'p' lines.
    """

        test_values = {
            'reject 1-65535': MicroExitPolicy('reject 1-65535'),
            'accept 80,110,143,443': MicroExitPolicy('accept 80,110,143,443'),
        }

        for p_line, expected in test_values.items():
            entry = RouterStatusEntryV3.create({'p': p_line})
            self.assertEqual(expected, entry.exit_policy)

        # tries some invalid inputs
        test_values = (
            '',
            'blarg',
            'reject -50',
            'accept 80,',
        )

        for p_line in test_values:
            expect_invalid_attr(self, {'p': p_line}, 'exit_policy')
Exemple #11
0
    def test_with_multiple_spaces(self):
        rule = ExitPolicyRule('accept    *:80')
        self.assertEqual('accept *:80', str(rule))

        policy = MicroExitPolicy('accept      80,443')
        self.assertTrue(policy.can_exit_to('75.119.206.243', 80))