2019-10-18 07:25:48 +01:00
|
|
|
# Test for VfsLittle using a RAM device, testing error handling
|
|
|
|
|
|
|
|
try:
|
2022-08-18 07:57:45 +01:00
|
|
|
import os
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2022-08-18 07:57:45 +01:00
|
|
|
os.VfsLfs1
|
|
|
|
os.VfsLfs2
|
2019-10-18 07:25:48 +01:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
class RAMBlockDevice:
|
|
|
|
ERASE_BLOCK_SIZE = 1024
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
|
|
|
if op == 4: # block count
|
|
|
|
return len(self.data) // self.ERASE_BLOCK_SIZE
|
|
|
|
if op == 5: # block size
|
|
|
|
return self.ERASE_BLOCK_SIZE
|
|
|
|
if op == 6: # erase block
|
|
|
|
return 0
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
def test(bdev, vfs_class):
|
|
|
|
print("test", vfs_class)
|
|
|
|
|
|
|
|
# mkfs with too-small block device
|
|
|
|
try:
|
|
|
|
vfs_class.mkfs(RAMBlockDevice(1))
|
|
|
|
except OSError:
|
|
|
|
print("mkfs OSError")
|
|
|
|
|
|
|
|
# mount with invalid filesystem
|
|
|
|
try:
|
|
|
|
vfs_class(bdev)
|
|
|
|
except OSError:
|
|
|
|
print("mount OSError")
|
|
|
|
|
|
|
|
# set up for following tests
|
|
|
|
vfs_class.mkfs(bdev)
|
|
|
|
vfs = vfs_class(bdev)
|
|
|
|
with vfs.open("testfile", "w") as f:
|
|
|
|
f.write("test")
|
|
|
|
vfs.mkdir("testdir")
|
|
|
|
|
|
|
|
# ilistdir
|
|
|
|
try:
|
|
|
|
vfs.ilistdir("noexist")
|
|
|
|
except OSError:
|
|
|
|
print("ilistdir OSError")
|
|
|
|
|
|
|
|
# remove
|
|
|
|
try:
|
|
|
|
vfs.remove("noexist")
|
|
|
|
except OSError:
|
|
|
|
print("remove OSError")
|
|
|
|
|
|
|
|
# rmdir
|
|
|
|
try:
|
|
|
|
vfs.rmdir("noexist")
|
|
|
|
except OSError:
|
|
|
|
print("rmdir OSError")
|
|
|
|
|
|
|
|
# rename
|
|
|
|
try:
|
|
|
|
vfs.rename("noexist", "somethingelse")
|
|
|
|
except OSError:
|
|
|
|
print("rename OSError")
|
|
|
|
|
|
|
|
# mkdir
|
|
|
|
try:
|
|
|
|
vfs.mkdir("testdir")
|
|
|
|
except OSError:
|
|
|
|
print("mkdir OSError")
|
|
|
|
|
|
|
|
# chdir to nonexistent
|
|
|
|
try:
|
|
|
|
vfs.chdir("noexist")
|
|
|
|
except OSError:
|
|
|
|
print("chdir OSError")
|
|
|
|
print(vfs.getcwd()) # check still at root
|
|
|
|
|
|
|
|
# chdir to file
|
|
|
|
try:
|
|
|
|
vfs.chdir("testfile")
|
|
|
|
except OSError:
|
|
|
|
print("chdir OSError")
|
|
|
|
print(vfs.getcwd()) # check still at root
|
|
|
|
|
|
|
|
# stat
|
|
|
|
try:
|
|
|
|
vfs.stat("noexist")
|
|
|
|
except OSError:
|
|
|
|
print("stat OSError")
|
|
|
|
|
|
|
|
# error during seek
|
|
|
|
with vfs.open("testfile", "r") as f:
|
2019-12-13 04:38:45 +00:00
|
|
|
f.seek(1 << 30) # SEEK_SET
|
2019-10-18 07:25:48 +01:00
|
|
|
try:
|
2019-12-13 04:38:45 +00:00
|
|
|
f.seek(1 << 30, 1) # SEEK_CUR
|
2019-10-18 07:25:48 +01:00
|
|
|
except OSError:
|
|
|
|
print("seek OSError")
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
|
|
|
|
bdev = RAMBlockDevice(30)
|
2022-08-18 07:57:45 +01:00
|
|
|
test(bdev, os.VfsLfs1)
|
|
|
|
test(bdev, os.VfsLfs2)
|