1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <file_lib.h>
/**
* This file contains file-locking tests that need to fork() a child process
* which totally confuses the unit test framework used by the other tests.
*/
#if defined(__ANDROID__)
# define TEMP_DIR "/data/data/com.termux/files/usr/tmp"
#else
# define TEMP_DIR "/tmp/file_lib_test"
#endif
#define TEST_FILE "file_lib_test.txt"
static bool failure = false;
#define assert_int_equal(expr1, expr2) \
if ((expr1) != (expr2)) \
{ \
fprintf(stderr, "FAIL: "#expr1" != "#expr2" [%d != %d] (%s:%d)\n", (expr1), (expr2), __FILE__, __LINE__); \
failure = true; \
}
#define assert_true(expr) \
if (!(expr)) \
{ \
fprintf(stderr, "FAIL: "#expr" is FALSE (%s:%d)\n", __FILE__, __LINE__); \
failure = true; \
}
#define assert_false(expr) \
if ((expr)) \
{ \
fprintf(stderr, "FAIL: "#expr" is TRUE (%s:%d)\n", __FILE__, __LINE__); \
failure = true; \
}
static void clear_tempfiles()
{
unlink(TEMP_DIR "/" TEST_FILE);
rmdir(TEMP_DIR);
}
int main()
{
atexit(&clear_tempfiles);
mkdir(TEMP_DIR, 0755);
/* TEST CASE 1 -- excl. lock in parent, try excl. lock in child */
FileLock lock = EMPTY_FILE_LOCK;
int fd = open(TEMP_DIR "/" TEST_FILE, O_CREAT | O_RDWR, 0644);
lock.fd = fd;
/* lock trying to wait */
assert_int_equal(ExclusiveFileLock(&lock, true), 0);
/* FD should not be changed */
assert_int_equal(lock.fd, fd);
/* we would be able to acquire the lock (we already have it) */
assert_true(ExclusiveFileLockCheck(&lock));
pid_t pid = fork();
if (pid == 0)
{
/* child */
failure = false;
/* FDs are inherited, fcntl() locks are not */
/* the lock is held by the parent process */
assert_false(ExclusiveFileLockCheck(&lock));
/* try to lock without waiting */
assert_int_equal(ExclusiveFileLock(&lock, false), -1);
/* should not affect parent's FD */
close(fd);
_exit(failure ? 1 : 0);
}
else
{
/* parent */
int status;
int ret = waitpid(pid, &status, 0);
assert_int_equal(ret, pid);
failure = (WEXITSTATUS(status) != 0);
}
/* unlock, but keep the FD open */
assert_int_equal(ExclusiveFileUnlock(&lock, false), 0);
/* should be able to close */
assert_int_equal(close(lock.fd), 0);
/* TEST CASE 2 -- shared lock in parent, try excl. lock in child, get shared
* lock in child, get excl. lock in parent */
fd = open(TEMP_DIR "/" TEST_FILE, O_CREAT | O_RDWR, 0644);
lock.fd = fd;
/* SHARED lock trying to wait */
assert_int_equal(SharedFileLock(&lock, true), 0);
/* FD should not be changed */
assert_int_equal(lock.fd, fd);
pid = fork();
if (pid == 0)
{
/* child */
failure = false;
/* FDs are inherited, fcntl() locks are not */
/* a shared lock is held by the parent process */
assert_false(ExclusiveFileLockCheck(&lock));
/* try to lock without waiting */
assert_int_equal(ExclusiveFileLock(&lock, false), -1);
/* try to get a shared lock without waiting */
assert_int_equal(SharedFileLock(&lock, false), 0);
/* should not affect parent's lock or FD */
assert_int_equal(SharedFileUnlock(&lock, true), 0);
_exit(failure ? 1 : 0);
}
else
{
/* parent */
int status;
int ret = waitpid(pid, &status, 0);
assert_int_equal(ret, pid);
failure = (WEXITSTATUS(status) != 0);
}
/* we are holding a shared lock so WE should be able to get an exclusive
* lock */
assert_true(ExclusiveFileLockCheck(&lock));
/* upgrade the lock to an exclusive one */
assert_int_equal(ExclusiveFileLock(&lock, true), 0);
/* unlock, but keep the FD open */
assert_int_equal(ExclusiveFileUnlock(&lock, false), 0);
/* should be able to close both FDs */
assert_int_equal(close(lock.fd), 0);
if (failure)
{
fprintf(stderr, "FAILED\n");
return 1;
}
else
{
fprintf(stderr, "SUCCESS\n");
return 0;
}
}
|