LCOV - code coverage report
Current view: top level - src/plugins/validation - testmod_validation.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 150 150 100.0 %
Date: 2019-09-12 12:28:41 Functions: 7 7 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             : #ifdef HAVE_KDBCONFIG_H
      10             : #include "kdbconfig.h"
      11             : #endif
      12             : 
      13             : #include <stdio.h>
      14             : #ifdef HAVE_STDLIB_H
      15             : #include <stdlib.h>
      16             : #endif
      17             : #ifdef HAVE_STRING_H
      18             : #include <string.h>
      19             : #endif
      20             : 
      21             : 
      22             : #include <langinfo.h>
      23             : 
      24             : #include <tests.h>
      25             : 
      26             : #include "validation.h"
      27             : 
      28             : #include <tests_internal.h>
      29             : #include <tests_plugin.h>
      30             : 
      31             : 
      32             : #define NR_KEYS 4
      33             : 
      34           2 : void test_lookupre (void)
      35             : {
      36           2 :         KeySet * ks = ksNew (5, keyNew ("user/a", KEY_VALUE, "a", KEY_COMMENT, "does not match", KEY_END),
      37             :                              keyNew ("user/b", KEY_VALUE, "  a  ", KEY_COMMENT, "does not match", KEY_END),
      38             :                              keyNew ("user/c", KEY_VALUE, "\t\t", KEY_COMMENT, "match", KEY_END),
      39             :                              keyNew ("user/d", KEY_VALUE, " \t \t ", KEY_COMMENT, "match", KEY_END), KS_END);
      40             : 
      41           2 :         Key * match = 0;
      42             :         regex_t regex;
      43             : 
      44           2 :         regcomp (&regex, "^[ \t]*$", REG_NOSUB);
      45             : 
      46             :         // we start from the first key
      47           2 :         ksRewind (ks);
      48             : 
      49             :         // show the key that match this string
      50           2 :         match = ksLookupRE (ks, &regex);
      51           2 :         succeed_if (!strcmp (keyName (match), "user/c"), "Key did not match");
      52             : 
      53           2 :         match = ksLookupRE (ks, &regex);
      54           2 :         succeed_if (!strcmp (keyName (match), "user/d"), "Key did not match");
      55             : 
      56           2 :         regfree (&regex); // free regex resources
      57           2 :         ksDel (ks);
      58           2 : }
      59             : 
      60           2 : void test_extended (void)
      61             : {
      62           2 :         KeySet * ks = ksNew (5, keyNew ("user/a", KEY_VALUE, "la", KEY_COMMENT, "match", KEY_END),
      63             :                              keyNew ("user/b", KEY_VALUE, "lalala", KEY_COMMENT, "match", KEY_END),
      64             :                              keyNew ("user/c", KEY_VALUE, "jump", KEY_COMMENT, "does not match", KEY_END),
      65             :                              keyNew ("user/d", KEY_VALUE, "lalalala", KEY_COMMENT, "match", KEY_END), KS_END);
      66             : 
      67           2 :         Key * match = 0;
      68             :         regex_t regex;
      69             : 
      70           2 :         regcomp (&regex, "^(la)+$", REG_NOSUB | REG_EXTENDED);
      71             : 
      72             :         // we start from the first key
      73           2 :         ksRewind (ks);
      74             : 
      75             :         // show the key that match this string
      76           2 :         match = ksLookupRE (ks, &regex);
      77           2 :         succeed_if (!strcmp (keyName (match), "user/a"), "Key did not match");
      78             : 
      79           2 :         match = ksLookupRE (ks, &regex);
      80           2 :         succeed_if (!strcmp (keyName (match), "user/b"), "Key did not match");
      81             : 
      82           2 :         match = ksLookupRE (ks, &regex);
      83           2 :         succeed_if (!strcmp (keyName (match), "user/d"), "Key did not match");
      84             : 
      85           2 :         regfree (&regex); // free regex resources
      86           2 :         ksDel (ks);
      87           2 : }
      88             : 
      89           2 : void word_test (void)
      90             : {
      91           2 :         Key * parentKey = keyNew ("user/tests/validation", KEY_VALUE, "", KEY_END);
      92           2 :         Key * k1 = keyNew ("user/tests/validation/valid1", KEY_VALUE, "word", KEY_META, "check/validation", "word", KEY_META,
      93             :                            "check/validation/match", "word", KEY_END);
      94           2 :         Key * k2 = keyNew ("user/tests/validation/valid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "word2", KEY_META,
      95             :                            "check/validation/match", "word", KEY_END);
      96           2 :         Key * k3 = keyNew ("user/tests/validation/invalid1", KEY_VALUE, "aworda", KEY_META, "check/validation", "word", KEY_META,
      97             :                            "check/validation/match", "word", KEY_END);
      98           2 :         Key * k4 = keyNew ("user/tests/validation/invalid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "word", KEY_META,
      99             :                            "check/validation/match", "word", KEY_END);
     100             : 
     101           2 :         KeySet * conf = ksNew (0, KS_END);
     102             :         KeySet * ks;
     103           2 :         PLUGIN_OPEN ("validation");
     104             : 
     105           2 :         ks = ksNew (2, KS_END);
     106           2 :         ksAppendKey (ks, k1);
     107           2 :         ksRewind (ks);
     108           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     109           2 :         ksDel (ks);
     110             : 
     111           2 :         ks = ksNew (2, KS_END);
     112           2 :         ksAppendKey (ks, k2);
     113           2 :         ksRewind (ks);
     114           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     115           2 :         ksDel (ks);
     116             : 
     117           2 :         ks = ksNew (2, KS_END);
     118           2 :         ksAppendKey (ks, k3);
     119           2 :         ksRewind (ks);
     120           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     121           2 :         ksDel (ks);
     122             : 
     123           2 :         ks = ksNew (2, KS_END);
     124           2 :         ksAppendKey (ks, k4);
     125           2 :         ksRewind (ks);
     126           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     127           2 :         ksDel (ks);
     128             : 
     129           2 :         keyDel (parentKey);
     130           2 :         PLUGIN_CLOSE ();
     131           2 : }
     132             : 
     133           2 : void line_test (void)
     134             : {
     135           2 :         Key * parentKey = keyNew ("user/tests/validation", KEY_VALUE, "", KEY_END);
     136           2 :         Key * k1 = keyNew ("user/tests/validation/valid1", KEY_VALUE, "line", KEY_META, "check/validation", "line", KEY_META,
     137             :                            "check/validation/match", "line", KEY_END);
     138           2 :         Key * k2 = keyNew ("user/tests/validation/valid2", KEY_VALUE, "line1\nline2\nline3", KEY_META, "check/validation", "line2",
     139             :                            KEY_META, "check/validation/match", "line", KEY_END);
     140           2 :         Key * k3 = keyNew ("user/tests/validation/invalid1", KEY_VALUE, "alinea", KEY_META, "check/validation", "line", KEY_META,
     141             :                            "check/validation/match", "line", KEY_END);
     142           2 :         Key * k4 = keyNew ("user/tests/validation/invalid2", KEY_VALUE, "line1\nline2\nline3", KEY_META, "check/validation", "line",
     143             :                            KEY_META, "check/validation/match", "line", KEY_END);
     144             : 
     145           2 :         KeySet * conf = ksNew (0, KS_END);
     146             :         KeySet * ks;
     147           2 :         PLUGIN_OPEN ("validation");
     148             : 
     149           2 :         ks = ksNew (2, KS_END);
     150           2 :         ksAppendKey (ks, k1);
     151           2 :         ksRewind (ks);
     152           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     153           2 :         ksDel (ks);
     154             : 
     155           2 :         ks = ksNew (2, KS_END);
     156           2 :         ksAppendKey (ks, k2);
     157           2 :         ksRewind (ks);
     158           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     159           2 :         ksDel (ks);
     160             : 
     161           2 :         ks = ksNew (2, KS_END);
     162           2 :         ksAppendKey (ks, k3);
     163           2 :         ksRewind (ks);
     164           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     165           2 :         ksDel (ks);
     166             : 
     167           2 :         ks = ksNew (2, KS_END);
     168           2 :         ksAppendKey (ks, k4);
     169           2 :         ksRewind (ks);
     170           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     171           2 :         ksDel (ks);
     172             : 
     173           2 :         keyDel (parentKey);
     174           2 :         PLUGIN_CLOSE ();
     175           2 : }
     176             : 
     177           2 : void invert_test (void)
     178             : {
     179           2 :         Key * parentKey = keyNew ("user/tests/validation", KEY_VALUE, "", KEY_END);
     180           2 :         Key * k1 = keyNew ("user/tests/validation/valid1", KEY_VALUE, "word", KEY_META, "check/validation", "word", KEY_META,
     181             :                            "check/validation/match", "word", KEY_META, "check/validation/invert", "", KEY_END);
     182           2 :         Key * k2 = keyNew ("user/tests/validation/valid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "word2", KEY_META,
     183             :                            "check/validation/match", "word", KEY_META, "check/validation/invert", "", KEY_END);
     184           2 :         Key * k3 = keyNew ("user/tests/validation/invalid1", KEY_VALUE, "aworda", KEY_META, "check/validation", "word", KEY_META,
     185             :                            "check/validation/match", "word", KEY_META, "check/validation/invert", "", KEY_END);
     186           2 :         Key * k4 = keyNew ("user/tests/validation/invalid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "word", KEY_META,
     187             :                            "check/validation/match", "word", KEY_META, "check/validation/invert", "", KEY_END);
     188             : 
     189           2 :         KeySet * conf = ksNew (0, KS_END);
     190             :         KeySet * ks;
     191           2 :         PLUGIN_OPEN ("validation");
     192             : 
     193           2 :         ks = ksNew (2, KS_END);
     194           2 :         ksAppendKey (ks, k1);
     195           2 :         ksRewind (ks);
     196           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     197           2 :         ksDel (ks);
     198             : 
     199           2 :         ks = ksNew (2, KS_END);
     200           2 :         ksAppendKey (ks, k2);
     201           2 :         ksRewind (ks);
     202           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "kdbSet failed");
     203           2 :         ksDel (ks);
     204             : 
     205           2 :         ks = ksNew (2, KS_END);
     206           2 :         ksAppendKey (ks, k3);
     207           2 :         ksRewind (ks);
     208           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     209           2 :         ksDel (ks);
     210             : 
     211           2 :         ks = ksNew (2, KS_END);
     212           2 :         ksAppendKey (ks, k4);
     213           2 :         ksRewind (ks);
     214           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     215           2 :         ksDel (ks);
     216             : 
     217           2 :         keyDel (parentKey);
     218           2 :         PLUGIN_CLOSE ();
     219           2 : }
     220             : 
     221           2 : void icase_test (void)
     222             : {
     223           2 :         Key * parentKey = keyNew ("user/tests/validation", KEY_VALUE, "", KEY_END);
     224           2 :         Key * k1 = keyNew ("user/tests/validation/valid1", KEY_VALUE, "WORD", KEY_META, "check/validation", "word", KEY_META,
     225             :                            "check/validation/word", "", KEY_META, "check/validation/ignorecase", "", KEY_END);
     226           2 :         Key * k2 = keyNew ("user/tests/validation/valid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "wORd2", KEY_META,
     227             :                            "check/validation/word", "", KEY_META, "check/validation/ignorecase", "", KEY_END);
     228             : 
     229           2 :         KeySet * conf = ksNew (0, KS_END);
     230             :         KeySet * ks;
     231           2 :         PLUGIN_OPEN ("validation");
     232             : 
     233           2 :         ks = ksNew (2, KS_END);
     234           2 :         ksAppendKey (ks, k1);
     235           2 :         ksRewind (ks);
     236           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     237           2 :         ksDel (ks);
     238             : 
     239           2 :         ks = ksNew (2, KS_END);
     240           2 :         ksAppendKey (ks, k2);
     241           2 :         ksRewind (ks);
     242           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed");
     243           2 :         ksDel (ks);
     244             : 
     245           2 :         keyDel (parentKey);
     246           2 :         PLUGIN_CLOSE ();
     247           2 : }
     248             : 
     249             : 
     250           2 : int main (int argc, char ** argv)
     251             : {
     252           2 :         printf ("   VALIDATION   TESTS\n");
     253           2 :         printf ("====================\n\n");
     254             : 
     255           2 :         init (argc, argv);
     256             : 
     257           2 :         test_lookupre ();
     258           2 :         test_extended ();
     259             : 
     260           2 :         word_test ();
     261           2 :         line_test ();
     262           2 :         icase_test ();
     263           2 :         invert_test ();
     264           2 :         print_result ("testmod_validation");
     265             : 
     266           2 :         return nbError;
     267             : }

Generated by: LCOV version 1.13