libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

test_ipc.c (4758B)


      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_ipc.c
     22  * @brief testcase for the extractor IPC using the "test" plugin
     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                    "test2"))
     68     return 0; /* ignore 'test2' plugins */
     69   if (0 != strcmp (plugin_name,
     70                    "test"))
     71   {
     72     fprintf (stderr, "plugin name invalid\n");
     73     ret = 4;
     74     return 1;
     75   }
     76   if (EXTRACTOR_METATYPE_COMMENT != type)
     77   {
     78     fprintf (stderr, "type invalid\n");
     79     ret = 5;
     80     return 1;
     81   }
     82   if (EXTRACTOR_METAFORMAT_UTF8 != format)
     83   {
     84     fprintf (stderr, "format invalid\n");
     85     ret = 6;
     86     return 1;
     87   }
     88   if ( (NULL == data_mime_type) ||
     89        (0 != strcmp ("<no mime>",
     90                      data_mime_type) ) )
     91   {
     92     fprintf (stderr, "bad mime type\n");
     93     ret = 7;
     94     return 1;
     95   }
     96   if ( (2 == ret) &&
     97        (data_len == strlen (HLO) + 1) &&
     98        (0 == strncmp (data,
     99                       HLO,
    100                       strlen (HLO))) )
    101   {
    102 #if 0
    103     fprintf (stderr, "Received '%s'\n", HLO);
    104 #endif
    105     ret = 1;
    106     return 0;
    107   }
    108   if ( (1 == ret) &&
    109        (data_len == strlen (GOB) + 1) &&
    110        (0 == strncmp (data,
    111                       GOB,
    112                       strlen (GOB))) )
    113   {
    114 #if 0
    115     fprintf (stderr, "Received '%s'\n", GOB);
    116 #endif
    117     ret = 0;
    118     return 1;
    119   }
    120   fprintf (stderr, "Invalid meta data\n");
    121   ret = 8;
    122   return 1;
    123 }
    124 
    125 
    126 /**
    127  * Main function for the IPC testcase.
    128  *
    129  * @param argc number of arguments (ignored)
    130  * @param argv arguments (ignored)
    131  * @return 0 on success
    132  */
    133 int
    134 main (int argc, char *argv[])
    135 {
    136   struct EXTRACTOR_PluginList *pl;
    137   unsigned char buf[1024 * 150];
    138   size_t i;
    139 
    140   /* initialize test buffer as expected by test plugin */
    141   for (i = 0; i<sizeof(buf); i++)
    142     buf[i] = (unsigned char) (i % 256);
    143   memcpy (buf, "test", 4);
    144 
    145   /* change environment to find 'extractor_test' plugin which is
    146      not installed but should be in the current directory (or .libs)
    147      on 'make check' */
    148   if (0 != putenv ("LIBEXTRACTOR_PREFIX=.:.libs/"))
    149     fprintf (stderr,
    150              "Failed to update my environment, plugin loading may fail: %s\n",
    151              strerror (errno));
    152   pl = EXTRACTOR_plugin_add_config (NULL, "test(test)",
    153                                     EXTRACTOR_OPTION_DEFAULT_POLICY);
    154   pl = EXTRACTOR_plugin_add_config (pl, "test2(test2)",
    155                                     EXTRACTOR_OPTION_DEFAULT_POLICY);
    156   if (NULL == pl)
    157   {
    158     fprintf (stderr, "failed to load test plugin\n");
    159     return 1;
    160   }
    161   EXTRACTOR_extract (pl, NULL, buf, sizeof (buf), &process_replies, "main-cls");
    162   if (0 == ret)
    163   {
    164     /* do it again... */
    165     ret = 2;
    166     EXTRACTOR_extract (pl, NULL, buf, sizeof (buf), &process_replies,
    167                        "main-cls");
    168   }
    169   EXTRACTOR_plugin_remove_all (pl);
    170   return ret;
    171 }
    172 
    173 
    174 /* end of test_ipc.c */