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