Ver código fonte

Add a unit test; currently hanging on final permutation.

Some block-device bound-checks are disabled during superblock search.
Brian Pugh 2 anos atrás
pai
commit
df238ebac6
3 arquivos alterados com 22 adições e 7 exclusões
  1. 3 3
      bd/lfs_emubd.c
  2. 4 4
      lfs.c
  3. 15 0
      tests/test_superblocks.toml

+ 3 - 3
bd/lfs_emubd.c

@@ -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);
@@ -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);
@@ -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]);

+ 4 - 4
lfs.c

@@ -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;
     }
 
@@ -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(
@@ -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;

+ 15 - 0
tests/test_superblocks.toml

@@ -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 = '''