2019-10-18 02:25:48 -04:00
|
|
|
# Test for VfsLittle using a RAM device, testing error handling from corrupt block device
|
|
|
|
|
|
|
|
try:
|
|
|
|
import uos
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
uos.VfsLfs1
|
|
|
|
uos.VfsLfs2
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
class RAMBlockDevice:
|
|
|
|
ERASE_BLOCK_SIZE = 1024
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
|
|
|
|
self.ret = 0
|
|
|
|
|
|
|
|
def readblocks(self, block, buf, off):
|
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[addr + i]
|
|
|
|
return self.ret
|
|
|
|
|
|
|
|
def writeblocks(self, block, buf, off):
|
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[addr + i] = buf[i]
|
|
|
|
return self.ret
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2020-03-22 22:26:08 -04:00
|
|
|
if op == 4: # block count
|
2019-10-18 02:25:48 -04:00
|
|
|
return len(self.data) // self.ERASE_BLOCK_SIZE
|
2020-03-22 22:26:08 -04:00
|
|
|
if op == 5: # block size
|
2019-10-18 02:25:48 -04:00
|
|
|
return self.ERASE_BLOCK_SIZE
|
2020-03-22 22:26:08 -04:00
|
|
|
if op == 6: # erase block
|
2019-10-18 02:25:48 -04:00
|
|
|
return 0
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
def corrupt(bdev, block):
|
|
|
|
addr = block * bdev.ERASE_BLOCK_SIZE
|
|
|
|
for i in range(bdev.ERASE_BLOCK_SIZE):
|
2020-03-22 22:26:08 -04:00
|
|
|
bdev.data[addr + i] = i & 0xFF
|
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
def create_vfs(bdev, vfs_class):
|
|
|
|
bdev.ret = 0
|
|
|
|
vfs_class.mkfs(bdev)
|
|
|
|
vfs = vfs_class(bdev)
|
2020-03-22 22:26:08 -04:00
|
|
|
with vfs.open("f", "w") as f:
|
2019-10-18 02:25:48 -04:00
|
|
|
for i in range(100):
|
2020-03-22 22:26:08 -04:00
|
|
|
f.write("test")
|
2019-10-18 02:25:48 -04:00
|
|
|
return vfs
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
def test(bdev, vfs_class):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("test", vfs_class)
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# statvfs
|
|
|
|
vfs = create_vfs(bdev, vfs_class)
|
|
|
|
corrupt(bdev, 0)
|
|
|
|
corrupt(bdev, 1)
|
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.statvfs(""))
|
2019-10-18 02:25:48 -04:00
|
|
|
except OSError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("statvfs OSError")
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# error during read
|
|
|
|
vfs = create_vfs(bdev, vfs_class)
|
2020-03-22 22:26:08 -04:00
|
|
|
f = vfs.open("f", "r")
|
|
|
|
bdev.ret = -5 # EIO
|
2019-10-18 02:25:48 -04:00
|
|
|
try:
|
|
|
|
f.read(10)
|
|
|
|
except OSError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("read OSError")
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# error during write
|
|
|
|
vfs = create_vfs(bdev, vfs_class)
|
2020-03-22 22:26:08 -04:00
|
|
|
f = vfs.open("f", "a")
|
|
|
|
bdev.ret = -5 # EIO
|
2019-10-18 02:25:48 -04:00
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
f.write("test")
|
2019-10-18 02:25:48 -04:00
|
|
|
except OSError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("write OSError")
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# error during close
|
|
|
|
vfs = create_vfs(bdev, vfs_class)
|
2020-03-22 22:26:08 -04:00
|
|
|
f = vfs.open("f", "w")
|
|
|
|
f.write("test")
|
|
|
|
bdev.ret = -5 # EIO
|
2019-10-18 02:25:48 -04:00
|
|
|
try:
|
|
|
|
f.close()
|
|
|
|
except OSError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("close OSError")
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# error during flush
|
|
|
|
vfs = create_vfs(bdev, vfs_class)
|
2020-03-22 22:26:08 -04:00
|
|
|
f = vfs.open("f", "w")
|
|
|
|
f.write("test")
|
|
|
|
bdev.ret = -5 # EIO
|
2019-10-18 02:25:48 -04:00
|
|
|
try:
|
|
|
|
f.flush()
|
|
|
|
except OSError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("flush OSError")
|
2019-10-18 02:25:48 -04:00
|
|
|
bdev.ret = 0
|
|
|
|
f.close()
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
bdev = RAMBlockDevice(30)
|
|
|
|
test(bdev, uos.VfsLfs1)
|
|
|
|
test(bdev, uos.VfsLfs2)
|