Skip to content

Commit 7c7dae4

Browse files
OmegaRelaykartben
authored andcommitted
tests: zms: add function to test zms handling of invalid inputs
The ZMS API should not unexpectedly crash the application; this commit adds tests to check if the ZMS API can safely handle and return -EINVAL if called with a NULL fs pointer. This commit also adds test to confirm the return value matches the documentation if user calls the ZMS API before ZMS is mounted Signed-off-by: Theis Mejnertsen <[email protected]>
1 parent aca602f commit 7c7dae4

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/subsys/fs/zms/src/main.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,62 @@ ZTEST_F(zms, test_zms_cache_hash_quality)
889889

890890
#endif
891891
}
892+
893+
ZTEST_F(zms, test_zms_input_validation)
894+
{
895+
int err;
896+
897+
err = zms_mount(NULL);
898+
zassert_true(err == -EINVAL, "zms_mount call with NULL fs failure: %d", err);
899+
900+
err = zms_clear(NULL);
901+
zassert_true(err == -EINVAL, "zms_clear call with NULL fs failure: %d", err);
902+
err = zms_clear(&fixture->fs);
903+
zassert_true(err == -EACCES, "zms_clear call before mount fs failure: %d", err);
904+
905+
err = zms_calc_free_space(NULL);
906+
zassert_true(err == -EINVAL, "zms_calc_free_space call with NULL fs failure: %d", err);
907+
err = zms_calc_free_space(&fixture->fs);
908+
zassert_true(err == -EACCES, "zms_calc_free_space call before mount fs failure: %d", err);
909+
910+
err = zms_active_sector_free_space(NULL);
911+
zassert_true(err == -EINVAL, "zms_active_sector_free_space call with NULL fs failure: %d",
912+
err);
913+
err = zms_active_sector_free_space(&fixture->fs);
914+
zassert_true(err == -EACCES, "zms_calc_free_space call before mount fs failure: %d", err);
915+
916+
err = zms_sector_use_next(NULL);
917+
zassert_true(err == -EINVAL, "zms_sector_use_next call with NULL fs failure: %d", err);
918+
err = zms_sector_use_next(&fixture->fs);
919+
zassert_true(err == -EACCES, "zms_sector_use_next call before mount fs failure: %d", err);
920+
921+
/* Read */
922+
err = zms_read(NULL, 0, NULL, 0);
923+
zassert_true(err == -EINVAL, "zms_read call with NULL fs failure: %d", err);
924+
err = zms_read(&fixture->fs, 0, NULL, 0);
925+
zassert_true(err == -EACCES, "zms_read call before mount fs failure: %d", err);
926+
927+
/* zms_read() and zms_get_data_length() are currently wrappers around zms_read_hist() but
928+
* add test here in case that is ever changed. Same is true for zms_delete() and zms_write()
929+
*/
930+
err = zms_read_hist(NULL, 0, NULL, 0, 0);
931+
zassert_true(err == -EINVAL, "zms_read_hist call with NULL fs failure: %d", err);
932+
err = zms_read_hist(&fixture->fs, 0, NULL, 0, 0);
933+
zassert_true(err == -EACCES, "zms_read_hist call before mount fs failure: %d", err);
934+
err = zms_get_data_length(NULL, 0);
935+
zassert_true(err == -EINVAL, "zms_get_data_length call with NULL fs failure: %d", err);
936+
err = zms_get_data_length(&fixture->fs, 0);
937+
zassert_true(err == -EACCES, "zms_get_data_length call before mount fs failure: %d", err);
938+
939+
/* Write */
940+
err = zms_write(NULL, 0, NULL, 0);
941+
zassert_true(err == -EINVAL, "zms_write call with NULL fs failure: %d", err);
942+
err = zms_write(&fixture->fs, 0, NULL, 0);
943+
zassert_true(err == -EACCES, "zms_write call before mount fs failure: %d", err);
944+
945+
/* Delete */
946+
err = zms_delete(NULL, 0);
947+
zassert_true(err == -EINVAL, "zms_delete call with NULL fs failure: %d", err);
948+
err = zms_delete(&fixture->fs, 0);
949+
zassert_true(err == -EACCES, "zms_delete call before mount fs failure: %d", err);
950+
}

0 commit comments

Comments
 (0)