nextcloud-desktop/tests/csync_tests/check_csync_lock.c

95 lines
1.8 KiB
C
Raw Normal View History

2008-02-27 20:56:47 +03:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2008-02-27 20:56:47 +03:00
#include <signal.h>
#include <stdlib.h>
2008-02-27 20:56:47 +03:00
#include <unistd.h>
#include "torture.h"
2008-02-27 20:56:47 +03:00
2008-04-26 12:13:09 +04:00
#define CSYNC_TEST 1
2008-02-27 20:56:47 +03:00
#include "std/c_file.h"
#include "csync_lock.h"
2008-05-05 12:55:05 +04:00
#define TEST_LOCK "/tmp/check_csync/test"
2008-02-27 20:56:47 +03:00
static void setup(void **state) {
int rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
rc = system("mkdir -p /tmp/check_csync");
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void teardown(void **state) {
int rc;
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
rc = system("rm -rf /tmp/check_csync");
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_csync_lock(void **state)
2008-02-27 20:56:47 +03:00
{
int rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
2012-10-27 18:05:16 +04:00
rc = csync_lock(NULL, TEST_LOCK);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
assert_true(c_isfile(TEST_LOCK));
2008-02-27 20:56:47 +03:00
2012-10-27 18:05:16 +04:00
rc = csync_lock(NULL, TEST_LOCK);
assert_int_equal(rc, -1);
2008-02-27 20:56:47 +03:00
2012-10-27 18:05:16 +04:00
csync_lock_remove(NULL, TEST_LOCK);
assert_false(c_isfile(TEST_LOCK));
}
2008-02-27 20:56:47 +03:00
static void check_csync_lock_content(void **state)
{
char buf[8] = {0};
int fd, pid, rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
2012-10-27 18:05:16 +04:00
rc = csync_lock(NULL, TEST_LOCK);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
assert_true(c_isfile(TEST_LOCK));
2008-02-27 20:56:47 +03:00
/* open lock file */
fd = open(TEST_LOCK, O_RDONLY);
assert_true(fd > 0);
2008-02-27 20:56:47 +03:00
/* read content */
pid = read(fd, buf, sizeof(buf));
close(fd);
2008-02-27 20:56:47 +03:00
assert_true(pid > 0);
2008-02-27 20:56:47 +03:00
/* get pid */
buf[sizeof(buf) - 1] = '\0';
pid = strtol(buf, NULL, 10);
2008-05-07 13:09:56 +04:00
assert_int_equal(pid, getpid());
2008-05-07 13:09:56 +04:00
2012-10-27 18:05:16 +04:00
csync_lock_remove(NULL, TEST_LOCK);
assert_false(c_isfile(TEST_LOCK));
}
2008-02-27 20:56:47 +03:00
int torture_run_tests(void)
{
const UnitTest tests[] = {
unit_test_setup_teardown(check_csync_lock, setup, teardown),
unit_test_setup_teardown(check_csync_lock_content, setup, teardown),
};
2008-02-27 20:56:47 +03:00
return run_tests(tests);
2008-02-27 20:56:47 +03:00
}