LCOV - code coverage report
Current view: top level - src/plugins/gopts - testmod_gopts.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 67 75 89.3 %
Date: 2019-09-12 12:28:41 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for opts 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             : #include <unistd.h>
      13             : 
      14             : #include <kdbconfig.h>
      15             : 
      16             : #include <tests_plugin.h>
      17             : 
      18             : #include <config.c>
      19             : 
      20             : #include <sys/types.h>
      21             : #include <sys/wait.h>
      22             : 
      23             : #include "testdata.h"
      24             : 
      25             : // version 6 and 7 of clang-format don't agree whether it is supposed to be *[] or * [] so disable it here
      26             : // TODO: re-enable clang-format once version 7 is used on build server
      27             : // clang-format off
      28             : #define ARGS(NAME, ...) ((const char *[]){ TESTAPP_PATH, NAME, __VA_ARGS__, NULL })
      29             : #define ENVP(LD_LIB_PATH, ...) ((const char *[]){ LD_LIB_PATH, __VA_ARGS__, NULL })
      30             : 
      31             : #define NO_ARGS(NAME) ((const char *[]){ TESTAPP_PATH, NAME, NULL })
      32             : #define NO_ENVP(LD_LIB_PATH) ((const char *[]){ LD_LIB_PATH, NULL })
      33             : // clang-format on
      34             : 
      35          92 : static void run_test (const char ** argv, const char ** envp)
      36             : {
      37          92 :         printf ("test %s\n", argv[1]);
      38             :         pid_t pid;
      39             : 
      40             : 
      41          92 :         pid = fork ();
      42             : 
      43         184 :         if (pid == -1)
      44             :         {
      45           0 :                 yield_error ("Could not execute testapp");
      46           0 :                 return;
      47             :         }
      48             : 
      49         184 :         if (pid == 0)
      50             :         {
      51             :                 /* child */
      52          92 :                 execve (TESTAPP_PATH, (char * const *) argv, (char * const *) envp);
      53             : 
      54          92 :                 exit (EXIT_FAILURE);
      55             :         }
      56             : 
      57             :         /* parent */
      58             :         int status;
      59             :         do
      60             :         {
      61          92 :                 pid_t w = waitpid (pid, &status, 0);
      62          92 :                 if (w == -1)
      63             :                 {
      64           0 :                         perror ("waitpid");
      65           0 :                         yield_error ("waitpid");
      66             :                 }
      67          92 :         } while (!WIFEXITED (status) && !WIFSIGNALED (status));
      68             : 
      69          92 :         if (WIFSIGNALED (status))
      70             :         {
      71           0 :                 printf ("child process was killed by signal: %s", strsignal (WTERMSIG (status)));
      72           0 :                 exit (1);
      73             :         }
      74             : 
      75          92 :         if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
      76             :         {
      77           0 :                 nbError += WEXITSTATUS (status);
      78           0 :                 yield_error ("child process test failed");
      79             :         }
      80             : }
      81             : 
      82             : 
      83           2 : int main (int argc, char ** argv)
      84             : {
      85           2 :         printf ("GOPTS     TESTS\n");
      86           2 :         printf ("==================\n\n");
      87             : 
      88           2 :         init (argc, argv);
      89             : 
      90           2 :         char * ldLibPath = elektraFormat ("LD_LIBRARY_PATH=%s", getenv ("LD_LIBRARY_PATH"));
      91             : 
      92           2 :         run_test (NO_ARGS (TEST_EMPTY), NO_ENVP (ldLibPath));
      93             : 
      94           2 :         run_test (NO_ARGS (TEST_SINGLEOPT), NO_ENVP (ldLibPath));
      95           2 :         run_test (ARGS (TEST_SINGLEOPT, "-capple"), NO_ENVP (ldLibPath));
      96           2 :         run_test (ARGS (TEST_SINGLEOPT, "-capple", "morearg"), NO_ENVP (ldLibPath));
      97           2 :         run_test (ARGS (TEST_SINGLEOPT, "-c", "apple"), NO_ENVP (ldLibPath));
      98           2 :         run_test (ARGS (TEST_SINGLEOPT, "-c", "apple", "morearg"), NO_ENVP (ldLibPath));
      99           2 :         run_test (ARGS (TEST_SINGLEOPT, "--longopt=apple"), NO_ENVP (ldLibPath));
     100           2 :         run_test (ARGS (TEST_SINGLEOPT, "--longopt=apple", "morearg"), NO_ENVP (ldLibPath));
     101           2 :         run_test (ARGS (TEST_SINGLEOPT, "--longopt", "apple"), NO_ENVP (ldLibPath));
     102           2 :         run_test (ARGS (TEST_SINGLEOPT, "--longopt", "apple", "morearg"), NO_ENVP (ldLibPath));
     103           2 :         run_test (ARGS (TEST_SINGLEOPT, "noopt"), NO_ENVP (ldLibPath));
     104             : 
     105           2 :         run_test (NO_ARGS (TEST_SINGLEENV), NO_ENVP (ldLibPath));
     106           2 :         run_test (NO_ARGS (TEST_SINGLEENV), ENVP (ldLibPath, "ENV_VAR=apple"));
     107           2 :         run_test (NO_ARGS (TEST_SINGLEENV), ENVP (ldLibPath, "OTHER_ENV_VAR=apple"));
     108             : 
     109           2 :         run_test (NO_ARGS (TEST_TWOOPT), NO_ENVP (ldLibPath));
     110           2 :         run_test (ARGS (TEST_TWOOPT, "-capple"), NO_ENVP (ldLibPath));
     111           2 :         run_test (ARGS (TEST_TWOOPT, "-capple", "morearg"), NO_ENVP (ldLibPath));
     112           2 :         run_test (ARGS (TEST_TWOOPT, "-c", "apple"), NO_ENVP (ldLibPath));
     113           2 :         run_test (ARGS (TEST_TWOOPT, "-c", "apple", "morearg"), NO_ENVP (ldLibPath));
     114           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt=apple"), NO_ENVP (ldLibPath));
     115           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt=apple", "morearg"), NO_ENVP (ldLibPath));
     116           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt", "apple"), NO_ENVP (ldLibPath));
     117           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt", "apple", "morearg"), NO_ENVP (ldLibPath));
     118           2 :         run_test (ARGS (TEST_TWOOPT, "noopt"), NO_ENVP (ldLibPath));
     119           2 :         run_test (ARGS (TEST_TWOOPT, "-bapple"), NO_ENVP (ldLibPath));
     120           2 :         run_test (ARGS (TEST_TWOOPT, "-bapple", "morearg"), NO_ENVP (ldLibPath));
     121           2 :         run_test (ARGS (TEST_TWOOPT, "-b", "apple"), NO_ENVP (ldLibPath));
     122           2 :         run_test (ARGS (TEST_TWOOPT, "-b", "apple", "morearg"), NO_ENVP (ldLibPath));
     123           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt2=apple"), NO_ENVP (ldLibPath));
     124           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt2=apple", "morearg"), NO_ENVP (ldLibPath));
     125           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple"), NO_ENVP (ldLibPath));
     126           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple", "morearg"), NO_ENVP (ldLibPath));
     127           2 :         run_test (ARGS (TEST_TWOOPT, "-bapple", "-capple"), NO_ENVP (ldLibPath));
     128           2 :         run_test (ARGS (TEST_TWOOPT, "-bapple", "morearg", "-c", "apple"), NO_ENVP (ldLibPath));
     129           2 :         run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple", "--longopt", "apple"), NO_ENVP (ldLibPath));
     130             : 
     131           2 :         run_test (NO_ARGS (TEST_TWOENV), NO_ENVP (ldLibPath));
     132           2 :         run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple"));
     133           2 :         run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "OTHER_ENV_VAR=apple"));
     134           2 :         run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple"));
     135           2 :         run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "OTHER_OTHER_ENV_VAR=apple"));
     136           2 :         run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple", "OTHER_OTHER_ENV_VAR=apple"));
     137             : 
     138           2 :         run_test (NO_ARGS (TEST_MIXED), NO_ENVP (ldLibPath));
     139           2 :         run_test (ARGS (TEST_MIXED, "-capple"), ENVP (ldLibPath, "ENV_VAR=apple"));
     140           2 :         run_test (ARGS (TEST_MIXED, "-c", "apple"), ENVP (ldLibPath, "OTHER_ENV_VAR=apple"));
     141           2 :         run_test (ARGS (TEST_MIXED, "--longopt=apple"),
     142           2 :                   ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple", "OTHER_OTHER_ENV_VAR=apple"));
     143           2 :         run_test (ARGS (TEST_MIXED, "--longopt", "apple"), ENVP (ldLibPath, "OTHER_ENV_VAR=apple"));
     144             : 
     145           2 :         elektraFree (ldLibPath);
     146             : 
     147           2 :         print_result ("testmod_gopts");
     148             : 
     149           2 :         return nbError;
     150             : }

Generated by: LCOV version 1.13