#include #include #include #include #include #include #include #include "ucore/read_file.h" START_TEST (test_read_file_non_existing_file) { char *content; size_t len; int saved_errno; errno = 0; content = uc_read_file("non existing file 123765", &len, 1000000000); saved_errno = errno; ck_assert_ptr_null(content); ck_assert_int_ne(saved_errno, 0); } END_TEST START_TEST (test_read_file_simple) { char *content; const char *filename = "test_read_file_simple"; size_t len; ssize_t rc; int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); ck_assert_int_ne(fd, -1); rc = write(fd, "hello", 5); ck_assert_int_eq(rc, 5); close(fd); content = uc_read_file(filename, &len, 1000000000); unlink(filename); ck_assert_ptr_nonnull(content); ck_assert_uint_eq(len, 5); ck_assert_uint_eq(strlen(content), 5); ck_assert_str_eq("hello", content); free(content); } END_TEST START_TEST (test_read_file_greater_than_max) { char *content; const char *filename = "test_read_file_simple"; size_t len; ssize_t rc; int saved_errno; int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); fail_if(fd == -1); rc = write(fd, "hello", 5); fail_if(rc != 5); close(fd); content = uc_read_file(filename, &len, 4); saved_errno = errno; unlink(filename); ck_assert_ptr_null(content); ck_assert_int_eq(saved_errno, EMSGSIZE); } END_TEST START_TEST (test_read_file_boundary) { char *content; const char *filename = "test_read_file_boundary"; size_t len; ssize_t rc; char buf[1030] = ""; strcpy(&buf[1022], "foobar"); int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); fail_if(fd == -1); rc = write(fd, buf, sizeof buf); ck_assert_uint_eq(rc, sizeof buf); close(fd); content = uc_read_file(filename, &len, 1000000000); unlink(filename); ck_assert_ptr_nonnull(content); ck_assert_uint_eq(len, sizeof buf); ck_assert_str_eq("foobar", &content[1022]); free(content); } END_TEST START_TEST (test_read_file_devnull) { char *content; size_t len = 123; content = uc_read_file("/dev/null", &len, 1024); ck_assert_ptr_nonnull(content); ck_assert_uint_eq(len, 0); free(content); } END_TEST START_TEST (test_read_file_too_big) { char *content; const char *filename = "test_read_file_too_big"; size_t len; ssize_t rc; int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); fail_if(fd == -1); rc = write(fd, "hello", 5); fail_if(rc != 5); close(fd); content = uc_read_file(filename, &len, 4); ck_assert_int_eq(errno, EMSGSIZE); ck_assert_ptr_null(content); unlink(filename); } END_TEST START_TEST (test_read_file_big) { char *content; const char *filename = "test_read_file_big"; char buf[4096 * 16]; size_t len; ssize_t rc; int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); ck_assert_int_ne(fd, -1); memset(buf, 'a', sizeof buf); rc = write(fd, buf, sizeof buf); ck_assert_uint_eq(rc, sizeof buf); close(fd); content = uc_read_file(filename, &len, 4096*16); fail_if(content == NULL); ck_assert_uint_eq(len, sizeof buf); ck_assert_int_eq(memcmp(content, buf, sizeof buf), 0); free(content); unlink(filename); } END_TEST Suite *read_file_suite(void) { Suite *s = suite_create("read_file"); TCase *tc = tcase_create("read_file tests"); tcase_add_test(tc, test_read_file_non_existing_file); tcase_add_test(tc, test_read_file_simple); tcase_add_test(tc, test_read_file_greater_than_max); tcase_add_test(tc, test_read_file_boundary); tcase_add_test(tc, test_read_file_devnull); tcase_add_test(tc, test_read_file_too_big); tcase_add_test(tc, test_read_file_big); suite_add_tcase(s, tc); return s; }