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

Generated by: LCOV version 1.13