107 lines
2.5 KiB
C
107 lines
2.5 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
|
|
|
|
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);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
}
|