LCOV - code coverage report
Current view: top level - tests - t-data.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 72 111 64.9 %
Date: 2018-11-15 08:49:49 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* t-data - Regression tests for the gpgme_data_t abstraction.
       2             :    Copyright (C) 2001, 2004 g10 Code GmbH
       3             : 
       4             :    This file is part of GPGME.
       5             : 
       6             :    GPGME is free software; you can redistribute it and/or modify it
       7             :    under the terms of the GNU Lesser General Public License as
       8             :    published by the Free Software Foundation; either version 2.1 of
       9             :    the License, or (at your option) any later version.
      10             : 
      11             :    GPGME is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :    Lesser General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU Lesser General Public
      17             :    License along with this program; if not, write to the Free Software
      18             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      19             :    02111-1307, USA.  */
      20             : 
      21             : /* We need to include config.h so that we know whether we are building
      22             :    with large file system (LFS) support. */
      23             : #ifdef HAVE_CONFIG_H
      24             : #include <config.h>
      25             : #endif
      26             : 
      27             : #include <stddef.h>
      28             : #include <stdio.h>
      29             : #include <stdlib.h>
      30             : #include <string.h>
      31             : #include <errno.h>
      32             : 
      33             : #define PGM "t-data"
      34             : #include "run-support.h"
      35             : 
      36             : #undef fail_if_err
      37             : #define fail_if_err(a) do { if(a) {                                          \
      38             :                                fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \
      39             :                                 "%s\n", __FILE__, __LINE__, round,           \
      40             :                                 gpgme_strerror(a));                          \
      41             :                                 exit (1); }                                  \
      42             :                              } while(0)
      43             : 
      44             : typedef enum
      45             :   {
      46             :     TEST_INITIALIZER,
      47             :     TEST_INVALID_ARGUMENT,
      48             :     TEST_INOUT_NONE,
      49             :     TEST_INOUT_MEM_NO_COPY,
      50             :     TEST_INOUT_MEM_COPY,
      51             :     TEST_INOUT_MEM_FROM_FILE_COPY,
      52             :     TEST_INOUT_MEM_FROM_INEXISTANT_FILE,
      53             :     TEST_INOUT_MEM_FROM_FILE_NO_COPY,
      54             :     TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME,
      55             :     TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART,
      56             :     TEST_INOUT_MEM_FROM_FILE_PART_BY_FP,
      57             :     TEST_END
      58             :   } round_t;
      59             : 
      60             : const char *text = "Just GNU it!\n";
      61             : const char *text2 = "Just GNU it!\nJust GNU it!\n";
      62             : 
      63             : int
      64           0 : read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
      65             : {
      66             :   static int off = 0;
      67           0 :   unsigned int amount = strlen (text) - off;
      68             :   /*  round_t round = *((round_t *) cb_value);  */
      69             : 
      70             :   (void)cb_value;
      71             : 
      72           0 :   if (!buffer && !count && !nread)
      73             :     {
      74             :       /* Rewind requested.  */
      75           0 :       off = 0;
      76           0 :       return 0;
      77             :     }
      78           0 :   if (! buffer || !nread)
      79           0 :     return -1;
      80           0 :   if (amount <= 0)
      81             :     {
      82             :       /* End of file.  */
      83           0 :       *nread = 0;
      84           0 :       return -1;
      85             :     }
      86           0 :   if (amount > count)
      87           0 :     amount = count;
      88           0 :   memcpy (buffer, text, amount);
      89           0 :   off += amount;
      90           0 :   *nread = amount;
      91           0 :   return 0;
      92             : }
      93             : 
      94             : void
      95          11 : read_once_test (round_t round, gpgme_data_t data)
      96             : {
      97             :   char buffer[1024];
      98             :   size_t read;
      99             : 
     100          11 :   read = gpgme_data_read (data, buffer, sizeof (buffer));
     101             : 
     102          11 :   if (read != strlen (text) || strncmp (buffer, text, strlen (text)))
     103             :     {
     104           0 :       fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
     105             :                __FILE__, __LINE__, round);
     106           0 :       exit (1);
     107             :     }
     108             : 
     109          11 :   read = gpgme_data_read (data, buffer, sizeof (buffer));
     110          11 :   if (read)
     111             :     {
     112           0 :       fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
     113             :                __FILE__, __LINE__, round);
     114           0 :       exit (1);
     115             :     }
     116          11 : }
     117             : 
     118             : void
     119           6 : read_test (round_t round, gpgme_data_t data)
     120             : {
     121             :   char buffer[1024];
     122             :   size_t read;
     123             : 
     124           6 :   if (round == TEST_INOUT_NONE)
     125             :     {
     126           1 :       read = gpgme_data_read (data, buffer, sizeof (buffer));
     127           1 :       if (read > 0)
     128             :         {
     129           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeeded unexpectedly\n",
     130             :                    __FILE__, __LINE__, round);
     131           0 :           exit (1);
     132             :         }
     133           1 :       return;
     134             :     }
     135             : 
     136           5 :   read_once_test (round, data);
     137           5 :   gpgme_data_seek (data, 0, SEEK_SET);
     138           5 :   read_once_test (round, data);
     139             : }
     140             : 
     141             : void
     142           6 : write_test (round_t round, gpgme_data_t data)
     143             : {
     144             :   char buffer[1024];
     145             :   size_t amt;
     146             : 
     147           6 :   amt = gpgme_data_write (data, text, strlen (text));
     148           6 :   if (amt != strlen (text))
     149           0 :     fail_if_err (gpgme_error_from_errno (errno));
     150             : 
     151           6 :   gpgme_data_seek (data, 0, SEEK_SET);
     152             : 
     153           6 :   if (round == TEST_INOUT_NONE)
     154           1 :     read_once_test (round, data);
     155             :   else
     156             :     {
     157           5 :       amt = gpgme_data_read (data, buffer, sizeof (buffer));
     158             : 
     159           5 :       if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
     160             :         {
     161           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
     162             :                    __FILE__, __LINE__, round);
     163           0 :           exit (1);
     164             :         }
     165             : 
     166           5 :       amt = gpgme_data_read (data, buffer, sizeof (buffer));
     167           5 :       if (amt)
     168             :         {
     169           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
     170             :                    __FILE__, __LINE__, round);
     171           0 :           exit (1);
     172             :         }
     173             :     }
     174           6 : }
     175             : 
     176             : 
     177             : int
     178           1 : main (void)
     179             : {
     180           1 :   round_t round = TEST_INITIALIZER;
     181           1 :   char *text_filename = make_filename ("t-data-1.txt");
     182           1 :   char *longer_text_filename = make_filename ("t-data-2.txt");
     183           1 :   const char *missing_filename = "this-file-surely-does-not-exist";
     184           1 :   gpgme_error_t err = 0;
     185             :   gpgme_data_t data;
     186             : 
     187           1 :   init_gpgme_basic ();
     188             : 
     189          12 :   while (++round)
     190             :     {
     191          11 :       switch (round)
     192             :         {
     193             :         case TEST_INVALID_ARGUMENT:
     194           1 :           err = gpgme_data_new (NULL);
     195           1 :           if (!err)
     196             :             {
     197           0 :               fprintf (stderr, "%s:%d: gpgme_data_new on NULL pointer succeeded "
     198             :                        "unexpectedly\n", __FILE__, __LINE__);
     199           0 :               exit (1);
     200             :             }
     201           1 :           continue;
     202             :         case TEST_INOUT_NONE:
     203           1 :           err = gpgme_data_new (&data);
     204           1 :           break;
     205             :         case TEST_INOUT_MEM_NO_COPY:
     206           1 :           err = gpgme_data_new_from_mem (&data, text, strlen (text), 0);
     207           1 :           break;
     208             :         case TEST_INOUT_MEM_COPY:
     209           1 :           err = gpgme_data_new_from_mem (&data, text, strlen (text), 1);
     210           1 :           break;
     211             :         case TEST_INOUT_MEM_FROM_FILE_COPY:
     212           1 :           err = gpgme_data_new_from_file (&data, text_filename, 1);
     213           1 :           break;
     214             :         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE:
     215           1 :           err = gpgme_data_new_from_file (&data, missing_filename, 1);
     216           1 :           if (!err)
     217             :             {
     218           0 :               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
     219             :                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
     220           0 :               exit (1);
     221             :             }
     222           1 :           continue;
     223             :         case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
     224           1 :           err = gpgme_data_new_from_file (&data, text_filename, 0);
     225             :           /* This is not implemented yet.  */
     226           1 :           if (gpgme_err_code (err) == GPG_ERR_NOT_IMPLEMENTED
     227           1 :               || gpgme_err_code (err) == GPG_ERR_INV_VALUE)
     228           1 :             continue;
     229           0 :           break;
     230             :         case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
     231           2 :           err = gpgme_data_new_from_filepart (&data, longer_text_filename, 0,
     232           1 :                                               strlen (text), strlen (text));
     233           1 :           break;
     234             :         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART:
     235           2 :           err = gpgme_data_new_from_filepart (&data, missing_filename, 0,
     236           1 :                                               strlen (text), strlen (text));
     237           1 :           if (!err)
     238             :             {
     239           0 :               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
     240             :                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
     241           0 :               exit (1);
     242             :             }
     243           1 :           continue;
     244             :         case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP:
     245             :           {
     246           1 :             FILE *fp = fopen (longer_text_filename, "rb");
     247           1 :             if (! fp)
     248             :               {
     249           0 :                 fprintf (stderr, "%s:%d: fopen: %s\n", __FILE__, __LINE__,
     250           0 :                          strerror (errno));
     251           0 :                 exit (1);
     252             :               }
     253           2 :             err = gpgme_data_new_from_filepart (&data, 0, fp,
     254           1 :                                                 strlen (text), strlen (text));
     255             :           }
     256           1 :           break;
     257             :         case TEST_END:
     258           1 :           goto out;
     259             :         case TEST_INITIALIZER:
     260             :           /* Shouldn't happen.  */
     261           0 :           fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__);
     262           0 :           exit (1);
     263             :         }
     264           6 :       fail_if_err (err);
     265             : 
     266           6 :       read_test (round, data);
     267           6 :       write_test (round, data);
     268           6 :       gpgme_data_release (data);
     269             :     }
     270             :  out:
     271           1 :   free (text_filename);
     272           1 :   free (longer_text_filename);
     273           1 :   return 0;
     274             : }

Generated by: LCOV version 1.13