def test_unrecognized_line(self): """ Includes unrecognized content in the descriptor. """ desc = HiddenServiceDescriptor.create({'pepperjack': 'is oh so tasty!'}) self.assertEqual(['pepperjack is oh so tasty!'], desc.get_unrecognized_lines())
def test_unrecognized_line(self): """ Includes unrecognized content in the descriptor. """ desc = HiddenServiceDescriptor.create({'pepperjack': 'is oh so tasty!'}) self.assertEqual(['pepperjack is oh so tasty!'], desc.get_unrecognized_lines())
def test_proceeding_line(self): """ Includes a line prior to the 'rendezvous-service-descriptor' entry. """ expect_invalid_attr_for_text( self, b'hibernate 1\n' + HiddenServiceDescriptor.content())
def test_trailing_line(self): """ Includes a line after the 'router-signature' entry. """ expect_invalid_attr_for_text( self, HiddenServiceDescriptor.content() + b'\nhibernate 1')
def test_introduction_points_when_empty(self): """ It's valid to advertise zero introduciton points. I'm not clear if this would mean an empty protocol-versions field or that it's omitted but either are valid according to the spec. """ missing_field_desc = HiddenServiceDescriptor.create(exclude = ('introduction-points',)) self.assertEqual(None, missing_field_desc.introduction_points_encoded) self.assertEqual([], missing_field_desc.introduction_points_auth) self.assertEqual(None, missing_field_desc.introduction_points_content) self.assertEqual([], missing_field_desc.introduction_points()) empty_field_desc = HiddenServiceDescriptor.create({'introduction-points': MESSAGE_BLOCK % ''}) self.assertEqual((MESSAGE_BLOCK % '').strip(), empty_field_desc.introduction_points_encoded) self.assertEqual([], empty_field_desc.introduction_points_auth) self.assertEqual(b'', empty_field_desc.introduction_points_content) self.assertEqual([], empty_field_desc.introduction_points())
def test_introduction_points_when_empty(self): """ It's valid to advertise zero introduciton points. I'm not clear if this would mean an empty protocol-versions field or that it's omitted but either are valid according to the spec. """ missing_field_desc = HiddenServiceDescriptor.create(exclude = ('introduction-points',)) self.assertEqual(None, missing_field_desc.introduction_points_encoded) self.assertEqual([], missing_field_desc.introduction_points_auth) self.assertEqual(None, missing_field_desc.introduction_points_content) self.assertEqual([], missing_field_desc.introduction_points()) empty_field_desc = HiddenServiceDescriptor.create({'introduction-points': MESSAGE_BLOCK % ''}) self.assertEqual((MESSAGE_BLOCK % '').strip(), empty_field_desc.introduction_points_encoded) self.assertEqual([], empty_field_desc.introduction_points_auth) self.assertEqual(b'', empty_field_desc.introduction_points_content) self.assertEqual([], empty_field_desc.introduction_points())
def test_minimal_hidden_service_descriptor(self): """ Basic sanity check that we can parse a hidden service descriptor with minimal attributes. """ desc = HiddenServiceDescriptor.create() self.assertEqual('y3olqqblqw2gbh6phimfuiroechjjafa', desc.descriptor_id) self.assertEqual(2, desc.version) self.assertEqual('e24kgecavwsznj7gpbktqsiwgvngsf4e', desc.secret_id_part) self.assertEqual([2, 3], desc.protocol_versions) self.assertEqual('-----BEGIN MESSAGE-----\n-----END MESSAGE-----', desc.introduction_points_encoded) self.assertEqual([], desc.introduction_points_auth) self.assertEqual(b'', desc.introduction_points_content) self.assertEqual([], desc.introduction_points())
def test_minimal_hidden_service_descriptor(self): """ Basic sanity check that we can parse a hidden service descriptor with minimal attributes. """ desc = HiddenServiceDescriptor.create() self.assertEqual('y3olqqblqw2gbh6phimfuiroechjjafa', desc.descriptor_id) self.assertEqual(2, desc.version) self.assertEqual('e24kgecavwsznj7gpbktqsiwgvngsf4e', desc.secret_id_part) self.assertEqual([2, 3], desc.protocol_versions) self.assertEqual('-----BEGIN MESSAGE-----\n-----END MESSAGE-----', desc.introduction_points_encoded) self.assertEqual([], desc.introduction_points_auth) self.assertEqual(b'', desc.introduction_points_content) self.assertEqual([], desc.introduction_points()) self.assertEqual('@type hidden-service-descriptor 1.0', str(desc.type_annotation()))
def _expect_invalid_attr(self, desc_text, attr = None, expected_value = None): """ Asserts that construction will fail due to desc_text having a malformed attribute. If an attr is provided then we check that it matches an expected value when we're constructed without validation. """ self.assertRaises(ValueError, HiddenServiceDescriptor, desc_text, True) desc = HiddenServiceDescriptor(desc_text, validate = False) if attr: # check that the invalid attribute matches the expected value when # constructed without validation self.assertEqual(expected_value, getattr(desc, attr)) else: # check a default attribute self.assertEqual('y3olqqblqw2gbh6phimfuiroechjjafa', desc.descriptor_id) return desc
def test_required_fields(self): """ Check that we require the mandatory fields. """ line_to_attr = { 'rendezvous-service-descriptor': 'descriptor_id', 'version': 'version', 'permanent-key': 'permanent_key', 'secret-id-part': 'secret_id_part', 'publication-time': 'published', 'introduction-points': 'introduction_points_encoded', 'protocol-versions': 'protocol_versions', 'signature': 'signature', } for line in REQUIRED_FIELDS: desc_text = HiddenServiceDescriptor.content(exclude = (line,)) expected = [] if line == 'protocol-versions' else None expect_invalid_attr_for_text(self, desc_text, line_to_attr[line], expected)
def test_required_fields(self): """ Check that we require the mandatory fields. """ line_to_attr = { 'rendezvous-service-descriptor': 'descriptor_id', 'version': 'version', 'permanent-key': 'permanent_key', 'secret-id-part': 'secret_id_part', 'publication-time': 'published', 'introduction-points': 'introduction_points_encoded', 'protocol-versions': 'protocol_versions', 'signature': 'signature', } for line in REQUIRED_FIELDS: desc_text = HiddenServiceDescriptor.content(exclude = (line,)) expected = [] if line == 'protocol-versions' else None expect_invalid_attr_for_text(self, desc_text, line_to_attr[line], expected)
def test_from_str(self): sig = HiddenServiceDescriptor.create() self.assertEqual(sig, HiddenServiceDescriptor.from_str(str(sig)))
def test_trailing_line(self): """ Includes a line after the 'router-signature' entry. """ expect_invalid_attr_for_text(self, HiddenServiceDescriptor.content() + b'\nhibernate 1')
def test_proceeding_line(self): """ Includes a line prior to the 'rendezvous-service-descriptor' entry. """ expect_invalid_attr_for_text(self, b'hibernate 1\n' + HiddenServiceDescriptor.content())