summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/framebuf_bounds.py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-10-04 22:13:18 +1100
committerDamien George <damien@micropython.org>2023-10-16 12:17:51 +1100
commitd040478d8aad7a05aac98651ec6ac5c2e9265a22 (patch)
tree78d13c2ebea6c041c5a609813ee6c3c1f662c318 /tests/extmod/framebuf_bounds.py
parenta1be5e1439c9c3056d9073981c4301bb1c5e17be (diff)
downloadmicropython-d040478d8aad7a05aac98651ec6ac5c2e9265a22.tar.gz
micropython-d040478d8aad7a05aac98651ec6ac5c2e9265a22.zip
extmod/modframebuf: Validate FrameBuffer bounds against input buffer.
This ensures that the buffer is large enough for the specified width, height, bits-per-pixel, and stride. Also makes the legacy FrameBuffer1 constructor re-use the FrameBuffer make_new to save some code size. Fixes issue #12562. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/extmod/framebuf_bounds.py')
-rw-r--r--tests/extmod/framebuf_bounds.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/extmod/framebuf_bounds.py b/tests/extmod/framebuf_bounds.py
new file mode 100644
index 0000000000..3035b365bb
--- /dev/null
+++ b/tests/extmod/framebuf_bounds.py
@@ -0,0 +1,60 @@
+try:
+ import framebuf
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+def test_constructor(*args):
+ try:
+ framebuf.FrameBuffer(*args)
+ print("Valid")
+ except ValueError:
+ print("ValueError")
+
+
+print(framebuf.MONO_HLSB)
+test_constructor(bytearray(20), 10, 10, framebuf.MONO_HLSB)
+test_constructor(bytearray(20), -1, 10, framebuf.MONO_HLSB)
+test_constructor(bytearray(20), 10, -1, framebuf.MONO_HLSB)
+test_constructor(bytearray(20), 10, 10, framebuf.MONO_HLSB, 11)
+test_constructor(bytearray(20), 10, 10, framebuf.MONO_HLSB, 10)
+test_constructor(bytearray(20), 10, 10, framebuf.MONO_HLSB, 9)
+test_constructor(bytearray(20), 10, 10, framebuf.MONO_HLSB, -1)
+
+print(framebuf.MONO_VLSB)
+test_constructor(bytearray(8), 8, 1, framebuf.MONO_VLSB)
+test_constructor(bytearray(7), 8, 1, framebuf.MONO_VLSB)
+test_constructor(bytearray(8), 8, 8, framebuf.MONO_VLSB)
+
+for f in (framebuf.MONO_HLSB, framebuf.MONO_HMSB):
+ print(f)
+ test_constructor(bytearray(1), 8, 1, f)
+ test_constructor(bytearray(0), 8, 1, f)
+ test_constructor(bytearray(8), 8, 8, f)
+ test_constructor(bytearray(9), 8, 9, f)
+ test_constructor(bytearray(9), 9, 8, f)
+
+print(framebuf.GS2_HMSB)
+test_constructor(bytearray(8), 4, 8, framebuf.GS2_HMSB)
+test_constructor(bytearray(15), 5, 8, framebuf.GS2_HMSB)
+test_constructor(bytearray(16), 5, 8, framebuf.GS2_HMSB)
+test_constructor(bytearray(9), 4, 9, framebuf.GS2_HMSB)
+
+print(framebuf.GS4_HMSB)
+test_constructor(bytearray(8), 2, 8, framebuf.GS4_HMSB)
+test_constructor(bytearray(15), 3, 8, framebuf.GS4_HMSB)
+test_constructor(bytearray(16), 3, 8, framebuf.GS4_HMSB)
+test_constructor(bytearray(9), 2, 9, framebuf.GS4_HMSB)
+
+print(framebuf.GS8)
+test_constructor(bytearray(63), 8, 8, framebuf.GS8)
+test_constructor(bytearray(64), 8, 8, framebuf.GS8)
+test_constructor(bytearray(64), 9, 8, framebuf.GS8)
+test_constructor(bytearray(64), 8, 9, framebuf.GS8)
+
+print(framebuf.RGB565)
+test_constructor(bytearray(127), 8, 8, framebuf.RGB565)
+test_constructor(bytearray(128), 8, 8, framebuf.RGB565)
+test_constructor(bytearray(128), 9, 8, framebuf.RGB565)
+test_constructor(bytearray(128), 8, 9, framebuf.RGB565)