#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Tests for the Google Chrome cookie database plugin."""

import unittest

from plaso.formatters import chrome_cookies  # pylint: disable=unused-import
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers.sqlite_plugins import chrome_cookies

from tests import test_lib as shared_test_lib
from tests.parsers.sqlite_plugins import test_lib


class ChromeCookiesPluginTest(test_lib.SQLitePluginTestCase):
  """Tests for the Google Chrome cookie database plugin."""

  @shared_test_lib.skipUnlessHasTestFile([u'cookies.db'])
  def testProcess(self):
    """Tests the Process function on a Chrome cookie database file."""
    plugin = chrome_cookies.ChromeCookiePlugin()
    storage_writer = self._ParseDatabaseFileWithPlugin(
        [u'cookies.db'], plugin)

    # Since we've got both events generated by cookie plugins and the Chrome
    # cookie plugin we need to separate them.
    events = []
    extra_objects = []

    for event in storage_writer.GetEvents():
      if event.data_type == u'chrome:cookie:entry':
        events.append(event)
      else:
        extra_objects.append(event)

    # The cookie database contains 560 entries:
    #     560 creation timestamps.
    #     560 last access timestamps.
    #     560 expired timestamps.
    # Then there are extra events created by plugins:
    #      75 events created by Google Analytics cookies.
    # In total: 1755 events.
    self.assertEqual(len(events), 3 * 560)
    self.assertEqual(len(extra_objects), 75)

    # Check few "random" events to verify.

    # Check one linkedin cookie.
    event = events[124]
    self.assertEqual(
        event.timestamp_desc, definitions.TIME_DESCRIPTION_LAST_ACCESS)
    self.assertEqual(event.host, u'www.linkedin.com')
    self.assertEqual(event.cookie_name, u'leo_auth_token')
    self.assertFalse(event.httponly)
    self.assertEqual(event.url, u'http://www.linkedin.com/')

    expected_timestamp = timelib.Timestamp.CopyFromString(
        u'2011-08-25 21:50:27.292367')
    self.assertEqual(event.timestamp, expected_timestamp)

    expected_message = (
        u'http://www.linkedin.com/ (leo_auth_token) Flags: [HTTP only] = False '
        u'[Persistent] = True')
    expected_short_message = u'www.linkedin.com (leo_auth_token)'
    self._TestGetMessageStrings(event, expected_message, expected_short_message)

    # Check one of the visits to rubiconproject.com.
    event = events[379]
    self.assertEqual(
        event.timestamp_desc, definitions.TIME_DESCRIPTION_LAST_ACCESS)

    expected_timestamp = timelib.Timestamp.CopyFromString(
        u'2012-04-01 13:54:34.949210')
    self.assertEqual(event.timestamp, expected_timestamp)

    self.assertEqual(event.url, u'http://rubiconproject.com/')
    self.assertEqual(event.path, u'/')
    self.assertFalse(event.secure)
    self.assertTrue(event.persistent)

    expected_message = (
        u'http://rubiconproject.com/ (put_2249) Flags: [HTTP only] = False '
        u'[Persistent] = True')
    self._TestGetMessageStrings(
        event, expected_message, u'rubiconproject.com (put_2249)')

    # Examine an event for a visit to a political blog site.
    event = events[444]
    self.assertEqual(
        event.path,
        u'/2012/03/21/romney-tries-to-clean-up-etch-a-sketch-mess/')
    self.assertEqual(event.host, u'politicalticker.blogs.cnn.com')

    expected_timestamp = timelib.Timestamp.CopyFromString(
        u'2012-03-22 01:47:21.012022')
    self.assertEqual(event.timestamp, expected_timestamp)

    # Examine a cookie that has an autologin entry.
    event = events[1425]

    expected_timestamp = timelib.Timestamp.CopyFromString(
        u'2012-04-01 13:52:56.189444')
    self.assertEqual(event.timestamp, expected_timestamp)

    self.assertEqual(event.host, u'marvel.com')
    self.assertEqual(event.cookie_name, u'autologin[timeout]')
    self.assertEqual(
        event.timestamp_desc, definitions.TIME_DESCRIPTION_CREATION)
    # This particular cookie value represents a timeout value that corresponds
    # to the expiration date of the cookie.
    self.assertEqual(event.data, u'1364824322')


if __name__ == '__main__':
  unittest.main()
