Files
libucore/test/test_read_file.c
T
2013-07-06 22:25:06 +02:00

170 lines
3.9 KiB
C

#include <check.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ucore/read_file.h>
START_TEST (test_read_file_non_existing_file)
char *content;
size_t len;
int saved_errno;
content = uc_read_file("non existing file 123765", &len, 1000000000);
saved_errno = errno;
fail_if(content != NULL);
fail_if(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);
fail_if(fd == -1);
rc = write(fd, "hello", 5);
fail_if(rc != 5, "rc was %ld", (long)rc);
close(fd);
content = uc_read_file(filename, &len, 1000000000);
unlink(filename);
fail_if(content == NULL);
fail_if(len != 5);
fail_if(strlen(content) != 5);
fail_if(strcmp("hello", content) != 0);
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, "rc was %ld", (long)rc);
close(fd);
content = uc_read_file(filename, &len, 4);
saved_errno = errno;
unlink(filename);
fail_if(content != NULL);
fail_if(saved_errno != EMSGSIZE, "was %d", saved_errno);
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);
fail_if(rc != sizeof buf, "rc was %ld", (long)rc);
close(fd);
content = uc_read_file(filename, &len, 1000000000);
unlink(filename);
fail_if(content == NULL);
fail_if(len != sizeof buf);
fail_if(strcmp("foobar", &content[1022]) != 0);
free(content);
END_TEST
START_TEST (test_read_file_devnull)
char *content;
size_t len = 123;
content = uc_read_file("/dev/null", &len, 1024);
fail_if(content == NULL);
fail_if(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, "rc was %ld", (long)rc);
close(fd);
content = uc_read_file(filename, &len, 4);
fail_if(errno != EMSGSIZE);
fail_if(content != NULL);
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);
fail_if(fd == -1);
memset(buf, 'a', sizeof buf);
rc = write(fd, buf, sizeof buf);
fail_if(rc != sizeof buf, "rc was %ld", (long)rc);
close(fd);
content = uc_read_file(filename, &len, 4096*16);
fail_if(content == NULL);
fail_if(len != sizeof buf);
fail_if(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;
}