1#!/usr/bin/env python
2#
3# Python-bindings support functions test script
4#
5# Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com>
6#
7# Refer to AUTHORS for acknowledgements.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU Lesser General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22import argparse
23import os
24import sys
25import unittest
26
27import pyevtx
28
29
30class SupportFunctionsTests(unittest.TestCase):
31  """Tests the support functions."""
32
33  def test_get_version(self):
34    """Tests the get_version function."""
35    version = pyevtx.get_version()
36    self.assertIsNotNone(version)
37
38  def test_check_file_signature(self):
39    """Tests the check_file_signature function."""
40    test_source = unittest.source
41    if not test_source:
42      raise unittest.SkipTest("missing source")
43
44    result = pyevtx.check_file_signature(test_source)
45    self.assertTrue(result)
46
47  def test_check_file_signature_file_object(self):
48    """Tests the check_file_signature_file_object function."""
49    test_source = unittest.source
50    if not test_source:
51      raise unittest.SkipTest("missing source")
52
53    with open(test_source, "rb") as file_object:
54      result = pyevtx.check_file_signature_file_object(file_object)
55      self.assertTrue(result)
56
57  def test_open(self):
58    """Tests the open function."""
59    test_source = unittest.source
60    if not test_source:
61      raise unittest.SkipTest("missing source")
62
63    evtx_file = pyevtx.open(test_source)
64    self.assertIsNotNone(evtx_file)
65
66    evtx_file.close()
67
68    with self.assertRaises(TypeError):
69      pyevtx.open(None)
70
71    with self.assertRaises(ValueError):
72      pyevtx.open(test_source, mode="w")
73
74  def test_open_file_object(self):
75    """Tests the open_file_object function."""
76    test_source = unittest.source
77    if not test_source:
78      raise unittest.SkipTest("missing source")
79
80    if not os.path.isfile(test_source):
81      raise unittest.SkipTest("source not a regular file")
82
83    with open(test_source, "rb") as file_object:
84      evtx_file = pyevtx.open_file_object(file_object)
85      self.assertIsNotNone(evtx_file)
86
87      evtx_file.close()
88
89      with self.assertRaises(TypeError):
90        pyevtx.open_file_object(None)
91
92      with self.assertRaises(ValueError):
93        pyevtx.open_file_object(file_object, mode="w")
94
95
96if __name__ == "__main__":
97  argument_parser = argparse.ArgumentParser()
98
99  argument_parser.add_argument(
100      "source", nargs="?", action="store", metavar="PATH",
101      default=None, help="path of the source file.")
102
103  options, unknown_options = argument_parser.parse_known_args()
104  unknown_options.insert(0, sys.argv[0])
105
106  setattr(unittest, "source", options.source)
107
108  unittest.main(argv=unknown_options, verbosity=2)
109