File: test_headers.py

package info (click to toggle)
eyed3 0.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,380 kB
  • sloc: python: 13,282; makefile: 411; sh: 69
file content (474 lines) | stat: -rw-r--r-- 17,140 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
import unittest
import pytest
from eyed3.id3.headers import *
from eyed3.id3 import ID3_DEFAULT_VERSION, TagException

from io import BytesIO


class TestTagHeader(unittest.TestCase):
    def testCtor(self):
        h = TagHeader()
        assert (h.version == ID3_DEFAULT_VERSION)
        assert not(h.unsync)
        assert not(h.extended)
        assert not(h.experimental)
        assert not(h.footer)
        assert h.tag_size == 0

    def testTagVersion(self):
        for maj, min, rev in [(1, 0, 0), (1, 1, 0), (2, 2, 0), (2, 3, 0),
                              (2, 4, 0)]:
            h = TagHeader((maj, min, rev))

            assert (h.major_version == maj)
            assert (h.minor_version == min)
            assert (h.rev_version == rev)

        for maj, min, rev in [(1, 0, None), (1, None, 0), (2, 5, 0), (3, 4, 0)]:
            try:
                h = TagHeader((maj, min, rev))
            except ValueError:
                pass
            else:
                assert not("Invalid version, expected ValueError")

    def testParse(self):
        # Incomplete headers
        for data in [b"", b"ID3", b"ID3\x04\x00",
                     b"ID3\x02\x00\x00",
                     b"ID3\x03\x00\x00",
                     b"ID3\x04\x00\x00",
                    ]:
            header = TagHeader()
            found = header.parse(BytesIO(data))
            assert not(found)

        # Invalid versions
        for data in [b"ID3\x01\x00\x00",
                     b"ID3\x05\x00\x00",
                     b"ID3\x06\x00\x00",
                    ]:
            header = TagHeader()
            try:
                found = header.parse(BytesIO(data))
            except TagException:
                pass
            else:
                assert not("Expected TagException invalid version")


        # Complete headers
        for data in [b"ID3\x02\x00\x00",
                     b"ID3\x03\x00\x00",
                     b"ID3\x04\x00\x00",
                    ]:
            for sz in [0, 10, 100, 1000, 2500, 5000, 7500, 10000]:
                sz_bytes = bin2bytes(bin2synchsafe(dec2bin(sz, 32)))
                header = TagHeader()
                found = header.parse(BytesIO(data + sz_bytes))
                assert (found)
                assert header.tag_size == sz

    def testRenderWithUnsyncTrue(self):
        h = TagHeader()
        h.unsync = True
        with pytest.raises(NotImplementedError):
            h.render(100)

    def testRender(self):
        h = TagHeader()
        h.unsync = False
        header = h.render(100)

        h2 = TagHeader()
        found = h2.parse(BytesIO(header))
        assert not(h2.unsync)
        assert (found)
        assert header == h2.render(100)

        h = TagHeader()
        h.footer = True
        h.extended = True
        header = h.render(666)

        h2 = TagHeader()
        found = h2.parse(BytesIO(header))
        assert (found)
        assert not(h2.unsync)
        assert not(h2.experimental)
        assert h2.footer
        assert h2.extended
        assert (h2.tag_size == 666)
        assert (header == h2.render(666))

class TestExtendedHeader(unittest.TestCase):
    def testCtor(self):
        h = ExtendedTagHeader()
        assert (h.size == 0)
        assert (h._flags == 0)
        assert (h.crc is None)
        assert (h._restrictions == 0)

        assert not(h.update_bit)
        assert not(h.crc_bit)
        assert not(h.restrictions_bit)

    def testUpdateBit(self):
        h = ExtendedTagHeader()

        h.update_bit = 1
        assert (h.update_bit)
        h.update_bit = 0
        assert not(h.update_bit)
        h.update_bit = 1
        assert (h.update_bit)
        h.update_bit = False
        assert not(h.update_bit)
        h.update_bit = True
        assert (h.update_bit)

    def testCrcBit(self):
        h = ExtendedTagHeader()
        h.update_bit = True

        h.crc_bit = 1
        assert (h.update_bit)
        assert (h.crc_bit)
        h.crc_bit = 0
        assert (h.update_bit)
        assert not(h.crc_bit)
        h.crc_bit = 1
        assert (h.update_bit)
        assert (h.crc_bit)
        h.crc_bit = False
        assert (h.update_bit)
        assert not(h.crc_bit)
        h.crc_bit = True
        assert (h.update_bit)
        assert (h.crc_bit)

    def testRestrictionsBit(self):
        h = ExtendedTagHeader()
        h.update_bit = True
        h.crc_bit = True

        h.restrictions_bit = 1
        assert (h.update_bit)
        assert (h.crc_bit)
        assert (h.restrictions_bit)
        h.restrictions_bit = 0
        assert (h.update_bit)
        assert (h.crc_bit)
        assert not(h.restrictions_bit)
        h.restrictions_bit = 1
        assert (h.update_bit)
        assert (h.crc_bit)
        assert (h.restrictions_bit)
        h.restrictions_bit = False
        assert (h.update_bit)
        assert (h.crc_bit)
        assert not(h.restrictions_bit)
        h.restrictions_bit = True
        assert (h.update_bit)
        assert (h.crc_bit)
        assert (h.restrictions_bit)

        h = ExtendedTagHeader()
        h.restrictions_bit = True
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_LARGE)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_NONE)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_NONE)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_NONE)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_NONE)

        h.tag_size_restriction = ExtendedTagHeader.RESTRICT_TAG_SZ_TINY
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_TINY)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_NONE)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_NONE)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_NONE)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_NONE)

        h.text_enc_restriction = ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_TINY)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_NONE)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_NONE)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_NONE)

        h.text_length_restriction = ExtendedTagHeader.RESTRICT_TEXT_LEN_30
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_TINY)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_30)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_NONE)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_NONE)

        h.image_enc_restriction = ExtendedTagHeader.RESTRICT_IMG_ENC_PNG_JPG
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_TINY)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_30)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_PNG_JPG)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_NONE)

        h.image_size_restriction = ExtendedTagHeader.RESTRICT_IMG_SZ_256
        assert (h.tag_size_restriction ==
                ExtendedTagHeader.RESTRICT_TAG_SZ_TINY)
        assert (h.text_enc_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8)
        assert (h.text_length_restriction ==
                ExtendedTagHeader.RESTRICT_TEXT_LEN_30)
        assert (h.image_enc_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_ENC_PNG_JPG)
        assert (h.image_size_restriction ==
                ExtendedTagHeader.RESTRICT_IMG_SZ_256)

        assert " 32 frames " in h.tag_size_restriction_description
        assert " 4 KB " in h.tag_size_restriction_description
        h.tag_size_restriction = ExtendedTagHeader.RESTRICT_TAG_SZ_LARGE
        assert " 128 frames " in h.tag_size_restriction_description
        h.tag_size_restriction = ExtendedTagHeader.RESTRICT_TAG_SZ_MED
        assert " 64 frames " in h.tag_size_restriction_description
        h.tag_size_restriction = ExtendedTagHeader.RESTRICT_TAG_SZ_SMALL
        assert " 32 frames " in h.tag_size_restriction_description
        assert " 40 KB " in h.tag_size_restriction_description

        assert (" UTF-8" in h.text_enc_restriction_description)
        h.text_enc_restriction = ExtendedTagHeader.RESTRICT_TEXT_ENC_NONE
        assert ("None" == h.text_enc_restriction_description)

        assert " 30 " in h.text_length_restriction_description
        h.text_length_restriction = ExtendedTagHeader.RESTRICT_TEXT_LEN_NONE
        assert ("None" == h.text_length_restriction_description)
        h.text_length_restriction = ExtendedTagHeader.RESTRICT_TEXT_LEN_1024
        assert " 1024 " in h.text_length_restriction_description
        h.text_length_restriction = ExtendedTagHeader.RESTRICT_TEXT_LEN_128
        assert " 128 " in h.text_length_restriction_description

        assert " PNG " in h.image_enc_restriction_description
        h.image_enc_restriction = ExtendedTagHeader.RESTRICT_IMG_ENC_NONE
        assert ("None" == h.image_enc_restriction_description)

        assert " 256x256 " in h.image_size_restriction_description
        h.image_size_restriction = ExtendedTagHeader.RESTRICT_IMG_SZ_NONE
        assert ("None" == h.image_size_restriction_description)
        h.image_size_restriction = ExtendedTagHeader.RESTRICT_IMG_SZ_64
        assert (" 64x64 pixels or smaller" in
                h.image_size_restriction_description)
        h.image_size_restriction = ExtendedTagHeader.RESTRICT_IMG_SZ_64_EXACT
        assert "exactly 64x64 pixels" in h.image_size_restriction_description

    def testRender(self):
        version = (2, 4, 0)
        dummy_data = b"\xab" * 50
        dummy_padding_len = 1024

        h = ExtendedTagHeader()
        h.update_bit = 1
        h.crc_bit = 1
        h.tag_size_restriction = ExtendedTagHeader.RESTRICT_TAG_SZ_MED
        h.text_enc_restriction = ExtendedTagHeader.RESTRICT_TEXT_ENC_UTF8
        h.text_length_restriction = ExtendedTagHeader.RESTRICT_TEXT_LEN_128
        h.image_enc_restriction = ExtendedTagHeader.RESTRICT_IMG_ENC_PNG_JPG
        h.image_size_restriction = ExtendedTagHeader.RESTRICT_IMG_SZ_256
        header = h.render(version, dummy_data, dummy_padding_len)

        h2 = ExtendedTagHeader()
        h2.parse(BytesIO(header), version)
        assert (h2.update_bit)
        assert (h2.crc_bit)
        assert (h2.restrictions_bit)
        assert (h.crc == h2.crc)
        assert (h.tag_size_restriction == h2.tag_size_restriction)
        assert (h.text_enc_restriction == h2.text_enc_restriction)
        assert (h.text_length_restriction == h2.text_length_restriction)
        assert (h.image_enc_restriction == h2.image_enc_restriction)
        assert (h.image_size_restriction == h2.image_size_restriction)

        assert h2.render(version, dummy_data, dummy_padding_len) == header

        # version 2.3
        header_23 = h.render((2,3,0), dummy_data, dummy_padding_len)

        h3 = ExtendedTagHeader()
        h3.parse(BytesIO(header_23), (2,3,0))
        assert not(h3.update_bit)
        assert (h3.crc_bit)
        assert not(h3.restrictions_bit)
        assert (h.crc == h3.crc)
        assert (0 == h3.tag_size_restriction)
        assert (0 == h3.text_enc_restriction)
        assert (0 == h3.text_length_restriction)
        assert (0 == h3.image_enc_restriction)
        assert (0 == h3.image_size_restriction)

    def testRenderCrcPadding(self):
        version = (2, 4, 0)

        h = ExtendedTagHeader()
        h.crc_bit = 1
        header = h.render(version, b"\x01", 0)

        h2 = ExtendedTagHeader()
        h2.parse(BytesIO(header), version)
        assert h.crc == h2.crc

    def testInvalidFlagBits(self):
        for bad_flags in [b"\x00\x20", b"\x01\x01"]:
            h = ExtendedTagHeader()
            try:
                h.parse(BytesIO(b"\x00\x00\x00\xff" + bad_flags), (2, 4, 0))
            except TagException:
                pass
            else:
                assert not("Bad ExtendedTagHeader flags, expected "
                             "TagException")

class TestFrameHeader(unittest.TestCase):
    def testCtor(self):
        h = FrameHeader(b"TIT2", ID3_DEFAULT_VERSION)
        assert (h.size == 10)
        assert (h.id == b"TIT2")
        assert (h.data_size == 0)
        assert (h._flags == [0] * 16)

        h = FrameHeader(b"TIT2", (2, 3, 0))
        assert (h.size == 10)
        assert (h.id == b"TIT2")
        assert (h.data_size == 0)
        assert (h._flags == [0] * 16)

        h = FrameHeader(b"TIT2", (2, 2, 0))
        assert (h.size == 6)
        assert (h.id == b"TIT2")
        assert (h.data_size == 0)
        assert (h._flags == [0] * 16)

    def testBitMask(self):
        for v in [(2, 2, 0), (2, 3, 0)]:
            h = FrameHeader(b"TXXX", v)
            assert (h.TAG_ALTER == 0)
            assert (h.FILE_ALTER == 1)
            assert (h.READ_ONLY == 2)
            assert (h.COMPRESSED == 8)
            assert (h.ENCRYPTED == 9)
            assert (h.GROUPED == 10)
            assert (h.UNSYNC == 14)
            assert (h.DATA_LEN == 4)

        for v in [(2, 4, 0), (1, 0, 0), (1, 1, 0)]:
            h = FrameHeader(b"TXXX", v)
            assert (h.TAG_ALTER == 1)
            assert (h.FILE_ALTER == 2)
            assert (h.READ_ONLY == 3)
            assert (h.COMPRESSED == 12)
            assert (h.ENCRYPTED == 13)
            assert (h.GROUPED == 9)
            assert (h.UNSYNC == 14)
            assert (h.DATA_LEN == 15)

        for v in [(2, 5, 0), (3, 0, 0)]:
            try:
                h = FrameHeader(b"TIT2", v)
            except ValueError:
                pass
            else:
                assert not("Expected a ValueError from invalid version, "
                             "but got success")

        for v in [1, "yes", "no", True, 23]:
            h = FrameHeader(b"APIC", (2, 4, 0))
            h.tag_alter = v
            h.file_alter = v
            h.read_only = v
            h.compressed = v
            h.encrypted = v
            h.grouped = v
            h.unsync = v
            h.data_length_indicator = v
            assert (h.tag_alter == 1)
            assert (h.file_alter == 1)
            assert (h.read_only == 1)
            assert (h.compressed == 1)
            assert (h.encrypted == 1)
            assert (h.grouped == 1)
            assert (h.unsync == 1)
            assert (h.data_length_indicator == 1)

        for v in [0, False, None]:
            h = FrameHeader(b"APIC", (2, 4, 0))
            h.tag_alter = v
            h.file_alter = v
            h.read_only = v
            h.compressed = v
            h.encrypted = v
            h.grouped = v
            h.unsync = v
            h.data_length_indicator = v
            assert (h.tag_alter == 0)
            assert (h.file_alter == 0)
            assert (h.read_only == 0)
            assert (h.compressed == 0)
            assert (h.encrypted == 0)
            assert (h.grouped == 0)
            assert (h.unsync == 0)
            assert (h.data_length_indicator == 0)

        h1 = FrameHeader(b"APIC", (2, 3, 0))
        h1.tag_alter = True
        h1.grouped = True
        h1.file_alter = 1
        h1.encrypted = None
        h1.compressed = 4
        h1.data_length_indicator = 0
        h1.read_only = 1
        h1.unsync = 1

        h2 = FrameHeader(b"APIC", (2, 4, 0))
        assert (h2.tag_alter == 0)
        assert (h2.grouped == 0)
        h2.copyFlags(h1)
        assert (h2.tag_alter)
        assert (h2.grouped)
        assert (h2.file_alter)
        assert not(h2.encrypted)
        assert (h2.compressed)
        assert not(h2.data_length_indicator)
        assert (h2.read_only)
        assert (h2.unsync)

    def testValidFrameId(self):
        for id in [b"", b"a", b"tx", b"tit", b"TIT", b"Tit2", b"aPic"]:
            assert not(FrameHeader._isValidFrameId(id))
        for id in [b"TIT2", b"APIC", b"1234"]:
            assert FrameHeader._isValidFrameId(id)

    def testRenderWithUnsyncTrue(self):
        h = FrameHeader(b"TIT2", ID3_DEFAULT_VERSION)
        h.unsync = True
        with pytest.raises(NotImplementedError):
            h.render(100)