Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "ccode.hpp"
10 :
11 : #include <kdbmodule.h>
12 : #include <kdbprivate.h>
13 :
14 : #include <tests.hpp>
15 :
16 : using std::string;
17 :
18 : using CppKeySet = kdb::KeySet;
19 : using CppKey = kdb::Key;
20 :
21 : using ckdb::elektraModulesClose;
22 : using ckdb::elektraModulesInit;
23 : using ckdb::elektraPluginClose;
24 : using ckdb::elektraPluginOpen;
25 : using ckdb::keyNew;
26 : using ckdb::ksDel;
27 :
28 : using ckdb::Plugin;
29 :
30 26 : CppKeySet defaultConfig ()
31 : {
32 : CppKeySet config{ 20,
33 : keyNew ("user/chars", KEY_END),
34 : keyNew ("user/chars/0A", KEY_VALUE, "6E", KEY_END), // new line -> n
35 : keyNew ("user/chars/20", KEY_VALUE, "77", KEY_END), // space -> w
36 : keyNew ("user/chars/23", KEY_VALUE, "72", KEY_END), // # -> r
37 : keyNew ("user/chars/5C", KEY_VALUE, "62", KEY_END), // \\ (backslash) -> b
38 : keyNew ("user/chars/3D", KEY_VALUE, "65", KEY_END), // = -> e
39 : keyNew ("user/chars/3B", KEY_VALUE, "73", KEY_END), // ; -> s
40 26 : KS_END };
41 26 : return config;
42 : }
43 :
44 2 : CppKeySet percentConfig ()
45 : {
46 : CppKeySet config{ 20,
47 : keyNew ("user/chars", KEY_END),
48 : keyNew ("user/chars/0A", KEY_VALUE, "6E", KEY_END), // new line -> n
49 : keyNew ("user/chars/20", KEY_VALUE, "77", KEY_END), // space -> w
50 : keyNew ("user/chars/23", KEY_VALUE, "72", KEY_END), // # -> r
51 : keyNew ("user/chars/5C", KEY_VALUE, "62", KEY_END), // \\ (backslash) -> b
52 : keyNew ("user/chars/3D", KEY_VALUE, "65", KEY_END), // = -> e
53 : keyNew ("user/chars/3B", KEY_VALUE, "73", KEY_END), // ; -> s
54 : keyNew ("user/escape", KEY_VALUE, "25", KEY_END), // use % as escape character
55 2 : KS_END };
56 2 : return config;
57 : }
58 :
59 28 : void testEnocdingDecoding (Plugin * const plugin, CppKey const & parent, string const decodedString, string const encodedString = "")
60 : {
61 56 : CppKeySet keys{ 20, keyNew ("user/tests/ccode/key", KEY_VALUE, decodedString.c_str (), KEY_END), KS_END };
62 140 : succeed_if_same (plugin->kdbSet (plugin, keys.getKeySet (), *parent), //! OCLint (empty if, too few branches switch)
63 : ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbset` was not successful");
64 :
65 28 : if (!encodedString.empty ())
66 : {
67 24 : CppKey encoded = keys.lookup ("user/tests/ccode/key");
68 16 : succeed_if_same (encoded.getString (), encodedString, //! OCLint (empty if, too few branches switch)
69 : "String not correctly encoded");
70 : }
71 :
72 140 : succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), //! OCLint (empty if, too few branches switch)
73 : ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbGet` was not successful");
74 168 : CppKey decoded = keys.lookup ("user/tests/ccode/key");
75 112 : succeed_if_same (decoded.getString (), decodedString, //! OCLint (empty if, too few branches switch)
76 : "String not correctly decoded");
77 : }
78 :
79 28 : void testRoundTrip (string const decodedString, string const encodedString = "", CppKeySet config = defaultConfig ())
80 : {
81 56 : CppKeySet modules{ 0, KS_END };
82 28 : elektraModulesInit (modules.getKeySet (), NULL);
83 :
84 56 : CppKey parent{ "system/elektra/modules/type", KEY_END };
85 84 : Plugin * plugin = elektraPluginOpen ("ccode", modules.getKeySet (), config.getKeySet (), *parent);
86 84 : exit_if_fail (plugin != NULL, "Could not open ccode plugin"); //! OCLint (empty if, too few branches switch)
87 :
88 112 : testEnocdingDecoding (plugin, parent, decodedString, encodedString);
89 :
90 28 : elektraPluginClose (plugin, 0);
91 28 : ksDel (modules.release ());
92 28 : config.release ();
93 28 : elektraModulesClose (modules.getKeySet (), 0);
94 28 : }
95 :
96 20 : TEST (type, roundtrip)
97 : {
98 16 : testRoundTrip ("a value\nwith=;# and \\ itself", "a\\wvalue\\nwith\\e\\s\\r\\wand\\w\\b\\witself");
99 16 : testRoundTrip ("hello world");
100 16 : testRoundTrip ("hello world!\nnew line");
101 16 : testRoundTrip ("\0");
102 16 : testRoundTrip ("\n");
103 16 : testRoundTrip ("\\");
104 16 : testRoundTrip (" ");
105 16 : testRoundTrip ("=");
106 16 : testRoundTrip (";");
107 16 : testRoundTrip ("#");
108 16 : testRoundTrip (" =;#");
109 16 : testRoundTrip ("\n\\");
110 16 : testRoundTrip ("");
111 16 : testRoundTrip ("a value\nwith=;# and \\ itself", "a%wvalue%nwith%e%s%r%wand%w%b%witself", percentConfig ());
112 8 : }
|