# allocator tests # note for these to work there are a number constraints on the device geometry if = 'BLOCK_CYCLES == -1' # parallel allocation test [cases.test_alloc_parallel] defines.FILES = 3 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)' defines.GC = [false, true] defines.COMPACT_THRESH = ['-1', '0', 'BLOCK_SIZE/2'] defines.INFER_BC = [false, true] code = ''' const char *names[] = {"bacon", "eggs", "pancakes"}; lfs2_file_t files[FILES]; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_open(&lfs2, &files[n], path, LFS2_O_WRONLY | LFS2_O_CREAT | LFS2_O_APPEND) => 0; } for (int n = 0; n < FILES; n++) { if (GC) { lfs2_fs_gc(&lfs2) => 0; } size_t size = strlen(names[n]); for (lfs2_size_t i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[n], names[n], size) => size; } } for (int n = 0; n < FILES; n++) { lfs2_file_close(&lfs2, &files[n]) => 0; } lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (lfs2_size_t i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; ''' # serial allocation test [cases.test_alloc_serial] defines.FILES = 3 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)' defines.GC = [false, true] defines.COMPACT_THRESH = ['-1', '0', 'BLOCK_SIZE/2'] defines.INFER_BC = [false, true] code = ''' const char *names[] = {"bacon", "eggs", "pancakes"}; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; for (int n = 0; n < FILES; n++) { lfs2_mount(&lfs2, &cfg_) => 0; char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_WRONLY | LFS2_O_CREAT | LFS2_O_APPEND) => 0; size_t size = strlen(names[n]); uint8_t buffer[1024]; memcpy(buffer, names[n], size); for (int i = 0; i < SIZE; i += size) { if (GC) { lfs2_fs_gc(&lfs2) => 0; } lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; } lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (int i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; ''' # parallel allocation reuse test [cases.test_alloc_parallel_reuse] defines.FILES = 3 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)' defines.CYCLES = [1, 10] defines.INFER_BC = [false, true] code = ''' const char *names[] = {"bacon", "eggs", "pancakes"}; lfs2_file_t files[FILES]; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } for (int c = 0; c < CYCLES; c++) { lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_open(&lfs2, &files[n], path, LFS2_O_WRONLY | LFS2_O_CREAT | LFS2_O_APPEND) => 0; } for (int n = 0; n < FILES; n++) { size_t size = strlen(names[n]); for (int i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[n], names[n], size) => size; } } for (int n = 0; n < FILES; n++) { lfs2_file_close(&lfs2, &files[n]) => 0; } lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (int i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_remove(&lfs2, path) => 0; } lfs2_remove(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; } ''' # serial allocation reuse test [cases.test_alloc_serial_reuse] defines.FILES = 3 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)' defines.CYCLES = [1, 10] defines.INFER_BC = [false, true] code = ''' const char *names[] = {"bacon", "eggs", "pancakes"}; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } for (int c = 0; c < CYCLES; c++) { lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; for (int n = 0; n < FILES; n++) { lfs2_mount(&lfs2, &cfg_) => 0; char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_WRONLY | LFS2_O_CREAT | LFS2_O_APPEND) => 0; size_t size = strlen(names[n]); uint8_t buffer[1024]; memcpy(buffer, names[n], size); for (int i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; } lfs2_mount(&lfs2, cfg) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (int i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, cfg) => 0; for (int n = 0; n < FILES; n++) { char path[1024]; sprintf(path, "breakfast/%s", names[n]); lfs2_remove(&lfs2, path) => 0; } lfs2_remove(&lfs2, "breakfast") => 0; lfs2_unmount(&lfs2) => 0; } ''' # multiple handle allocation test # # this tests that multiple open handles to the same file don't clobber # each other [cases.test_alloc_multihandle] defines.FILES = 2 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)' defines.GC = [false, true] defines.COMPACT_THRESH = ['-1', '0', 'BLOCK_SIZE/2'] defines.INFER_BC = [false, true] defines.SYNC = [false, true] code = ''' const char *names[] = {"eggs", "spinach"}; lfs2_file_t files[FILES]; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; // write one file char path[1024]; sprintf(path, "breakfast/quiche"); lfs2_file_open(&lfs2, &files[0], path, LFS2_O_RDWR | LFS2_O_CREAT | LFS2_O_EXCL) => 0; if (GC) { lfs2_fs_gc(&lfs2) => 0; } size_t size = strlen(names[0]); for (lfs2_size_t i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[0], names[0], size) => size; } // sync? if (SYNC) { lfs2_file_sync(&lfs2, &files[0]) => 0; } // write the other file sprintf(path, "breakfast/quiche"); lfs2_file_open(&lfs2, &files[1], path, LFS2_O_RDWR | LFS2_O_CREAT | LFS2_O_TRUNC) => 0; if (GC) { lfs2_fs_gc(&lfs2) => 0; } size = strlen(names[1]); for (lfs2_size_t i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[1], names[1], size) => size; } // sync? if (SYNC) { lfs2_file_sync(&lfs2, &files[1]) => 0; } // try to read from both for (int n = 0; n < FILES; n++) { lfs2_file_rewind(&lfs2, &files[n]) => 0; size_t size = strlen(names[n]); for (lfs2_size_t i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &files[n], buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } } for (int n = 0; n < FILES; n++) { lfs2_file_close(&lfs2, &files[n]) => 0; } lfs2_unmount(&lfs2) => 0; // check after remounting lfs2_mount(&lfs2, &cfg_) => 0; { // last one wins int n = FILES-1; char path[1024]; sprintf(path, "breakfast/quiche"); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (lfs2_size_t i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; ''' # multiple handle allocation reuse test [cases.test_alloc_multihandle_reuse] defines.FILES = 2 defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / (FILES+1))' defines.CYCLES = [1, 10] defines.INFER_BC = [false, true] defines.SYNC = [false, true] code = ''' const char *names[] = {"eggs", "spinach"}; lfs2_file_t files[FILES]; lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_mkdir(&lfs2, "breakfast") => 0; // write one file char path[1024]; sprintf(path, "breakfast/quiche"); lfs2_file_open(&lfs2, &files[0], path, LFS2_O_RDWR | LFS2_O_CREAT | LFS2_O_EXCL) => 0; if (GC) { lfs2_fs_gc(&lfs2) => 0; } size_t size = strlen(names[0]); for (lfs2_size_t i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[0], names[0], size) => size; } // sync? if (SYNC) { lfs2_file_sync(&lfs2, &files[0]) => 0; } for (int c = 0; c < CYCLES; c++) { // write the other file sprintf(path, "breakfast/quiche"); lfs2_file_open(&lfs2, &files[1], path, LFS2_O_RDWR | LFS2_O_CREAT | LFS2_O_TRUNC) => 0; if (GC) { lfs2_fs_gc(&lfs2) => 0; } size = strlen(names[1]); for (lfs2_size_t i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &files[1], names[1], size) => size; } // sync? if (SYNC) { lfs2_file_sync(&lfs2, &files[1]) => 0; } // try to read from both for (int n = 0; n < FILES; n++) { lfs2_file_rewind(&lfs2, &files[n]) => 0; size_t size = strlen(names[n]); for (lfs2_size_t i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &files[n], buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } } lfs2_file_close(&lfs2, &files[1]) => 0; } lfs2_file_close(&lfs2, &files[0]) => 0; lfs2_unmount(&lfs2) => 0; // check after remounting lfs2_mount(&lfs2, &cfg_) => 0; { // last one wins int n = (SYNC) ? FILES-1 : 0; char path[1024]; sprintf(path, "breakfast/quiche"); lfs2_file_t file; lfs2_file_open(&lfs2, &file, path, LFS2_O_RDONLY) => 0; size_t size = strlen(names[n]); for (int i = 0; i < SIZE; i += size) { uint8_t buffer[1024]; lfs2_file_read(&lfs2, &file, buffer, size) => size; assert(memcmp(buffer, names[n], size) == 0); } lfs2_file_close(&lfs2, &file) => 0; } lfs2_unmount(&lfs2) => 0; ''' # exhaustion test [cases.test_alloc_exhaustion] defines.INFER_BC = [false, true] code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_file_t file; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); size_t size = strlen("exhaustion"); uint8_t buffer[1024]; memcpy(buffer, "exhaustion", size); lfs2_file_write(&lfs2, &file, buffer, size) => size; lfs2_file_sync(&lfs2, &file) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); lfs2_ssize_t res; while (true) { res = lfs2_file_write(&lfs2, &file, buffer, size); if (res < 0) { break; } res => size; } res => LFS2_ERR_NOSPC; // note that lfs2_fs_gc should not error here lfs2_fs_gc(&lfs2) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_RDONLY); size = strlen("exhaustion"); lfs2_file_size(&lfs2, &file) => size; lfs2_file_read(&lfs2, &file, buffer, size) => size; memcmp(buffer, "exhaustion", size) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; ''' # exhaustion wraparound test [cases.test_alloc_exhaustion_wraparound] defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-4)) / 3)' defines.INFER_BC = [false, true] code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; lfs2_file_t file; lfs2_file_open(&lfs2, &file, "padding", LFS2_O_WRONLY | LFS2_O_CREAT); size_t size = strlen("buffering"); uint8_t buffer[1024]; memcpy(buffer, "buffering", size); for (int i = 0; i < SIZE; i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_remove(&lfs2, "padding") => 0; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); size = strlen("exhaustion"); memcpy(buffer, "exhaustion", size); lfs2_file_write(&lfs2, &file, buffer, size) => size; lfs2_file_sync(&lfs2, &file) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); lfs2_ssize_t res; while (true) { res = lfs2_file_write(&lfs2, &file, buffer, size); if (res < 0) { break; } res => size; } res => LFS2_ERR_NOSPC; // note that lfs2_fs_gc should not error here lfs2_fs_gc(&lfs2) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, &cfg_) => 0; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_RDONLY); size = strlen("exhaustion"); lfs2_file_size(&lfs2, &file) => size; lfs2_file_read(&lfs2, &file, buffer, size) => size; memcmp(buffer, "exhaustion", size) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_remove(&lfs2, "exhaustion") => 0; lfs2_unmount(&lfs2) => 0; ''' # dir exhaustion test [cases.test_alloc_dir_exhaustion] defines.INFER_BC = [false, true] code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; struct lfs2_config cfg_ = *cfg; if (INFER_BC) { cfg_.block_count = 0; } lfs2_mount(&lfs2, &cfg_) => 0; // find out max file size lfs2_mkdir(&lfs2, "exhaustiondir") => 0; size_t size = strlen("blahblahblahblah"); uint8_t buffer[1024]; memcpy(buffer, "blahblahblahblah", size); lfs2_file_t file; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); int count = 0; int err; while (true) { err = lfs2_file_write(&lfs2, &file, buffer, size); if (err < 0) { break; } count += 1; } err => LFS2_ERR_NOSPC; // note that lfs2_fs_gc should not error here lfs2_fs_gc(&lfs2) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_remove(&lfs2, "exhaustion") => 0; lfs2_remove(&lfs2, "exhaustiondir") => 0; // see if dir fits with max file size lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); for (int i = 0; i < count; i++) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_mkdir(&lfs2, "exhaustiondir") => 0; lfs2_remove(&lfs2, "exhaustiondir") => 0; lfs2_remove(&lfs2, "exhaustion") => 0; // see if dir fits with > max file size lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); for (int i = 0; i < count+1; i++) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_mkdir(&lfs2, "exhaustiondir") => LFS2_ERR_NOSPC; lfs2_remove(&lfs2, "exhaustion") => 0; lfs2_unmount(&lfs2) => 0; ''' # what if we have a bad block during an allocation scan? [cases.test_alloc_bad_blocks] in = "lfs2.c" defines.ERASE_CYCLES = 0xffffffff defines.BADBLOCK_BEHAVIOR = 'LFS2_EMUBD_BADBLOCK_READERROR' code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; lfs2_mount(&lfs2, cfg) => 0; // first fill to exhaustion to find available space lfs2_file_t file; lfs2_file_open(&lfs2, &file, "pacman", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; uint8_t buffer[1024]; strcpy((char*)buffer, "waka"); size_t size = strlen("waka"); lfs2_size_t filesize = 0; while (true) { lfs2_ssize_t res = lfs2_file_write(&lfs2, &file, buffer, size); assert(res == (lfs2_ssize_t)size || res == LFS2_ERR_NOSPC); if (res == LFS2_ERR_NOSPC) { break; } filesize += size; } lfs2_file_close(&lfs2, &file) => 0; // now fill all but a couple of blocks of the filesystem with data filesize -= 3*BLOCK_SIZE; lfs2_file_open(&lfs2, &file, "pacman", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; strcpy((char*)buffer, "waka"); size = strlen("waka"); for (lfs2_size_t i = 0; i < filesize/size; i++) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // also save head of file so we can error during lookahead scan lfs2_block_t fileblock = file.ctz.head; lfs2_unmount(&lfs2) => 0; // remount to force an alloc scan lfs2_mount(&lfs2, cfg) => 0; // but mark the head of our file as a "bad block", this is force our // scan to bail early lfs2_emubd_setwear(cfg, fileblock, 0xffffffff) => 0; lfs2_file_open(&lfs2, &file, "ghost", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; strcpy((char*)buffer, "chomp"); size = strlen("chomp"); while (true) { lfs2_ssize_t res = lfs2_file_write(&lfs2, &file, buffer, size); assert(res == (lfs2_ssize_t)size || res == LFS2_ERR_CORRUPT); if (res == LFS2_ERR_CORRUPT) { break; } } lfs2_file_close(&lfs2, &file) => 0; // now reverse the "bad block" and try to write the file again until we // run out of space lfs2_emubd_setwear(cfg, fileblock, 0) => 0; lfs2_file_open(&lfs2, &file, "ghost", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; strcpy((char*)buffer, "chomp"); size = strlen("chomp"); while (true) { lfs2_ssize_t res = lfs2_file_write(&lfs2, &file, buffer, size); assert(res == (lfs2_ssize_t)size || res == LFS2_ERR_NOSPC); if (res == LFS2_ERR_NOSPC) { break; } } // note that lfs2_fs_gc should not error here lfs2_fs_gc(&lfs2) => 0; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; // check that the disk isn't hurt lfs2_mount(&lfs2, cfg) => 0; lfs2_file_open(&lfs2, &file, "pacman", LFS2_O_RDONLY) => 0; strcpy((char*)buffer, "waka"); size = strlen("waka"); for (lfs2_size_t i = 0; i < filesize/size; i++) { uint8_t rbuffer[4]; lfs2_file_read(&lfs2, &file, rbuffer, size) => size; assert(memcmp(rbuffer, buffer, size) == 0); } lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; ''' # Below, I don't like these tests. They're fragile and depend _heavily_ # on the geometry of the block device. But they are valuable. Eventually they # should be removed and replaced with generalized tests. # chained dir exhaustion test [cases.test_alloc_chained_dir_exhaustion] if = 'ERASE_SIZE == 512' defines.ERASE_COUNT = 1024 code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; lfs2_mount(&lfs2, cfg) => 0; // find out max file size lfs2_mkdir(&lfs2, "exhaustiondir") => 0; for (int i = 0; i < 10; i++) { char path[1024]; sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i); lfs2_mkdir(&lfs2, path) => 0; } size_t size = strlen("blahblahblahblah"); uint8_t buffer[1024]; memcpy(buffer, "blahblahblahblah", size); lfs2_file_t file; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); int count = 0; int err; while (true) { err = lfs2_file_write(&lfs2, &file, buffer, size); if (err < 0) { break; } count += 1; } err => LFS2_ERR_NOSPC; lfs2_file_close(&lfs2, &file) => 0; lfs2_remove(&lfs2, "exhaustion") => 0; lfs2_remove(&lfs2, "exhaustiondir") => 0; for (int i = 0; i < 10; i++) { char path[1024]; sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i); lfs2_remove(&lfs2, path) => 0; } // see that chained dir fails lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); for (int i = 0; i < count+1; i++) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_sync(&lfs2, &file) => 0; for (int i = 0; i < 10; i++) { char path[1024]; sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i); lfs2_mkdir(&lfs2, path) => 0; } lfs2_mkdir(&lfs2, "exhaustiondir") => LFS2_ERR_NOSPC; // shorten file to try a second chained dir while (true) { err = lfs2_mkdir(&lfs2, "exhaustiondir"); if (err != LFS2_ERR_NOSPC) { break; } lfs2_ssize_t filesize = lfs2_file_size(&lfs2, &file); filesize > 0 => true; lfs2_file_truncate(&lfs2, &file, filesize - size) => 0; lfs2_file_sync(&lfs2, &file) => 0; } err => 0; lfs2_mkdir(&lfs2, "exhaustiondir2") => LFS2_ERR_NOSPC; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; ''' # split dir test [cases.test_alloc_split_dir] if = 'ERASE_SIZE == 512' defines.ERASE_COUNT = 1024 code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; lfs2_mount(&lfs2, cfg) => 0; // create one block hole for half a directory lfs2_file_t file; lfs2_file_open(&lfs2, &file, "bump", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; for (lfs2_size_t i = 0; i < cfg->block_size; i += 2) { uint8_t buffer[1024]; memcpy(&buffer[i], "hi", 2); } uint8_t buffer[1024]; lfs2_file_write(&lfs2, &file, buffer, cfg->block_size) => cfg->block_size; lfs2_file_close(&lfs2, &file) => 0; lfs2_file_open(&lfs2, &file, "exhaustion", LFS2_O_WRONLY | LFS2_O_CREAT); size_t size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < (cfg->block_count-4)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // remount to force reset of lookahead lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, cfg) => 0; // open hole lfs2_remove(&lfs2, "bump") => 0; lfs2_mkdir(&lfs2, "splitdir") => 0; lfs2_file_open(&lfs2, &file, "splitdir/bump", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; for (lfs2_size_t i = 0; i < cfg->block_size; i += 2) { memcpy(&buffer[i], "hi", 2); } lfs2_file_write(&lfs2, &file, buffer, 2*cfg->block_size) => LFS2_ERR_NOSPC; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; ''' # outdated lookahead test [cases.test_alloc_outdated_lookahead] if = 'ERASE_SIZE == 512' defines.ERASE_COUNT = 1024 code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; lfs2_mount(&lfs2, cfg) => 0; // fill completely with two files lfs2_file_t file; lfs2_file_open(&lfs2, &file, "exhaustion1", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; size_t size = strlen("blahblahblahblah"); uint8_t buffer[1024]; memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_file_open(&lfs2, &file, "exhaustion2", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // remount to force reset of lookahead lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, cfg) => 0; // rewrite one file lfs2_file_open(&lfs2, &file, "exhaustion1", LFS2_O_WRONLY | LFS2_O_TRUNC) => 0; lfs2_file_sync(&lfs2, &file) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // rewrite second file, this requires lookahead does not // use old population lfs2_file_open(&lfs2, &file, "exhaustion2", LFS2_O_WRONLY | LFS2_O_TRUNC) => 0; lfs2_file_sync(&lfs2, &file) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; ''' # outdated lookahead and split dir test [cases.test_alloc_outdated_lookahead_split_dir] if = 'ERASE_SIZE == 512' defines.ERASE_COUNT = 1024 code = ''' lfs2_t lfs2; lfs2_format(&lfs2, cfg) => 0; lfs2_mount(&lfs2, cfg) => 0; // fill completely with two files lfs2_file_t file; lfs2_file_open(&lfs2, &file, "exhaustion1", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; size_t size = strlen("blahblahblahblah"); uint8_t buffer[1024]; memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; lfs2_file_open(&lfs2, &file, "exhaustion2", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // remount to force reset of lookahead lfs2_unmount(&lfs2) => 0; lfs2_mount(&lfs2, cfg) => 0; // rewrite one file with a hole of one block lfs2_file_open(&lfs2, &file, "exhaustion1", LFS2_O_WRONLY | LFS2_O_TRUNC) => 0; lfs2_file_sync(&lfs2, &file) => 0; size = strlen("blahblahblahblah"); memcpy(buffer, "blahblahblahblah", size); for (lfs2_size_t i = 0; i < ((cfg->block_count-2)/2 - 1)*(cfg->block_size-8); i += size) { lfs2_file_write(&lfs2, &file, buffer, size) => size; } lfs2_file_close(&lfs2, &file) => 0; // try to allocate a directory, should fail! lfs2_mkdir(&lfs2, "split") => LFS2_ERR_NOSPC; // file should not fail lfs2_file_open(&lfs2, &file, "notasplit", LFS2_O_WRONLY | LFS2_O_CREAT) => 0; lfs2_file_write(&lfs2, &file, "hi", 2) => 2; lfs2_file_close(&lfs2, &file) => 0; lfs2_unmount(&lfs2) => 0; '''