circuitpython/tests/micropython/ringio.py
Jeff Epler b0175bb2f7 ringio: Detect incorrect constructor calls.
ringbuffer.size must be at least 2, and is a 16-bit quantity.

This fixes several cases including the one the fuzzer discovered,
which would lead to a fatal signal when accessing the object.

Closes: #17847
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-08-13 08:39:52 -05:00

66 lines
1.1 KiB
Python

# Check that micropython.RingIO works correctly.
import micropython
try:
micropython.RingIO
except AttributeError:
print("SKIP")
raise SystemExit
rb = micropython.RingIO(16)
print(rb)
print(rb.any())
rb.write(b"\x00")
print(rb.any())
rb.write(b"\x00")
print(rb.any())
print(rb.read(2))
print(rb.any())
rb.write(b"\x00\x01")
print(rb.read())
print(rb.read(1))
# Try to write more data than can fit at one go.
print(rb.write(b"\x00\x01" * 10))
print(rb.write(b"\x00"))
print(rb.read())
ba = bytearray(17)
rb = micropython.RingIO(ba)
print(rb)
print(rb.write(b"\x00\x01" * 10))
print(rb.write(b"\x00"))
print(rb.read())
try:
# Size must be int.
micropython.RingIO(None)
except TypeError as ex:
print(type(ex))
try:
# Buffer may not be empty
micropython.RingIO(bytearray(0))
except ValueError as ex:
print(type(ex))
try:
# Buffer may not be too small
micropython.RingIO(bytearray(1))
except ValueError as ex:
print(type(ex))
try:
# Size may not be too small
micropython.RingIO(0)
except ValueError as ex:
print(type(ex))