LCOV - code coverage report
Current view: top level - tests - t-data.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 80 123 65.0 %
Date: 2016-11-29 15:07:43 Functions: 5 6 83.3 %

          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             : #include <gpgme.h>
      34             : 
      35             : #define fail_if_err(a) do { if(a) {                                          \
      36             :                                fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \
      37             :                                 "%s\n", __FILE__, __LINE__, round,           \
      38             :                                 gpgme_strerror(a));                          \
      39             :                                 exit (1); }                                  \
      40             :                              } while(0)
      41             : 
      42             : static char *
      43           2 : make_filename (const char *fname)
      44             : {
      45           2 :   const char *srcdir = getenv ("srcdir");
      46             :   char *buf;
      47             : 
      48           2 :   if (!srcdir)
      49           0 :     srcdir = ".";
      50           2 :   buf = malloc (strlen(srcdir) + strlen(fname) + 2 );
      51           2 :   if (!buf)
      52             :     {
      53           0 :       fprintf (stderr, "%s:%d: could not allocate string: %s\n",
      54           0 :                __FILE__, __LINE__, strerror (errno));
      55           0 :       exit (1);
      56             :     }
      57           2 :   strcpy (buf, srcdir);
      58           2 :   strcat (buf, "/");
      59           2 :   strcat (buf, fname);
      60           2 :   return buf;
      61             : }
      62             : 
      63             : typedef enum
      64             :   {
      65             :     TEST_INITIALIZER,
      66             :     TEST_INVALID_ARGUMENT,
      67             :     TEST_INOUT_NONE,
      68             :     TEST_INOUT_MEM_NO_COPY,
      69             :     TEST_INOUT_MEM_COPY,
      70             :     TEST_INOUT_MEM_FROM_FILE_COPY,
      71             :     TEST_INOUT_MEM_FROM_INEXISTANT_FILE,
      72             :     TEST_INOUT_MEM_FROM_FILE_NO_COPY,
      73             :     TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME,
      74             :     TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART,
      75             :     TEST_INOUT_MEM_FROM_FILE_PART_BY_FP,
      76             :     TEST_END
      77             :   } round_t;
      78             : 
      79             : const char *text = "Just GNU it!\n";
      80             : const char *text2 = "Just GNU it!\nJust GNU it!\n";
      81             : 
      82             : int
      83           0 : read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
      84             : {
      85             :   static int off = 0;
      86           0 :   unsigned int amount = strlen (text) - off;
      87             :   /*  round_t round = *((round_t *) cb_value);  */
      88             : 
      89             :   (void)cb_value;
      90             : 
      91           0 :   if (!buffer && !count && !nread)
      92             :     {
      93             :       /* Rewind requested.  */
      94           0 :       off = 0;
      95           0 :       return 0;
      96             :     }
      97           0 :   if (! buffer || !nread)
      98           0 :     return -1;
      99           0 :   if (amount <= 0)
     100             :     {
     101             :       /* End of file.  */
     102           0 :       *nread = 0;
     103           0 :       return -1;
     104             :     }
     105           0 :   if (amount > count)
     106           0 :     amount = count;
     107           0 :   memcpy (buffer, text, amount);
     108           0 :   off += amount;
     109           0 :   *nread = amount;
     110           0 :   return 0;
     111             : }
     112             : 
     113             : void
     114          11 : read_once_test (round_t round, gpgme_data_t data)
     115             : {
     116             :   char buffer[1024];
     117             :   size_t read;
     118             : 
     119          11 :   read = gpgme_data_read (data, buffer, sizeof (buffer));
     120             : 
     121          11 :   if (read != strlen (text) || strncmp (buffer, text, strlen (text)))
     122             :     {
     123           0 :       fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
     124             :                __FILE__, __LINE__, round);
     125           0 :       exit (1);
     126             :     }
     127             : 
     128          11 :   read = gpgme_data_read (data, buffer, sizeof (buffer));
     129          11 :   if (read)
     130             :     {
     131           0 :       fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
     132             :                __FILE__, __LINE__, round);
     133           0 :       exit (1);
     134             :     }
     135          11 : }
     136             : 
     137             : void
     138           6 : read_test (round_t round, gpgme_data_t data)
     139             : {
     140             :   char buffer[1024];
     141             :   size_t read;
     142             : 
     143           6 :   if (round == TEST_INOUT_NONE)
     144             :     {
     145           1 :       read = gpgme_data_read (data, buffer, sizeof (buffer));
     146           1 :       if (read > 0)
     147             :         {
     148           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeeded unexpectedly\n",
     149             :                    __FILE__, __LINE__, round);
     150           0 :           exit (1);
     151             :         }
     152           7 :       return;
     153             :     }
     154             : 
     155           5 :   read_once_test (round, data);
     156           5 :   gpgme_data_seek (data, 0, SEEK_SET);
     157           5 :   read_once_test (round, data);
     158             : }
     159             : 
     160             : void
     161           6 : write_test (round_t round, gpgme_data_t data)
     162             : {
     163             :   char buffer[1024];
     164             :   size_t amt;
     165             : 
     166           6 :   amt = gpgme_data_write (data, text, strlen (text));
     167           6 :   if (amt != strlen (text))
     168           0 :     fail_if_err (gpgme_error_from_errno (errno));
     169             : 
     170           6 :   gpgme_data_seek (data, 0, SEEK_SET);
     171             : 
     172           6 :   if (round == TEST_INOUT_NONE)
     173           1 :     read_once_test (round, data);
     174             :   else
     175             :     {
     176           5 :       amt = gpgme_data_read (data, buffer, sizeof (buffer));
     177             : 
     178           5 :       if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
     179             :         {
     180           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
     181             :                    __FILE__, __LINE__, round);
     182           0 :           exit (1);
     183             :         }
     184             : 
     185           5 :       amt = gpgme_data_read (data, buffer, sizeof (buffer));
     186           5 :       if (amt)
     187             :         {
     188           0 :           fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
     189             :                    __FILE__, __LINE__, round);
     190           0 :           exit (1);
     191             :         }
     192             :     }
     193           6 : }
     194             : 
     195             : 
     196             : int
     197           1 : main (void)
     198             : {
     199           1 :   round_t round = TEST_INITIALIZER;
     200           1 :   char *text_filename = make_filename ("t-data-1.txt");
     201           1 :   char *longer_text_filename = make_filename ("t-data-2.txt");
     202           1 :   const char *missing_filename = "this-file-surely-does-not-exist";
     203           1 :   gpgme_error_t err = 0;
     204             :   gpgme_data_t data;
     205             : 
     206          12 :   while (++round)
     207             :     {
     208          11 :       switch (round)
     209             :         {
     210             :         case TEST_INVALID_ARGUMENT:
     211           1 :           err = gpgme_data_new (NULL);
     212           1 :           if (!err)
     213             :             {
     214           0 :               fprintf (stderr, "%s:%d: gpgme_data_new on NULL pointer succeeded "
     215             :                        "unexpectedly\n", __FILE__, __LINE__);
     216           0 :               exit (1);
     217             :             }
     218           1 :           continue;
     219             :         case TEST_INOUT_NONE:
     220           1 :           err = gpgme_data_new (&data);
     221           1 :           break;
     222             :         case TEST_INOUT_MEM_NO_COPY:
     223           1 :           err = gpgme_data_new_from_mem (&data, text, strlen (text), 0);
     224           1 :           break;
     225             :         case TEST_INOUT_MEM_COPY:
     226           1 :           err = gpgme_data_new_from_mem (&data, text, strlen (text), 1);
     227           1 :           break;
     228             :         case TEST_INOUT_MEM_FROM_FILE_COPY:
     229           1 :           err = gpgme_data_new_from_file (&data, text_filename, 1);
     230           1 :           break;
     231             :         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE:
     232           1 :           err = gpgme_data_new_from_file (&data, missing_filename, 1);
     233           1 :           if (!err)
     234             :             {
     235           0 :               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
     236             :                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
     237           0 :               exit (1);
     238             :             }
     239           1 :           continue;
     240             :         case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
     241           1 :           err = gpgme_data_new_from_file (&data, text_filename, 0);
     242             :           /* This is not implemented yet.  */
     243           1 :           if (gpgme_err_code (err) == GPG_ERR_NOT_IMPLEMENTED
     244           1 :               || gpgme_err_code (err) == GPG_ERR_INV_VALUE)
     245           1 :             continue;
     246           0 :           break;
     247             :         case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
     248           2 :           err = gpgme_data_new_from_filepart (&data, longer_text_filename, 0,
     249           1 :                                               strlen (text), strlen (text));
     250           1 :           break;
     251             :         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART:
     252           2 :           err = gpgme_data_new_from_filepart (&data, missing_filename, 0,
     253           1 :                                               strlen (text), strlen (text));
     254           1 :           if (!err)
     255             :             {
     256           0 :               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
     257             :                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
     258           0 :               exit (1);
     259             :             }
     260           1 :           continue;
     261             :         case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP:
     262             :           {
     263           1 :             FILE *fp = fopen (longer_text_filename, "rb");
     264           1 :             if (! fp)
     265             :               {
     266           0 :                 fprintf (stderr, "%s:%d: fopen: %s\n", __FILE__, __LINE__,
     267           0 :                          strerror (errno));
     268           0 :                 exit (1);
     269             :               }
     270           2 :             err = gpgme_data_new_from_filepart (&data, 0, fp,
     271           1 :                                                 strlen (text), strlen (text));
     272             :           }
     273           1 :           break;
     274             :         case TEST_END:
     275           1 :           goto out;
     276             :         case TEST_INITIALIZER:
     277             :           /* Shouldn't happen.  */
     278           0 :           fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__);
     279           0 :           exit (1);
     280             :         }
     281           6 :       fail_if_err (err);
     282             : 
     283           6 :       read_test (round, data);
     284           6 :       write_test (round, data);
     285           6 :       gpgme_data_release (data);
     286             :     }
     287             :  out:
     288           1 :   free (text_filename);
     289           1 :   free (longer_text_filename);
     290           1 :   return 0;
     291             : }

Generated by: LCOV version 1.11