File: pyqcow_test_file.py

package info (click to toggle)
libqcow 20240308-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,228 kB
  • sloc: ansic: 169,834; sh: 6,451; makefile: 1,161; python: 573; cpp: 88; sed: 16
file content (533 lines) | stat: -rw-r--r-- 14,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env python
#
# Python-bindings file type test script
#
# Copyright (C) 2010-2024, Joachim Metz <joachim.metz@gmail.com>
#
# Refer to AUTHORS for acknowledgements.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import argparse
import os
import random
import sys
import unittest

import pyqcow


class FileTypeTests(unittest.TestCase):
  """Tests the file type."""

  def test_signal_abort(self):
    """Tests the signal_abort function."""
    qcow_file = pyqcow.file()

    qcow_file.signal_abort()

  def test_open(self):
    """Tests the open function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    with self.assertRaises(IOError):
      qcow_file.open(test_source)

    qcow_file.close()

    with self.assertRaises(TypeError):
      qcow_file.open(None)

    with self.assertRaises(ValueError):
      qcow_file.open(test_source, mode="w")

  def test_open_file_object(self):
    """Tests the open_file_object function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    if not os.path.isfile(test_source):
      raise unittest.SkipTest("source not a regular file")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    with open(test_source, "rb") as file_object:

      qcow_file.open_file_object(file_object)

      with self.assertRaises(IOError):
        qcow_file.open_file_object(file_object)

      qcow_file.close()

      with self.assertRaises(TypeError):
        qcow_file.open_file_object(None)

      with self.assertRaises(ValueError):
        qcow_file.open_file_object(file_object, mode="w")

  def test_close(self):
    """Tests the close function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    with self.assertRaises(IOError):
      qcow_file.close()

  def test_open_close(self):
    """Tests the open and close functions."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      return

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    # Test open and close.
    qcow_file.open(test_source)
    qcow_file.close()

    # Test open and close a second time to validate clean up on close.
    qcow_file.open(test_source)
    qcow_file.close()

    if os.path.isfile(test_source):
      with open(test_source, "rb") as file_object:

        # Test open_file_object and close.
        qcow_file.open_file_object(file_object)
        qcow_file.close()

        # Test open_file_object and close a second time to validate clean up on close.
        qcow_file.open_file_object(file_object)
        qcow_file.close()

        # Test open_file_object and close and dereferencing file_object.
        qcow_file.open_file_object(file_object)
        del file_object
        qcow_file.close()

  def test_is_locked(self):
    """Tests the is_locked function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    qcow_file.open(test_source)

    _ = qcow_file.is_locked()

    qcow_file.close()

  def test_read_buffer(self):
    """Tests the read_buffer function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    qcow_parent_file = None
    if qcow_file.backing_filename:
      qcow_parent_file = pyqcow.file()

      parent_filename = os.path.join(
        os.path.dirname(test_source), qcow_file.backing_filename)
      qcow_parent_file.open(parent_filename, "r")

      qcow_file.set_parent(qcow_parent_file)

    media_size = qcow_file.get_media_size()

    if media_size < 4096:
      # Test read without maximum size.
      qcow_file.seek_offset(0, os.SEEK_SET)

      data = qcow_file.read_buffer()

      self.assertIsNotNone(data)
      self.assertEqual(len(data), media_size)

    # Test read with maximum size.
    qcow_file.seek_offset(0, os.SEEK_SET)

    data = qcow_file.read_buffer(size=4096)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(media_size, 4096))

    if media_size > 8:
      qcow_file.seek_offset(-8, os.SEEK_END)

      # Read buffer on media_size boundary.
      data = qcow_file.read_buffer(size=4096)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 8)

      # Read buffer beyond media_size boundary.
      data = qcow_file.read_buffer(size=4096)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 0)

    # Stress test read buffer.
    qcow_file.seek_offset(0, os.SEEK_SET)

    remaining_media_size = media_size

    for _ in range(1024):
      read_size = int(random.random() * 4096)

      data = qcow_file.read_buffer(size=read_size)

      self.assertIsNotNone(data)

      data_size = len(data)

      if read_size > remaining_media_size:
        read_size = remaining_media_size

      self.assertEqual(data_size, read_size)

      remaining_media_size -= data_size

      if not remaining_media_size:
        qcow_file.seek_offset(0, os.SEEK_SET)

        remaining_media_size = media_size

    with self.assertRaises(ValueError):
      qcow_file.read_buffer(size=-1)

    qcow_file.close()

    if qcow_parent_file:
      qcow_parent_file.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      qcow_file.read_buffer(size=4096)

  def test_read_buffer_file_object(self):
    """Tests the read_buffer function on a file-like object."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    if not os.path.isfile(test_source):
      raise unittest.SkipTest("source not a regular file")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    with open(test_source, "rb") as file_object:
      qcow_file.open_file_object(file_object)

      qcow_parent_file = None
      if qcow_file.backing_filename:
        qcow_parent_file = pyqcow.file()

        parent_filename = os.path.join(
          os.path.dirname(test_source), qcow_file.backing_filename)
        qcow_parent_file.open(parent_filename, "r")

        qcow_file.set_parent(qcow_parent_file)

      media_size = qcow_file.get_media_size()

      # Test normal read.
      data = qcow_file.read_buffer(size=4096)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), min(media_size, 4096))

      qcow_file.close()

      if qcow_parent_file:
        qcow_parent_file.close()

  def test_read_buffer_at_offset(self):
    """Tests the read_buffer_at_offset function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    qcow_parent_file = None
    if qcow_file.backing_filename:
      qcow_parent_file = pyqcow.file()

      parent_filename = os.path.join(
        os.path.dirname(test_source), qcow_file.backing_filename)
      qcow_parent_file.open(parent_filename, "r")

      qcow_file.set_parent(qcow_parent_file)

    media_size = qcow_file.get_media_size()

    # Test normal read.
    data = qcow_file.read_buffer_at_offset(4096, 0)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(media_size, 4096))

    if media_size > 8:
      # Read buffer on media_size boundary.
      data = qcow_file.read_buffer_at_offset(4096, media_size - 8)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 8)

      # Read buffer beyond media_size boundary.
      data = qcow_file.read_buffer_at_offset(4096, media_size + 8)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 0)

    # Stress test read buffer.
    for _ in range(1024):
      random_number = random.random()

      media_offset = int(random_number * media_size)
      read_size = int(random_number * 4096)

      data = qcow_file.read_buffer_at_offset(read_size, media_offset)

      self.assertIsNotNone(data)

      remaining_media_size = media_size - media_offset

      data_size = len(data)

      if read_size > remaining_media_size:
        read_size = remaining_media_size

      self.assertEqual(data_size, read_size)

      remaining_media_size -= data_size

      if not remaining_media_size:
        qcow_file.seek_offset(0, os.SEEK_SET)

    with self.assertRaises(ValueError):
      qcow_file.read_buffer_at_offset(-1, 0)

    with self.assertRaises(ValueError):
      qcow_file.read_buffer_at_offset(4096, -1)

    qcow_file.close()

    if qcow_parent_file:
      qcow_parent_file.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      qcow_file.read_buffer_at_offset(4096, 0)

  def test_seek_offset(self):
    """Tests the seek_offset function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    qcow_parent_file = None
    if qcow_file.backing_filename:
      qcow_parent_file = pyqcow.file()

      parent_filename = os.path.join(
        os.path.dirname(test_source), qcow_file.backing_filename)
      qcow_parent_file.open(parent_filename, "r")

      qcow_file.set_parent(qcow_parent_file)

    media_size = qcow_file.get_media_size()

    qcow_file.seek_offset(16, os.SEEK_SET)

    offset = qcow_file.get_offset()
    self.assertEqual(offset, 16)

    qcow_file.seek_offset(16, os.SEEK_CUR)

    offset = qcow_file.get_offset()
    self.assertEqual(offset, 32)

    qcow_file.seek_offset(-16, os.SEEK_CUR)

    offset = qcow_file.get_offset()
    self.assertEqual(offset, 16)

    if media_size > 16:
      qcow_file.seek_offset(-16, os.SEEK_END)

      offset = qcow_file.get_offset()
      self.assertEqual(offset, media_size - 16)

    qcow_file.seek_offset(16, os.SEEK_END)

    offset = qcow_file.get_offset()
    self.assertEqual(offset, media_size + 16)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      qcow_file.seek_offset(-1, os.SEEK_SET)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      qcow_file.seek_offset(-32 - media_size, os.SEEK_CUR)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      qcow_file.seek_offset(-32 - media_size, os.SEEK_END)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      qcow_file.seek_offset(0, -1)

    qcow_file.close()

    if qcow_parent_file:
      qcow_parent_file.close()

    # Test the seek without open.
    with self.assertRaises(IOError):
      qcow_file.seek_offset(16, os.SEEK_SET)

  def test_get_offset(self):
    """Tests the get_offset function."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    offset = qcow_file.get_offset()
    self.assertIsNotNone(offset)

    qcow_file.close()

  def test_get_media_size(self):
    """Tests the get_media_size function and media_size property."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    test_password = getattr(unittest, "password", None)
    if test_password:
      qcow_file.set_password(test_password)

    qcow_file.open(test_source)

    media_size = qcow_file.get_media_size()
    self.assertIsNotNone(media_size)

    self.assertIsNotNone(qcow_file.media_size)

    qcow_file.close()

  def test_get_backing_filename(self):
    """Tests the get_backing_filename function and backing_filename property."""
    test_source = getattr(unittest, "source", None)
    if not test_source:
      raise unittest.SkipTest("missing source")

    qcow_file = pyqcow.file()

    qcow_file.open(test_source)

    _ = qcow_file.get_backing_filename()

    _ = qcow_file.backing_filename

    qcow_file.close()


if __name__ == "__main__":
  argument_parser = argparse.ArgumentParser()

  argument_parser.add_argument(
      "-p", "--password", dest="password", action="store", default=None,
      type=str, help="password to unlock the source file.")

  argument_parser.add_argument(
      "source", nargs="?", action="store", metavar="PATH",
      default=None, help="path of the source file.")

  options, unknown_options = argument_parser.parse_known_args()
  unknown_options.insert(0, sys.argv[0])

  setattr(unittest, "password", options.password)
  setattr(unittest, "source", options.source)

  unittest.main(argv=unknown_options, verbosity=2)