LCOV - code coverage report
Current view: top level - src/plugins/specload - test_testapp.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 39 51 76.5 %
Date: 2019-09-12 12:28:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for specload plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdio.h>
      11             : #include <stdlib.h>
      12             : #include <string.h>
      13             : 
      14             : #include <tests_plugin.h>
      15             : 
      16             : #include "testdata.h"
      17             : 
      18             : #include <config.c>
      19             : 
      20             : #include <unistd.h>
      21             : 
      22             : #define BACKUP_STDIN() int stdin_copy = dup (STDIN_FILENO);
      23             : 
      24             : #define RESTORE_STDIN()                                                                                                                    \
      25             :         if (dup2 (stdin_copy, STDIN_FILENO) == -1)                                                                                         \
      26             :         {                                                                                                                                  \
      27             :                 yield_error ("Could not execute testapp");                                                                                 \
      28             :         }                                                                                                                                  \
      29             :         close (stdin_copy);
      30             : 
      31             : #define START_TESTAPP(...)                                                                                                                 \
      32             :         BACKUP_STDIN ();                                                                                                                   \
      33             :         startTestApp (TESTAPP_PATH, ((char * const[]){ TESTAPP_PATH, __VA_ARGS__, NULL }));
      34             : 
      35           4 : void startTestApp (const char * app, char * const argv[])
      36             : {
      37             :         pid_t pid;
      38             :         int fd[2];
      39             : 
      40           4 :         if (pipe (fd) != 0)
      41             :         {
      42           0 :                 yield_error ("Could not execute testapp");
      43           0 :                 return;
      44             :         }
      45             : 
      46           4 :         pid = fork ();
      47             : 
      48           8 :         if (pid == -1)
      49             :         {
      50           0 :                 yield_error ("Could not execute testapp");
      51           0 :                 return;
      52             :         }
      53             : 
      54           8 :         if (pid == 0)
      55             :         { /* child */
      56           4 :                 if (dup2 (fd[1], STDOUT_FILENO) == -1)
      57             :                 {
      58           0 :                         exit (EXIT_FAILURE);
      59             :                 }
      60             : 
      61           4 :                 close (fd[0]);
      62           4 :                 close (fd[1]);
      63             : 
      64           4 :                 execv (app, argv);
      65             : 
      66           4 :                 exit (EXIT_FAILURE);
      67             :         }
      68             : 
      69           4 :         close (fd[1]);
      70             : 
      71             : 
      72           4 :         if (dup2 (fd[0], STDIN_FILENO) == -1)
      73             :         {
      74           0 :                 yield_error ("Could not execute testapp");
      75           0 :                 return;
      76             :         }
      77             : 
      78           4 :         close (fd[0]);
      79             : }
      80             : 
      81           4 : static bool check_binary_file (int fd, size_t expectedSize, const unsigned char * expectedData)
      82             : {
      83           4 :         FILE * file = fdopen (fd, "rb");
      84             : 
      85             :         int c;
      86           4 :         size_t cur = 0;
      87         120 :         while (cur < expectedSize && (c = fgetc (file)) != EOF)
      88             :         {
      89         112 :                 if (c != expectedData[cur])
      90             :                 {
      91             :                         char buf[255];
      92           0 :                         snprintf (buf, 255, "byte %zd differs", cur);
      93           0 :                         yield_error (buf)
      94             :                 }
      95         112 :                 ++cur;
      96             :         }
      97             : 
      98           4 :         while (fgetc (file) != EOF)
      99             :         {
     100           0 :                 cur++;
     101             :         }
     102             : 
     103           4 :         if (cur == expectedSize)
     104             :         {
     105             :                 return true;
     106             :         }
     107             : 
     108             :         char buf[255];
     109           0 :         snprintf (buf, 255, "actual size %zd differs from expected size %zd", cur, expectedSize);
     110           0 :         yield_error (buf) return false;
     111             : }
     112             : 
     113           2 : void test_default (void)
     114             : {
     115           2 :         START_TESTAPP ("--elektra-spec");
     116             : 
     117           2 :         succeed_if (check_binary_file (STDIN_FILENO, default_spec_expected_size, default_spec_expected), "output differs");
     118             : 
     119           2 :         RESTORE_STDIN ();
     120           2 : }
     121             : 
     122           2 : void test_noparent (void)
     123             : {
     124           2 :         START_TESTAPP ("spec", "noparent");
     125             : 
     126           2 :         succeed_if (check_binary_file (STDIN_FILENO, noparent_spec_expected_size, noparent_spec_expected), "output differs");
     127             : 
     128           2 :         RESTORE_STDIN ();
     129           2 : }
     130             : 
     131           2 : int main (int argc, char ** argv)
     132             : {
     133           2 :         printf ("SPECLOAD_TESTAPP     TESTS\n");
     134           2 :         printf ("==================\n\n");
     135             : 
     136           2 :         init (argc, argv);
     137             : 
     138           2 :         test_default ();
     139           2 :         test_noparent ();
     140             : 
     141           2 :         print_result ("test_testapp");
     142             : 
     143           2 :         return nbError;
     144             : }

Generated by: LCOV version 1.13