LCOV - code coverage report
Current view: top level - src/plugins/ruby - testmod_ruby.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 174 174 100.0 %
Date: 2022-05-21 16:19:22 Functions: 12 12 100.0 %

          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 <kdbmacros.h>
      10             : #include <stdlib.h>
      11             : 
      12             : #ifdef HAVE_KDBCONFIG_H
      13             : #include "kdbconfig.h"
      14             : #endif
      15             : 
      16             : #include <tests_plugin.h>
      17             : 
      18             : #define PLUGIN_NAME "ruby"
      19             : 
      20             : #define SCRIPTS_DIR "ruby_test_scripts/"
      21             : 
      22             : 
      23           2 : static void test_plugin_open_without_script (void)
      24             : {
      25           2 :         KeySet * conf = ksNew (0, KS_END);
      26           2 :         PLUGIN_OPEN (PLUGIN_NAME);
      27           2 :         PLUGIN_CLOSE ();
      28           2 : }
      29             : 
      30           2 : static void test_plugin_open (void)
      31             : {
      32           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "simple.rb"), KEY_END), KS_END);
      33           2 :         PLUGIN_OPEN (PLUGIN_NAME);
      34           2 :         PLUGIN_CLOSE ();
      35           2 : }
      36             : 
      37           2 : static void test_plugin_open_script_not_found (void)
      38             : {
      39           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "does_not_eXiSt.rb"), KEY_END), KS_END);
      40             : 
      41           2 :         KeySet * modules = ksNew (0, KS_END);
      42           2 :         elektraModulesInit (modules, 0);
      43           2 :         Key * errorKey = keyNew ("/", KEY_END);
      44           2 :         Plugin * plugin = elektraPluginOpen (PLUGIN_NAME, modules, conf, errorKey);
      45             : 
      46           2 :         succeed_if_same_string (keyString (keyGetMeta (errorKey, "warnings/#0/description")), "Plugin Misbehavior");
      47             : 
      48           2 :         const char * exp_warning_msg = "Ruby Exception: LoadError: cannot load such file -- ";
      49           2 :         succeed_if (strncmp (keyString (keyGetMeta (errorKey, "warnings/#0/reason")), exp_warning_msg, strlen (exp_warning_msg)) == 0,
      50           2 :                     "unexpected warning message");
      51             : 
      52           2 :         keyDel (errorKey);
      53           2 :         PLUGIN_CLOSE ();
      54           2 : }
      55             : 
      56           2 : static void test_plugin_open_invalid_script (void)
      57             : {
      58           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "invalid.rb"), KEY_END), KS_END);
      59             : 
      60           2 :         KeySet * modules = ksNew (0, KS_END);
      61           2 :         elektraModulesInit (modules, 0);
      62           2 :         Key * errorKey = keyNew ("/", KEY_END);
      63           2 :         Plugin * plugin = elektraPluginOpen (PLUGIN_NAME, modules, conf, errorKey);
      64             : 
      65           2 :         succeed_if_same_string (keyString (keyGetMeta (errorKey, "warnings/#0/description")), "Plugin Misbehavior");
      66             : 
      67           2 :         const char * exp_warning_msg = "Error in Ruby-plugin, didn't call Kdb::Plugin.define";
      68           2 :         succeed_if (strncmp (keyString (keyGetMeta (errorKey, "warnings/#0/reason")), exp_warning_msg, strlen (exp_warning_msg)) == 0,
      69           2 :                     "unexpected warning message");
      70             : 
      71           2 :         keyDel (errorKey);
      72           2 :         PLUGIN_CLOSE ();
      73           2 : }
      74             : 
      75           2 : static void test_plugin_open_not_a_script (void)
      76             : {
      77           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "not_a_ruby_script.txt"), KEY_END), KS_END);
      78           2 :         KeySet * modules = ksNew (0, KS_END);
      79           2 :         elektraModulesInit (modules, 0);
      80           2 :         Key * errorKey = keyNew ("/", KEY_END);
      81           2 :         Plugin * plugin = elektraPluginOpen (PLUGIN_NAME, modules, conf, errorKey);
      82             : 
      83           2 :         succeed_if_same_string (keyString (keyGetMeta (errorKey, "warnings/#0/description")), "Plugin Misbehavior");
      84             : 
      85           2 :         const char * exp_warning_msg = "Ruby Exception: SyntaxError:";
      86           2 :         succeed_if (strncmp (keyString (keyGetMeta (errorKey, "warnings/#0/reason")), exp_warning_msg, strlen (exp_warning_msg)) == 0,
      87           2 :                     "unexpected warning message");
      88             : 
      89           2 :         keyDel (errorKey);
      90           2 :         PLUGIN_CLOSE ();
      91           2 : }
      92             : 
      93           2 : static void test_simple_get (void)
      94             : {
      95           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "simple_get.rb"), KEY_END), KS_END);
      96           2 :         PLUGIN_OPEN (PLUGIN_NAME);
      97             : 
      98           2 :         Key * parentKey = keyNew ("user:/rubytest", KEY_END);
      99           2 :         KeySet * ks = ksNew (0, KS_END);
     100           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
     101             : 
     102           2 :         output_warnings (parentKey);
     103           2 :         output_error (parentKey);
     104             : 
     105           2 :         succeed_if (ksGetSize (ks) == 5, "unexpected key set size");
     106             : 
     107           2 :         Key * head = ksHead (ks);
     108           2 :         Key * tail = ksTail (ks);
     109           2 :         succeed_if_same_string (keyString (head), "myvalue0");
     110           2 :         succeed_if_same_string (keyString (tail), "myvalue4");
     111             : 
     112           2 :         ksDel (ks);
     113             : 
     114           2 :         PLUGIN_CLOSE ();
     115           2 : }
     116             : 
     117           2 : static void test_get_with_exception (void)
     118             : {
     119           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "get_with_exception.rb"), KEY_END), KS_END);
     120           2 :         PLUGIN_OPEN (PLUGIN_NAME);
     121             : 
     122           2 :         Key * parentKey = keyNew ("user:/rubytest", KEY_END);
     123           2 :         KeySet * ks = ksNew (0, KS_END);
     124           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) < 0, "call to kdbGet was successful but it should not");
     125             : 
     126           2 :         const char * exp_error_msg = "Ruby Exception: RuntimeError: Throwing that expected exception";
     127           2 :         succeed_if (strncmp (keyString (keyGetMeta (parentKey, "error/reason")), exp_error_msg, strlen (exp_error_msg)) == 0,
     128           2 :                     "unexpected error message");
     129             : 
     130           2 :         succeed_if (ksGetSize (ks) == 5, "unexpected key set size");
     131             : 
     132           2 :         Key * head = ksHead (ks);
     133           2 :         Key * tail = ksTail (ks);
     134           2 :         succeed_if_same_string (keyString (head), "myvalue0");
     135           2 :         succeed_if_same_string (keyString (tail), "myvalue4");
     136             : 
     137           2 :         ksDel (ks);
     138             : 
     139           2 :         PLUGIN_CLOSE ();
     140           2 : }
     141             : 
     142             : 
     143           2 : static void test_simple_set (void)
     144             : {
     145           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "simple_set.rb"), KEY_END), KS_END);
     146           2 :         PLUGIN_OPEN (PLUGIN_NAME);
     147             : 
     148           2 :         Key * parentKey = keyNew ("user:/rubytest", KEY_END);
     149           2 :         KeySet * ks = ksNew (5, keyNew ("user:/rubytest/key1", KEY_VALUE, "myvalue1", KEY_END),
     150             :                              keyNew ("user:/rubytest/key2", KEY_VALUE, "myvalue2", KEY_END),
     151             :                              keyNew ("user:/rubytest/key3", KEY_VALUE, "myvalue3", KEY_END),
     152             :                              keyNew ("user:/rubytest/key4", KEY_VALUE, "myvalue4", KEY_END),
     153             :                              keyNew ("user:/rubytest/key5", KEY_VALUE, "myvalue5", KEY_END), KS_END);
     154           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 5, "call to kdbSet was not successful");
     155             : 
     156           2 :         output_warnings (parentKey);
     157           2 :         output_error (parentKey);
     158             : 
     159           2 :         ksDel (ks);
     160             : 
     161           2 :         PLUGIN_CLOSE ();
     162           2 : }
     163             : 
     164           6 : static void set_and_test_state (Plugin * plugin, KeySet * ksSet, KeySet * ksGet)
     165             : {
     166           6 :         Key * parentKey = keyNew ("user:/rubytest", KEY_END);
     167           6 :         succeed_if (plugin->kdbSet (plugin, ksSet, parentKey) == 1, "call to kdbSet was not successful");
     168             : 
     169           6 :         output_warnings (parentKey);
     170           6 :         output_error (parentKey);
     171             : 
     172           6 :         succeed_if (plugin->kdbGet (plugin, ksGet, parentKey) == 0, "call to kdbGet was not successful");
     173             : 
     174           6 :         output_warnings (parentKey);
     175           6 :         output_error (parentKey);
     176           6 :         keyDel (parentKey);
     177           6 : }
     178             : 
     179           2 : static void test_statefull (void)
     180             : {
     181           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "statefull.rb"), KEY_END), KS_END);
     182           2 :         PLUGIN_OPEN (PLUGIN_NAME);
     183             : 
     184           2 :         KeySet * ksSet = ksNew (5, keyNew ("user:/rubytest/key1", KEY_VALUE, "myvalue1", KEY_END),
     185             :                                 keyNew ("user:/rubytest/key2", KEY_VALUE, "myvalue2", KEY_END),
     186             :                                 keyNew ("user:/rubytest/key3", KEY_VALUE, "myvalue3", KEY_END),
     187             :                                 keyNew ("user:/rubytest/key4", KEY_VALUE, "myvalue4", KEY_END),
     188             :                                 keyNew ("user:/rubytest/key5", KEY_VALUE, "myvalue5", KEY_END), KS_END);
     189             : 
     190           2 :         KeySet * ksGet = ksNew (0, KS_END);
     191             : 
     192           2 :         set_and_test_state (plugin, ksSet, ksGet);
     193             : 
     194           2 :         succeed_if (ksGetSize (ksGet) == 5, "unexpected key set size");
     195             : 
     196           2 :         Key * head = ksHead (ksGet);
     197           2 :         Key * tail = ksTail (ksGet);
     198           2 :         succeed_if_same_string (keyString (head), "myvalue1");
     199           2 :         succeed_if_same_string (keyString (tail), "myvalue5");
     200             : 
     201           2 :         ksDel (ksSet);
     202           2 :         ksDel (ksGet);
     203             : 
     204           2 :         PLUGIN_CLOSE ();
     205           2 : }
     206             : 
     207             : 
     208           2 : static void test_two_plugin_instances (void)
     209             : {
     210           2 :         KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file (SCRIPTS_DIR "statefull.rb"), KEY_END), KS_END);
     211           2 :         KeySet * modules = ksNew (0, KS_END);
     212           2 :         elektraModulesInit (modules, 0);
     213           2 :         Key * errorKey1 = keyNew ("/", KEY_END);
     214           2 :         Plugin * plugin1 = elektraPluginOpen (PLUGIN_NAME, modules, conf, errorKey1);
     215             : 
     216           2 :         succeed_if (plugin1 != NULL, "could not open plugin instance 1");
     217             : 
     218           2 :         Key * errorKey2 = keyNew ("/", KEY_END);
     219           2 :         Plugin * plugin2 = elektraPluginOpen (PLUGIN_NAME, modules, conf, errorKey2);
     220             : 
     221           2 :         succeed_if (plugin2 != NULL, "could not open plugin instance 1");
     222             : 
     223             :         // Set and test state for plugin1
     224           2 :         KeySet * ksSet1 = ksNew (5, keyNew ("user:/rubytest/key1", KEY_VALUE, "myvalue1", KEY_END),
     225             :                                  keyNew ("user:/rubytest/key2", KEY_VALUE, "myvalue2", KEY_END),
     226             :                                  keyNew ("user:/rubytest/key3", KEY_VALUE, "myvalue3", KEY_END),
     227             :                                  keyNew ("user:/rubytest/key4", KEY_VALUE, "myvalue4", KEY_END),
     228             :                                  keyNew ("user:/rubytest/key5", KEY_VALUE, "myvalue5", KEY_END), KS_END);
     229             : 
     230           2 :         KeySet * ksGet1 = ksNew (0, KS_END);
     231             : 
     232           2 :         set_and_test_state (plugin1, ksSet1, ksGet1);
     233             : 
     234           2 :         succeed_if (ksGetSize (ksGet1) == 5, "unexpected key set size");
     235             : 
     236           2 :         Key * head1 = ksHead (ksGet1);
     237           2 :         Key * tail1 = ksTail (ksGet1);
     238           2 :         succeed_if_same_string (keyString (head1), "myvalue1");
     239           2 :         succeed_if_same_string (keyString (tail1), "myvalue5");
     240             : 
     241             :         // Set and test state for plugin2
     242           2 :         KeySet * ksSet2 = ksNew (5, keyNew ("user:/rubytest/key1", KEY_VALUE, "myvalue_1", KEY_END),
     243             :                                  keyNew ("user:/rubytest/key2", KEY_VALUE, "myvalue_2", KEY_END),
     244             :                                  keyNew ("user:/rubytest/key3", KEY_VALUE, "myvalue_3", KEY_END),
     245             :                                  keyNew ("user:/rubytest/key4", KEY_VALUE, "myvalue_4", KEY_END),
     246             :                                  keyNew ("user:/rubytest/key5", KEY_VALUE, "myvalue_5", KEY_END),
     247             :                                  keyNew ("user:/rubytest/key6", KEY_VALUE, "myvalue_6", KEY_END), KS_END);
     248             : 
     249           2 :         KeySet * ksGet2 = ksNew (0, KS_END);
     250             : 
     251           2 :         set_and_test_state (plugin2, ksSet2, ksGet2);
     252             : 
     253           2 :         succeed_if (ksGetSize (ksGet2) == 6, "unexpected key set size");
     254             : 
     255           2 :         Key * head2 = ksHead (ksGet2);
     256           2 :         Key * tail2 = ksTail (ksGet2);
     257           2 :         succeed_if_same_string (keyString (head2), "myvalue_1");
     258           2 :         succeed_if_same_string (keyString (tail2), "myvalue_6");
     259             : 
     260             :         // clean up
     261           2 :         ksDel (ksSet1);
     262           2 :         ksDel (ksGet1);
     263           2 :         ksDel (ksSet2);
     264           2 :         ksDel (ksGet2);
     265             : 
     266           2 :         keyDel (errorKey1);
     267           2 :         keyDel (errorKey2);
     268           2 :         elektraPluginClose (plugin1, 0);
     269             :         // this results in a segfault
     270             :         // elektraPluginClose (plugin2, 0);
     271           2 :         elektraModulesClose (modules, 0);
     272           2 :         ksDel (modules);
     273           2 : }
     274             : 
     275           2 : int main (int argc, char ** argv)
     276             : {
     277           2 :         printf ("RUBY         TESTS\n");
     278           2 :         printf ("==================\n\n");
     279             : 
     280           2 :         init (argc, argv);
     281             : 
     282             :         // some debugging
     283           2 :         char * ld_library_path = getenv ("LD_LIBRARY_PATH");
     284           2 :         char * rubylib = getenv ("RUBYLIB");
     285             : 
     286           2 :         printf ("env LD_LIBRARY_PATH: %s\n", ld_library_path);
     287           2 :         printf ("env RUBYLIB: %s\n", rubylib);
     288             : 
     289             :         // Plugin open
     290             :         //
     291           2 :         test_plugin_open_without_script ();
     292             : 
     293           2 :         test_plugin_open_script_not_found ();
     294           2 :         test_plugin_open_not_a_script ();
     295           2 :         test_plugin_open_invalid_script ();
     296             : 
     297           2 :         test_plugin_open ();
     298             : 
     299             :         // Plugin get
     300             :         //
     301           2 :         test_simple_get ();
     302           2 :         test_get_with_exception ();
     303             : 
     304             :         // Plugin set
     305             :         //
     306           2 :         test_simple_set ();
     307             : 
     308           2 :         test_statefull ();
     309             : 
     310           2 :         test_two_plugin_instances ();
     311             : 
     312           6 :         print_result ("test_ruby");
     313             : 
     314           2 :         return nbError;
     315             : }

Generated by: LCOV version 1.13