Files
littlefs/tests/test_paths.toml
T
Christopher Haster acfae9e072 Extended lfsr_mount to accept mount flags
This has been a long-time coming, mount flags are just too useful for
configuring a filesystem at runtime.

Currently this is limited to LFS_M_RDONLY and LFS_M_CKPROGS, but there
are a few more planned in the future:

  LFS_M_RDWR     = 0x0000, // Mount the filesystem as read and write
  LFS_M_RDONLY   = 0x0001, // Mount the filesystem as readonly
  LFS_M_STRICT*  = 0x0002, // Error if on-disk config does not match
  LFS_M_FORCE*   = 0x0004, // Ignore compat flags, mount readonly
  LFS_M_FORCEWITHRECKLESSABANDON*
                 = 0x0008, // Ignore compat flags, mount read write

  LFS_M_CKPROGS  = 0x0010, // Check progs by reading back progged data
  LFS_M_CKREADS* = 0x0020, // Check reads via checksums

  * Hypothetical

As a convenience, we also return mount flags in the struct lfs_fsinfo's
flags field as their relevant LFS_I_* variants. Though only to match
statvfs, and only because it's cheap, littlefs's API is low-level and we
should expect users to know what flags they passed to lfsr_mount.

As for the new mount flags:

- LFS_M_RDONLY - For consistency with existing APIs, this just asserts
  on write operations, which makes it a bit useless... But the info flag
  LFS_I_RDONLY may be useful for falling back to a readonly mode if
  we encounter on-disk compat issues.

  At least if implement the theoretical LFS_UNTRUSTED_USER mode
  LFS_M_RDONLY could become a runtime error.

- LFS_M_RDWR - This really just exists to compliment LFS_M_RDONLY and to
  match LFS_O_RDONLY/LFS_O_RDWR. It's just an alias for 0, and I don't
  think there will ever be a reason to make it non-0 (but I can always
  be wrong!).

- LFS_M_CKPROGS - This replaces the check_progs config option and avoids
  using a full byte to store a bool.

  We should probably also have a compile-time option to compile this out
  (LFS_NO_CKPROGS?), but that's a future thing to do.

This ended up adding a surprising bit of code, considering we're just
moving flags around, and noise in lfs_alloc added a bit of stack again:

           code          stack
  before: 35880           2672
  after:  35932 (+0.1%)   2680 (+0.3%)
2024-07-17 20:39:31 -05:00

327 lines
10 KiB
TOML

# Test various path-related corner cases
after = 'test_dirs'
# simple path test
[cases.test_paths_normal]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "/tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_mkdir(&lfs, "/milk") => 0;
lfsr_stat(&lfs, "/milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_stat(&lfs, "milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_unmount(&lfs) => 0;
'''
# redundant slashes
[cases.test_paths_redundant_slashes]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "/tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "//tea//green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "///tea///green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_mkdir(&lfs, "////milk") => 0;
lfsr_stat(&lfs, "////milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_stat(&lfs, "milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_unmount(&lfs) => 0;
'''
# dot path test
[cases.test_paths_dot]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "./tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "/./tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "/././tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "/./tea/./green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_mkdir(&lfs, "/./milk") => 0;
lfsr_stat(&lfs, "/./milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_stat(&lfs, "milk", &info) => 0;
assert(strcmp(info.name, "milk") == 0);
lfsr_unmount(&lfs) => 0;
'''
# dot dot path test
[cases.test_paths_dot_dot]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
lfsr_mkdir(&lfs, "coffee") => 0;
lfsr_mkdir(&lfs, "coffee/drip") => 0;
lfsr_mkdir(&lfs, "coffee/espresso") => 0;
lfsr_mkdir(&lfs, "coffee/coldbrew") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "coffee/../tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "tea/black/../green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "coffee/coldbrew/../../tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "coffee/../coffee/../tea/green", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_mkdir(&lfs, "coffee/../milk") => 0;
lfsr_stat(&lfs, "coffee/../milk", &info) => 0;
strcmp(info.name, "milk") => 0;
lfsr_stat(&lfs, "milk", &info) => 0;
strcmp(info.name, "milk") => 0;
lfsr_unmount(&lfs) => 0;
'''
# trailing dot path test
[cases.test_paths_trailing_dot]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "tea/green/", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "tea/green/.", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "tea/green/./.", &info) => 0;
assert(strcmp(info.name, "green") == 0);
lfsr_stat(&lfs, "tea/green/..", &info) => 0;
assert(strcmp(info.name, "tea") == 0);
lfsr_stat(&lfs, "tea/green/../.", &info) => 0;
assert(strcmp(info.name, "tea") == 0);
lfsr_unmount(&lfs) => 0;
'''
# leading dot path test
[cases.test_paths_leading_dot]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, ".milk") => 0;
struct lfs_info info;
lfsr_stat(&lfs, ".milk", &info) => 0;
strcmp(info.name, ".milk") => 0;
lfsr_stat(&lfs, "tea/.././.milk", &info) => 0;
strcmp(info.name, ".milk") => 0;
lfsr_unmount(&lfs) => 0;
'''
# root dot dot path test
[cases.test_paths_root_dot_dot]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "tea") => 0;
lfsr_mkdir(&lfs, "tea/green") => 0;
lfsr_mkdir(&lfs, "tea/oolong") => 0;
lfsr_mkdir(&lfs, "tea/black") => 0;
lfsr_mkdir(&lfs, "coffee") => 0;
lfsr_mkdir(&lfs, "coffee/drip") => 0;
lfsr_mkdir(&lfs, "coffee/espresso") => 0;
lfsr_mkdir(&lfs, "coffee/coldbrew") => 0;
struct lfs_info info;
lfsr_stat(&lfs, "coffee/../../../../../../tea/green", &info) => 0;
strcmp(info.name, "green") => 0;
lfsr_mkdir(&lfs, "coffee/../../../../../../milk") => 0;
lfsr_stat(&lfs, "coffee/../../../../../../milk", &info) => 0;
strcmp(info.name, "milk") => 0;
lfsr_stat(&lfs, "milk", &info) => 0;
strcmp(info.name, "milk") => 0;
lfsr_unmount(&lfs) => 0;
'''
# invalid path tests
[cases.test_paths_invalid]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG);
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
struct lfs_info info;
lfsr_stat(&lfs, "milk", &info) => LFS_ERR_NOENT;
lfsr_stat(&lfs, "milk/cream", &info) => LFS_ERR_NOENT;
lfsr_stat(&lfs, "milk/cream/foam", &info) => LFS_ERR_NOENT;
lfsr_remove(&lfs, "milk") => LFS_ERR_NOENT;
lfsr_remove(&lfs, "milk/cream") => LFS_ERR_NOENT;
lfsr_remove(&lfs, "milk/cream/foam") => LFS_ERR_NOENT;
lfsr_mkdir(&lfs, "milk/cream") => LFS_ERR_NOENT;
lfsr_file_t file;
lfsr_file_open(&lfs, &file, "milk/cream",
LFS_O_WRONLY | LFS_O_CREAT) => LFS_ERR_NOENT;
lfsr_mkdir(&lfs, "milk/cream/foam") => LFS_ERR_NOENT;
lfsr_file_open(&lfs, &file, "milk/cream/foam",
LFS_O_WRONLY | LFS_O_CREAT) => LFS_ERR_NOENT;
lfsr_unmount(&lfs) => 0;
'''
# root operations
[cases.test_paths_root]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
struct lfs_info info;
lfsr_stat(&lfs, "/", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_mkdir(&lfs, "/") => LFS_ERR_EXIST;
lfsr_file_t file;
lfsr_file_open(&lfs, &file, "/",
LFS_O_WRONLY | LFS_O_CREAT) => LFS_ERR_ISDIR;
lfsr_remove(&lfs, "/") => LFS_ERR_INVAL;
lfsr_unmount(&lfs) => 0;
'''
# root representations
[cases.test_paths_root_reprs]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
struct lfs_info info;
lfsr_stat(&lfs, "/", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_stat(&lfs, "", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_stat(&lfs, ".", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_stat(&lfs, "..", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_stat(&lfs, "//", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_stat(&lfs, "./", &info) => 0;
assert(strcmp(info.name, "/") == 0);
assert(info.type == LFS_TYPE_DIR);
assert(info.size == 0);
lfsr_unmount(&lfs) => 0;
'''
# max path test
[cases.test_paths_max]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "coffee") => 0;
lfsr_mkdir(&lfs, "coffee/drip") => 0;
lfsr_mkdir(&lfs, "coffee/espresso") => 0;
lfsr_mkdir(&lfs, "coffee/coldbrew") => 0;
char path[2*LFS_NAME_MAX+2];
memset(path, 'w', LFS_NAME_MAX+1);
path[LFS_NAME_MAX+1] = '\0';
lfsr_mkdir(&lfs, path) => LFS_ERR_NAMETOOLONG;
lfsr_file_t file;
lfsr_file_open(&lfs, &file, path,
LFS_O_WRONLY | LFS_O_CREAT) => LFS_ERR_NAMETOOLONG;
memcpy(path, "coffee/", strlen("coffee/"));
memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX+1);
path[strlen("coffee/")+LFS_NAME_MAX+1] = '\0';
lfsr_mkdir(&lfs, path) => LFS_ERR_NAMETOOLONG;
lfsr_file_open(&lfs, &file, path,
LFS_O_WRONLY | LFS_O_CREAT) => LFS_ERR_NAMETOOLONG;
lfsr_unmount(&lfs) => 0;
'''
# really big path test
[cases.test_paths_really_big]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_mkdir(&lfs, "coffee") => 0;
lfsr_mkdir(&lfs, "coffee/drip") => 0;
lfsr_mkdir(&lfs, "coffee/espresso") => 0;
lfsr_mkdir(&lfs, "coffee/coldbrew") => 0;
char path[2*LFS_NAME_MAX+2];
memset(path, 'w', LFS_NAME_MAX);
path[LFS_NAME_MAX] = '\0';
lfsr_mkdir(&lfs, path) => 0;
lfsr_remove(&lfs, path) => 0;
lfsr_file_t file;
lfsr_file_open(&lfs, &file, path,
LFS_O_WRONLY | LFS_O_CREAT) => 0;
lfsr_file_close(&lfs, &file) => 0;
lfsr_remove(&lfs, path) => 0;
memcpy(path, "coffee/", strlen("coffee/"));
memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX);
path[strlen("coffee/")+LFS_NAME_MAX] = '\0';
lfsr_mkdir(&lfs, path) => 0;
lfsr_remove(&lfs, path) => 0;
lfsr_file_open(&lfs, &file, path,
LFS_O_WRONLY | LFS_O_CREAT) => 0;
lfsr_file_close(&lfs, &file) => 0;
lfsr_remove(&lfs, path) => 0;
lfsr_unmount(&lfs) => 0;
'''