Skip to content

Commit ce4a080

Browse files
committed
Added test coverage
1 parent edee8e3 commit ce4a080

File tree

7 files changed

+186
-3
lines changed

7 files changed

+186
-3
lines changed

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ SRC_BITMAP := \
3838
shared-bindings/rainbowio/__init__.c \
3939
shared-bindings/traceback/__init__.c \
4040
shared-bindings/util.c \
41+
shared-bindings/zlib/__init__.c \
42+
shared-bindings/zlib/DecompIO.c \
4143
shared-module/aesio/aes.c \
4244
shared-module/aesio/__init__.c \
4345
shared-module/bitmaptools/__init__.c \
@@ -46,7 +48,9 @@ SRC_BITMAP := \
4648
shared-module/displayio/ColorConverter.c \
4749
shared-module/displayio/ColorConverter.c \
4850
shared-module/rainbowio/__init__.c \
49-
shared-module/traceback/__init__.c
51+
shared-module/traceback/__init__.c \
52+
shared-module/zlib/__init__.c \
53+
shared-module/zlib/DecompIO.c
5054

5155
$(info $(SRC_BITMAP))
5256
SRC_C += $(SRC_BITMAP)
@@ -57,7 +61,8 @@ CFLAGS += \
5761
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
5862
-DCIRCUITPY_GIFIO=1 \
5963
-DCIRCUITPY_RAINBOWIO=1 \
60-
-DCIRCUITPY_TRACEBACK=1
64+
-DCIRCUITPY_TRACEBACK=1 \
65+
-DCIRCUITPY_ZLIB=1
6166

6267
SRC_C += coverage.c
6368
SRC_CXX += coveragecpp.cpp

tests/circuitpython/zlib_decompio.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
try:
2+
import zlib
3+
import uio as io
4+
except ImportError:
5+
print("SKIP")
6+
raise SystemExit
7+
8+
9+
# Raw DEFLATE bitstream
10+
buf = io.BytesIO(b"\xcbH\xcd\xc9\xc9\x07\x00")
11+
inp = zlib.DecompIO(buf, -8)
12+
print(buf.seek(0, 1))
13+
print(inp.read(1))
14+
print(buf.seek(0, 1))
15+
print(inp.read(2))
16+
print(inp.read())
17+
print(buf.seek(0, 1))
18+
print(inp.read(1))
19+
print(inp.read())
20+
print(buf.seek(0, 1))
21+
22+
23+
# zlib bitstream
24+
inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc1"))
25+
print(inp.read(10))
26+
print(inp.read())
27+
28+
# zlib bitstream, wrong checksum
29+
inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc0"))
30+
try:
31+
print(inp.read())
32+
except OSError as e:
33+
print(repr(e))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
0
2+
b'h'
3+
2
4+
b'el'
5+
b'lo'
6+
7
7+
b''
8+
b''
9+
7
10+
b'0000000000'
11+
b'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
12+
OSError(22,)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
try:
2+
import zlib
3+
import uio as io
4+
except ImportError:
5+
print("SKIP")
6+
raise SystemExit
7+
8+
9+
# gzip bitstream
10+
buf = io.BytesIO(
11+
b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
12+
)
13+
inp = zlib.DecompIO(buf, 16 + 8)
14+
print(buf.seek(0, 1))
15+
print(inp.read(1))
16+
print(buf.seek(0, 1))
17+
print(inp.read(2))
18+
print(inp.read())
19+
print(buf.seek(0, 1))
20+
print(inp.read(1))
21+
print(inp.read())
22+
print(buf.seek(0, 1))
23+
24+
# Check FHCRC field
25+
buf = io.BytesIO(
26+
b"\x1f\x8b\x08\x02\x99\x0c\xe5W\x00\x03\x00\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
27+
)
28+
inp = zlib.DecompIO(buf, 16 + 8)
29+
print(inp.read())
30+
31+
# Check FEXTRA field
32+
buf = io.BytesIO(
33+
b"\x1f\x8b\x08\x04\x99\x0c\xe5W\x00\x03\x01\x00X\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
34+
)
35+
inp = zlib.DecompIO(buf, 16 + 8)
36+
print(inp.read())
37+
38+
# broken header
39+
buf = io.BytesIO(
40+
b"\x1f\x8c\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
41+
)
42+
try:
43+
inp = zlib.DecompIO(buf, 16 + 8)
44+
except ValueError:
45+
print("ValueError")
46+
47+
# broken crc32
48+
buf = io.BytesIO(
49+
b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa7\x106\x05\x00\x00\x00"
50+
)
51+
inp = zlib.DecompIO(buf, 16 + 8)
52+
try:
53+
inp.read(6)
54+
except OSError as e:
55+
print(repr(e))
56+
57+
# broken uncompressed size - not checked so far
58+
# buf = io.BytesIO(b'\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x06\x00\x00\x00')
59+
# inp = zlib.DecompIO(buf, 16 + 8)
60+
# inp.read(6)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
16
2+
b'h'
3+
18
4+
b'el'
5+
b'lo'
6+
31
7+
b''
8+
b''
9+
31
10+
b'hello'
11+
b'hello'
12+
ValueError
13+
OSError(22,)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
try:
2+
import zlib
3+
except ImportError:
4+
try:
5+
import zlib as zlib
6+
except ImportError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
PATTERNS = [
11+
# Packed results produced by CPy's zlib.compress()
12+
(b"0", b"x\x9c3\x00\x00\x001\x001"),
13+
(b"a", b"x\x9cK\x04\x00\x00b\x00b"),
14+
(b"0" * 100, b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc1"),
15+
(
16+
bytes(range(64)),
17+
b"x\x9cc`dbfaec\xe7\xe0\xe4\xe2\xe6\xe1\xe5\xe3\x17\x10\x14\x12\x16\x11\x15\x13\x97\x90\x94\x92\x96\x91\x95\x93WPTRVQUS\xd7\xd0\xd4\xd2\xd6\xd1\xd5\xd370426153\xb7\xb0\xb4\xb2\xb6\xb1\xb5\xb3\x07\x00\xaa\xe0\x07\xe1",
18+
),
19+
(b"hello", b"x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15"), # compression level 0
20+
# adaptive/dynamic huffman tree
21+
(
22+
b"13371813150|13764518736|12345678901",
23+
b"x\x9c\x05\xc1\x81\x01\x000\x04\x04\xb1\x95\\\x1f\xcfn\x86o\x82d\x06Qq\xc8\x9d\xc5X}<e\xb5g\x83\x0f\x89X\x07\xab",
24+
),
25+
# dynamic Huffman tree with "case 17" (repeat code for 3-10 times)
26+
(
27+
b">I}\x00\x951D>I}\x00\x951D>I}\x00\x951D>I}\x00\x951D",
28+
b"x\x9c\x05\xc11\x01\x00\x00\x00\x010\x95\x14py\x84\x12C_\x9bR\x8cV\x8a\xd1J1Z)F\x1fw`\x089",
29+
),
30+
]
31+
32+
for unpacked, packed in PATTERNS:
33+
assert zlib.decompress(packed) == unpacked
34+
print(unpacked)
35+
36+
37+
# Raw DEFLATE bitstream
38+
v = b"\xcbH\xcd\xc9\xc9\x07\x00"
39+
exp = b"hello"
40+
out = zlib.decompress(v, -15)
41+
assert out == exp
42+
print(exp)
43+
# Even when you ask CPython zlib.compress to produce Raw DEFLATE stream,
44+
# it returns it with adler2 and oriignal size appended, as if it was a
45+
# zlib stream. Make sure there're no random issues decompressing such.
46+
v = b"\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
47+
out = zlib.decompress(v, -15)
48+
assert out == exp
49+
50+
# this should error
51+
try:
52+
zlib.decompress(b"abc")
53+
except Exception:
54+
print("Exception")
55+
56+
# invalid block type
57+
try:
58+
zlib.decompress(b"\x07", -15) # final-block, block-type=3 (invalid)
59+
except Exception as er:
60+
print("Exception")

tests/unix/extra_coverage.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sys termios traceback ubinascii
3939
uctypes uerrno uheapq uio
4040
ujson ulab uos urandom
4141
ure uselect ustruct utime
42-
utimeq uzlib
42+
utimeq uzlib zlib
4343
ime
4444

4545
utime utimeq

0 commit comments

Comments
 (0)