This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_lzma.py
1596 lines (1409 loc) · 72.1 KB
/
test_lzma.py
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from io import BytesIO, UnsupportedOperation
import os
import sys
import random
import unittest
try:
# Try the location used on Python 3 first,
from test.support import _4G, TESTFN, import_module, bigmemtest, run_unittest, unlink
except ImportError:
# Must be under Python 2 then,
from test.test_support import _4G, TESTFN, import_module, bigmemtest, run_unittest, unlink
import inspect
if "size" not in inspect.getargspec(bigmemtest).args:
# The size aregument is new in Python 3.3 so we can't use
# it as below, e.g. @bigmemtest(size=_4G + 100, memuse=2)
# Bit of a hack, but define a dummy decorator here instead,
# using the default size 5147 used in Python 3.3,
def bigmemtest(size, memuse, dry_run=True):
def decorator(f):
def wrapper(self):
return f(self, 5147)
return wrapper
return decorator
# TODO - Change the unittests to work with older bigmemtest?
# The following style import doesn't work on Python 3.0,
# from test.support import import_module
# lzma = import_module("backports.lzma")
from backports import lzma
from backports.lzma import LZMACompressor, LZMADecompressor, LZMAError, LZMAFile
class CompressorDecompressorTestCase(unittest.TestCase):
# Test error cases.
def test_simple_bad_args(self):
self.assertRaises(TypeError, LZMACompressor, [])
if sys.version_info >= (2,7):
# Under Python 2.6 the float format would be automatically
# truncated into an integer and a deprecation warning issued.
self.assertRaises(TypeError, LZMACompressor, format=3.45)
self.assertRaises(TypeError, LZMACompressor, check="")
self.assertRaises((TypeError, SystemError), LZMACompressor, preset="asdf")
self.assertRaises(TypeError, LZMACompressor, filters=3)
# Can't specify FORMAT_AUTO when compressing.
self.assertRaises(ValueError, LZMACompressor, format=lzma.FORMAT_AUTO)
# Can't specify a preset and a custom filter chain at the same time.
self.assertRaises(ValueError, LZMACompressor,
preset=7, filters=[{"id": lzma.FILTER_LZMA2}])
self.assertRaises(TypeError, LZMADecompressor, ())
self.assertRaises((TypeError, SystemError), LZMADecompressor, memlimit=b"qw")
self.assertRaises(TypeError, LZMADecompressor,
lzma.FORMAT_RAW, filters="zzz")
# Cannot specify a memory limit with FILTER_RAW.
self.assertRaises(ValueError, LZMADecompressor,
lzma.FORMAT_RAW, memlimit=0x1000000)
# Can only specify a custom filter chain with FILTER_RAW.
self.assertRaises(ValueError, LZMADecompressor, filters=FILTERS_RAW_1)
self.assertRaises(ValueError, LZMADecompressor,
format=lzma.FORMAT_XZ, filters=FILTERS_RAW_1)
self.assertRaises(ValueError, LZMADecompressor,
format=lzma.FORMAT_ALONE, filters=FILTERS_RAW_1)
lzc = LZMACompressor()
self.assertRaises(TypeError, lzc.compress)
self.assertRaises(TypeError, lzc.compress, b"foo", b"bar")
self.assertRaises(TypeError, lzc.flush, b"blah")
empty = lzc.flush()
self.assertRaises(ValueError, lzc.compress, b"quux")
self.assertRaises(ValueError, lzc.flush)
lzd = LZMADecompressor()
self.assertRaises(TypeError, lzd.decompress)
self.assertRaises(TypeError, lzd.decompress, b"foo", b"bar")
lzd.decompress(empty)
self.assertRaises(EOFError, lzd.decompress, b"quux")
def test_bad_filter_spec(self):
self.assertRaises(TypeError, LZMACompressor, filters=[b"wobsite"])
self.assertRaises(ValueError, LZMACompressor, filters=[{"xyzzy": 3}])
self.assertRaises(ValueError, LZMACompressor, filters=[{"id": 98765}])
self.assertRaises(ValueError, LZMACompressor,
filters=[{"id": lzma.FILTER_LZMA2, "foo": 0}])
self.assertRaises(ValueError, LZMACompressor,
filters=[{"id": lzma.FILTER_DELTA, "foo": 0}])
self.assertRaises(ValueError, LZMACompressor,
filters=[{"id": lzma.FILTER_X86, "foo": 0}])
def test_decompressor_after_eof(self):
lzd = LZMADecompressor()
lzd.decompress(COMPRESSED_XZ)
self.assertRaises(EOFError, lzd.decompress, b"nyan")
def test_decompressor_memlimit(self):
lzd = LZMADecompressor(memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_XZ, memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_ALONE, memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_ALONE)
# Test LZMADecompressor on known-good input data.
def _test_decompressor(self, lzd, data, check, unused_data=b""):
self.assertFalse(lzd.eof)
out = lzd.decompress(data)
self.assertEqual(out, INPUT)
self.assertEqual(lzd.check, check)
self.assertTrue(lzd.eof)
self.assertEqual(lzd.unused_data, unused_data)
def test_decompressor_auto(self):
lzd = LZMADecompressor()
self._test_decompressor(lzd, COMPRESSED_XZ, lzma.CHECK_CRC64)
lzd = LZMADecompressor()
self._test_decompressor(lzd, COMPRESSED_ALONE, lzma.CHECK_NONE)
def test_decompressor_xz(self):
lzd = LZMADecompressor(lzma.FORMAT_XZ)
self._test_decompressor(lzd, COMPRESSED_XZ, lzma.CHECK_CRC64)
def test_decompressor_alone(self):
lzd = LZMADecompressor(lzma.FORMAT_ALONE)
self._test_decompressor(lzd, COMPRESSED_ALONE, lzma.CHECK_NONE)
def test_decompressor_raw_1(self):
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self._test_decompressor(lzd, COMPRESSED_RAW_1, lzma.CHECK_NONE)
def test_decompressor_raw_2(self):
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_2)
self._test_decompressor(lzd, COMPRESSED_RAW_2, lzma.CHECK_NONE)
def test_decompressor_raw_3(self):
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_3)
self._test_decompressor(lzd, COMPRESSED_RAW_3, lzma.CHECK_NONE)
def test_decompressor_raw_4(self):
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
self._test_decompressor(lzd, COMPRESSED_RAW_4, lzma.CHECK_NONE)
def test_decompressor_chunks(self):
lzd = LZMADecompressor()
out = []
for i in range(0, len(COMPRESSED_XZ), 10):
self.assertFalse(lzd.eof)
out.append(lzd.decompress(COMPRESSED_XZ[i:i+10]))
out = b"".join(out)
self.assertEqual(out, INPUT)
self.assertEqual(lzd.check, lzma.CHECK_CRC64)
self.assertTrue(lzd.eof)
self.assertEqual(lzd.unused_data, b"")
def test_decompressor_unused_data(self):
lzd = LZMADecompressor()
extra = b"fooblibar"
self._test_decompressor(lzd, COMPRESSED_XZ + extra, lzma.CHECK_CRC64,
unused_data=extra)
def test_decompressor_bad_input(self):
lzd = LZMADecompressor()
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_RAW_1)
lzd = LZMADecompressor(lzma.FORMAT_XZ)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_ALONE)
lzd = LZMADecompressor(lzma.FORMAT_ALONE)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
# Test that LZMACompressor->LZMADecompressor preserves the input data.
def test_roundtrip_xz(self):
lzc = LZMACompressor()
cdata = lzc.compress(INPUT) + lzc.flush()
lzd = LZMADecompressor()
self._test_decompressor(lzd, cdata, lzma.CHECK_CRC64)
def test_roundtrip_alone(self):
lzc = LZMACompressor(lzma.FORMAT_ALONE)
cdata = lzc.compress(INPUT) + lzc.flush()
lzd = LZMADecompressor()
self._test_decompressor(lzd, cdata, lzma.CHECK_NONE)
def test_roundtrip_raw(self):
lzc = LZMACompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
cdata = lzc.compress(INPUT) + lzc.flush()
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
self._test_decompressor(lzd, cdata, lzma.CHECK_NONE)
def test_roundtrip_chunks(self):
lzc = LZMACompressor()
cdata = []
for i in range(0, len(INPUT), 10):
cdata.append(lzc.compress(INPUT[i:i+10]))
cdata.append(lzc.flush())
cdata = b"".join(cdata)
lzd = LZMADecompressor()
self._test_decompressor(lzd, cdata, lzma.CHECK_CRC64)
# LZMADecompressor intentionally does not handle concatenated streams.
def test_decompressor_multistream(self):
lzd = LZMADecompressor()
self._test_decompressor(lzd, COMPRESSED_XZ + COMPRESSED_ALONE,
lzma.CHECK_CRC64, unused_data=COMPRESSED_ALONE)
# Test with inputs larger than 4GiB.
@bigmemtest(size=_4G + 100, memuse=2)
def test_compressor_bigmem(self, size):
lzc = LZMACompressor()
cdata = lzc.compress(b"x" * size) + lzc.flush()
ddata = lzma.decompress(cdata)
try:
self.assertEqual(len(ddata), size)
self.assertEqual(len(ddata.strip(b"x")), 0)
finally:
ddata = None
@bigmemtest(size=_4G + 100, memuse=3)
def test_decompressor_bigmem(self, size):
lzd = LZMADecompressor()
blocksize = 10 * 1024 * 1024
if sys.version_info >= (3,2):
block = random.getrandbits(blocksize * 8).to_bytes(blocksize, "little")
#Note as the data is random, don't care if big-endian or little-endian
else:
#The to_bytes method was added in Python 3.2, so can't use it here.
block = os.urandom(blocksize)
try:
input = block * (size // blocksize + 1)
cdata = lzma.compress(input)
ddata = lzd.decompress(cdata)
self.assertEqual(ddata, input)
finally:
input = cdata = ddata = None
class CompressDecompressFunctionTestCase(unittest.TestCase):
# Test error cases:
def test_bad_args(self):
self.assertRaises(TypeError, lzma.compress)
self.assertRaises(TypeError, lzma.compress, [])
self.assertRaises((TypeError, SystemError), lzma.compress, b"", format="xz")
self.assertRaises(TypeError, lzma.compress, b"", check="none")
self.assertRaises((TypeError, SystemError), lzma.compress, b"", preset="blah")
self.assertRaises(TypeError, lzma.compress, b"", filters=1024)
# Can't specify a preset and a custom filter chain at the same time.
self.assertRaises(ValueError, lzma.compress,
b"", preset=3, filters=[{"id": lzma.FILTER_LZMA2}])
self.assertRaises(TypeError, lzma.decompress)
self.assertRaises(TypeError, lzma.decompress, [])
self.assertRaises(TypeError, lzma.decompress, b"", format="lzma")
self.assertRaises((TypeError, SystemError), lzma.decompress, b"", memlimit=7.3e9)
self.assertRaises(TypeError, lzma.decompress,
b"", format=lzma.FORMAT_RAW, filters={})
# Cannot specify a memory limit with FILTER_RAW.
self.assertRaises(ValueError, lzma.decompress,
b"", format=lzma.FORMAT_RAW, memlimit=0x1000000)
# Can only specify a custom filter chain with FILTER_RAW.
self.assertRaises(ValueError, lzma.decompress,
b"", filters=FILTERS_RAW_1)
self.assertRaises(ValueError, lzma.decompress,
b"", format=lzma.FORMAT_XZ, filters=FILTERS_RAW_1)
self.assertRaises(ValueError, lzma.decompress,
b"", format=lzma.FORMAT_ALONE, filters=FILTERS_RAW_1)
def test_decompress_memlimit(self):
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_XZ, memlimit=1024)
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_XZ, format=lzma.FORMAT_XZ, memlimit=1024)
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_ALONE, format=lzma.FORMAT_ALONE, memlimit=1024)
# Test LZMADecompressor on known-good input data.
def test_decompress_good_input(self):
ddata = lzma.decompress(COMPRESSED_XZ)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(COMPRESSED_ALONE)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(COMPRESSED_XZ, lzma.FORMAT_XZ)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(COMPRESSED_ALONE, lzma.FORMAT_ALONE)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(
COMPRESSED_RAW_1, lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(
COMPRESSED_RAW_2, lzma.FORMAT_RAW, filters=FILTERS_RAW_2)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(
COMPRESSED_RAW_3, lzma.FORMAT_RAW, filters=FILTERS_RAW_3)
self.assertEqual(ddata, INPUT)
ddata = lzma.decompress(
COMPRESSED_RAW_4, lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
self.assertEqual(ddata, INPUT)
def test_decompress_incomplete_input(self):
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_XZ[:128])
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_ALONE[:128])
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_1[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_2[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_3[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_4[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
def test_decompress_bad_input(self):
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_BOGUS);
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_RAW_1)
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_ALONE, format=lzma.FORMAT_XZ)
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_XZ, format=lzma.FORMAT_ALONE)
self.assertRaises(LZMAError, lzma.decompress,
COMPRESSED_XZ, format=lzma.FORMAT_RAW,
filters=FILTERS_RAW_1)
# Test that compress()->decompress() preserves the input data.
def test_roundtrip(self):
cdata = lzma.compress(INPUT)
ddata = lzma.decompress(cdata)
self.assertEqual(ddata, INPUT)
cdata = lzma.compress(INPUT, lzma.FORMAT_XZ)
ddata = lzma.decompress(cdata)
self.assertEqual(ddata, INPUT)
cdata = lzma.compress(INPUT, lzma.FORMAT_ALONE)
ddata = lzma.decompress(cdata)
self.assertEqual(ddata, INPUT)
cdata = lzma.compress(INPUT, lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
ddata = lzma.decompress(cdata, lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
self.assertEqual(ddata, INPUT)
# Unlike LZMADecompressor, decompress() *does* handle concatenated streams.
def test_decompress_multistream(self):
ddata = lzma.decompress(COMPRESSED_XZ + COMPRESSED_ALONE)
self.assertEqual(ddata, INPUT * 2)
# Test robust handling of non-LZMA data following the compressed stream(s).
def test_decompress_trailing_junk(self):
ddata = lzma.decompress(COMPRESSED_XZ + COMPRESSED_BOGUS)
self.assertEqual(ddata, INPUT)
def test_decompress_multistream_trailing_junk(self):
ddata = lzma.decompress(COMPRESSED_XZ * 3 + COMPRESSED_BOGUS)
self.assertEqual(ddata, INPUT * 3)
class TempFile:
"""Context manager - creates a file, and deletes it on __exit__."""
def __init__(self, filename, data=b""):
self.filename = filename
self.data = data
def __enter__(self):
with open(self.filename, "wb") as f:
f.write(self.data)
def __exit__(self, *args):
unlink(self.filename)
class FileTestCase(unittest.TestCase):
def test_init(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
pass
with LZMAFile(BytesIO(), "w") as f:
pass
with LZMAFile(BytesIO(), "a") as f:
pass
def test_init_with_filename(self):
with TempFile(TESTFN, COMPRESSED_XZ):
with LZMAFile(TESTFN) as f:
pass
with LZMAFile(TESTFN, "w") as f:
pass
with LZMAFile(TESTFN, "a") as f:
pass
def test_init_mode(self):
with TempFile(TESTFN):
with LZMAFile(TESTFN, "r"):
pass
with LZMAFile(TESTFN, "rb"):
pass
with LZMAFile(TESTFN, "w"):
pass
with LZMAFile(TESTFN, "wb"):
pass
with LZMAFile(TESTFN, "a"):
pass
with LZMAFile(TESTFN, "ab"):
pass
def test_init_bad_mode(self):
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), (3, "x"))
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "x")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "rt")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "r+")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "wt")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "w+")
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), "rw")
def test_init_bad_check(self):
self.assertRaises(TypeError, LZMAFile,
BytesIO(), "w", check=b"asd")
# CHECK_UNKNOWN and anything above CHECK_ID_MAX should be invalid.
self.assertRaises(LZMAError, LZMAFile,
BytesIO(), "w", check=lzma.CHECK_UNKNOWN)
self.assertRaises(LZMAError, LZMAFile,
BytesIO(), "w", check=lzma.CHECK_ID_MAX + 3)
# Cannot specify a check with mode="r".
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), check=lzma.CHECK_NONE)
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC32)
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC64)
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), check=lzma.CHECK_SHA256)
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), check=lzma.CHECK_UNKNOWN)
def test_init_bad_preset(self):
self.assertRaises((TypeError, SystemError), LZMAFile,
BytesIO(), "w", preset=4.39)
self.assertRaises(LZMAError, LZMAFile,
BytesIO(), "w", preset=10)
self.assertRaises(LZMAError, LZMAFile,
BytesIO(), "w", preset=23)
#Under Python 3.0 get:
#TypeError: can't convert negative int to unsigned
self.assertRaises((OverflowError, TypeError), LZMAFile,
BytesIO(), "w", preset=-1)
self.assertRaises((OverflowError, TypeError), LZMAFile,
BytesIO(), "w", preset=-7)
self.assertRaises((TypeError, SystemError), LZMAFile,
BytesIO(), "w", preset="foo")
# Cannot specify a preset with mode="r".
self.assertRaises(ValueError, LZMAFile,
BytesIO(COMPRESSED_XZ), preset=3)
def test_init_bad_filter_spec(self):
self.assertRaises(TypeError, LZMAFile,
BytesIO(), "w", filters=[b"wobsite"])
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w", filters=[{"xyzzy": 3}])
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w", filters=[{"id": 98765}])
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w",
filters=[{"id": lzma.FILTER_LZMA2, "foo": 0}])
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w",
filters=[{"id": lzma.FILTER_DELTA, "foo": 0}])
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w",
filters=[{"id": lzma.FILTER_X86, "foo": 0}])
def test_init_with_preset_and_filters(self):
self.assertRaises(ValueError, LZMAFile,
BytesIO(), "w", format=lzma.FORMAT_RAW,
preset=6, filters=FILTERS_RAW_1)
def test_close(self):
with BytesIO(COMPRESSED_XZ) as src:
f = LZMAFile(src)
f.close()
# LZMAFile.close() should not close the underlying file object.
self.assertFalse(src.closed)
# Try closing an already-closed LZMAFile.
f.close()
self.assertFalse(src.closed)
# Test with a real file on disk, opened directly by LZMAFile.
with TempFile(TESTFN, COMPRESSED_XZ):
f = LZMAFile(TESTFN)
fp = f._fp
f.close()
# Here, LZMAFile.close() *should* close the underlying file object.
self.assertTrue(fp.closed)
# Try closing an already-closed LZMAFile.
f.close()
def test_closed(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertFalse(f.closed)
f.read()
self.assertFalse(f.closed)
finally:
f.close()
self.assertTrue(f.closed)
f = LZMAFile(BytesIO(), "w")
try:
self.assertFalse(f.closed)
finally:
f.close()
self.assertTrue(f.closed)
def test_fileno(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertRaises(UnsupportedOperation, f.fileno)
finally:
f.close()
self.assertRaises(ValueError, f.fileno)
with TempFile(TESTFN, COMPRESSED_XZ):
f = LZMAFile(TESTFN)
try:
self.assertEqual(f.fileno(), f._fp.fileno())
#assertIsInstance added in Python 2.7 and 3.2
#self.assertIsInstance(f.fileno(), int)
self.assertTrue(isinstance(f.fileno(), int))
finally:
f.close()
self.assertRaises(ValueError, f.fileno)
def test_seekable(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertTrue(f.seekable())
f.read()
self.assertTrue(f.seekable())
finally:
f.close()
self.assertRaises(ValueError, f.seekable)
with TempFile(TESTFN, COMPRESSED_XZ):
f = LZMAFile(TESTFN)
try:
self.assertTrue(f.seekable())
f.read()
self.assertTrue(f.seekable())
finally:
f.close()
self.assertRaises(ValueError, f.seekable)
f = LZMAFile(BytesIO(), "w")
try:
self.assertFalse(f.seekable())
finally:
f.close()
self.assertRaises(ValueError, f.seekable)
src = BytesIO(COMPRESSED_XZ)
src.seekable = lambda: False
f = LZMAFile(src)
try:
self.assertFalse(f.seekable())
finally:
f.close()
self.assertRaises(ValueError, f.seekable)
def test_readable(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertTrue(f.readable())
f.read()
self.assertTrue(f.readable())
finally:
f.close()
self.assertRaises(ValueError, f.readable)
f = LZMAFile(BytesIO(), "w")
try:
self.assertFalse(f.readable())
finally:
f.close()
self.assertRaises(ValueError, f.readable)
def test_writable(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertFalse(f.writable())
f.read()
self.assertFalse(f.writable())
finally:
f.close()
self.assertRaises(ValueError, f.writable)
f = LZMAFile(BytesIO(), "w")
try:
self.assertTrue(f.writable())
finally:
f.close()
self.assertRaises(ValueError, f.writable)
def test_read(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f:
self.assertEqual(f.read(), INPUT)
with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_RAW_1),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_1) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_RAW_2),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_RAW_3),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
with LZMAFile(BytesIO(COMPRESSED_RAW_4),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_4) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
def test_read_0(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertEqual(f.read(0), b"")
with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f:
self.assertEqual(f.read(0), b"")
with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f:
self.assertEqual(f.read(0), b"")
with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f:
self.assertEqual(f.read(0), b"")
def test_read_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
chunks = []
while True:
result = f.read(10)
if not result:
break
self.assertTrue(len(result) <= 10)
chunks.append(result)
self.assertEqual(b"".join(chunks), INPUT)
def test_read_multistream(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
self.assertEqual(f.read(), INPUT * 5)
with LZMAFile(BytesIO(COMPRESSED_XZ + COMPRESSED_ALONE)) as f:
self.assertEqual(f.read(), INPUT * 2)
with LZMAFile(BytesIO(COMPRESSED_RAW_3 * 4),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3) as f:
self.assertEqual(f.read(), INPUT * 4)
def test_read_multistream_buffer_size_aligned(self):
# Test the case where a stream boundary coincides with the end
# of the raw read buffer.
saved_buffer_size = lzma._BUFFER_SIZE
lzma._BUFFER_SIZE = len(COMPRESSED_XZ)
try:
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
self.assertEqual(f.read(), INPUT * 5)
finally:
lzma._BUFFER_SIZE = saved_buffer_size
def test_read_trailing_junk(self):
with LZMAFile(BytesIO(COMPRESSED_XZ + COMPRESSED_BOGUS)) as f:
self.assertEqual(f.read(), INPUT)
def test_read_multistream_trailing_junk(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 5 + COMPRESSED_BOGUS)) as f:
self.assertEqual(f.read(), INPUT * 5)
def test_read_from_file(self):
with TempFile(TESTFN, COMPRESSED_XZ):
with LZMAFile(TESTFN) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
def test_read_from_file_with_bytes_filename(self):
try:
bytes_filename = TESTFN.encode("ascii")
except UnicodeEncodeError:
self.skipTest("Temporary file name needs to be ASCII")
with TempFile(TESTFN, COMPRESSED_XZ):
with LZMAFile(bytes_filename) as f:
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
def test_read_incomplete(self):
with LZMAFile(BytesIO(COMPRESSED_XZ[:128])) as f:
self.assertRaises(EOFError, f.read)
def test_read_bad_args(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
f.close()
self.assertRaises(ValueError, f.read)
with LZMAFile(BytesIO(), "w") as f:
self.assertRaises(ValueError, f.read)
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertRaises(TypeError, f.read, None)
def test_read_bad_data(self):
with LZMAFile(BytesIO(COMPRESSED_BOGUS)) as f:
self.assertRaises(LZMAError, f.read)
def test_read1(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = []
while True:
result = f.read1()
if not result:
break
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"")
def test_read1_0(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertEqual(f.read1(0), b"")
def test_read1_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = []
while True:
result = f.read1(10)
if not result:
break
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"")
def test_read1_multistream(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
blocks = []
while True:
result = f.read1()
if not result:
break
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT * 5)
self.assertEqual(f.read1(), b"")
def test_read1_bad_args(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
f.close()
self.assertRaises(ValueError, f.read1)
with LZMAFile(BytesIO(), "w") as f:
self.assertRaises(ValueError, f.read1)
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertRaises(TypeError, f.read1, None)
def test_peek(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
result = f.peek()
self.assertTrue(len(result) > 0)
self.assertTrue(INPUT.startswith(result))
self.assertEqual(f.read(), INPUT)
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
result = f.peek(10)
self.assertTrue(len(result) > 0)
self.assertTrue(INPUT.startswith(result))
self.assertEqual(f.read(), INPUT)
def test_peek_bad_args(self):
with LZMAFile(BytesIO(), "w") as f:
self.assertRaises(ValueError, f.peek)
def test_iterator(self):
with BytesIO(INPUT) as f:
lines = f.readlines()
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertEqual(list(iter(f)), lines)
with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f:
self.assertEqual(list(iter(f)), lines)
with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f:
self.assertEqual(list(iter(f)), lines)
with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f:
self.assertEqual(list(iter(f)), lines)
with LZMAFile(BytesIO(COMPRESSED_RAW_2),
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2) as f:
self.assertEqual(list(iter(f)), lines)
def test_readline(self):
with BytesIO(INPUT) as f:
lines = f.readlines()
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
for line in lines:
self.assertEqual(f.readline(), line)
def test_readlines(self):
with BytesIO(INPUT) as f:
lines = f.readlines()
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
self.assertEqual(f.readlines(), lines)
def test_write(self):
with BytesIO() as dst:
with LZMAFile(dst, "w") as f:
f.write(INPUT)
expected = lzma.compress(INPUT)
self.assertEqual(dst.getvalue(), expected)
with BytesIO() as dst:
with LZMAFile(dst, "w", format=lzma.FORMAT_XZ) as f:
f.write(INPUT)
expected = lzma.compress(INPUT, format=lzma.FORMAT_XZ)
self.assertEqual(dst.getvalue(), expected)
with BytesIO() as dst:
with LZMAFile(dst, "w", format=lzma.FORMAT_ALONE) as f:
f.write(INPUT)
expected = lzma.compress(INPUT, format=lzma.FORMAT_ALONE)
self.assertEqual(dst.getvalue(), expected)
with BytesIO() as dst:
with LZMAFile(dst, "w", format=lzma.FORMAT_RAW,
filters=FILTERS_RAW_2) as f:
f.write(INPUT)
expected = lzma.compress(INPUT, format=lzma.FORMAT_RAW,
filters=FILTERS_RAW_2)
self.assertEqual(dst.getvalue(), expected)
def test_write_10(self):
with BytesIO() as dst:
with LZMAFile(dst, "w") as f:
for start in range(0, len(INPUT), 10):
f.write(INPUT[start:start+10])
expected = lzma.compress(INPUT)
self.assertEqual(dst.getvalue(), expected)
def test_write_append(self):
part1 = INPUT[:1024]
part2 = INPUT[1024:1536]
part3 = INPUT[1536:]
expected = b"".join(lzma.compress(x) for x in (part1, part2, part3))
with BytesIO() as dst:
with LZMAFile(dst, "w") as f:
f.write(part1)
with LZMAFile(dst, "a") as f:
f.write(part2)
with LZMAFile(dst, "a") as f:
f.write(part3)
self.assertEqual(dst.getvalue(), expected)
def test_write_to_file(self):
try:
with LZMAFile(TESTFN, "w") as f:
f.write(INPUT)
expected = lzma.compress(INPUT)
with open(TESTFN, "rb") as f:
self.assertEqual(f.read(), expected)
finally:
unlink(TESTFN)
def test_write_to_file_with_bytes_filename(self):
try:
bytes_filename = TESTFN.encode("ascii")
except UnicodeEncodeError:
self.skipTest("Temporary file name needs to be ASCII")
try:
with LZMAFile(bytes_filename, "w") as f:
f.write(INPUT)
expected = lzma.compress(INPUT)
with open(TESTFN, "rb") as f:
self.assertEqual(f.read(), expected)
finally:
unlink(TESTFN)
def test_write_append_to_file(self):
part1 = INPUT[:1024]
part2 = INPUT[1024:1536]
part3 = INPUT[1536:]
expected = b"".join(lzma.compress(x) for x in (part1, part2, part3))
try:
with LZMAFile(TESTFN, "w") as f:
f.write(part1)
with LZMAFile(TESTFN, "a") as f:
f.write(part2)
with LZMAFile(TESTFN, "a") as f:
f.write(part3)
with open(TESTFN, "rb") as f:
self.assertEqual(f.read(), expected)
finally:
unlink(TESTFN)
def test_write_bad_args(self):
f = LZMAFile(BytesIO(), "w")
f.close()
self.assertRaises(ValueError, f.write, b"foo")
with LZMAFile(BytesIO(COMPRESSED_XZ), "r") as f:
self.assertRaises(ValueError, f.write, b"bar")
with LZMAFile(BytesIO(), "w") as f:
self.assertRaises(TypeError, f.write, None)
if sys.version_info < (3,):
#Should we allow both byte-strings and unicode?
pass
else:
self.assertRaises(TypeError, f.write, "text")
self.assertRaises(TypeError, f.write, 789)
def test_writelines(self):
with BytesIO(INPUT) as f:
lines = f.readlines()
with BytesIO() as dst:
with LZMAFile(dst, "w") as f:
f.writelines(lines)
expected = lzma.compress(INPUT)
self.assertEqual(dst.getvalue(), expected)
def test_seek_forward(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.seek(555)
self.assertEqual(f.read(), INPUT[555:])
def test_seek_forward_across_streams(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 2)) as f:
f.seek(len(INPUT) + 123)
self.assertEqual(f.read(), INPUT[123:])
def test_seek_forward_relative_to_current(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.read(100)
f.seek(1236, 1)
self.assertEqual(f.read(), INPUT[1336:])
def test_seek_forward_relative_to_end(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.seek(-555, 2)
self.assertEqual(f.read(), INPUT[-555:])
def test_seek_backward(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.read(1001)
f.seek(211)
self.assertEqual(f.read(), INPUT[211:])
def test_seek_backward_across_streams(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 2)) as f:
f.read(len(INPUT) + 333)
f.seek(737)
self.assertEqual(f.read(), INPUT[737:] + INPUT)
def test_seek_backward_relative_to_end(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.seek(-150, 2)
self.assertEqual(f.read(), INPUT[-150:])
def test_seek_past_end(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.seek(len(INPUT) + 9001)
self.assertEqual(f.tell(), len(INPUT))
self.assertEqual(f.read(), b"")
def test_seek_past_start(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
f.seek(-88)
self.assertEqual(f.tell(), 0)
self.assertEqual(f.read(), INPUT)
def test_seek_bad_args(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
f.close()
self.assertRaises(ValueError, f.seek, 0)
with LZMAFile(BytesIO(), "w") as f: