This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
testmod_recorder.c
171 lines (126 loc) · 5.05 KB
/
testmod_recorder.c
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
/**
* @file
*
* @brief Tests for recorder plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <kdbprivate.h>
#include <stdlib.h>
#include <tests_plugin.h>
#include <unistd.h>
static void test_basics (void)
{
printf ("test basics\n");
Key * parentKey = keyNew ("user:/tests/recorder", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("recorder");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "call to kdbGet was not successful");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
static kdbHookRecordPtr getRecordFunction (Plugin * plugin)
{
return (kdbHookRecordPtr) elektraPluginGetFunction (plugin, "hook/record/record");
}
static kdbHookRecordLockPtr getLockFunction (Plugin * plugin)
{
return (kdbHookRecordLockPtr) elektraPluginGetFunction (plugin, "hook/record/lock");
}
static kdbHookRecordUnlockPtr getUnlockFunction (Plugin * plugin)
{
return (kdbHookRecordUnlockPtr) elektraPluginGetFunction (plugin, "hook/record/unlock");
}
static void test_noKdbInGlobalKeySet_shouldReturnError (void)
{
printf ("Test %s\n", __func__);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("recorder");
KeySet * returned = ksNew (0, KS_END);
Key * parentKey = keyNew ("user:/tests/recorder", KEY_END);
succeed_if (getRecordFunction (plugin) (plugin, returned, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "call should return error");
ksDel (returned);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_locking_should_work (void)
{
printf ("Test %s\n", __func__);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("recorder");
KeySet * returned = ksNew (0, KS_END);
Key * parentKey = keyNew ("user:/tests/recorder", KEY_END);
KDB * kdb = kdbOpen (NULL, parentKey);
plugin->global = ksNew (1, keyNew ("system:/elektra/kdb", KEY_BINARY, KEY_SIZE, sizeof (kdb), KEY_VALUE, &kdb, KEY_END), KS_END);
ksAppendKey (kdb->global, keyNew ("system:" ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KEY_VALUE, keyName (parentKey), KEY_END));
int lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_SUCCESS, "first lock should be successful");
lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_ERROR, "second lock should not be successful");
getUnlockFunction (plugin) (plugin, parentKey);
lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_SUCCESS, "third lock should be successful");
getUnlockFunction (plugin) (plugin, parentKey);
ksDel (plugin->global);
PLUGIN_CLOSE ();
kdbClose (kdb, parentKey);
ksDel (returned);
keyDel (parentKey);
}
static void test_locking_without_recording_should_always_work (void)
{
printf ("Test %s\n", __func__);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("recorder");
KeySet * returned = ksNew (0, KS_END);
Key * parentKey = keyNew ("user:/tests/recorder", KEY_END);
KDB * kdb = kdbOpen (NULL, parentKey);
plugin->global = ksNew (1, keyNew ("system:/elektra/kdb", KEY_BINARY, KEY_SIZE, sizeof (kdb), KEY_VALUE, &kdb, KEY_END), KS_END);
int lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_SUCCESS, "first lock should be successful");
lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_SUCCESS, "second lock should be successful");
ksDel (plugin->global);
PLUGIN_CLOSE ();
kdbClose (kdb, parentKey);
ksDel (returned);
keyDel (parentKey);
}
static void test_locking_should_create_specified_lockfile (void)
{
printf ("Test %s\n", __func__);
const char * lockfileName = "/tmp/my_test_elektra_record_lockfile";
KeySet * conf = ksNew (1, keyNew ("/lockfile", KEY_VALUE, lockfileName, KEY_END), KS_END);
PLUGIN_OPEN ("recorder");
KeySet * returned = ksNew (0, KS_END);
Key * parentKey = keyNew ("user:/tests/recorder", KEY_END);
KDB * kdb = kdbOpen (NULL, parentKey);
plugin->global = ksNew (1, keyNew ("system:/elektra/kdb", KEY_BINARY, KEY_SIZE, sizeof (kdb), KEY_VALUE, &kdb, KEY_END), KS_END);
ksAppendKey (kdb->global, keyNew ("system:" ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KEY_VALUE, keyName (parentKey), KEY_END));
int lockResult = getLockFunction (plugin) (plugin, parentKey);
succeed_if (lockResult == ELEKTRA_PLUGIN_STATUS_SUCCESS, "first lock should be successful");
int accessResult = access (lockfileName, F_OK);
succeed_if_fmt (accessResult == 0, "file %s should exist", lockfileName);
getUnlockFunction (plugin) (plugin, parentKey);
ksDel (plugin->global);
PLUGIN_CLOSE ();
kdbClose (kdb, parentKey);
ksDel (returned);
keyDel (parentKey);
}
int main (int argc, char ** argv)
{
printf ("RECORDER TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_basics ();
test_noKdbInGlobalKeySet_shouldReturnError ();
test_locking_should_create_specified_lockfile ();
test_locking_without_recording_should_always_work ();
test_locking_should_work ();
print_result ("testmod_recorder");
return nbError;
}