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