Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for notification library.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <stdio.h>
11 : #include <stdlib.h>
12 :
13 : #include <kdbio.h>
14 : #include <kdbnotification.h>
15 : #include <tests.h>
16 :
17 : int callback_called;
18 :
19 3 : static void test_registerInt (void)
20 : {
21 3 : printf ("test elektraNotificationRegisterInt\n");
22 :
23 3 : Key * key = keyNew ("system:/elektra/version/constants", KEY_END);
24 3 : Key * valueKey = keyNew ("system:/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);
25 :
26 3 : int startValue = -1;
27 3 : int value = startValue;
28 :
29 3 : KDB * kdb = kdbOpen (NULL, key);
30 :
31 3 : succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value) == 0, "register should fail without contract");
32 :
33 3 : kdbClose (kdb, key);
34 :
35 3 : KeySet * contract = ksNew (0, KS_END);
36 3 : elektraNotificationContract (contract);
37 3 : kdb = kdbOpen (contract, key);
38 :
39 3 : succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value), "register failed");
40 :
41 : // call kdbGet; value gets automatically updated
42 3 : KeySet * config = ksNew (0, KS_END);
43 3 : succeed_if (kdbGet (kdb, config, key), "kdbGet failed");
44 :
45 3 : succeed_if (value != startValue, "value was not changed");
46 :
47 : // cleanup
48 3 : ksDel (config);
49 3 : ksDel (contract);
50 3 : kdbClose (kdb, key);
51 3 : keyDel (key);
52 3 : keyDel (valueKey);
53 3 : }
54 :
55 3 : static void testCallback (Key * key ELEKTRA_UNUSED, void * context ELEKTRA_UNUSED)
56 : {
57 3 : callback_called = 1;
58 3 : }
59 :
60 3 : static void test_registerCallback (void)
61 : {
62 3 : printf ("test elektraNotificationRegisterCallback\n");
63 :
64 3 : Key * key = keyNew ("system:/elektra/version/constants", KEY_END);
65 3 : Key * valueKey = keyNew ("system:/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);
66 3 : callback_called = 0;
67 :
68 3 : KDB * kdb = kdbOpen (NULL, key);
69 :
70 3 : succeed_if (elektraNotificationRegisterCallback (kdb, valueKey, testCallback, NULL) == 0, "register should fail without contract");
71 :
72 3 : kdbClose (kdb, key);
73 :
74 3 : KeySet * contract = ksNew (0, KS_END);
75 3 : elektraNotificationContract (contract);
76 3 : kdb = kdbOpen (contract, key);
77 :
78 3 : succeed_if (elektraNotificationRegisterCallback (kdb, valueKey, testCallback, NULL), "register failed");
79 :
80 : // call kdbGet; value gets automatically updated
81 3 : KeySet * config = ksNew (0, KS_END);
82 3 : succeed_if (kdbGet (kdb, config, key), "kdbGet failed");
83 :
84 3 : succeed_if (callback_called, "callback was not called");
85 :
86 : // cleanup
87 3 : ksDel (config);
88 3 : ksDel (contract);
89 3 : kdbClose (kdb, key);
90 3 : keyDel (key);
91 3 : keyDel (valueKey);
92 3 : }
93 :
94 3 : int main (int argc, char ** argv)
95 : {
96 3 : init (argc, argv);
97 :
98 : // Test elektraNotificationRegisterInt
99 3 : test_registerInt ();
100 :
101 : // Test elektraNotificationRegisterCallback
102 3 : test_registerCallback ();
103 :
104 9 : print_result ("libnotification");
105 :
106 3 : return nbError;
107 : }
|