Skip to content

Commit

Permalink
Add a unit test; currently hanging on final permutation.
Browse files Browse the repository at this point in the history
Some block-device bound-checks are disabled during superblock search.
  • Loading branch information
BrianPugh committed Aug 17, 2023
1 parent be68122 commit df238eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions bd/lfs_emubd.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
lfs_emubd_t *bd = cfg->context;

// check if read is valid
LFS_ASSERT(block < cfg->block_count);
LFS_ASSERT(!cfg->block_count || block < cfg->block_count);
LFS_ASSERT(off % cfg->read_size == 0);
LFS_ASSERT(size % cfg->read_size == 0);
LFS_ASSERT(off+size <= cfg->block_size);
Expand Down Expand Up @@ -287,7 +287,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
lfs_emubd_t *bd = cfg->context;

// check if write is valid
LFS_ASSERT(block < cfg->block_count);
LFS_ASSERT(!cfg->block_count || block < cfg->block_count);
LFS_ASSERT(off % cfg->prog_size == 0);
LFS_ASSERT(size % cfg->prog_size == 0);
LFS_ASSERT(off+size <= cfg->block_size);
Expand Down Expand Up @@ -376,7 +376,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
lfs_emubd_t *bd = cfg->context;

// check if erase is valid
LFS_ASSERT(block < cfg->block_count);
LFS_ASSERT(!cfg->block_count || block < cfg->block_count);

// get the block
lfs_emubd_block_t *b = lfs_emubd_mutblock(cfg, &bd->blocks[block]);
Expand Down
8 changes: 4 additions & 4 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ static int lfs_bd_read(lfs_t *lfs,
lfs_block_t block, lfs_off_t off,
void *buffer, lfs_size_t size) {
uint8_t *data = buffer;
if (block >= lfs->block_count ||
off+size > lfs->cfg->block_size) {
if (lfs->block_count &&
(block >= lfs->block_count || off+size > lfs->cfg->block_size)) {
return LFS_ERR_CORRUPT;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ static int lfs_bd_read(lfs_t *lfs,
}

// load to cache, first condition can no longer fail
LFS_ASSERT(block < lfs->block_count);
LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
rcache->block = block;
rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
rcache->size = lfs_min(
Expand Down Expand Up @@ -4389,7 +4389,7 @@ static int lfs_validate_superblock(lfs_t *lfs, lfs_superblock_t *superblock){
lfs->attr_max = superblock->attr_max;
}

if (superblock->block_count != lfs->cfg->block_count) {
if (lfs->cfg->block_count && superblock->block_count != lfs->cfg->block_count) {
LFS_ERROR("Invalid block count (%"PRIu32" != %"PRIu32")",
superblock->block_count, lfs->cfg->block_count);
return LFS_ERR_INVAL;
Expand Down
15 changes: 15 additions & 0 deletions tests/test_superblocks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ code = '''
lfs_format(&lfs, cfg) => 0;
'''

# tests formatting from interpretting a previous superblock
[cases.test_superblocks_format_unknown_block_count]
code = '''
lfs_t lfs;
lfs_format(&lfs, cfg) => 0;
assert(lfs.block_count == cfg->block_count);

memset(&lfs, 0, sizeof(lfs));
struct lfs_config tweaked_cfg = *cfg;
tweaked_cfg.block_count = 0;
lfs_format(&lfs, &tweaked_cfg) => 0;
assert(lfs.block_count == cfg->block_count);
'''


# mount/unmount
[cases.test_superblocks_mount]
code = '''
Expand Down

0 comments on commit df238eb

Please sign in to comment.