diff --git a/tests/subsys/fs/ext2/src/testfs_ext_specific.c b/tests/subsys/fs/ext2/src/testfs_ext_specific.c index 63367d7272..47863a987a 100644 --- a/tests/subsys/fs/ext2/src/testfs_ext_specific.c +++ b/tests/subsys/fs/ext2/src/testfs_ext_specific.c @@ -39,41 +39,17 @@ uint32_t calculate_blocks(uint32_t freeb, uint32_t B) /* TODO: revisit this and extend when 3rd level blocks will be possible */ } -void writing_test(struct ext2_cfg *config) +static void write_to_file(const char *file_path, uint32_t bytes_to_write) { int64_t ret = 0; struct fs_file_t file; - struct fs_statvfs sbuf; struct fs_dirent entry; - struct fs_mount_t *mp = &testfs_mnt; - static const char *file_path = "/sml/file"; - - ret = fs_mkfs(FS_EXT2, (uintptr_t)mp->storage_dev, config, 0); - zassert_equal(ret, 0, "Failed to mkfs with 2K blocks"); - - mp->flags = FS_MOUNT_FLAG_NO_FORMAT; - ret = fs_mount(mp); - zassert_equal(ret, 0, "Mount failed (ret=%d)", ret); fs_file_t_init(&file); ret = fs_open(&file, file_path, FS_O_RDWR | FS_O_CREATE); zassert_equal(ret, 0, "File open failed (ret=%d)", ret); - ret = fs_statvfs(mp->mnt_point, &sbuf); - zassert_equal(ret, 0, "Expected success (ret=%d)", ret); - - - /* Calculate how many numbers will be written (use all available memory) */ - uint32_t freeb = sbuf.f_bfree; - uint32_t bsize = sbuf.f_bsize; - uint32_t available_blocks = calculate_blocks(freeb, bsize / sizeof(uint32_t)); - - uint32_t bytes_to_write = bsize * available_blocks; - - TC_PRINT("Available blocks: %d\nBlock size: %d\nBytes_to_write: %d\n", - available_blocks, bsize, bytes_to_write); - - ret = testfs_write_incrementing(&file, 0, available_blocks * bsize); + ret = testfs_write_incrementing(&file, 0, bytes_to_write); zassert_equal(ret, bytes_to_write, "Different number of bytes written %ld (expected %ld)", ret, bytes_to_write); @@ -93,40 +69,121 @@ void writing_test(struct ext2_cfg *config) zassert_equal(ret, 0, "File open failed (ret=%d)", ret); - ret = testfs_verify_incrementing(&file, 0, available_blocks * bsize); + ret = testfs_verify_incrementing(&file, 0, bytes_to_write); zassert_equal(ret, bytes_to_write, "Different number of bytes read %ld (expected %ld)", ret, bytes_to_write); ret = fs_close(&file); zassert_equal(ret, 0, "File close failed (ret=%d)", ret); +} + +static void truncate_file(const char *file_path, uint32_t new_size) +{ + int64_t ret = 0; + struct fs_file_t file; + struct fs_dirent entry; + + fs_file_t_init(&file); + + TC_PRINT("Truncating to %d\n", new_size); + + ret = fs_open(&file, file_path, FS_O_RDWR); + zassert_equal(ret, 0, "File open failed (ret=%d)", ret); + + ret = fs_truncate(&file, new_size); + zassert_equal(ret, 0, "File truncate failed (ret=%d)", ret); + + ret = fs_stat(file_path, &entry); + zassert_equal(ret, 0, "File stat failed (ret=%d)", ret); + zassert_equal(entry.size, new_size, + "Wrong file size %d (expected %d)", entry.size, new_size); + + ret = fs_seek(&file, 0, FS_SEEK_SET); + zassert_equal(ret, 0, "File seek failed (ret=%d)", ret); + + ret = testfs_verify_incrementing(&file, 0, new_size); + zassert_equal(ret, new_size, "Different number of bytes read %ld (expected %ld)", + ret, new_size); + + ret = fs_close(&file); + zassert_equal(ret, 0, "File close failed (ret=%d)", ret); +} + +void writing_test(struct ext2_cfg *config) +{ + int64_t ret = 0; + struct fs_statvfs sbuf; + struct fs_mount_t *mp = &testfs_mnt; + static const char *file_path = "/sml/file"; + + ret = fs_mkfs(FS_EXT2, (uintptr_t)mp->storage_dev, config, 0); + zassert_equal(ret, 0, "Failed to mkfs with 2K blocks"); + + mp->flags = FS_MOUNT_FLAG_NO_FORMAT; + ret = fs_mount(mp); + zassert_equal(ret, 0, "Mount failed (ret=%d)", ret); + + ret = fs_statvfs(mp->mnt_point, &sbuf); + zassert_equal(ret, 0, "Expected success (ret=%d)", ret); + + /* Calculate how many numbers will be written (use all available memory) */ + uint32_t freeb = sbuf.f_bfree; + uint32_t bsize = sbuf.f_bsize; + uint32_t available_blocks = calculate_blocks(freeb, bsize / sizeof(uint32_t)); + + uint32_t bytes_to_write = bsize * available_blocks; + + TC_PRINT("Available blocks: %d\nBlock size: %d\nBytes_to_write: %d\n", + available_blocks, bsize, bytes_to_write); + + write_to_file(file_path, bytes_to_write); uint32_t new_size = bytes_to_write; while (new_size > 1) { new_size = new_size / 8 * 3; + truncate_file(file_path, new_size); + } - TC_PRINT("Truncating to %d\n", new_size); + ret = fs_unmount(mp); + zassert_equal(ret, 0, "Unmount failed (ret=%d)", ret); +} - ret = fs_open(&file, file_path, FS_O_RDWR); - zassert_equal(ret, 0, "File open failed (ret=%d)", ret); +ZTEST(ext2tests, test_indirect_block_removal) +{ + int64_t ret = 0; + struct fs_statvfs sbuf; + struct fs_mount_t *mp = &testfs_mnt; + static const char *file_path = "/sml/file"; - ret = fs_truncate(&file, new_size); - zassert_equal(ret, 0, "File truncate failed (ret=%d)", ret); + ret = fs_mkfs(FS_EXT2, (uintptr_t)mp->storage_dev, NULL, 0); + zassert_equal(ret, 0, "Failed to mkfs with 2K blocks"); - ret = fs_stat(file_path, &entry); - zassert_equal(ret, 0, "File stat failed (ret=%d)", ret); - zassert_equal(entry.size, new_size, - "Wrong file size %d (expected %d)", entry.size, new_size); + mp->flags = FS_MOUNT_FLAG_NO_FORMAT; + ret = fs_mount(mp); + zassert_equal(ret, 0, "Mount failed (ret=%d)", ret); - ret = fs_seek(&file, 0, FS_SEEK_SET); - zassert_equal(ret, 0, "File seek failed (ret=%d)", ret); + ret = fs_statvfs(mp->mnt_point, &sbuf); + zassert_equal(ret, 0, "Expected success (ret=%d)", ret); - ret = testfs_verify_incrementing(&file, 0, new_size); - zassert_equal(ret, new_size, "Different number of bytes read %ld (expected %ld)", - ret, new_size); + uint32_t bsize = sbuf.f_bsize; - ret = fs_close(&file); - zassert_equal(ret, 0, "File close failed (ret=%d)", ret); + uint32_t bytes_to_write = bsize * 13; + + TC_PRINT("Block size: %d\nBytes_to_write: %d\n", bsize, bytes_to_write); + + write_to_file(file_path, bytes_to_write); + + /* First truncate will remove all blocks under first indirect block. + * Second truncate will remove rest of blocks; + * it will check if first indirect block was removed properly. + */ + uint32_t sizes[] = {12 * bsize, 0}; + + for (int i = 0; i < ARRAY_SIZE(sizes); i++) { + uint32_t new_size = sizes[i]; + + truncate_file(file_path, new_size); } ret = fs_unmount(mp);