From cb20d999bc2d4f7e842f3e0b26e8fdc484acf82a Mon Sep 17 00:00:00 2001 From: Alex March Date: Thu, 13 Oct 2016 10:48:54 +0100 Subject: [PATCH] tests/extmod/vfs_fat: Improve VFS test coverage. Covered case: - Stat cases - Invalid read/write/flush/close - Invalid mkdir/rmdir/remove/getcwd - File seek/tell, modes a/x/+, t/b - Writing to a full disk - Full path rename, slash trim - Rename cases - Bytestring listdir - File object printing --- tests/extmod/vfs_fat_fileio.py | 160 ++++++++++++++++++++++++++++ tests/extmod/vfs_fat_fileio.py.exp | 23 ++++ tests/extmod/vfs_fat_ramdisk.py | 69 +++++------- tests/extmod/vfs_fat_ramdisk.py.exp | 14 +-- 4 files changed, 212 insertions(+), 54 deletions(-) create mode 100644 tests/extmod/vfs_fat_fileio.py create mode 100644 tests/extmod/vfs_fat_fileio.py.exp diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py new file mode 100644 index 0000000000..26fec78281 --- /dev/null +++ b/tests/extmod/vfs_fat_fileio.py @@ -0,0 +1,160 @@ +import sys +import uos +import uerrno +try: + uos.VfsFat +except AttributeError: + print("SKIP") + sys.exit() + + +class RAMFS: + + SEC_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.SEC_SIZE) + + def readblocks(self, n, buf): + #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + for i in range(len(buf)): + buf[i] = self.data[n * self.SEC_SIZE + i] + + def writeblocks(self, n, buf): + #print("writeblocks(%s, %x)" % (n, id(buf))) + for i in range(len(buf)): + self.data[n * self.SEC_SIZE + i] = buf[i] + + def ioctl(self, op, arg): + #print("ioctl(%d, %r)" % (op, arg)) + if op == 4: # BP_IOCTL_SEC_COUNT + return len(self.data) // self.SEC_SIZE + if op == 5: # BP_IOCTL_SEC_SIZE + return self.SEC_SIZE + + +try: + bdev = RAMFS(48) +except MemoryError: + print("SKIP") + sys.exit() + +uos.VfsFat.mkfs(bdev) +vfs = uos.VfsFat(bdev, "/ramdisk") + +# file IO +f = vfs.open("foo_file.txt", "w") +print(str(f)[:17], str(f)[-1:]) +f.write("hello!") +f.flush() +f.close() +try: + f.write("world!") +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.read() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.flush() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.close() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + vfs.open("foo_file.txt", "x") +except OSError as e: + print(e.args[0] == uerrno.EEXIST) + +with vfs.open("foo_file.txt", "a") as f: + f.write("world!") + +with vfs.open("foo_file.txt") as f2: + print(f2.read()) + print(f2.tell()) + + f2.seek(0, 0) # SEEK_SET + print(f2.read(1)) + + f2.seek(0, 1) # SEEK_CUR + print(f2.read(1)) + try: + f2.seek(1, 1) # SEEK_END + except OSError as e: + print(e.args[0] == uerrno.EOPNOTSUPP) + + f2.seek(-2, 2) # SEEK_END + print(f2.read(1)) + +# dirs +vfs.mkdir("foo_dir") + +try: + vfs.rmdir("foo_file.txt") +except OSError as e: + print(e.args[0] == 20) # uerrno.ENOTDIR + +try: + vfs.mkdir("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EEXIST) + +try: + vfs.remove("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EISDIR) + +try: + vfs.remove("no_file.txt") +except OSError as e: + print(e.args[0] == uerrno.ENOENT) + +try: + vfs.rename("foo_dir", "/null") +except OSError as e: + print(e.args[0] == uerrno.ENODEV) + +# file in dir +with vfs.open("foo_dir/file-in-dir.txt", "w+t") as f: + f.write("data in file") + +with vfs.open("foo_dir/file-in-dir.txt", "r+b") as f: + print(f.read()) + +with vfs.open("foo_dir/sub_file.txt", "w") as f: + f.write("subdir file") + +# directory not empty +try: + vfs.rmdir("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EACCES) + +# trim full path +vfs.rename("foo_dir/file-in-dir.txt", "/ramdisk/foo_dir/file.txt") +print(vfs.listdir("foo_dir")) + +vfs.rename("foo_dir/file.txt", "moved-to-root.txt") +print(vfs.listdir()) + +# valid removes +vfs.remove("foo_dir/sub_file.txt") +vfs.remove("foo_file.txt") +vfs.rmdir("foo_dir") +print(vfs.listdir()) + +# disk full +try: + bsize = vfs.statvfs("/ramdisk")[0] + free = vfs.statvfs("/ramdisk")[2] + 1 + f = vfs.open("large_file.txt", "wb") + f.write(bytearray(bsize * free)) +except OSError as e: + print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp new file mode 100644 index 0000000000..9f0edb31ef --- /dev/null +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -0,0 +1,23 @@ + +True +True +True +True +True +hello!world! +12 +h +e +True +d +True +True +True +True +True +b'data in file' +True +['sub_file.txt', 'file.txt'] +['foo_file.txt', 'foo_dir', 'moved-to-root.txt'] +['moved-to-root.txt'] +ENOSPC: True diff --git a/tests/extmod/vfs_fat_ramdisk.py b/tests/extmod/vfs_fat_ramdisk.py index 6380761c6d..184672ff15 100644 --- a/tests/extmod/vfs_fat_ramdisk.py +++ b/tests/extmod/vfs_fat_ramdisk.py @@ -45,55 +45,38 @@ print(b"FOO_FILETXT" not in bdev.data) print(b"hello!" not in bdev.data) vfs = uos.VfsFat(bdev, "/ramdisk") -print("statvfs:", vfs.statvfs("/ramdisk")) +try: + vfs.statvfs("/null") +except OSError as e: + print(e.args[0] == uerrno.ENODEV) + +print("statvfs:", vfs.statvfs("/ramdisk")) print("getcwd:", vfs.getcwd()) -f = vfs.open("foo_file.txt", "w") -f.write("hello!") -f.close() +try: + vfs.stat("no_file.txt") +except OSError as e: + print(e.args[0] == uerrno.ENOENT) -f2 = vfs.open("foo_file.txt") -print(f2.read()) -f2.close() +with vfs.open("foo_file.txt", "w") as f: + f.write("hello!") +print(vfs.listdir()) + +print("stat root:", vfs.stat("/")) +print("stat disk:", vfs.stat("/ramdisk/")) +print("stat file:", vfs.stat("foo_file.txt")) print(b"FOO_FILETXT" in bdev.data) print(b"hello!" in bdev.data) -print(vfs.listdir()) - -try: - vfs.rmdir("foo_file.txt") -except OSError as e: - print(e.args[0] == 20) # uerrno.ENOTDIR - -vfs.remove('foo_file.txt') -print(vfs.listdir()) - vfs.mkdir("foo_dir") -print(vfs.listdir()) - -try: - vfs.remove("foo_dir") -except OSError as e: - print(e.args[0] == uerrno.EISDIR) - -f = vfs.open("foo_dir/file-in-dir.txt", "w") -f.write("data in file") -f.close() - -print(vfs.listdir("foo_dir")) - -vfs.rename("foo_dir/file-in-dir.txt", "moved-to-root.txt") -print(vfs.listdir()) - vfs.chdir("foo_dir") print("getcwd:", vfs.getcwd()) print(vfs.listdir()) with vfs.open("sub_file.txt", "w") as f: - f.write("test2") -print(vfs.listdir()) + f.write("subdir file") try: vfs.chdir("sub_file.txt") @@ -103,20 +86,16 @@ except OSError as e: vfs.chdir("..") print("getcwd:", vfs.getcwd()) -try: - vfs.rmdir("foo_dir") -except OSError as e: - print(e.args[0] == uerrno.EACCES) - -vfs.remove("foo_dir/sub_file.txt") -vfs.rmdir("foo_dir") -print(vfs.listdir()) - vfs.umount() try: vfs.listdir() except OSError as e: print(e.args[0] == uerrno.ENODEV) +try: + vfs.getcwd() +except OSError as e: + print(e.args[0] == uerrno.ENODEV) + vfs = uos.VfsFat(bdev, "/ramdisk") -print(vfs.listdir()) +print(vfs.listdir(b"")) diff --git a/tests/extmod/vfs_fat_ramdisk.py.exp b/tests/extmod/vfs_fat_ramdisk.py.exp index 8a498b2fc4..eaf6371998 100644 --- a/tests/extmod/vfs_fat_ramdisk.py.exp +++ b/tests/extmod/vfs_fat_ramdisk.py.exp @@ -1,23 +1,19 @@ True True +True statvfs: (512, 512, 14, 14, 14, 0, 0, 0, 0, 255) getcwd: /ramdisk -hello! -True True ['foo_file.txt'] +stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +stat disk: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +stat file: (32768, 0, 0, 0, 0, 0, 6, -631238400, -631238400, -631238400) True -[] -['foo_dir'] True -['file-in-dir.txt'] -['foo_dir', 'moved-to-root.txt'] getcwd: /ramdisk/foo_dir [] -['sub_file.txt'] True getcwd: /ramdisk True -['moved-to-root.txt'] True -['moved-to-root.txt'] +[b'foo_file.txt', b'foo_dir']