Line data Source code
1 : /**
2 : * @file
3 : * @brief Tests the unit plugin
4 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
5 : *
6 : */
7 :
8 : #include <kdbconfig.h>
9 : #include <kdbtypes.h>
10 : #include <stdlib.h>
11 : #include <string.h>
12 : #include <tests_plugin.h>
13 :
14 18 : static void test_unit_normalization (const char * unitstring, const char * unitexpected)
15 : {
16 18 : Key * parentKey = keyNew ("user/tests/unit", KEY_END);
17 18 : Key * hexkey = keyNew ("user/test/unit/unittestval", KEY_VALUE, unitstring, KEY_META, "check/unit", "any", KEY_END);
18 18 : KeySet * conf = ksNew (0, KS_END);
19 18 : KeySet * ks = ksNew (20, hexkey, KS_END);
20 :
21 18 : PLUGIN_OPEN ("unit");
22 :
23 18 : succeed_if ((plugin->kdbGet (plugin, ks, parentKey) >= 1), "kdbGet did not succeed");
24 18 : Key * foundKey = ksLookupByName (ks, "user/test/unit/unittestval", 0);
25 18 : succeed_if (!strcmp (keyString (foundKey), unitexpected), "Values dont match");
26 18 : printf ("test unit plugin normalization test - returned value: %s, expected value: %s\n", keyString (foundKey), unitexpected);
27 :
28 18 : succeed_if ((plugin->kdbSet (plugin, ks, parentKey) >= 1), "kdbSet did not succeed");
29 18 : foundKey = ksLookupByName (ks, "user/test/unit/unittestval", 0);
30 18 : succeed_if (!strcmp (keyString (foundKey), unitstring), "Values dont match");
31 18 : printf ("test unit plugin restoration test - returned value: %s, expected value: %s\n", keyString (foundKey), unitstring);
32 :
33 18 : ksDel (ks);
34 18 : keyDel (parentKey);
35 :
36 18 : PLUGIN_CLOSE ();
37 18 : }
38 :
39 :
40 12 : static void test_unit_normalization_error_expected (const char * unitstring, const char * unitexpected)
41 : {
42 12 : Key * parentKey = keyNew ("user/tests/unit", KEY_END);
43 12 : Key * hexkey = keyNew ("user/test/unit/unittestval", KEY_VALUE, unitstring, KEY_META, "check/unit", "any", KEY_END);
44 12 : KeySet * conf = ksNew (0, KS_END);
45 12 : KeySet * ks = ksNew (20, hexkey, KS_END);
46 :
47 12 : PLUGIN_OPEN ("unit");
48 12 : printf ("Testing usage of false value %s\n", unitstring);
49 12 : int statusCode = plugin->kdbGet (plugin, ks, parentKey);
50 12 : succeed_if (statusCode < 1, "kdbGet did succeed, despite it should not\n");
51 12 : printf ("kdbGet status code %s expected , result: %d\n", unitexpected, statusCode);
52 :
53 12 : statusCode = plugin->kdbSet (plugin, ks, parentKey);
54 12 : succeed_if (statusCode < 1, "kdbSet did succeed, despite it should not\n");
55 12 : printf ("kdbSet status code %s expected, result: %d\n", unitexpected, statusCode);
56 :
57 12 : ksDel (ks);
58 12 : keyDel (parentKey);
59 :
60 12 : PLUGIN_CLOSE ();
61 12 : }
62 :
63 :
64 12 : static void test_unit_validation (const char * unit, const short e_ret)
65 : {
66 12 : Key * parentKey = keyNew ("user/tests/unit", KEY_END);
67 12 : Key * hexkey = keyNew ("user/test/unit/testvalue", KEY_VALUE, unit, KEY_META, "check/unit", "any", KEY_END);
68 12 : KeySet * conf = ksNew (0, KS_END);
69 12 : KeySet * ks = ksNew (20, hexkey, KS_END);
70 :
71 12 : PLUGIN_OPEN ("unit");
72 :
73 12 : int ret = plugin->kdbSet (plugin, ks, parentKey);
74 :
75 12 : printf ("Test unit Validity %s, returned value: %d, expected value: %d\n", unit, ret, e_ret);
76 12 : succeed_if (ret == e_ret, "Test failed");
77 :
78 12 : ksDel (ks);
79 12 : keyDel (parentKey);
80 :
81 12 : PLUGIN_CLOSE ();
82 12 : }
83 :
84 :
85 2 : static void test_unitplugin (void)
86 : {
87 :
88 2 : test_unit_validation ("1Z", -1);
89 2 : test_unit_validation ("PSOB", -1);
90 2 : test_unit_validation ("123YMB", -1);
91 2 : test_unit_validation ("123YGB", -1);
92 2 : test_unit_validation ("1B", 1);
93 2 : test_unit_validation ("10 B", 1);
94 :
95 2 : test_unit_normalization_error_expected ("MB10GB", "-1");
96 2 : test_unit_normalization_error_expected ("X10B", "-1");
97 :
98 2 : test_unit_normalization_error_expected ("1B1B", "-1");
99 2 : test_unit_normalization_error_expected ("1BB", "-1");
100 2 : test_unit_normalization_error_expected ("10GB20MB", "-1");
101 2 : test_unit_normalization_error_expected ("MB10", "-1");
102 :
103 2 : test_unit_normalization ("10B", "10");
104 2 : test_unit_normalization ("10MB", "10000000");
105 2 : test_unit_normalization ("10 MB", "10000000");
106 2 : test_unit_normalization ("90 MB", "90000000");
107 2 : test_unit_normalization ("256 MB", "256000000");
108 2 : test_unit_normalization ("10GB", "10000000000");
109 2 : test_unit_normalization ("5TB", "5000000000000");
110 2 : test_unit_normalization ("10PB", "10000000000000000");
111 2 : test_unit_normalization ("100PB", "100000000000000000");
112 2 : }
113 :
114 2 : int main (int argc, char ** argv)
115 : {
116 : (void) argc;
117 : (void) argv;
118 2 : test_unitplugin ();
119 2 : print_result ("testmod_unit");
120 2 : return nbError;
121 : }
|