Debian Codegraph @

tests/pyvhdi_test_support.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # Python-bindings support functions test script
4 #
5 # Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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
22 import argparse
23 import os
24 import sys
25 import unittest
26
27 import pyvhdi
28
29
30 class SupportFunctionsTests(unittest.TestCase):
31 """Tests the support functions."""
32
33 def test_get_version(self):
34 """Tests the get_version function."""
35 version = pyvhdi.get_version()
36 self.assertIsNotNone(version)
37
38 def test_check_file_signature(self):
39 """Tests the check_file_signature function."""
40 test_source = getattr(unittest, "source", None)
41 if not test_source:
42 raise unittest.SkipTest("missing source")
43
44 result = pyvhdi.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 = getattr(unittest, "source", None)
50 if not test_source:
51 raise unittest.SkipTest("missing source")
52
53 if not os.path.isfile(test_source):
54 raise unittest.SkipTest("source not a regular file")
55
56 with open(test_source, "rb") as file_object:
57 result = pyvhdi.check_file_signature_file_object(file_object)
58 self.assertTrue(result)
59
60 def test_open(self):
61 """Tests the open function."""
62 test_source = getattr(unittest, "source", None)
63 if not test_source:
64 raise unittest.SkipTest("missing source")
65
66 vhdi_file = pyvhdi.open(test_source)
67 self.assertIsNotNone(vhdi_file)
68
69 vhdi_file.close()
70
71 with self.assertRaises(TypeError):
72 pyvhdi.open(None)
73
74 with self.assertRaises(ValueError):
75 pyvhdi.open(test_source, mode="w")
76
77 def test_open_file_object(self):
78 """Tests the open_file_object function."""
79 test_source = getattr(unittest, "source", None)
80 if not test_source:
81 raise unittest.SkipTest("missing source")
82
83 if not os.path.isfile(test_source):
84 raise unittest.SkipTest("source not a regular file")
85
86 with open(test_source, "rb") as file_object:
87 vhdi_file = pyvhdi.open_file_object(file_object)
88 self.assertIsNotNone(vhdi_file)
89
90 vhdi_file.close()
91
92 with self.assertRaises(TypeError):
93 pyvhdi.open_file_object(None)
94
95 with self.assertRaises(ValueError):
96 pyvhdi.open_file_object(file_object, mode="w")
97
98
99 if __name__ == "__main__":
100 argument_parser = argparse.ArgumentParser()
101
102 argument_parser.add_argument(
103 "source", nargs="?", action="store", metavar="PATH",
104 default=None, help="path of the source file.")
105
106 options, unknown_options = argument_parser.parse_known_args()
107 unknown_options.insert(0, sys.argv[0])
108
109 setattr(unittest, "source", options.source)
110
111 unittest.main(argv=unknown_options, verbosity=2)