File: test_headervd.py

package info (click to toggle)
python-pycdlib 1.12.0%2Bds1-4%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,044 kB
  • sloc: python: 35,639; makefile: 63
file content (393 lines) | stat: -rw-r--r-- 16,376 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
from __future__ import absolute_import

import pytest
import os
import sys
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

prefix = '.'
for i in range(0, 3):
    if os.path.isdir(os.path.join(prefix, 'pycdlib')):
        sys.path.insert(0, prefix)
        break
    else:
        prefix = '../' + prefix

import pycdlib.headervd

# PrimaryOrSupplementaryVolumeDescriptor
def test_pvd_parse_initialized_twice():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)
    pvd.parse(b'\x01CD001\x01' + b'\x00'*2041, 16)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.parse(b'\x01CD001\x01' + b'\x00'*2041, 16)
    assert(str(excinfo.value) == 'This Primary Volume Descriptor is already initialized')

def test_pvd_parse_invalid_vd_type():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        pvd.parse(b'\x00'*2048, 16)
    assert(str(excinfo.value) == 'Invalid volume descriptor')

def test_pvd_parse_invalid_identifier():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        pvd.parse(b'\x01CD002' + b'\x00'*2042, 16)
    assert(str(excinfo.value) == 'invalid CD isoIdentification')

def test_pvd_new_initialized_twice():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)
    pvd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 1, b'')

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 1, b'')
    assert(str(excinfo.value) == 'This Primary Volume Descriptor is already initialized')

def test_pvd_new_pvd_invalid_flags():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidInput) as excinfo:
        pvd.new(1, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 1, b'')
    assert(str(excinfo.value) == 'Non-zero flags not allowed for a PVD')

def test_pvd_new_pvd_invalid_escape_sequence():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidInput) as excinfo:
        pvd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 1, b'\x00')
    assert(str(excinfo.value) == 'Non-empty escape sequence not allowed for a PVD')

def test_pvd_new_pvd_invalid_version():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(1)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidInput) as excinfo:
        pvd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 2, b'')
    assert(str(excinfo.value) == 'Only version 1 supported for a PVD')

def test_pvd_new_svd_invalid_version():
    svd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidInput) as excinfo:
        svd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 3, b'')
    assert(str(excinfo.value) == 'Only version 1 and version 2 supported for a Supplementary Volume Descriptor')

def test_pvd_copy_initialized_twice():
    svd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)
    svd.new(0, b'', b'', 0, 0, 0, b'', b'', b'', b'', b'', b'', b'', 0.0, b'', False, 2, b'')

    svd2 = pycdlib.headervd.PrimaryOrSupplementaryVD(2)
    svd2.copy(svd)
    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        svd2.copy(svd)
    assert(str(excinfo.value) == 'This Volume Descriptor is already initialized')

def test_pvd_record_not_initialized():
    svd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        svd.record()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_track_rr_ce_entry_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.track_rr_ce_entry(0, 0, 0)
    assert(str(excinfo.value) == 'This Primary Volume Descriptor is not initialized')

def test_pvd_add_rr_ce_entry_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.add_rr_ce_entry(0)
    assert(str(excinfo.value) == 'This Primary Volume Descriptor is not initialized')

def test_pvd_clear_rr_ce_entries_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.clear_rr_ce_entries()
    assert(str(excinfo.value) == 'This Primary Volume Descriptor is not initialized')

def test_pvd_path_table_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.path_table_size()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_add_to_space_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.add_to_space_size(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_remove_from_space_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.remove_from_space_size(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_root_directory_record_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.root_directory_record()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_logical_block_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.logical_block_size()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_add_to_ptr_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.add_to_ptr_size(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_remove_from_ptr_size_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.remove_from_ptr_size(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_sequence_number_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.sequence_number()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_copy_sizes_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.copy_sizes(None)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_extent_location_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.extent_location()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_set_extent_location_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.set_extent_location(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

def test_pvd_is_pvd_not_initialized():
    pvd = pycdlib.headervd.PrimaryOrSupplementaryVD(2)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        pvd.is_pvd()
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

# FileOrTextIdentifer
def test_fot_parse_initialized_twice():
    fot = pycdlib.headervd.FileOrTextIdentifier()
    fot.parse(b'a'*128)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        fot.parse(b'a'*128)
    assert(str(excinfo.value) == 'This File or Text identifier is already initialized')

def test_fot_new_initialized_twice():
    fot = pycdlib.headervd.FileOrTextIdentifier()
    fot.new(b'a'*128)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        fot.new(b'a'*128)
    assert(str(excinfo.value) == 'This File or Text identifier is already initialized')

def test_fot_new_bad_length():
    fot = pycdlib.headervd.FileOrTextIdentifier()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidInput) as excinfo:
        fot.new(b'a'*127)
    assert(str(excinfo.value) == 'Length of text must be 128')

def test_fot_record_not_initialized():
    fot = pycdlib.headervd.FileOrTextIdentifier()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        fot.record()
    assert(str(excinfo.value) == 'This File or Text identifier is not initialized')

# Volume Descriptor Set Terminator
def test_vdst_parse_initialized_twice():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()
    vdst.parse(b'\xffCD001\x01' + b'\x00'*2041, 0)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        vdst.parse(b'\xffCD001\x01' + b'\x00'*2041, 0)
    assert(str(excinfo.value) == 'Volume Descriptor Set Terminator already initialized')

def test_vdst_parse_bad_descriptor_type():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        vdst.parse(b'\xfeCD001\x01' + b'\x00'*2041, 0)
    assert(str(excinfo.value) == 'Invalid VDST descriptor type')

def test_vdst_parse_bad_identifier():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        vdst.parse(b'\xffCD002\x01' + b'\x00'*2041, 0)
    assert(str(excinfo.value) == 'Invalid VDST identifier')

def test_vdst_new_initialized_twice():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()
    vdst.new()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        vdst.new()
    assert(str(excinfo.value) == 'Volume Descriptor Set Terminator already initialized')

def test_vdst_record_not_initialized():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        vdst.record()
    assert(str(excinfo.value) == 'Volume Descriptor Set Terminator not initialized')

def test_vdst_extent_location_not_initialized():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        vdst.extent_location()
    assert(str(excinfo.value) == 'Volume Descriptor Set Terminator not initialized')

def test_vdst_set_extent_location_not_initialized():
    vdst = pycdlib.headervd.VolumeDescriptorSetTerminator()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        vdst.set_extent_location(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

# Boot Record
def test_br_parse_initialized_twice():
    br = pycdlib.headervd.BootRecord()
    br.parse(b'\x00CD001\x01' + b'\x00'*32 + b'\x00'*32 + b'\x00'*1977, 0)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.parse(b'\x00CD001\x01' + b'\x00'*32 + b'\x00'*32 + b'\x00'*1977, 0)
    assert(str(excinfo.value) == 'Boot Record already initialized')

def test_br_parse_bad_descriptor_type():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        br.parse(b'\x01CD001\x01' + b'\x00'*32 + b'\x00'*32 + b'\x00'*1977, 0)
    assert(str(excinfo.value) == 'Invalid boot record descriptor type')

def test_br_parse_bad_identifier():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInvalidISO) as excinfo:
        br.parse(b'\x00CD002\x01' + b'\x00'*32 + b'\x00'*32 + b'\x00'*1977, 0)
    assert(str(excinfo.value) == 'Invalid boot record identifier')

def test_br_new_initialized_twice():
    br = pycdlib.headervd.BootRecord()
    br.new(b'foo')

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.new(b'foo')
    assert(str(excinfo.value) == 'Boot Record already initialized')

def test_br_record_not_initialized():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.record()
    assert(str(excinfo.value) == 'Boot Record not initialized')

def test_br_update_boot_system_use_not_initialized():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.update_boot_system_use(b'\x00'*1977)
    assert(str(excinfo.value) == 'Boot Record not initialized')

def test_br_update_boot_system_use_invalid_length():
    br = pycdlib.headervd.BootRecord()
    br.new(b'foo')

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.update_boot_system_use(b'\x00'*1976)
    assert(str(excinfo.value) == 'Boot system use field must be 1977 bytes')

def test_br_extent_location_not_initialized():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.extent_location()
    assert(str(excinfo.value) == 'Boot Record not initialized')

def test_br_set_extent_location_not_initialized():
    br = pycdlib.headervd.BootRecord()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        br.set_extent_location(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')

# Version Volume Descriptor
def test_version_parse_initialized_twice():
    version = pycdlib.headervd.VersionVolumeDescriptor()
    version.parse(b'\x00'*2048, 0)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        version.parse(b'\x00'*2048, 0)
    assert(str(excinfo.value) == 'This Version Volume Descriptor is already initialized')

def test_version_parse_not_version():
    version = pycdlib.headervd.VersionVolumeDescriptor()
    assert(not(version.parse(b'\x00'*2047, 0)))

def test_version_new_initialized_twice():
    version = pycdlib.headervd.VersionVolumeDescriptor()
    version.new(2048)

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        version.new(2048)
    assert(str(excinfo.value) == 'This Version Volume Descriptor is already initialized')

def test_version_record_not_initialized():
    version = pycdlib.headervd.VersionVolumeDescriptor()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        version.record()
    assert(str(excinfo.value) == 'This Version Volume Descriptor is not initialized')

def test_version_extent_location_not_initialized():
    version = pycdlib.headervd.VersionVolumeDescriptor()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        version.extent_location()
    assert(str(excinfo.value) == 'This Version Volume Descriptor is not initialized')

def test_version_set_extent_location_not_initialized():
    version = pycdlib.headervd.VersionVolumeDescriptor()

    with pytest.raises(pycdlib.pycdlibexception.PyCdlibInternalError) as excinfo:
        version.set_extent_location(0)
    assert(str(excinfo.value) == 'This Volume Descriptor is not initialized')