Translation update done using Pootle.
[gammu.git] / tests / locking.c
blobb69b1684f33c4832b88c6c484d432959660b5d35
1 #include <gammu.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 #include "../libgammu/device/devfunc.h"
8 #include "common.h"
10 #define lock_path "/var/lock/LCK.."
11 #define TEST_DEVICE "/dev/foo/bar"
12 #define TEST_LOCK lock_path "bar"
14 void create_lock(const char *name, const void *lock_data, const size_t lock_data_len)
16 FILE *fd;
17 fd = fopen(name, "w");
18 test_result(fd != NULL);
19 test_result(fwrite(lock_data, 1, lock_data_len, fd) == lock_data_len);
20 test_result(fclose(fd) == 0);
23 int main(int argc UNUSED, char **argv UNUSED)
25 GSM_Debug_Info *debug_info;
26 char *lock = NULL;
27 int pid;
28 char pids[20];
30 pid = getpid();
31 sprintf(pids, "%d", pid);
33 debug_info = GSM_GetGlobalDebug();
34 GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
35 GSM_SetDebugLevel("textall", debug_info);
37 /* Non existing PID, ASCII */
38 create_lock(TEST_LOCK, "1234567890", 10);
39 test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
40 test_result(lock != NULL);
41 test_result(unlock_device(NULL, &lock) == TRUE);
43 unlink(TEST_LOCK);
45 /* Existing PID, ASCII */
46 create_lock(TEST_LOCK, pids, strlen(pids));
47 test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_DEVICELOCKED);
48 test_result(lock == NULL);
49 test_result(unlock_device(NULL, &lock) == FALSE);
51 unlink(TEST_LOCK);
53 /* Existing PID, binary */
54 create_lock(TEST_LOCK, &pid, sizeof(int));
55 test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_DEVICELOCKED);
56 test_result(lock == NULL);
57 test_result(unlock_device(NULL, &lock) == FALSE);
59 unlink(TEST_LOCK);
61 /* Existing PID, binary */
62 pid = 0xfffffff;
63 create_lock(TEST_LOCK, &pid, sizeof(int));
64 test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
65 test_result(lock != NULL);
66 test_result(unlock_device(NULL, &lock) == TRUE);
68 unlink(TEST_LOCK);
70 /* No existing lock */
71 test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
72 test_result(lock != NULL);
73 test_result(unlock_device(NULL, &lock) == TRUE);
75 return 0;
78 /* Editor configuration
79 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72: