Files
littlefs/tests/test_paths.toml
T
Christopher Haster acad3a3143 Added format flags to lfsr_format
This is mainly to solve the weird check-hole where passing CKPROGS/
CKREADS as mount flags has no effect on lfsr_format (I mean, it'd be a
bit silly if it did somehow):

  LFS_F_RDWR              0  // Format the filesystem as read and write
  LFS_F_CKPROGS  0x00000010  // Check progs by reading back progged data
  LFS_F_CKREADS  0x00000020  // Check reads via parity bits/checksums

This makes lfsr_format a more cumbersome interface, but I don't know if
this is necessarily a bad thing. There's always risk of data loss when
calling lfsr_format, so maybe it should be a pain to call.

At the very least, format flags may be useful in the future for
enabling/disabling format-time things such as the planned block-map,
parity-tree, etc. Though it's unclear if such significant settings
should be format flags or somehow encoded as fields in our config
struct.

---

The LFS_F_* format flags of course ended up conflicting with our
internal LFS_F_* flags, so I renamed most of the internal flags to match
the closest flag set they participate in:

- LFS_F_TYPE        -> LFS_O_TYPE
- LFS_F_UNFLUSH     -> LFS_O_UNFLUSH
- LFS_F_UNSYNC      -> LFS_O_UNSYNC
- LFS_F_ORPHAN      -> LFS_O_ORPHAN
- LFS_F_ZOMBIE      -> LFS_O_ZOMBIE

- LFS_F_ORPHANS     -> LFS_I_ORPHANS
- LFS_F_UNCOMPACTED -> LFS_I_UNCOMPACTED

- LFS_F_TSTATE      -> LFS_T_TSTATE
- LFS_F_BTYPE       -> LFS_T_BTYPE
- LFS_F_DIRTY       -> LFS_T_DIRTY
- LFS_F_MUTATED     -> LFS_T_MUTATED

This may make it a bit less clear which flags are a part of the public
API, vs intended only for internal use, but at the very least our asserts
in format/mount/open/etc should catch most of these mistakes.

---

Code cost ended up being pretty minimal. Actually negative. This is the
second time we're _adding_ a feature that somehow saves code, though the
reality for this one is we're really just pushing constants up into the
user's stack frame. Still, it's a good indication the cost of format
flags is small:

           code          stack
  before: 36452           2680
  after:  36448 (-0.0%)   2680 (+0.0%)
2024-08-16 01:04:13 -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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, CFG) => 0;
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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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, LFS_F_RDWR, 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;
'''