Debian Codegraph @

tests/pyvhdi_test_file.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # Python-bindings file type 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 random
25 import sys
26 import unittest
27
28 import pyvhdi
29
30
31 class FileTypeTests(unittest.TestCase):
32 """Tests the file type."""
33
34 def test_signal_abort(self):
35 """Tests the signal_abort function."""
36 vhdi_file = pyvhdi.file()
37
38 vhdi_file.signal_abort()
39
40 def test_open(self):
41 """Tests the open function."""
42 test_source = getattr(unittest, "source", None)
43 if not test_source:
44 raise unittest.SkipTest("missing source")
45
46 vhdi_file = pyvhdi.file()
47
48 vhdi_file.open(test_source)
49
50 with self.assertRaises(IOError):
51 vhdi_file.open(test_source)
52
53 vhdi_file.close()
54
55 with self.assertRaises(TypeError):
56 vhdi_file.open(None)
57
58 with self.assertRaises(ValueError):
59 vhdi_file.open(test_source, mode="w")
60
61 def test_open_file_object(self):
62 """Tests the open_file_object function."""
63 test_source = getattr(unittest, "source", None)
64 if not test_source:
65 raise unittest.SkipTest("missing source")
66
67 if not os.path.isfile(test_source):
68 raise unittest.SkipTest("source not a regular file")
69
70 vhdi_file = pyvhdi.file()
71
72 with open(test_source, "rb") as file_object:
73
74 vhdi_file.open_file_object(file_object)
75
76 with self.assertRaises(IOError):
77 vhdi_file.open_file_object(file_object)
78
79 vhdi_file.close()
80
81 with self.assertRaises(TypeError):
82 vhdi_file.open_file_object(None)
83
84 with self.assertRaises(ValueError):
85 vhdi_file.open_file_object(file_object, mode="w")
86
87 def test_close(self):
88 """Tests the close function."""
89 test_source = getattr(unittest, "source", None)
90 if not test_source:
91 raise unittest.SkipTest("missing source")
92
93 vhdi_file = pyvhdi.file()
94
95 with self.assertRaises(IOError):
96 vhdi_file.close()
97
98 def test_open_close(self):
99 """Tests the open and close functions."""
100 test_source = getattr(unittest, "source", None)
101 if not test_source:
102 return
103
104 vhdi_file = pyvhdi.file()
105
106 # Test open and close.
107 vhdi_file.open(test_source)
108 vhdi_file.close()
109
110 # Test open and close a second time to validate clean up on close.
111 vhdi_file.open(test_source)
112 vhdi_file.close()
113
114 if os.path.isfile(test_source):
115 with open(test_source, "rb") as file_object:
116
117 # Test open_file_object and close.
118 vhdi_file.open_file_object(file_object)
119 vhdi_file.close()
120
121 # Test open_file_object and close a second time to validate clean up on close.
122 vhdi_file.open_file_object(file_object)
123 vhdi_file.close()
124
125 # Test open_file_object and close and dereferencing file_object.
126 vhdi_file.open_file_object(file_object)
127 del file_object
128 vhdi_file.close()
129
130 def test_read_buffer(self):
131 """Tests the read_buffer function."""
132 test_source = getattr(unittest, "source", None)
133 if not test_source:
134 raise unittest.SkipTest("missing source")
135
136 vhdi_file = pyvhdi.file()
137
138 vhdi_file.open(test_source)
139
140 vhdi_parent_file = None
141 if vhdi_file.parent_identifier:
142 vhdi_parent_file = pyvhdi.file()
143
144 _, _, parent_filename = vhdi_file.parent_filename.rpartition('\\')
145 parent_filename = os.path.join(
146 os.path.dirname(test_source), parent_filename)
147 vhdi_parent_file.open(parent_filename, "r")
148
149 vhdi_file.set_parent(vhdi_parent_file)
150
151 media_size = vhdi_file.get_media_size()
152
153 if media_size < 4096:
154 # Test read without maximum size.
155 vhdi_file.seek_offset(0, os.SEEK_SET)
156
157 data = vhdi_file.read_buffer()
158
159 self.assertIsNotNone(data)
160 self.assertEqual(len(data), media_size)
161
162 # Test read with maximum size.
163 vhdi_file.seek_offset(0, os.SEEK_SET)
164
165 data = vhdi_file.read_buffer(size=4096)
166
167 self.assertIsNotNone(data)
168 self.assertEqual(len(data), min(media_size, 4096))
169
170 if media_size > 8:
171 vhdi_file.seek_offset(-8, os.SEEK_END)
172
173 # Read buffer on media_size boundary.
174 data = vhdi_file.read_buffer(size=4096)
175
176 self.assertIsNotNone(data)
177 self.assertEqual(len(data), 8)
178
179 # Read buffer beyond media_size boundary.
180 data = vhdi_file.read_buffer(size=4096)
181
182 self.assertIsNotNone(data)
183 self.assertEqual(len(data), 0)
184
185 # Stress test read buffer.
186 vhdi_file.seek_offset(0, os.SEEK_SET)
187
188 remaining_media_size = media_size
189
190 for _ in range(1024):
191 read_size = int(random.random() * 4096)
192
193 data = vhdi_file.read_buffer(size=read_size)
194
195 self.assertIsNotNone(data)
196
197 data_size = len(data)
198
199 if read_size > remaining_media_size:
200 read_size = remaining_media_size
201
202 self.assertEqual(data_size, read_size)
203
204 remaining_media_size -= data_size
205
206 if not remaining_media_size:
207 vhdi_file.seek_offset(0, os.SEEK_SET)
208
209 remaining_media_size = media_size
210
211 with self.assertRaises(ValueError):
212 vhdi_file.read_buffer(size=-1)
213
214 vhdi_file.close()
215
216 if vhdi_parent_file:
217 vhdi_parent_file.close()
218
219 # Test the read without open.
220 with self.assertRaises(IOError):
221 vhdi_file.read_buffer(size=4096)
222
223 def test_read_buffer_file_object(self):
224 """Tests the read_buffer function on a file-like object."""
225 test_source = getattr(unittest, "source", None)
226 if not test_source:
227 raise unittest.SkipTest("missing source")
228
229 if not os.path.isfile(test_source):
230 raise unittest.SkipTest("source not a regular file")
231
232 with open(test_source, "rb") as file_object:
233 vhdi_file = pyvhdi.file()
234
235 vhdi_file.open_file_object(file_object)
236
237 vhdi_parent_file = None
238 if vhdi_file.parent_identifier:
239 vhdi_parent_file = pyvhdi.file()
240
241 _, _, parent_filename = vhdi_file.parent_filename.rpartition('\\')
242 parent_filename = os.path.join(
243 os.path.dirname(test_source), parent_filename)
244 vhdi_parent_file.open(parent_filename, "r")
245
246 vhdi_file.set_parent(vhdi_parent_file)
247
248 media_size = vhdi_file.get_media_size()
249
250 # Test normal read.
251 data = vhdi_file.read_buffer(size=4096)
252
253 self.assertIsNotNone(data)
254 self.assertEqual(len(data), min(media_size, 4096))
255
256 vhdi_file.close()
257
258 if vhdi_parent_file:
259 vhdi_parent_file.close()
260
261 def test_read_buffer_at_offset(self):
262 """Tests the read_buffer_at_offset function."""
263 test_source = getattr(unittest, "source", None)
264 if not test_source:
265 raise unittest.SkipTest("missing source")
266
267 vhdi_file = pyvhdi.file()
268
269 vhdi_file.open(test_source)
270
271 vhdi_parent_file = None
272 if vhdi_file.parent_identifier:
273 vhdi_parent_file = pyvhdi.file()
274
275 _, _, parent_filename = vhdi_file.parent_filename.rpartition('\\')
276 parent_filename = os.path.join(
277 os.path.dirname(test_source), parent_filename)
278 vhdi_parent_file.open(parent_filename, "r")
279
280 vhdi_file.set_parent(vhdi_parent_file)
281
282 media_size = vhdi_file.get_media_size()
283
284 # Test normal read.
285 data = vhdi_file.read_buffer_at_offset(4096, 0)
286
287 self.assertIsNotNone(data)
288 self.assertEqual(len(data), min(media_size, 4096))
289
290 if media_size > 8:
291 # Read buffer on media_size boundary.
292 data = vhdi_file.read_buffer_at_offset(4096, media_size - 8)
293
294 self.assertIsNotNone(data)
295 self.assertEqual(len(data), 8)
296
297 # Read buffer beyond media_size boundary.
298 data = vhdi_file.read_buffer_at_offset(4096, media_size + 8)
299
300 self.assertIsNotNone(data)
301 self.assertEqual(len(data), 0)
302
303 # Stress test read buffer.
304 for _ in range(1024):
305 random_number = random.random()
306
307 media_offset = int(random_number * media_size)
308 read_size = int(random_number * 4096)
309
310 data = vhdi_file.read_buffer_at_offset(read_size, media_offset)
311
312 self.assertIsNotNone(data)
313
314 remaining_media_size = media_size - media_offset
315
316 data_size = len(data)
317
318 if read_size > remaining_media_size:
319 read_size = remaining_media_size
320
321 self.assertEqual(data_size, read_size)
322
323 remaining_media_size -= data_size
324
325 if not remaining_media_size:
326 vhdi_file.seek_offset(0, os.SEEK_SET)
327
328 with self.assertRaises(ValueError):
329 vhdi_file.read_buffer_at_offset(-1, 0)
330
331 with self.assertRaises(ValueError):
332 vhdi_file.read_buffer_at_offset(4096, -1)
333
334 vhdi_file.close()
335
336 if vhdi_parent_file:
337 vhdi_parent_file.close()
338
339 # Test the read without open.
340 with self.assertRaises(IOError):
341 vhdi_file.read_buffer_at_offset(4096, 0)
342
343 def test_seek_offset(self):
344 """Tests the seek_offset function."""
345 test_source = getattr(unittest, "source", None)
346 if not test_source:
347 raise unittest.SkipTest("missing source")
348
349 vhdi_file = pyvhdi.file()
350
351 vhdi_file.open(test_source)
352
353 vhdi_parent_file = None
354 if vhdi_file.parent_identifier:
355 vhdi_parent_file = pyvhdi.file()
356
357 _, _, parent_filename = vhdi_file.parent_filename.rpartition('\\')
358 parent_filename = os.path.join(
359 os.path.dirname(test_source), parent_filename)
360 vhdi_parent_file.open(parent_filename, "r")
361
362 vhdi_file.set_parent(vhdi_parent_file)
363
364 media_size = vhdi_file.get_media_size()
365
366 vhdi_file.seek_offset(16, os.SEEK_SET)
367
368 offset = vhdi_file.get_offset()
369 self.assertEqual(offset, 16)
370
371 vhdi_file.seek_offset(16, os.SEEK_CUR)
372
373 offset = vhdi_file.get_offset()
374 self.assertEqual(offset, 32)
375
376 vhdi_file.seek_offset(-16, os.SEEK_CUR)
377
378 offset = vhdi_file.get_offset()
379 self.assertEqual(offset, 16)
380
381 if media_size > 16:
382 vhdi_file.seek_offset(-16, os.SEEK_END)
383
384 offset = vhdi_file.get_offset()
385 self.assertEqual(offset, media_size - 16)
386
387 vhdi_file.seek_offset(16, os.SEEK_END)
388
389 offset = vhdi_file.get_offset()
390 self.assertEqual(offset, media_size + 16)
391
392 # TODO: change IOError into ValueError
393 with self.assertRaises(IOError):
394 vhdi_file.seek_offset(-1, os.SEEK_SET)
395
396 # TODO: change IOError into ValueError
397 with self.assertRaises(IOError):
398 vhdi_file.seek_offset(-32 - media_size, os.SEEK_CUR)
399
400 # TODO: change IOError into ValueError
401 with self.assertRaises(IOError):
402 vhdi_file.seek_offset(-32 - media_size, os.SEEK_END)
403
404 # TODO: change IOError into ValueError
405 with self.assertRaises(IOError):
406 vhdi_file.seek_offset(0, -1)
407
408 vhdi_file.close()
409
410 if vhdi_parent_file:
411 vhdi_parent_file.close()
412
413 # Test the seek without open.
414 with self.assertRaises(IOError):
415 vhdi_file.seek_offset(16, os.SEEK_SET)
416
417 def test_get_offset(self):
418 """Tests the get_offset function."""
419 test_source = getattr(unittest, "source", None)
420 if not test_source:
421 raise unittest.SkipTest("missing source")
422
423 vhdi_file = pyvhdi.file()
424
425 vhdi_file.open(test_source)
426
427 offset = vhdi_file.get_offset()
428 self.assertIsNotNone(offset)
429
430 vhdi_file.close()
431
432 def test_get_media_size(self):
433 """Tests the get_media_size function and media_size property."""
434 test_source = getattr(unittest, "source", None)
435 if not test_source:
436 raise unittest.SkipTest("missing source")
437
438 vhdi_file = pyvhdi.file()
439
440 vhdi_file.open(test_source)
441
442 media_size = vhdi_file.get_media_size()
443 self.assertIsNotNone(media_size)
444
445 self.assertIsNotNone(vhdi_file.media_size)
446
447 vhdi_file.close()
448
449 def test_get_disk_type(self):
450 """Tests the get_disk_type function and disk_type property."""
451 test_source = getattr(unittest, "source", None)
452 if not test_source:
453 raise unittest.SkipTest("missing source")
454
455 vhdi_file = pyvhdi.file()
456
457 vhdi_file.open(test_source)
458
459 disk_type = vhdi_file.get_disk_type()
460 self.assertIsNotNone(disk_type)
461
462 self.assertIsNotNone(vhdi_file.disk_type)
463
464 vhdi_file.close()
465
466 def test_get_parent_filename(self):
467 """Tests the get_parent_filename function and parent_filename property."""
468 test_source = getattr(unittest, "source", None)
469 if not test_source:
470 raise unittest.SkipTest("missing source")
471
472 vhdi_file = pyvhdi.file()
473
474 vhdi_file.open(test_source)
475
476 _ = vhdi_file.get_parent_filename()
477
478 _ = vhdi_file.parent_filename
479
480 vhdi_file.close()
481
482
483 if __name__ == "__main__":
484 argument_parser = argparse.ArgumentParser()
485
486 argument_parser.add_argument(
487 "source", nargs="?", action="store", metavar="PATH",
488 default=None, help="path of the source file.")
489
490 options, unknown_options = argument_parser.parse_known_args()
491 unknown_options.insert(0, sys.argv[0])
492
493 setattr(unittest, "source", options.source)
494
495 unittest.main(argv=unknown_options, verbosity=2)