Use gpgme interface for error handling to avoid linking with gpg-error.
[gpgme.git] / tests / t-data.c
index 47a8926bfaae93023b20b79765b0f9561875fbed..a319484f21471752dab3c2efd3b6e8323fb32558 100644 (file)
@@ -1,35 +1,42 @@
-/* t-data - Regression tests for the GpgmeData abstraction.
- *      Copyright (C) 2001 g10 Code GmbH
- *
- * This file is part of GPGME.
- *
- * GPGME is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * GPGME is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- */
+/* t-data - Regression tests for the gpgme_data_t abstraction.
+   Copyright (C) 2001, 2004 g10 Code GmbH
 
+   This file is part of GPGME.
+
+   GPGME is free software; you can redistribute it and/or modify it
+   under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+   
+   GPGME is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+   
+   You should have received a copy of the GNU Lesser General Public
+   License along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.  */
+
+/* We need to include config.h so that we know whether we are building
+   with large file system (LFS) support. */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 
-#include "../gpgme/gpgme.h"
+#include <gpgme.h>
 
-#define fail_if_err(a) do { if(a) {                                       \
-                               fprintf (stderr, "%s:%d: (%i) GpgmeError " \
-                                "%s\n", __FILE__, __LINE__, round,        \
-                                gpgme_strerror(a));                       \
-                                exit (1); }                               \
+#define fail_if_err(a) do { if(a) {                                          \
+                               fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \
+                                "%s\n", __FILE__, __LINE__, round,           \
+                                gpgme_strerror(a));                          \
+                                exit (1); }                                  \
                              } while(0)
 
 static char *
@@ -66,7 +73,6 @@ typedef enum
     TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME,
     TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART,
     TEST_INOUT_MEM_FROM_FILE_PART_BY_FP,
-    TEST_OUT_CB,
     TEST_END
   } round_t;
 
@@ -77,7 +83,7 @@ int
 read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
 {
   static int off = 0;
-  int amount = strlen (text) - off;
+  unsigned int amount = strlen (text) - off;
   /*  round_t round = *((round_t *) cb_value);  */
 
   if (!buffer && !count && !nread)
@@ -103,14 +109,12 @@ read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
 }
 
 void
-read_once_test (round_t round, GpgmeData data)
+read_once_test (round_t round, gpgme_data_t data)
 {
-  GpgmeError err;
   char buffer[1024];
-  int read;
+  size_t read;
 
-  err = gpgme_data_read (data, buffer, sizeof (buffer), &read);
-  fail_if_err (err);
+  read = gpgme_data_read (data, buffer, sizeof (buffer));
 
   if (read != strlen (text) || strncmp (buffer, text, strlen (text)))
     {
@@ -119,8 +123,8 @@ read_once_test (round_t round, GpgmeData data)
       exit (1);
     }
 
-  err = gpgme_data_read (data, buffer, sizeof (buffer), &read);
-  if (err != GPGME_EOF)
+  read = gpgme_data_read (data, buffer, sizeof (buffer));
+  if (read)
     {
       fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
               __FILE__, __LINE__, round);
@@ -129,16 +133,15 @@ read_once_test (round_t round, GpgmeData data)
 }
 
 void
-read_test (round_t round, GpgmeData data)
+read_test (round_t round, gpgme_data_t data)
 {
-  GpgmeError err;
   char buffer[1024];
-  int read;
+  size_t read;
 
   if (round == TEST_INOUT_NONE)
     {
-      err = gpgme_data_read (data, buffer, sizeof (buffer), &read);
-      if (!err)
+      read = gpgme_data_read (data, buffer, sizeof (buffer));
+      if (read > 0)
        {
          fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeded unexpectedly\n",
                   __FILE__, __LINE__, round);
@@ -148,41 +151,37 @@ read_test (round_t round, GpgmeData data)
     }
 
   read_once_test (round, data);
-  err = gpgme_data_rewind (data);
-  fail_if_err (err);
+  gpgme_data_seek (data, 0, SEEK_SET);
   read_once_test (round, data);
 }
 
 void
-write_test (round_t round, GpgmeData data)
+write_test (round_t round, gpgme_data_t data)
 {
-  GpgmeError err;
   char buffer[1024];
-  int read;
+  size_t amt;
 
-  err = gpgme_data_write (data, text, strlen (text));
-  fail_if_err (err);
+  amt = gpgme_data_write (data, text, strlen (text));
+  if (amt != strlen (text))
+    fail_if_err (gpgme_error_from_errno (errno));
 
-  read_once_test (round, data);
-  err = gpgme_data_rewind (data);
-  fail_if_err (err);
+  gpgme_data_seek (data, 0, SEEK_SET);
 
   if (round == TEST_INOUT_NONE)
     read_once_test (round, data);
   else
     {
-      err = gpgme_data_read (data, buffer, sizeof (buffer), &read);
-      fail_if_err (err);
+      amt = gpgme_data_read (data, buffer, sizeof (buffer));
 
-      if (read != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
+      if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
        {
          fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
                   __FILE__, __LINE__, round);
          exit (1);
        }
 
-      err = gpgme_data_read (data, buffer, sizeof (buffer), &read);
-      if (err != GPGME_EOF)
+      amt = gpgme_data_read (data, buffer, sizeof (buffer));
+      if (amt)
        {
          fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
                   __FILE__, __LINE__, round);
@@ -192,14 +191,14 @@ write_test (round_t round, GpgmeData data)
 }
 
 int 
-main (int argc, char **argv )
+main (int argc, char **argv)
 {
   round_t round = TEST_INITIALIZER;
   const char *text_filename = make_filename ("t-data-1.txt");
   const char *longer_text_filename = make_filename ("t-data-2.txt");
   const char *missing_filename = "this-file-surely-does-not-exist";
-  GpgmeError err = GPGME_No_Error;
-  GpgmeData data;
+  gpgme_error_t err = 0;
+  gpgme_data_t data;
 
   while (++round)
     {
@@ -213,12 +212,6 @@ main (int argc, char **argv )
                       "unexpectedly\n", __FILE__, __LINE__);
              exit (1);
            }
-         if (gpgme_data_get_type (NULL) != GPGME_DATA_TYPE_NONE)
-           {
-             fprintf (stderr, "%s:%d: gpgme_data_get_type on NULL incorrect\n",
-                      __FILE__, __LINE__);
-             exit (1);
-           }
          continue;
        case TEST_INOUT_NONE:
          err = gpgme_data_new (&data);
@@ -244,7 +237,8 @@ main (int argc, char **argv )
        case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
          err = gpgme_data_new_from_file (&data, text_filename, 0);
          /* This is not implemented yet.  */
-         if (err == GPGME_Not_Implemented)
+         if (gpgme_err_code (err) == GPG_ERR_NOT_IMPLEMENTED
+             || gpgme_err_code (err) == GPG_ERR_INV_VALUE)
            continue;
          break;
        case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
@@ -274,9 +268,6 @@ main (int argc, char **argv )
                                                strlen (text), strlen (text));
          }
          break;
-       case TEST_OUT_CB:
-         err = gpgme_data_new_with_read_cb (&data, read_cb, &round);
-         break;
        case TEST_END:
          return 0;
        case TEST_INITIALIZER:
@@ -286,37 +277,8 @@ main (int argc, char **argv )
        }
       fail_if_err (err);
 
-      switch (round)
-       {
-       case TEST_INOUT_NONE:
-         if (gpgme_data_get_type (data) != GPGME_DATA_TYPE_NONE)
-           err = GPGME_Invalid_Type;
-         break;
-       case TEST_INOUT_MEM_NO_COPY:
-       case TEST_INOUT_MEM_COPY:
-       case TEST_INOUT_MEM_FROM_FILE_COPY:
-       case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
-       case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
-       case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP:
-         if (gpgme_data_get_type (data) != GPGME_DATA_TYPE_MEM)
-           err = GPGME_Invalid_Type;
-         break;
-       case TEST_OUT_CB:
-         if (gpgme_data_get_type (data) != GPGME_DATA_TYPE_CB)
-           err = GPGME_Invalid_Type;
-         break;
-       case TEST_INITIALIZER:
-       case TEST_INVALID_ARGUMENT:
-       case TEST_INOUT_MEM_FROM_INEXISTANT_FILE:
-       case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART:
-       case TEST_END:
-         /* Shouldn't happen.  */
-         fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__);
-         exit (1);
-       }
       read_test (round, data);
-      if (round != TEST_OUT_CB)
-       write_test (round, data);
+      write_test (round, data);
       gpgme_data_release (data);
     }
   return 0;