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 2 : static void test_openclose (void)
20 : {
21 2 : printf ("test open & close\n");
22 :
23 2 : Key * key = keyNew ("system/sw/tests/testlib_notification", KEY_END);
24 2 : KDB * kdb = kdbOpen (key);
25 2 : exit_if_fail (kdb, "opening kdb failed");
26 :
27 2 : succeed_if (!elektraNotificationClose (kdb), "could close notification system without open");
28 :
29 2 : succeed_if (elektraNotificationOpen (kdb), "could not open notification system");
30 2 : succeed_if (!elektraNotificationOpen (kdb), "could open notification system twice");
31 :
32 2 : succeed_if (elektraNotificationClose (kdb), "could not close notification system");
33 2 : succeed_if (elektraNotificationOpen (kdb), "could not re-open notification system");
34 :
35 : // cleanup
36 2 : succeed_if (elektraNotificationClose (kdb), "could not close notification system");
37 2 : succeed_if (kdbClose (kdb, key) == 0, "could not close kdb");
38 2 : keyDel (key);
39 2 : }
40 :
41 2 : static void test_registerInt (void)
42 : {
43 2 : printf ("test elektraNotificationRegisterInt\n");
44 :
45 2 : Key * key = keyNew ("system/elektra/version/constants", KEY_END);
46 2 : Key * valueKey = keyNew ("system/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);
47 :
48 2 : int startValue = -1;
49 2 : int value = startValue;
50 :
51 2 : KDB * kdb = kdbOpen (key);
52 :
53 2 : succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value) == 0, "register should fail before open");
54 :
55 2 : elektraNotificationOpen (kdb);
56 :
57 2 : succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value), "register failed");
58 :
59 : // call kdbGet; value gets automatically updated
60 2 : KeySet * config = ksNew (0, KS_END);
61 2 : succeed_if (kdbGet (kdb, config, key), "kdbGet failed");
62 :
63 2 : succeed_if (value != startValue, "value was not changed");
64 :
65 : // cleanup
66 2 : ksDel (config);
67 2 : elektraNotificationClose (kdb);
68 2 : kdbClose (kdb, key);
69 2 : keyDel (key);
70 2 : keyDel (valueKey);
71 2 : }
72 :
73 2 : static void testCallback (Key * key ELEKTRA_UNUSED, void * context ELEKTRA_UNUSED)
74 : {
75 2 : callback_called = 1;
76 2 : }
77 :
78 2 : static void test_registerCallback (void)
79 : {
80 2 : printf ("test elektraNotificationRegisterCallback\n");
81 :
82 2 : Key * key = keyNew ("system/elektra/version/constants", KEY_END);
83 2 : Key * valueKey = keyNew ("system/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);
84 2 : callback_called = 0;
85 :
86 2 : KDB * kdb = kdbOpen (key);
87 :
88 2 : succeed_if (elektraNotificationRegisterCallback (kdb, valueKey, testCallback, NULL) == 0, "register should fail before open");
89 :
90 2 : elektraNotificationOpen (kdb);
91 :
92 2 : succeed_if (elektraNotificationRegisterCallback (kdb, valueKey, testCallback, NULL), "register failed");
93 :
94 : // call kdbGet; value gets automatically updated
95 2 : KeySet * config = ksNew (0, KS_END);
96 2 : succeed_if (kdbGet (kdb, config, key), "kdbGet failed");
97 :
98 2 : succeed_if (callback_called, "callback was not called");
99 :
100 : // cleanup
101 2 : ksDel (config);
102 2 : elektraNotificationClose (kdb);
103 2 : kdbClose (kdb, key);
104 2 : keyDel (key);
105 2 : keyDel (valueKey);
106 2 : }
107 :
108 2 : int main (int argc, char ** argv)
109 : {
110 2 : init (argc, argv);
111 :
112 : // Test elektraNotificationOpen and elektraNotificationClose
113 2 : test_openclose ();
114 :
115 : // Test elektraNotificationRegisterInt
116 2 : test_registerInt ();
117 :
118 : // Test elektraNotificationRegisterCallback
119 2 : test_registerCallback ();
120 :
121 2 : print_result ("libnotification");
122 :
123 2 : return nbError;
124 : }
|