#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""A module with test cases for the YARA database method."""

import os

from grr_response_server.databases import db
from grr_response_server.rdfvalues import objects as rdf_objects


class DatabaseTestYaraMixin(object):
  """A mixin class for testing YARA methods of database implementations."""

  def testWriteYaraSignatureReferenceIncorrectUsername(self):
    blob_id = rdf_objects.BlobID(os.urandom(32))

    with self.assertRaises(db.UnknownGRRUserError) as context:
      self.db.WriteYaraSignatureReference(blob_id=blob_id, username="quux")

    self.assertEqual(context.exception.username, "quux")

  def testWriteYaraSignatureReferenceDuplicated(self):
    self.db.WriteGRRUser("foo")

    blob_id = rdf_objects.BlobID(os.urandom(32))

    # Writing duplicated signatures is possible, it should not raise.
    self.db.WriteYaraSignatureReference(blob_id=blob_id, username="foo")
    self.db.WriteYaraSignatureReference(blob_id=blob_id, username="foo")

  def testVerifyYaraSignatureReferenceSimple(self):
    self.db.WriteGRRUser("foo")

    blob_id = rdf_objects.BlobID(os.urandom(32))
    self.db.WriteYaraSignatureReference(blob_id=blob_id, username="foo")

    self.assertTrue(self.db.VerifyYaraSignatureReference(blob_id))

  def testVerifyYaraSignatureReferenceIncorrect(self):
    blob_id = rdf_objects.BlobID(os.urandom(32))

    self.assertFalse(self.db.VerifyYaraSignatureReference(blob_id))

[((15, 33, 15, 47), 'os.urandom', 'os.urandom', ({(15, 44, 15, 46): '32'}, {}), '(32)', False, 'import os\n'), ((25, 33, 25, 47), 'os.urandom', 'os.urandom', ({(25, 44, 25, 46): '32'}, {}), '(32)', False, 'import os\n'), ((34, 33, 34, 47), 'os.urandom', 'os.urandom', ({(34, 44, 34, 46): '32'}, {}), '(32)', False, 'import os\n'), ((40, 33, 40, 47), 'os.urandom', 'os.urandom', ({(40, 44, 40, 46): '32'}, {}), '(32)', False, 'import os\n')]