def test_trailing_line(self):
    """
    Includes a line after the 'router-signature' entry.
    """

    desc_text = get_hidden_service_descriptor(content = True) + b'\nhibernate 1'
    self._expect_invalid_attr(desc_text)
  def test_proceeding_line(self):
    """
    Includes a line prior to the 'rendezvous-service-descriptor' entry.
    """

    desc_text = b'hibernate 1\n' + get_hidden_service_descriptor(content = True)
    self._expect_invalid_attr(desc_text)
  def test_unrecognized_line(self):
    """
    Includes unrecognized content in the descriptor.
    """

    desc = get_hidden_service_descriptor({'pepperjack': 'is oh so tasty!'})
    self.assertEqual(['pepperjack is oh so tasty!'], desc.get_unrecognized_lines())
  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 = get_hidden_service_descriptor(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 = get_hidden_service_descriptor({'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_invalid_version(self):
    """
    Checks that our version field expects a numeric value.
    """

    test_values = (
      '',
      '-10',
      'hello',
    )

    for test_value in test_values:
      desc_text = get_hidden_service_descriptor({'version': test_value}, content = True)
      self._expect_invalid_attr(desc_text, 'version')
  def test_introduction_points_when_not_base64(self):
    """
    Checks the introduction-points field when the content isn't base64 encoded.
    """

    test_values = (
      MESSAGE_BLOCK % '12345',
      MESSAGE_BLOCK % 'hello',
    )

    for test_value in test_values:
      desc_text = get_hidden_service_descriptor({'introduction-points': test_value}, content = True)

      desc = self._expect_invalid_attr(desc_text, 'introduction_points_encoded', test_value.strip())
      self.assertEqual([], desc.introduction_points_auth)
      self.assertEqual(None, 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 = get_hidden_service_descriptor()

    self.assertEqual('y3olqqblqw2gbh6phimfuiroechjjafa', desc.descriptor_id)
    self.assertEqual(2, desc.version)
    self.assertTrue(CRYPTO_BLOB in desc.permanent_key)
    self.assertEqual('e24kgecavwsznj7gpbktqsiwgvngsf4e', desc.secret_id_part)
    self.assertEqual(datetime.datetime(2015, 2, 23, 20, 0, 0), desc.published)
    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.assertTrue(CRYPTO_BLOB in desc.signature)
    self.assertEqual([], desc.introduction_points())
  def test_invalid_protocol_versions(self):
    """
    Checks that our protocol-versions field expects comma separated numeric
    values.
    """

    test_values = (
      '',
      '-10',
      'hello',
      '10,',
      ',10',
      '10,-10',
      '10,hello',
    )

    for test_value in test_values:
      desc_text = get_hidden_service_descriptor({'protocol-versions': test_value}, content = True)
      self._expect_invalid_attr(desc_text, 'protocol_versions', [])
  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 = get_hidden_service_descriptor(content = True, exclude = (line,))

      expected = [] if line == 'protocol-versions' else None
      self._expect_invalid_attr(desc_text, line_to_attr[line], expected)