LCOV - code coverage report
Current view: top level - src/plugins/crypto - gpgagent_teardown.h (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 6 19 31.6 %
Date: 2022-05-21 16:19:22 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief common method for shutting down the gpg-agent in unit tests
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : #ifndef ELEKTRA_PLUGIN_CRYPTO_GPGAGENT_TEARDOWN_H
      10             : #define ELEKTRA_PLUGIN_CRYPTO_GPGAGENT_TEARDOWN_H
      11             : 
      12             : #include <sys/wait.h>
      13             : #include <unistd.h>
      14             : 
      15             : #define GPG_CONNECT_AGENT_CMD "gpg-connect-agent"
      16             : 
      17           6 : static inline void test_teardown (void)
      18             : {
      19           6 :         pid_t pid;
      20           6 :         int status;
      21           6 :         char * argv[] = { GPG_CONNECT_AGENT_CMD, "--quiet", "KILLAGENT", "/bye", NULL };
      22             : 
      23             :         // check if gpg-connect-agent is available and executable
      24           6 :         if (access (GPG_CONNECT_AGENT_CMD, F_OK))
      25             :         {
      26           6 :                 return;
      27             :         }
      28           0 :         if (access (GPG_CONNECT_AGENT_CMD, X_OK))
      29             :         {
      30             :                 return;
      31             :         }
      32             : 
      33             :         // execute the shutdown command
      34           0 :         switch (pid = fork ())
      35             :         {
      36           0 :         case -1: // failure
      37           0 :                 yield_error ("fork failed");
      38           0 :                 return;
      39             : 
      40           0 :         case 0: // child process
      41           0 :                 if (execv (GPG_CONNECT_AGENT_CMD, argv) < 0)
      42             :                 {
      43           0 :                         exit (-1);
      44             :                 }
      45             :                 // end of the child process
      46             :         }
      47             : 
      48             :         // parent process - check if execv failed
      49             :         // NOTE the return value of gpg-connect-agent is irrelevant because it will
      50             :         //      always return 0 (see source code of GnuPG).
      51           0 :         waitpid (pid, &status, 0);
      52           0 :         succeed_if (status != -1, "failed to execute gpg-connect-agent");
      53             : 
      54             :         // wait for the agent to properly shut down
      55           0 :         if (status > 0)
      56             :         {
      57           0 :                 pid = status;
      58           0 :                 waitpid (pid, &status, 0);
      59             :         }
      60             : }
      61             : 
      62             : #endif

Generated by: LCOV version 1.13