LCOV - code coverage report
Current view: top level - src/plugins/passwd - testmod_passwd.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 48 48 100.0 %
Date: 2019-09-12 12:28:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for passwd plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbconfig.h>
      14             : 
      15             : #include <tests_plugin.h>
      16             : 
      17           2 : void test_read (void)
      18             : {
      19           2 :         Key * parentKey = keyNew ("user/tests/passwd-read", KEY_VALUE, srcdir_file ("passwd/passwd_in"), KEY_END);
      20           2 :         KeySet * conf = ksNew (10, keyNew ("system/index", KEY_VALUE, "name", KEY_END), KS_END);
      21           2 :         PLUGIN_OPEN ("passwd");
      22           2 :         KeySet * ks = ksNew (0, KS_END);
      23           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet failed\n");
      24           2 :         Key * key = ksLookupByName (ks, "user/tests/passwd-read/root/uid", 0);
      25           2 :         succeed_if (key, "key root/uid not found\n");
      26           2 :         succeed_if (strcmp (keyString (key), "32") == 0, "root/uid doesn't match\n");
      27           2 :         ksDel (ks);
      28           2 :         keyDel (parentKey);
      29           2 :         PLUGIN_CLOSE ();
      30           2 : }
      31             : 
      32           2 : void test_write (void)
      33             : {
      34           2 :         Key * parentKey = keyNew ("user/tests/passwd-write", KEY_VALUE, elektraFilename (), KEY_END);
      35           2 :         KeySet * conf = ksNew (30, keyNew ("system/index", KEY_VALUE, "name", KEY_END), KS_END);
      36           2 :         PLUGIN_OPEN ("passwd");
      37           2 :         KeySet * ks = ksNew (30, keyNew ("user/tests/passwd-write/root", KEY_BINARY, KEY_END),
      38             :                              keyNew ("user/tests/passwd-write/root/gecos", KEY_VALUE, "root", KEY_END),
      39             :                              keyNew ("user/tests/passwd-write/root/gid", KEY_VALUE, "1024", KEY_END),
      40             :                              keyNew ("user/tests/passwd-write/root/home", KEY_VALUE, "/root", KEY_END),
      41             :                              keyNew ("user/tests/passwd-write/root/passwd", KEY_VALUE, "x", KEY_END),
      42             :                              keyNew ("user/tests/passwd-write/root/shell", KEY_VALUE, "/bin/zsh", KEY_END),
      43             :                              keyNew ("user/tests/passwd-write/root/uid", KEY_VALUE, "1024", KEY_END), KS_END);
      44           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "failed to write passwd file\n");
      45           2 :         succeed_if (compare_line_files (srcdir_file ("passwd/passwd_out"), keyString (parentKey)), "files do nat match as expected\n");
      46           2 :         ksDel (ks);
      47           2 :         keyDel (parentKey);
      48           2 :         PLUGIN_CLOSE ();
      49           2 : }
      50             : 
      51           2 : void test_read_write (void)
      52             : {
      53           2 :         Key * parentKeyRead = keyNew ("user/tests/passwd-write", KEY_VALUE, srcdir_file ("passwd/passwd_in"), KEY_END);
      54           2 :         Key * parentKeyWrite = keyNew ("user/tests/passwd-write", KEY_VALUE, elektraFilename (), KEY_END);
      55           2 :         KeySet * conf = ksNew (10, keyNew ("system/index", KEY_VALUE, "name", KEY_END), KS_END);
      56           2 :         PLUGIN_OPEN ("passwd");
      57           2 :         KeySet * ks = ksNew (0, KS_END);
      58           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKeyRead) == 1, "kdbGet failed\n");
      59           2 :         ksRewind (ks);
      60           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKeyWrite) == 1, "kdbSet failed\n");
      61           2 :         succeed_if (compare_line_files (srcdir_file ("passwd/passwd_in"), keyString (parentKeyWrite)), "files do not match as expected\n");
      62           2 :         ksDel (ks);
      63           2 :         keyDel (parentKeyRead);
      64           2 :         keyDel (parentKeyWrite);
      65           2 :         PLUGIN_CLOSE ();
      66           2 : }
      67             : 
      68             : 
      69           2 : int main (int argc, char ** argv)
      70             : {
      71           2 :         printf ("PASSWD     TESTS\n");
      72           2 :         printf ("==================\n\n");
      73             : 
      74           2 :         init (argc, argv);
      75             : 
      76           2 :         test_read ();
      77           2 :         test_write ();
      78           2 :         test_read_write ();
      79             : 
      80           2 :         print_result ("testmod_passwd");
      81             : 
      82           2 :         return nbError;
      83             : }

Generated by: LCOV version 1.13