"Fossies" - the Fresh Open Source Software Archive

Member "libextractor-1.11/src/main/test_gzip.c" (30 Jan 2021, 4305 Bytes) of package /linux/privat/libextractor-1.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_gzip.c": 1.10_vs_1.11.

    1 /*
    2      This file is part of libextractor.
    3      Copyright (C) 2012 Vidyut Samanta and Christian Grothoff
    4 
    5      libextractor is free software; you can redistribute it and/or modify
    6      it under the terms of the GNU General Public License as published
    7      by the Free Software Foundation; either version 3, or (at your
    8      option) any later version.
    9 
   10      libextractor is distributed in the hope that it will be useful, but
   11      WITHOUT ANY WARRANTY; without even the implied warranty of
   12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13      General Public License for more details.
   14 
   15      You should have received a copy of the GNU General Public License
   16      along with libextractor; see the file COPYING.  If not, write to the
   17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   18      Boston, MA 02110-1301, USA.
   19 */
   20 /**
   21  * @file main/test_gzip.c
   22  * @brief testcase for gzip decompression in the extractor
   23  * @author Christian Grothoff
   24  */
   25 #include "platform.h"
   26 #include "extractor.h"
   27 
   28 /**
   29  * Return value from main, set to 0 for test to succeed.
   30  */
   31 static int ret = 2;
   32 
   33 #define HLO "Hello world!"
   34 #define GOB "Goodbye!"
   35 
   36 /**
   37  * Function that libextractor calls for each
   38  * meta data item found.  Should be called once
   39  * with 'Hello World!" and once with "Goodbye!".
   40  *
   41  * @param cls closure should be "main-cls"
   42  * @param plugin_name should be "test"
   43  * @param type should be "COMMENT"
   44  * @param format should be "UTF8"
   45  * @param data_mime_type should be "<no mime>"
   46  * @param data hello world or good bye
   47  * @param data_len number of bytes in data
   48  * @return 0 on hello world, 1 on goodbye
   49  */
   50 static int
   51 process_replies (void *cls,
   52                  const char *plugin_name,
   53                  enum EXTRACTOR_MetaType type,
   54                  enum EXTRACTOR_MetaFormat format,
   55                  const char *data_mime_type,
   56                  const char *data,
   57                  size_t data_len)
   58 {
   59   if (0 != strcmp (cls,
   60                    "main-cls"))
   61   {
   62     fprintf (stderr, "closure invalid\n");
   63     ret = 3;
   64     return 1;
   65   }
   66   if (0 == strcmp (plugin_name,
   67                    "<zlib>"))
   68     return 0; /* skip this one */
   69   if (0 != strcmp (plugin_name,
   70                    "test"))
   71   {
   72     fprintf (stderr, "plugin name invalid: `%s'\n",
   73              plugin_name);
   74     ret = 4;
   75     return 1;
   76   }
   77   if (EXTRACTOR_METATYPE_COMMENT != type)
   78   {
   79     fprintf (stderr, "type invalid\n");
   80     ret = 5;
   81     return 1;
   82   }
   83   if (EXTRACTOR_METAFORMAT_UTF8 != format)
   84   {
   85     fprintf (stderr, "format invalid\n");
   86     ret = 6;
   87     return 1;
   88   }
   89   if ( (NULL == data_mime_type) ||
   90        (0 != strcmp ("<no mime>",
   91                      data_mime_type) ) )
   92   {
   93     fprintf (stderr, "bad mime type\n");
   94     ret = 7;
   95     return 1;
   96   }
   97   if ( (2 == ret) &&
   98        (data_len == strlen (HLO) + 1) &&
   99        (0 == strncmp (data,
  100                       HLO,
  101                       strlen (HLO))) )
  102   {
  103 #if 0
  104     fprintf (stderr, "Received '%s'\n", HLO);
  105 #endif
  106     ret = 1;
  107     return 0;
  108   }
  109   if ( (1 == ret) &&
  110        (data_len == strlen (GOB) + 1) &&
  111        (0 == strncmp (data,
  112                       GOB,
  113                       strlen (GOB))) )
  114   {
  115 #if 0
  116     fprintf (stderr, "Received '%s'\n", GOB);
  117 #endif
  118     ret = 0;
  119     return 1;
  120   }
  121   fprintf (stderr, "Invalid meta data\n");
  122   ret = 8;
  123   return 1;
  124 }
  125 
  126 
  127 /**
  128  * Main function for the gzip testcase.
  129  *
  130  * @param argc number of arguments (ignored)
  131  * @param argv arguments (ignored)
  132  * @return 0 on success
  133  */
  134 int
  135 main (int argc, char *argv[])
  136 {
  137   struct EXTRACTOR_PluginList *pl;
  138 
  139   /* change environment to find 'extractor_test' plugin which is
  140      not installed but should be in the current directory (or .libs)
  141      on 'make check' */
  142   if (0 != putenv ("LIBEXTRACTOR_PREFIX=.:.libs/"))
  143     fprintf (stderr,
  144              "Failed to update my environment, plugin loading may fail: %s\n",
  145              strerror (errno));
  146   pl = EXTRACTOR_plugin_add_config (NULL, "test(test)",
  147                                     EXTRACTOR_OPTION_DEFAULT_POLICY);
  148   if (NULL == pl)
  149   {
  150     fprintf (stderr, "failed to load test plugin\n");
  151     return 1;
  152   }
  153   EXTRACTOR_extract (pl, "test_file.dat.gz", NULL, 0, &process_replies,
  154                      "main-cls");
  155   EXTRACTOR_plugin_remove_all (pl);
  156   return ret;
  157 }
  158 
  159 
  160 /* end of test_gzip.c */