LCOV - code coverage report
Current view: top level - tests/ctest - test_keyname.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 84 84 100.0 %
Date: 2019-09-12 12:28:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Test suite for Libease functions accessing key name data.
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <kdbease.h>
      10             : 
      11             : #include "tests.h"
      12             : 
      13             : #define ERROR_STRING_SIZE 1024
      14             : 
      15             : #define test_relative(expected, child, parent)                                                                                             \
      16             :         {                                                                                                                                  \
      17             :                 char error[ERROR_STRING_SIZE];                                                                                             \
      18             :                 const char * result = elektraKeyGetRelativeName (child, parent);                                                           \
      19             :                 snprintf (error, sizeof (error),                                                                                           \
      20             :                           "Relative name of key was wrong.\nParent:   %s\nChild:    %s\nExpected: %s\nResult:   %s\n", keyName (parent),   \
      21             :                           keyName (child), expected, result);                                                                              \
      22             :                 succeed_if (strcmp (result, expected) == 0, error);                                                                        \
      23             :         }
      24             : 
      25           2 : static void test_relative_root (void)
      26             : {
      27           2 :         printf ("Get relative name of key with backend mounted at `/`\n");
      28             : 
      29           2 :         Key * parent = keyNew ("/", KEY_END);
      30           2 :         Key * child = keyNew ("spec/ni/test", KEY_END);
      31             : 
      32           2 :         test_relative ("spec/ni/test", child, parent);
      33           2 :         keyDel (child);
      34           2 :         child = keyNew ("system/💩🦄/Fjørt", KEY_END);
      35           2 :         test_relative ("system/💩🦄/Fjørt", child, parent);
      36           2 :         keyDel (child);
      37           2 :         child = keyNew ("user/\\/dot", KEY_END);
      38           2 :         test_relative ("user/\\/dot", child, parent);
      39           2 :         keyDel (child);
      40           2 :         keyDel (parent);
      41           2 : }
      42             : 
      43           2 : static void test_relative_cascading (void)
      44             : {
      45           2 :         printf ("Get relative name of key with cascading mountpoint\n");
      46             : 
      47           2 :         Key * parent = keyNew ("/cascading", KEY_END);
      48           2 :         Key * child = keyNew ("/cascading/k", KEY_END);
      49             : 
      50           2 :         test_relative ("k", child, parent);
      51           2 :         keyDel (child);
      52           2 :         child = keyNew ("system/cascading/deep/deeper/deepest", KEY_END);
      53           2 :         test_relative ("deep/deeper/deepest", child, parent);
      54           2 :         keyDel (parent);
      55           2 :         keyDel (child);
      56           2 :         parent = keyNew ("/cascading\\/mountpoint/", KEY_END);
      57           2 :         child = keyNew ("user/cascading\\/mountpoint/\\/dot", KEY_END);
      58           2 :         test_relative ("\\/dot", child, parent);
      59           2 :         keyDel (child);
      60           2 :         child = keyNew ("user/second_level/cascading\\/mountpoint/\\/dot", KEY_END);
      61           2 :         test_relative ("\\/dot", child, parent);
      62           2 :         keyDel (child);
      63           2 :         keyDel (parent);
      64           2 : }
      65             : 
      66           2 : static void test_relative_generic (void)
      67             : {
      68           2 :         printf ("Get relative name of key with generic mountpoint\n");
      69             : 
      70           2 :         Key * parent = keyNew ("system/", KEY_END);
      71           2 :         Key * child = keyNew ("system/key/🔑/🗝", KEY_END);
      72             : 
      73           2 :         test_relative ("key/🔑/🗝", child, parent);
      74           2 :         keyDel (child);
      75           2 :         child = keyNew ("system/Käfer/K", KEY_END);
      76           2 :         test_relative ("Käfer/K", child, parent);
      77           2 :         keyDel (child);
      78           2 :         keyDel (parent);
      79           2 :         parent = keyNew ("user", KEY_END);
      80           2 :         child = keyNew ("user/K", KEY_END);
      81           2 :         test_relative ("K", child, parent);
      82           2 :         test_relative ("user", parent, child);
      83           2 :         keyDel (child);
      84           2 :         child = keyNew ("user/KK\\/Kitchens/What/Were/You/Thinking?", KEY_END);
      85           2 :         test_relative ("KK\\/Kitchens/What/Were/You/Thinking?", child, parent);
      86           2 :         keyDel (child);
      87           2 :         keyDel (parent);
      88           2 : }
      89             : 
      90           2 : static void test_relative_equal (void)
      91             : {
      92           2 :         printf ("Get relative name of key which is the same as the parent key\n");
      93             : 
      94           2 :         Key * parent = keyNew ("system/parentChild", KEY_END);
      95           2 :         Key * child = keyNew ("system/parentChild", KEY_END);
      96           2 :         test_relative ("", child, parent);
      97             : 
      98           2 :         keyDel (parent);
      99           2 :         parent = keyNew ("/parentChild", KEY_END);
     100           2 :         test_relative ("", child, parent);
     101             : 
     102           2 :         keyDel (child);
     103           2 :         keyDel (parent);
     104           2 :         child = keyNew ("system/parentChild/#", KEY_END);
     105           2 :         parent = keyNew ("system/parentChild/#", KEY_END);
     106           2 :         test_relative ("", child, parent);
     107             : 
     108           2 :         keyDel (child);
     109           2 :         child = keyNew ("system/parentChild/#123", KEY_END);
     110             :         // expected according to it's spec
     111           2 :         test_relative ("23", child, parent);
     112             : 
     113           2 :         keyDel (child);
     114           2 :         keyDel (parent);
     115           2 : }
     116             : 
     117           2 : int main (int argc, char ** argv)
     118             : {
     119           2 :         char * program_name = "test_keyname";
     120           2 :         if (argc >= 1)
     121             :         {
     122           2 :                 program_name = argv[0];
     123             :         }
     124             : 
     125           2 :         printf ("KEY   NAME   TESTS\n");
     126           2 :         printf ("==================\n\n");
     127             : 
     128           2 :         test_relative_root ();
     129           2 :         test_relative_cascading ();
     130           2 :         test_relative_generic ();
     131           2 :         test_relative_equal ();
     132             : 
     133           2 :         printf ("\n%s RESULTS: %d test(s) done. %d error(s).\n", program_name, nbTest, nbError);
     134             : 
     135           2 :         return nbError;
     136             : }

Generated by: LCOV version 1.13