Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for file plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <stdlib.h>
11 : #include <string.h>
12 :
13 : #include <kdbconfig.h>
14 :
15 : #include <tests_plugin.h>
16 :
17 :
18 2 : void testReadSingleLine (const char * fileName)
19 : {
20 2 : Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
21 :
22 2 : KeySet * conf = ksNew (0, KS_END);
23 2 : PLUGIN_OPEN ("file");
24 :
25 2 : KeySet * ks = ksNew (0, KS_END);
26 :
27 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
28 :
29 2 : const Key * key = ksLookupByName (ks, "user/tests/file", KDB_O_NONE);
30 2 : exit_if_fail (key, "key not found");
31 :
32 2 : succeed_if (!strcmp ("this is a single line testfile\n", keyString (key)), "read single line data doesn't match expected string");
33 :
34 2 : ksDel (ks);
35 2 : keyDel (parentKey);
36 :
37 2 : PLUGIN_CLOSE ();
38 2 : }
39 :
40 2 : void testReadMultiLine (const char * fileName)
41 : {
42 2 : Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
43 :
44 2 : KeySet * conf = ksNew (0, KS_END);
45 2 : PLUGIN_OPEN ("file");
46 :
47 2 : KeySet * ks = ksNew (0, KS_END);
48 :
49 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
50 :
51 2 : const Key * key = ksLookupByName (ks, "user/tests/file", KDB_O_NONE);
52 2 : exit_if_fail (key, "key not found");
53 :
54 2 : succeed_if (!strcmp ("\nthis\n\n\tis a\n multi line test-\nfile\n\n", keyString (key)),
55 : "read multiline data doesn't match expected string");
56 :
57 2 : ksDel (ks);
58 2 : keyDel (parentKey);
59 :
60 2 : PLUGIN_CLOSE ();
61 2 : }
62 :
63 :
64 2 : void testWriteSingleLine (const char * compareTo)
65 : {
66 2 : Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, elektraFilename (), KEY_END);
67 :
68 2 : KeySet * conf = ksNew (0, KS_END);
69 2 : PLUGIN_OPEN ("file");
70 :
71 2 : KeySet * ks = ksNew (3, keyNew ("user/tests/file", KEY_VALUE, "this is a single line testfile\n", KEY_END), KS_END);
72 :
73 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
74 :
75 2 : succeed_if (compare_line_files (srcdir_file (compareTo), keyString (parentKey)), "files do not match as expected");
76 :
77 2 : ksDel (ks);
78 2 : keyDel (parentKey);
79 :
80 2 : PLUGIN_CLOSE ();
81 2 : }
82 :
83 :
84 2 : void testWriteMultiLine (const char * compareTo)
85 : {
86 2 : Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, elektraFilename (), KEY_END);
87 :
88 2 : KeySet * conf = ksNew (0, KS_END);
89 2 : PLUGIN_OPEN ("file");
90 :
91 2 : KeySet * ks = ksNew (3, keyNew ("user/tests/file", KEY_VALUE, "\nthis\n\n\tis a\n multi line test-\nfile\n\n", KEY_END), KS_END);
92 :
93 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
94 :
95 2 : succeed_if (compare_line_files (srcdir_file (compareTo), keyString (parentKey)), "files do not match as expected");
96 :
97 2 : ksDel (ks);
98 2 : keyDel (parentKey);
99 :
100 2 : PLUGIN_CLOSE ();
101 2 : }
102 :
103 2 : void testRoundTrip (const char * fileName)
104 : {
105 2 : Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
106 :
107 2 : KeySet * conf = ksNew (0, KS_END);
108 2 : PLUGIN_OPEN ("file");
109 :
110 2 : KeySet * ks = ksNew (0, KS_END);
111 :
112 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
113 :
114 2 : keySetString (parentKey, elektraFilename ());
115 :
116 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
117 :
118 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
119 :
120 2 : ksDel (ks);
121 2 : keyDel (parentKey);
122 :
123 2 : PLUGIN_CLOSE ();
124 2 : }
125 :
126 2 : int main (int argc, char ** argv)
127 : {
128 2 : printf ("FILE TESTS\n");
129 2 : printf ("==================\n\n");
130 :
131 2 : init (argc, argv);
132 2 : testReadSingleLine ("file/singleline");
133 2 : testReadMultiLine ("file/multiline");
134 2 : testWriteSingleLine ("file/singleline");
135 2 : testWriteMultiLine ("file/multiline");
136 2 : testRoundTrip ("file/multiline");
137 :
138 2 : print_result ("testmod_file");
139 :
140 2 : return nbError;
141 : }
|