<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the Windows Shortcut (LNK) parser."""
import os
import unittest

from plaso.formatters import winlnk
from plaso.lib import eventdata
from plaso.lib import preprocess
from plaso.parsers import winlnk


class WinLnkParserTest(unittest.TestCase):
  """Tests for the Windows Shortcut (LNK) parser."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    pre_obj = preprocess.PlasoPreprocess()
    self.test_parser = winlnk.WinLnkParser(pre_obj)
    self.maxDiff = None

  def testParseFile(self):
    """Read a LNK file and run a few tests."""
    test_file = os.path.join('test_data', 'example.lnk')

    events = None
    with open(test_file, 'rb') as file_object:
      events = list(self.test_parser.Parse(file_object))

    # Link information:
    # 	Creation time			: Jul 13, 2009 23:29:02.849131000 UTC
    # 	Modification time		: Jul 14, 2009 01:39:18.220000000 UTC
    # 	Access time			: Jul 13, 2009 23:29:02.849131000 UTC
    # 	Description			: @%windir%\system32\migwiz\wet.dll,-590
    # 	Relative path			: .\migwiz\migwiz.exe
    # 	Working directory		: %windir%\system32\migwiz
    # 	Icon location			: %windir%\system32\migwiz\migwiz.exe
    # 	Environment variables location	: %windir%\system32\migwiz\migwiz.exe

    self.assertEquals(len(events), 3)

    # The Last Accessed timestamp.
    event_object = events[0]

    expected_string = u'@%windir%\system32\migwiz\wet.dll,-590'
    self.assertEquals(event_object.description, expected_string)

    expected_string = u'.\migwiz\migwiz.exe'
    self.assertEquals(event_object.relative_path, expected_string)

    expected_string = u'%windir%\system32\migwiz'
    self.assertEquals(event_object.working_directory, expected_string)

    expected_string = u'%windir%\system32\migwiz\migwiz.exe'
    self.assertEquals(event_object.icon_location, expected_string)
    self.assertEquals(event_object.env_var_location, expected_string)

    # date -u -d"Jul 13, 2009 23:29:02.849131000" +"%s.%N"
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.ACCESS_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247527742 * 1000000) + int(849131000 / 1000))

    # date -u -d"Jul 13, 2009 23:29:02.849131000" +"%s.%N"
    event_object = events[1]
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.CREATION_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247527742 * 1000000) + int(849131000 / 1000))

    # date -u -d"Jul 14, 2009 01:39:18.220000000" +"%s.%N"
    event_object = events[2]
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.MODIFICATION_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247535558 * 1000000) + int(220000000 / 1000))

    # Test the event specific formatter.
    msg, msg_short = eventdata.EventFormatterManager.GetMessageStrings(
         event_object)

    expected_msg = (u'[@%windir%\\system32\\migwiz\\wet.dll,-590] '<commit_msg>Code review: 7599043: .<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the Windows Shortcut (LNK) parser."""
import os
import unittest

from plaso.formatters import winlnk
from plaso.lib import eventdata
from plaso.lib import preprocess
from plaso.parsers import winlnk


class WinLnkParserTest(unittest.TestCase):
  """Tests for the Windows Shortcut (LNK) parser."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    pre_obj = preprocess.PlasoPreprocess()
    self.test_parser = winlnk.WinLnkParser(pre_obj)
    self.maxDiff = None

  def testParseFile(self):
    """Read a LNK file and run a few tests."""
    test_file = os.path.join('test_data', 'example.lnk')

    events = None
    with open(test_file, 'rb') as file_object:
      events = list(self.test_parser.Parse(file_object))

    # Link information:
    # 	Creation time			: Jul 13, 2009 23:29:02.849131000 UTC
    # 	Modification time		: Jul 14, 2009 01:39:18.220000000 UTC
    # 	Access time			: Jul 13, 2009 23:29:02.849131000 UTC
    # 	Description			: @%windir%\system32\migwiz\wet.dll,-590
    # 	Relative path			: .\migwiz\migwiz.exe
    # 	Working directory		: %windir%\system32\migwiz
    # 	Icon location			: %windir%\system32\migwiz\migwiz.exe
    # 	Environment variables location	: %windir%\system32\migwiz\migwiz.exe

    self.assertEquals(len(events), 3)

    # The Last Accessed timestamp.
    event_object = events[0]

    expected_string = u'@%windir%\\system32\\migwiz\\wet.dll,-590'
    self.assertEquals(event_object.description, expected_string)

    expected_string = u'.\\migwiz\\migwiz.exe'
    self.assertEquals(event_object.relative_path, expected_string)

    expected_string = u'%windir%\\system32\\migwiz'
    self.assertEquals(event_object.working_directory, expected_string)

    expected_string = u'%windir%\\system32\\migwiz\\migwiz.exe'
    self.assertEquals(event_object.icon_location, expected_string)
    self.assertEquals(event_object.env_var_location, expected_string)

    # date -u -d"Jul 13, 2009 23:29:02.849131000" +"%s.%N"
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.ACCESS_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247527742 * 1000000) + int(849131000 / 1000))

    # date -u -d"Jul 13, 2009 23:29:02.849131000" +"%s.%N"
    event_object = events[1]
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.CREATION_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247527742 * 1000000) + int(849131000 / 1000))

    # date -u -d"Jul 14, 2009 01:39:18.220000000" +"%s.%N"
    event_object = events[2]
    self.assertEquals(event_object.timestamp_desc,
                      eventdata.EventTimestamp.MODIFICATION_TIME)
    self.assertEquals(event_object.timestamp,
                      (1247535558 * 1000000) + int(220000000 / 1000))

    # Test the event specific formatter.
    msg, msg_short = eventdata.EventFormatterManager.GetMessageStrings(
         event_object)

    expected_msg = (u'[@%windir%\\system32\\migwiz\\wet.dll,-590] '
{'fraction_non_alphanumeric': 0.10902054977315186, 'fraction_numerical': 0.06872164398185215, 'mean_word_length': 3.3907439953134153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6575421', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2375, 'n_words': 573}