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_kconfig.cpp
216 lines (169 loc) · 6.08 KB
/
testmod_kconfig.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @file
*
* @brief Tests for kconfig plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "kconfig.hpp"
#include <fstream>
#include <kdbmodule.h>
#include <kdbprivate.h>
#include <tests.h>
#include <tests.hpp>
using ckdb::keyNew;
using ckdb::ksNew;
using CppKeySet = kdb::KeySet;
using CppKey = kdb::Key;
// BEGIN: COPY UTIL FUNCTIONS FROM testmod_yamlcpp.cpp
#define OPEN_KCONFIG_PLUGIN() \
CppKeySet modules{ 0, KS_END }; \
CppKeySet config{ 0, KS_END }; \
elektraModulesInit (modules.getKeySet (), 0); \
CppKey parent{ "system:/elektra/modules/kconfig", KEY_END }; \
Plugin * plugin = elektraPluginOpen ("kconfig", modules.getKeySet (), config.getKeySet (), *parent); \
exit_if_fail (plugin != NULL, "Could not open kconfig plugin")
#define CLOSE_PLUGIN() \
elektraPluginClose (plugin, 0); \
elektraModulesClose (modules.getKeySet (), 0); \
ksDel (modules.release ()); \
config.release ()
static void test_files_equal (string const & filenameA, string const & filenameB)
{
std::ifstream fileA{ filenameA };
std::ifstream fileB{ filenameB };
succeed_if_same (true, fileA.is_open (), "Could not open " + filenameA);
succeed_if_same (true, fileB.is_open (), "Could not open " + filenameB);
std::size_t counter{ 0 };
std::string lineA;
std::string lineB;
while (true)
{
counter++;
bool isEndOfFileA = fileA.eof ();
bool isEndOfFileB = fileB.eof ();
stringstream errorMessageStream;
errorMessageStream << "Difference in line " << counter << " when comparing " << filenameA << " to " << filenameB;
string errorMessage{ errorMessageStream.str () };
succeed_if_same (isEndOfFileA, isEndOfFileB, errorMessage);
if (isEndOfFileA || isEndOfFileB) break;
getline (fileA, lineA);
getline (fileB, lineB);
succeed_if_same (lineA, lineB, errorMessage);
}
}
static void read_from_kconfig_file (CppKey & loadParent, CppKeySet & loadKeySet)
{
OPEN_KCONFIG_PLUGIN ();
succeed_if_same (plugin->kdbGet (plugin, loadKeySet.getKeySet (), *loadParent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
"Call of `kdbGet` failed");
CLOSE_PLUGIN ();
}
static void check_read_from_kconfig_file_fails (CppKey & loadParent)
{
OPEN_KCONFIG_PLUGIN ();
CppKeySet loadKeySet;
succeed_if_same (plugin->kdbGet (plugin, loadKeySet.getKeySet (), *loadParent), ELEKTRA_PLUGIN_STATUS_ERROR,
"Call of `kdbGet` failed");
CLOSE_PLUGIN ();
}
static void save_to_kconfig_file (CppKey & saveParent, CppKeySet & saveKeySet)
{
OPEN_KCONFIG_PLUGIN ();
succeed_if_same (plugin->kdbSet (plugin, saveKeySet.getKeySet (), *saveParent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
"Call of `kdbGet` failed");
CLOSE_PLUGIN ();
}
static void test_write (CppKey & parent, CppKeySet & expected)
{
string filePath = elektraFilename ();
CppKey newParent = parent.dup ();
newParent.setString (filePath);
save_to_kconfig_file (newParent, expected);
test_files_equal (srcdir_file (parent.getString ().c_str ()), filePath);
}
static void test_read (CppKey & parent, CppKeySet & expected)
{
CppKeySet result;
CppKey loadParent = parent.dup ();
loadParent.setString (srcdir_file (parent.getString ().c_str ()));
read_from_kconfig_file (loadParent, result);
compare_keyset (expected, result);
}
static void test_read_fails (std::string const & content)
{
string filePath = elektraFilename ();
ofstream file{ filePath };
file << content;
file.close ();
CppKey loadParent;
loadParent.setString (filePath);
check_read_from_kconfig_file_fails (loadParent);
}
static void test_read_and_write (CppKey & parent, CppKeySet & expected)
{
test_write (parent, expected);
test_read (parent, expected);
}
TEST (kconfig, read_and_write_test)
{
// Test without prefix
{
#define TEST_VALID_PREFIX ""
CppKeySet keys{
#include "kconfig/test_valid.h"
};
CppKey parent;
parent.setString ("kconfig/test_validrc");
test_read_and_write (parent, keys);
}
// Test with prefix
{
#undef TEST_VALID_PREFIX
#define TEST_VALID_PREFIX "/kconfig/prefix"
CppKeySet keys{
#include "kconfig/test_valid.h"
};
CppKey parent{ "/kconfig/prefix", KEY_END };
parent.setString ("kconfig/test_validrc");
test_read_and_write (parent, keys);
}
}
TEST (kconfig, read_tests)
{
{
CppKeySet keys{
#include "kconfig/meta_example.h"
};
CppKey parent;
parent.setString ("kconfig/meta_examplerc");
test_read (parent, keys);
}
{
CppKeySet keys{
#include "kconfig/simple_example.h"
};
CppKey parent;
parent.setString ("kconfig/simple_examplerc");
test_read (parent, keys);
}
}
TEST (kconfig, invalid_read_tests)
{
test_read_fails ("[openGroupName");
test_read_fails ("multiple.locales[en][de]");
test_read_fails ("[content after the group]name\n");
test_read_fails ("[content after the group name] \n");
test_read_fails ("content after[locale] textt");
test_read_fails ("[invalid escape \\character]");
test_read_fails ("invalid escape \\character in=key name");
test_read_fails ("invalid escape=\\character in key value");
}
// -- Main ---------------------------------------------------------------------------------------------------------------------------------
int main (int argc, char * argv[])
{
init (argc, argv); // Required for `srcdir_file` to work properly
::testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}