Files
libucore/test/test_restart_file.c
T
2016-02-08 13:34:22 +01:00

155 lines
4.0 KiB
C

#include <check.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ucore/restart_counter.h"
#define FILE_NAME "restart_counter_test"
//Note that these tests are comewhat fragile as they depend on the
//file sizes that are created, which depends on the (relative)paths of this file.
//for cleaning the created log files..
static void logging_rm_files(void)
{
remove(FILE_NAME);
}
START_TEST (test_restart_counter)
UCRCResult rc;
struct UCRestartCounter cnt;
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK);
ck_assert_int_eq(0, uc_restart_counter_get(&cnt));
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK);
ck_assert_int_eq(1, uc_restart_counter_get(&cnt));
END_TEST
START_TEST (test_restart_counter_existing)
UCRCResult rc;
struct UCRestartCounter cnt;
FILE *f = fopen(FILE_NAME, "w");
fail_if(f == NULL);
fputs("99999\n", f);
fclose(f);
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK);
ck_assert_int_eq(100000, uc_restart_counter_get(&cnt));
END_TEST
START_TEST (test_restart_counter_inaccessible_file)
UCRCResult rc;
struct UCRestartCounter cnt;
rc = uc_restart_counter_init(&cnt, "/should/not/exist/abc", 0);
fail_if(rc != UC_RC_ERR_FILE_ACCESS_ERROR);
END_TEST
START_TEST (test_restart_counter_garble)
UCRCResult rc;
struct UCRestartCounter cnt;
FILE *f = fopen(FILE_NAME, "w");
fail_if(f == NULL);
fputs("Q123456789\n\n\n", f);
fclose(f);
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_ERR_FILE_CONTENT_ERROR);
ck_assert_int_eq(0, uc_restart_counter_get(&cnt));
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK, "%d", rc);
ck_assert_int_eq(1, uc_restart_counter_get(&cnt));
END_TEST
START_TEST (test_restart_counter_full_filesystem)
UCRCResult rc;
struct UCRestartCounter cnt;
rc = uc_restart_counter_init(&cnt, "/dev/full", 0);
fail_if(rc != UC_RC_ERR_UNSPECIFIED);
END_TEST
START_TEST (test_restart_counter_lock_file)
UCRCResult rc;
struct UCRestartCounter cnt;
sem_t *sem;
sem = mmap(NULL, 2 * sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS,-1,0);
fail_if(sem == NULL);
fail_if(sem_init(&sem[0], 1 , 0) != 0);
fail_if(sem_init(&sem[1], 1 , 0) != 0);
rc = uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE);
fail_if(rc != UC_RC_OK);
ck_assert_int_eq(0, uc_restart_counter_get(&cnt));
rc = uc_restart_counter_close(&cnt);
fail_if(rc != UC_RC_OK);
rc = uc_restart_counter_close(&cnt);
fail_if(rc != UC_RC_ERR_NOT_OPENED);
pid_t pid = fork();
fail_if(pid < 0);
if (pid > 0) {
sem_wait(&sem[0]);
rc = uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE);
sem_post(&sem[1]);
printf("rc = %d\n", rc);
fail_if(rc != UC_RC_ERR_FILE_LOCKED);
ck_assert_int_eq(0, uc_restart_counter_get(&cnt));
} else {
uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE);
sem_post(&sem[0]);
sem_wait(&sem[1]);
_exit(0);
}
END_TEST
Suite *restart_counter_suite(void)
{
Suite *s = suite_create("restart_counter");
TCase *tc1 = tcase_create("restart_counter");
tcase_add_test(tc1, test_restart_counter);
tcase_add_test(tc1, test_restart_counter_existing);
tcase_add_test(tc1, test_restart_counter_inaccessible_file);
tcase_add_test(tc1, test_restart_counter_garble);
if (access("/dev/full", R_OK) == 0) {
tcase_add_test(tc1, test_restart_counter_full_filesystem);
} else {
puts("Cannot access /dev/full. Not testing test_logfile_full_filesystem");
}
tcase_add_test(tc1, test_restart_counter_lock_file);
tcase_add_checked_fixture(tc1, logging_rm_files, logging_rm_files);
suite_add_tcase(s, tc1);
return s;
}