aboutsummaryrefslogtreecommitdiff
path: root/src/main/test_ipc.c
blob: 4da3ef5d3d63aa60fccb45c1f21db26e9046cd6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
     This file is part of libextractor.
     Copyright (C) 2012 Vidyut Samanta and Christian Grothoff

     libextractor 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 3, or (at your
     option) any later version.

     libextractor 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 libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file main/test_ipc.c
 * @brief testcase for the extractor IPC using the "test" plugin
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"

/**
 * Return value from main, set to 0 for test to succeed.
 */
static int ret = 2;

#define HLO "Hello world!"
#define GOB "Goodbye!"

/**
 * Function that libextractor calls for each
 * meta data item found.  Should be called once
 * with 'Hello World!" and once with "Goodbye!".
 *
 * @param cls closure should be "main-cls"
 * @param plugin_name should be "test"
 * @param type should be "COMMENT"
 * @param format should be "UTF8"
 * @param data_mime_type should be "<no mime>"
 * @param data hello world or good bye
 * @param data_len number of bytes in data
 * @return 0 on hello world, 1 on goodbye
 */
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)
{
  if (0 != strcmp (cls,
                   "main-cls"))
  {
    fprintf (stderr, "closure invalid\n");
    ret = 3;
    return 1;
  }
  if (0 == strcmp (plugin_name,
                   "test2"))
    return 0; /* ignore 'test2' plugins */
  if (0 != strcmp (plugin_name,
                   "test"))
  {
    fprintf (stderr, "plugin name invalid\n");
    ret = 4;
    return 1;
  }
  if (EXTRACTOR_METATYPE_COMMENT != type)
  {
    fprintf (stderr, "type invalid\n");
    ret = 5;
    return 1;
  }
  if (EXTRACTOR_METAFORMAT_UTF8 != format)
  {
    fprintf (stderr, "format invalid\n");
    ret = 6;
    return 1;
  }
  if ( (NULL == data_mime_type) ||
       (0 != strcmp ("<no mime>",
                     data_mime_type) ) )
  {
    fprintf (stderr, "bad mime type\n");
    ret = 7;
    return 1;
  }
  if ( (2 == ret) &&
       (data_len == strlen (HLO) + 1) &&
       (0 == strncmp (data,
                      HLO,
                      strlen (HLO))) )
  {
#if 0
    fprintf (stderr, "Received '%s'\n", HLO);
#endif
    ret = 1;
    return 0;
  }
  if ( (1 == ret) &&
       (data_len == strlen (GOB) + 1) &&
       (0 == strncmp (data,
                      GOB,
                      strlen (GOB))) )
  {
#if 0
    fprintf (stderr, "Received '%s'\n", GOB);
#endif
    ret = 0;
    return 1;
  }
  fprintf (stderr, "Invalid meta data\n");
  ret = 8;
  return 1;
}


/**
 * Main function for the IPC testcase.
 *
 * @param argc number of arguments (ignored)
 * @param argv arguments (ignored)
 * @return 0 on success
 */
int
main (int argc, char *argv[])
{
  struct EXTRACTOR_PluginList *pl;
  unsigned char buf[1024 * 150];
  size_t i;

  /* initialize test buffer as expected by test plugin */
  for (i = 0; i<sizeof(buf); i++)
    buf[i] = (unsigned char) (i % 256);
  memcpy (buf, "test", 4);

  /* change environment to find 'extractor_test' plugin which is
     not installed but should be in the current directory (or .libs)
     on 'make check' */
  if (0 != putenv ("LIBEXTRACTOR_PREFIX=." PATH_SEPARATOR_STR ".libs/"))
    fprintf (stderr,
             "Failed to update my environment, plugin loading may fail: %s\n",
             strerror (errno));
  pl = EXTRACTOR_plugin_add_config (NULL, "test(test)",
                                    EXTRACTOR_OPTION_DEFAULT_POLICY);
  pl = EXTRACTOR_plugin_add_config (pl, "test2(test2)",
                                    EXTRACTOR_OPTION_DEFAULT_POLICY);
  if (NULL == pl)
  {
    fprintf (stderr, "failed to load test plugin\n");
    return 1;
  }
  EXTRACTOR_extract (pl, NULL, buf, sizeof (buf), &process_replies, "main-cls");
  if (0 == ret)
  {
    /* do it again... */
    ret = 2;
    EXTRACTOR_extract (pl, NULL, buf, sizeof (buf), &process_replies,
                       "main-cls");
  }
  EXTRACTOR_plugin_remove_all (pl);
  return ret;
}


/* end of test_ipc.c */