Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for mini plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : /* -- Imports --------------------------------------------------------------------------------------------------------------------------- */
11 :
12 : #include <tests_plugin.h>
13 :
14 : /* -- Macros ---------------------------------------------------------------------------------------------------------------------------- */
15 :
16 : #define MAX_LENGTH_TEXT 500
17 :
18 : /* -- Functions ------------------------------------------------------------------------------------------------------------------------- */
19 :
20 2 : static void test_basics (void)
21 : {
22 2 : printf ("⢠Test basic functionality of plugin\n");
23 :
24 2 : Key * parentKey = keyNew ("system/elektra/modules/mini", KEY_END);
25 2 : KeySet * conf = ksNew (0, KS_END);
26 2 : PLUGIN_OPEN ("mini");
27 :
28 2 : KeySet * ks = ksNew (0, KS_END);
29 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Could not retrieve plugin contract");
30 :
31 2 : keyDel (parentKey);
32 2 : ksDel (ks);
33 2 : PLUGIN_CLOSE ();
34 2 : }
35 :
36 2 : static void test_get (void)
37 : {
38 2 : char const * const fileName = "mini/read.ini";
39 2 : printf ("⢠Parse file ā%sā\n", fileName);
40 :
41 2 : char const * const prefix = "user/mini/tests/read";
42 2 : Key * parentKey = keyNew (prefix, KEY_VALUE, srcdir_file (fileName), KEY_END);
43 2 : KeySet * conf = ksNew (0, KS_END);
44 2 : PLUGIN_OPEN ("mini");
45 :
46 2 : KeySet * keySet = ksNew (0, KS_END);
47 2 : succeed_if (plugin->kdbGet (plugin, keySet, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to open or parse file");
48 2 : succeed_if (output_error (parentKey), "Received unexpected error while reading the configuration");
49 :
50 2 : char keyValues[][2][50] = {
51 : { "keyWithoutLeadingWhitespace", "valueWithLeadingWhiteSpace" },
52 : { "keyWithLeadingWhitespace", "valueWithoutLeadingWhiteSpace" },
53 : { "keyNoSpace", "valueNoSpace" },
54 : { "wide", "open spaces" },
55 : { "key containing space", "value" },
56 : { "empty", "" },
57 : { "esc\\/a\\/ped/level1/level2", "š»" },
58 : };
59 : Key * key;
60 : char text[MAX_LENGTH_TEXT];
61 16 : for (size_t pair = 0; pair < sizeof (keyValues) / sizeof (keyValues[0]); pair++)
62 : {
63 14 : Key * reference = keyNew (prefix, KEY_VALUE, keyValues[pair][1], KEY_END);
64 14 : keyAddName (reference, keyValues[pair][0]);
65 14 : key = ksLookupByName (keySet, keyName (reference), KDB_O_NONE);
66 :
67 14 : snprintf (text, MAX_LENGTH_TEXT, "key ā%.100sā not found", keyName (reference));
68 14 : exit_if_fail (key, text);
69 :
70 14 : succeed_if_same_string (keyString (key), keyString (reference));
71 14 : keyDel (reference);
72 : }
73 :
74 2 : keyDel (parentKey);
75 2 : ksDel (keySet);
76 2 : PLUGIN_CLOSE ();
77 2 : }
78 :
79 2 : static void test_set (void)
80 : {
81 2 : printf ("⢠Write configuration data\n");
82 :
83 2 : char const * const fileName = "mini/write.ini";
84 2 : char const * const prefix = "user/mini/tests/write";
85 :
86 2 : Key * parentKey = keyNew (prefix, KEY_VALUE, elektraFilename (), KEY_END);
87 2 : KeySet * conf = ksNew (0, KS_END);
88 2 : PLUGIN_OPEN ("mini");
89 :
90 2 : char keyValues[][2][50] = {
91 : { "key", "value" }, { "space", "wide open spaces" }, { "empty", "" }, { "esc\\/aped/level1/", "š" }
92 : };
93 : char text[MAX_LENGTH_TEXT];
94 2 : KeySet * keySet = ksNew (0, KS_END);
95 10 : for (size_t pair = 0; pair < sizeof (keyValues) / sizeof (keyValues[0]); pair++)
96 : {
97 8 : char * name = keyValues[pair][0];
98 8 : char * value = keyValues[pair][1];
99 8 : snprintf (text, MAX_LENGTH_TEXT, "%s/%s", prefix, name);
100 8 : ksAppendKey (keySet, keyNew (text, KEY_VALUE, value, KEY_END));
101 : }
102 :
103 2 : succeed_if (plugin->kdbSet (plugin, keySet, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write to file");
104 2 : succeed_if (output_error (parentKey), "Received unexpected error while writing the configuration");
105 2 : succeed_if (output_warnings (parentKey), "Received unexpected warning while writing the configuration");
106 :
107 2 : snprintf (text, MAX_LENGTH_TEXT, "Output of plugin stored in ā%sā does not match the expected output stored in ā%sā",
108 : keyString (parentKey), srcdir_file (fileName));
109 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), text);
110 :
111 2 : keyDel (parentKey);
112 2 : ksDel (keySet);
113 2 : PLUGIN_CLOSE ();
114 2 : }
115 :
116 : /* -- Main ------------------------------------------------------------------------------------------------------------------------------ */
117 :
118 2 : int main (int argc, char ** argv)
119 : {
120 2 : printf ("mINI Tests š\n");
121 2 : printf ("==============\n\n");
122 :
123 2 : init (argc, argv);
124 :
125 2 : test_basics ();
126 2 : test_get ();
127 2 : test_set ();
128 :
129 2 : print_result ("testmod_mini");
130 :
131 2 : return nbError;
132 : }
|