Skip to content

Commit 90723c8

Browse files
Maksymilian Goryszewskicfriedt
authored andcommitted
tests: posix: fs: Added fstat tests
Test posix fstat() function Signed-off-by: Maksymilian Goryszewski <[email protected]>
1 parent ba30c7b commit 90723c8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/posix/fs/src/test_fs_stat.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,60 @@ ZTEST(posix_fs_stat_test, test_fs_stat_dir)
105105
/* note: for posix compatibility should should actually work */
106106
zassert_not_equal(0, stat(TEST_ROOT, &buf));
107107
}
108+
109+
/**
110+
* @brief Test fstat command on file
111+
*
112+
* @details Tests file in root, file in directroy, and empty file
113+
*/
114+
ZTEST(posix_fs_stat_test, test_fs_fstat_file)
115+
{
116+
struct stat buf;
117+
118+
int test_file_fd = open(TEST_FILE, O_RDONLY);
119+
int dir_file_fd = open(TEST_DIR_FILE, O_RDONLY);
120+
int empty_file_fd = open(TEST_EMPTY_FILE, O_RDONLY);
121+
122+
zassert_not_equal(-1, test_file_fd);
123+
zassert_equal(0, fstat(test_file_fd, &buf));
124+
zassert_equal(TEST_FILE_SIZE, buf.st_size);
125+
zassert_equal(S_IFREG, buf.st_mode);
126+
close(test_file_fd);
127+
128+
zassert_not_equal(-1, dir_file_fd);
129+
zassert_equal(0, fstat(dir_file_fd, &buf));
130+
zassert_equal(TEST_DIR_FILE_SIZE, buf.st_size);
131+
zassert_equal(S_IFREG, buf.st_mode);
132+
close(dir_file_fd);
133+
134+
zassert_not_equal(-1, empty_file_fd);
135+
zassert_equal(0, fstat(empty_file_fd, &buf));
136+
zassert_equal(0, buf.st_size);
137+
zassert_equal(S_IFREG, buf.st_mode);
138+
close(empty_file_fd);
139+
}
140+
141+
/**
142+
* @brief Test fstat command on dir
143+
*
144+
* @details Tests if we can retrieve stastics for a directory.
145+
*/
146+
ZTEST(posix_fs_stat_test, test_fs_fstat_dir)
147+
{
148+
struct stat buf;
149+
150+
int fd = open(TEST_DIR, O_RDONLY);
151+
152+
/*
153+
* if this failed it means open doesn't support directories
154+
* so skip the rest of the test
155+
*/
156+
if (fd == -1) {
157+
ztest_test_skip();
158+
}
159+
160+
zassert_equal(0, fstat(fd, &buf));
161+
zassert_equal(0, buf.st_size);
162+
zassert_equal(S_IFDIR, buf.st_mode);
163+
close(fd);
164+
}

0 commit comments

Comments
 (0)