Blame tests/t-data.c

Packit Service 672cf4
/* t-data - Regression tests for the gpgme_data_t abstraction.
Packit Service 0ef63b
 * Copyright (C) 2001, 2004 g10 Code GmbH
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * This file is part of GPGME.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 0ef63b
 * under the terms of the GNU Lesser General Public License as
Packit Service 0ef63b
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 0ef63b
 * the License, or (at your option) any later version.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 0ef63b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0ef63b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 0ef63b
 * Lesser General Public License for more details.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * You should have received a copy of the GNU Lesser General Public
Packit Service 0ef63b
 * License along with this program; if not, see <https://gnu.org/licenses/>.
Packit Service 0ef63b
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit Service 0ef63b
 */
Packit Service 672cf4
Packit Service 672cf4
/* We need to include config.h so that we know whether we are building
Packit Service 672cf4
   with large file system (LFS) support. */
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <stddef.h>
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
Packit Service 672cf4
#define PGM "t-data"
Packit Service 672cf4
#include "run-support.h"
Packit Service 672cf4
Packit Service 672cf4
#undef fail_if_err
Packit Service 672cf4
#define fail_if_err(a) do { if(a) {                                          \
Packit Service 672cf4
                               fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \
Packit Service 672cf4
                                "%s\n", __FILE__, __LINE__, round,           \
Packit Service 672cf4
                                gpgme_strerror(a));                          \
Packit Service 672cf4
                                exit (1); }                                  \
Packit Service 672cf4
                             } while(0)
Packit Service 672cf4
Packit Service 672cf4
typedef enum
Packit Service 672cf4
  {
Packit Service 672cf4
    TEST_INITIALIZER,
Packit Service 672cf4
    TEST_INVALID_ARGUMENT,
Packit Service 672cf4
    TEST_INOUT_NONE,
Packit Service 672cf4
    TEST_INOUT_MEM_NO_COPY,
Packit Service 672cf4
    TEST_INOUT_MEM_COPY,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_FILE_COPY,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_INEXISTANT_FILE,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_FILE_NO_COPY,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART,
Packit Service 672cf4
    TEST_INOUT_MEM_FROM_FILE_PART_BY_FP,
Packit Service 672cf4
    TEST_END
Packit Service 672cf4
  } round_t;
Packit Service 672cf4
Packit Service 672cf4
const char *text = "Just GNU it!\n";
Packit Service 672cf4
const char *text2 = "Just GNU it!\nJust GNU it!\n";
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
Packit Service 672cf4
{
Packit Service 672cf4
  static int off = 0;
Packit Service 672cf4
  unsigned int amount = strlen (text) - off;
Packit Service 672cf4
  /*  round_t round = *((round_t *) cb_value);  */
Packit Service 672cf4
Packit Service 672cf4
  (void)cb_value;
Packit Service 672cf4
Packit Service 672cf4
  if (!buffer && !count && !nread)
Packit Service 672cf4
    {
Packit Service 672cf4
      /* Rewind requested.  */
Packit Service 672cf4
      off = 0;
Packit Service 672cf4
      return 0;
Packit Service 672cf4
    }
Packit Service 672cf4
  if (! buffer || !nread)
Packit Service 672cf4
    return -1;
Packit Service 672cf4
  if (amount <= 0)
Packit Service 672cf4
    {
Packit Service 672cf4
      /* End of file.  */
Packit Service 672cf4
      *nread = 0;
Packit Service 672cf4
      return -1;
Packit Service 672cf4
    }
Packit Service 672cf4
  if (amount > count)
Packit Service 672cf4
    amount = count;
Packit Service 672cf4
  memcpy (buffer, text, amount);
Packit Service 672cf4
  off += amount;
Packit Service 672cf4
  *nread = amount;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
read_once_test (round_t round, gpgme_data_t data)
Packit Service 672cf4
{
Packit Service 672cf4
  char buffer[1024];
Packit Service 672cf4
  size_t read;
Packit Service 672cf4
Packit Service 672cf4
  read = gpgme_data_read (data, buffer, sizeof (buffer));
Packit Service 672cf4
Packit Service 672cf4
  if (read != strlen (text) || strncmp (buffer, text, strlen (text)))
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
Packit Service 672cf4
	       __FILE__, __LINE__, round);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  read = gpgme_data_read (data, buffer, sizeof (buffer));
Packit Service 672cf4
  if (read)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
Packit Service 672cf4
	       __FILE__, __LINE__, round);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
read_test (round_t round, gpgme_data_t data)
Packit Service 672cf4
{
Packit Service 672cf4
  char buffer[1024];
Packit Service 672cf4
  size_t read;
Packit Service 672cf4
Packit Service 672cf4
  if (round == TEST_INOUT_NONE)
Packit Service 672cf4
    {
Packit Service 672cf4
      read = gpgme_data_read (data, buffer, sizeof (buffer));
Packit Service 672cf4
      if (read > 0)
Packit Service 672cf4
	{
Packit Service 672cf4
	  fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeeded unexpectedly\n",
Packit Service 672cf4
		   __FILE__, __LINE__, round);
Packit Service 672cf4
	  exit (1);
Packit Service 672cf4
	}
Packit Service 672cf4
      return;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  read_once_test (round, data);
Packit Service 672cf4
  gpgme_data_seek (data, 0, SEEK_SET);
Packit Service 672cf4
  read_once_test (round, data);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
write_test (round_t round, gpgme_data_t data)
Packit Service 672cf4
{
Packit Service 672cf4
  char buffer[1024];
Packit Service 672cf4
  size_t amt;
Packit Service 672cf4
Packit Service 672cf4
  amt = gpgme_data_write (data, text, strlen (text));
Packit Service 672cf4
  if (amt != strlen (text))
Packit Service 672cf4
    fail_if_err (gpgme_error_from_errno (errno));
Packit Service 672cf4
Packit Service 672cf4
  gpgme_data_seek (data, 0, SEEK_SET);
Packit Service 672cf4
Packit Service 672cf4
  if (round == TEST_INOUT_NONE)
Packit Service 672cf4
    read_once_test (round, data);
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      amt = gpgme_data_read (data, buffer, sizeof (buffer));
Packit Service 672cf4
Packit Service 672cf4
      if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
Packit Service 672cf4
	{
Packit Service 672cf4
	  fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
Packit Service 672cf4
		   __FILE__, __LINE__, round);
Packit Service 672cf4
	  exit (1);
Packit Service 672cf4
	}
Packit Service 672cf4
Packit Service 672cf4
      amt = gpgme_data_read (data, buffer, sizeof (buffer));
Packit Service 672cf4
      if (amt)
Packit Service 672cf4
	{
Packit Service 672cf4
	  fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
Packit Service 672cf4
		   __FILE__, __LINE__, round);
Packit Service 672cf4
	  exit (1);
Packit Service 672cf4
	}
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
main (void)
Packit Service 672cf4
{
Packit Service 672cf4
  round_t round = TEST_INITIALIZER;
Packit Service 672cf4
  char *text_filename = make_filename ("t-data-1.txt");
Packit Service 672cf4
  char *longer_text_filename = make_filename ("t-data-2.txt");
Packit Service 672cf4
  const char *missing_filename = "this-file-surely-does-not-exist";
Packit Service 672cf4
  gpgme_error_t err = 0;
Packit Service 672cf4
  gpgme_data_t data;
Packit Service 672cf4
Packit Service 672cf4
  init_gpgme_basic ();
Packit Service 672cf4
Packit Service 672cf4
  while (++round)
Packit Service 672cf4
    {
Packit Service 672cf4
      switch (round)
Packit Service 672cf4
	{
Packit Service 672cf4
	case TEST_INVALID_ARGUMENT:
Packit Service 672cf4
	  err = gpgme_data_new (NULL);
Packit Service 672cf4
	  if (!err)
Packit Service 672cf4
	    {
Packit Service 672cf4
	      fprintf (stderr, "%s:%d: gpgme_data_new on NULL pointer succeeded "
Packit Service 672cf4
		       "unexpectedly\n", __FILE__, __LINE__);
Packit Service 672cf4
	      exit (1);
Packit Service 672cf4
	    }
Packit Service 672cf4
	  continue;
Packit Service 672cf4
	case TEST_INOUT_NONE:
Packit Service 672cf4
	  err = gpgme_data_new (&data);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_NO_COPY:
Packit Service 672cf4
	  err = gpgme_data_new_from_mem (&data, text, strlen (text), 0);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_COPY:
Packit Service 672cf4
	  err = gpgme_data_new_from_mem (&data, text, strlen (text), 1);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_FILE_COPY:
Packit Service 672cf4
	  err = gpgme_data_new_from_file (&data, text_filename, 1);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_INEXISTANT_FILE:
Packit Service 672cf4
	  err = gpgme_data_new_from_file (&data, missing_filename, 1);
Packit Service 672cf4
	  if (!err)
Packit Service 672cf4
	    {
Packit Service 672cf4
	      fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
Packit Service 672cf4
		       "file succeeded unexpectedly\n", __FILE__, __LINE__);
Packit Service 672cf4
	      exit (1);
Packit Service 672cf4
	    }
Packit Service 672cf4
	  continue;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
Packit Service 672cf4
	  err = gpgme_data_new_from_file (&data, text_filename, 0);
Packit Service 672cf4
	  /* This is not implemented yet.  */
Packit Service 672cf4
	  if (gpgme_err_code (err) == GPG_ERR_NOT_IMPLEMENTED
Packit Service 672cf4
	      || gpgme_err_code (err) == GPG_ERR_INV_VALUE)
Packit Service 672cf4
	    continue;
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
Packit Service 672cf4
	  err = gpgme_data_new_from_filepart (&data, longer_text_filename, 0,
Packit Service 672cf4
					      strlen (text), strlen (text));
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART:
Packit Service 672cf4
	  err = gpgme_data_new_from_filepart (&data, missing_filename, 0,
Packit Service 672cf4
					      strlen (text), strlen (text));
Packit Service 672cf4
	  if (!err)
Packit Service 672cf4
	    {
Packit Service 672cf4
	      fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
Packit Service 672cf4
		       "file succeeded unexpectedly\n", __FILE__, __LINE__);
Packit Service 672cf4
	      exit (1);
Packit Service 672cf4
	    }
Packit Service 672cf4
	  continue;
Packit Service 672cf4
	case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP:
Packit Service 672cf4
	  {
Packit Service 672cf4
	    FILE *fp = fopen (longer_text_filename, "rb");
Packit Service 672cf4
	    if (! fp)
Packit Service 672cf4
	      {
Packit Service 672cf4
		fprintf (stderr, "%s:%d: fopen: %s\n", __FILE__, __LINE__,
Packit Service 672cf4
			 strerror (errno));
Packit Service 672cf4
		exit (1);
Packit Service 672cf4
	      }
Packit Service 672cf4
	    err = gpgme_data_new_from_filepart (&data, 0, fp,
Packit Service 672cf4
						strlen (text), strlen (text));
Packit Service 672cf4
	  }
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case TEST_END:
Packit Service 672cf4
	  goto out;
Packit Service 672cf4
	case TEST_INITIALIZER:
Packit Service 672cf4
	  /* Shouldn't happen.  */
Packit Service 672cf4
	  fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__);
Packit Service 672cf4
	  exit (1);
Packit Service 672cf4
	}
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
      read_test (round, data);
Packit Service 672cf4
      write_test (round, data);
Packit Service 672cf4
      gpgme_data_release (data);
Packit Service 672cf4
    }
Packit Service 672cf4
 out:
Packit Service 672cf4
  free (text_filename);
Packit Service 672cf4
  free (longer_text_filename);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}