From ce4a0806b3d7ea63ec13fe60738211edf193d337 Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Mon, 21 Feb 2022 10:24:13 -0600 Subject: [PATCH] Added test coverage --- .../unix/variants/coverage/mpconfigvariant.mk | 9 ++- tests/circuitpython/zlib_decompio.py | 33 ++++++++++ tests/circuitpython/zlib_decompio.py.exp | 12 ++++ tests/circuitpython/zlib_decompio_gz.py | 60 +++++++++++++++++++ tests/circuitpython/zlib_decompio_gz.py.exp | 13 ++++ tests/circuitpython/zlib_decompress.py | 60 +++++++++++++++++++ tests/unix/extra_coverage.py.exp | 2 +- 7 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 tests/circuitpython/zlib_decompio.py create mode 100644 tests/circuitpython/zlib_decompio.py.exp create mode 100644 tests/circuitpython/zlib_decompio_gz.py create mode 100644 tests/circuitpython/zlib_decompio_gz.py.exp create mode 100644 tests/circuitpython/zlib_decompress.py diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 7e8aabef5b..681cc16e19 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -38,6 +38,8 @@ SRC_BITMAP := \ shared-bindings/rainbowio/__init__.c \ shared-bindings/traceback/__init__.c \ shared-bindings/util.c \ + shared-bindings/zlib/__init__.c \ + shared-bindings/zlib/DecompIO.c \ shared-module/aesio/aes.c \ shared-module/aesio/__init__.c \ shared-module/bitmaptools/__init__.c \ @@ -46,7 +48,9 @@ SRC_BITMAP := \ shared-module/displayio/ColorConverter.c \ shared-module/displayio/ColorConverter.c \ shared-module/rainbowio/__init__.c \ - shared-module/traceback/__init__.c + shared-module/traceback/__init__.c \ + shared-module/zlib/__init__.c \ + shared-module/zlib/DecompIO.c $(info $(SRC_BITMAP)) SRC_C += $(SRC_BITMAP) @@ -57,7 +61,8 @@ CFLAGS += \ -DCIRCUITPY_DISPLAYIO_UNIX=1 \ -DCIRCUITPY_GIFIO=1 \ -DCIRCUITPY_RAINBOWIO=1 \ - -DCIRCUITPY_TRACEBACK=1 + -DCIRCUITPY_TRACEBACK=1 \ + -DCIRCUITPY_ZLIB=1 SRC_C += coverage.c SRC_CXX += coveragecpp.cpp diff --git a/tests/circuitpython/zlib_decompio.py b/tests/circuitpython/zlib_decompio.py new file mode 100644 index 0000000000..0ded0b6966 --- /dev/null +++ b/tests/circuitpython/zlib_decompio.py @@ -0,0 +1,33 @@ +try: + import zlib + import uio as io +except ImportError: + print("SKIP") + raise SystemExit + + +# Raw DEFLATE bitstream +buf = io.BytesIO(b"\xcbH\xcd\xc9\xc9\x07\x00") +inp = zlib.DecompIO(buf, -8) +print(buf.seek(0, 1)) +print(inp.read(1)) +print(buf.seek(0, 1)) +print(inp.read(2)) +print(inp.read()) +print(buf.seek(0, 1)) +print(inp.read(1)) +print(inp.read()) +print(buf.seek(0, 1)) + + +# zlib bitstream +inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc1")) +print(inp.read(10)) +print(inp.read()) + +# zlib bitstream, wrong checksum +inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc0")) +try: + print(inp.read()) +except OSError as e: + print(repr(e)) diff --git a/tests/circuitpython/zlib_decompio.py.exp b/tests/circuitpython/zlib_decompio.py.exp new file mode 100644 index 0000000000..3f5f360fa3 --- /dev/null +++ b/tests/circuitpython/zlib_decompio.py.exp @@ -0,0 +1,12 @@ +0 +b'h' +2 +b'el' +b'lo' +7 +b'' +b'' +7 +b'0000000000' +b'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' +OSError(22,) diff --git a/tests/circuitpython/zlib_decompio_gz.py b/tests/circuitpython/zlib_decompio_gz.py new file mode 100644 index 0000000000..e752d50a84 --- /dev/null +++ b/tests/circuitpython/zlib_decompio_gz.py @@ -0,0 +1,60 @@ +try: + import zlib + import uio as io +except ImportError: + print("SKIP") + raise SystemExit + + +# gzip bitstream +buf = io.BytesIO( + b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00" +) +inp = zlib.DecompIO(buf, 16 + 8) +print(buf.seek(0, 1)) +print(inp.read(1)) +print(buf.seek(0, 1)) +print(inp.read(2)) +print(inp.read()) +print(buf.seek(0, 1)) +print(inp.read(1)) +print(inp.read()) +print(buf.seek(0, 1)) + +# Check FHCRC field +buf = io.BytesIO( + 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" +) +inp = zlib.DecompIO(buf, 16 + 8) +print(inp.read()) + +# Check FEXTRA field +buf = io.BytesIO( + 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" +) +inp = zlib.DecompIO(buf, 16 + 8) +print(inp.read()) + +# broken header +buf = io.BytesIO( + b"\x1f\x8c\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00" +) +try: + inp = zlib.DecompIO(buf, 16 + 8) +except ValueError: + print("ValueError") + +# broken crc32 +buf = io.BytesIO( + b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa7\x106\x05\x00\x00\x00" +) +inp = zlib.DecompIO(buf, 16 + 8) +try: + inp.read(6) +except OSError as e: + print(repr(e)) + +# broken uncompressed size - not checked so far +# 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') +# inp = zlib.DecompIO(buf, 16 + 8) +# inp.read(6) diff --git a/tests/circuitpython/zlib_decompio_gz.py.exp b/tests/circuitpython/zlib_decompio_gz.py.exp new file mode 100644 index 0000000000..20a30c82a3 --- /dev/null +++ b/tests/circuitpython/zlib_decompio_gz.py.exp @@ -0,0 +1,13 @@ +16 +b'h' +18 +b'el' +b'lo' +31 +b'' +b'' +31 +b'hello' +b'hello' +ValueError +OSError(22,) diff --git a/tests/circuitpython/zlib_decompress.py b/tests/circuitpython/zlib_decompress.py new file mode 100644 index 0000000000..05ea5868ee --- /dev/null +++ b/tests/circuitpython/zlib_decompress.py @@ -0,0 +1,60 @@ +try: + import zlib +except ImportError: + try: + import zlib as zlib + except ImportError: + print("SKIP") + raise SystemExit + +PATTERNS = [ + # Packed results produced by CPy's zlib.compress() + (b"0", b"x\x9c3\x00\x00\x001\x001"), + (b"a", b"x\x9cK\x04\x00\x00b\x00b"), + (b"0" * 100, b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc1"), + ( + bytes(range(64)), + 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", + ), + (b"hello", b"x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15"), # compression level 0 + # adaptive/dynamic huffman tree + ( + b"13371813150|13764518736|12345678901", + b"x\x9c\x05\xc1\x81\x01\x000\x04\x04\xb1\x95\\\x1f\xcfn\x86o\x82d\x06Qq\xc8\x9d\xc5X}I}\x00\x951D>I}\x00\x951D>I}\x00\x951D>I}\x00\x951D", + b"x\x9c\x05\xc11\x01\x00\x00\x00\x010\x95\x14py\x84\x12C_\x9bR\x8cV\x8a\xd1J1Z)F\x1fw`\x089", + ), +] + +for unpacked, packed in PATTERNS: + assert zlib.decompress(packed) == unpacked + print(unpacked) + + +# Raw DEFLATE bitstream +v = b"\xcbH\xcd\xc9\xc9\x07\x00" +exp = b"hello" +out = zlib.decompress(v, -15) +assert out == exp +print(exp) +# Even when you ask CPython zlib.compress to produce Raw DEFLATE stream, +# it returns it with adler2 and oriignal size appended, as if it was a +# zlib stream. Make sure there're no random issues decompressing such. +v = b"\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00" +out = zlib.decompress(v, -15) +assert out == exp + +# this should error +try: + zlib.decompress(b"abc") +except Exception: + print("Exception") + +# invalid block type +try: + zlib.decompress(b"\x07", -15) # final-block, block-type=3 (invalid) +except Exception as er: + print("Exception") diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4cce80a412..ea440166ca 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -39,7 +39,7 @@ sys termios traceback ubinascii uctypes uerrno uheapq uio ujson ulab uos urandom ure uselect ustruct utime -utimeq uzlib +utimeq uzlib zlib ime utime utimeq