first batch of license fixes (boring)
[oweals/gnunet.git] / src / namecache / test_plugin_namecache.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet 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      Affero General Public License for more details.
14 */
15 /*
16  * @file namecache/test_plugin_namecache.c
17  * @brief Test for the namecache plugins
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_namecache_plugin.h"
23 #include "gnunet_testing_lib.h"
24
25
26 static int ok;
27
28 /**
29  * Name of plugin under test.
30  */
31 static const char *plugin_name;
32
33
34 /**
35  * Function called when the service shuts down.  Unloads our namecache
36  * plugin.
37  *
38  * @param api api to unload
39  */
40 static void
41 unload_plugin (struct GNUNET_NAMECACHE_PluginFunctions *api)
42 {
43   char *libname;
44
45   GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
46   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
47   GNUNET_free (libname);
48 }
49
50
51 /**
52  * Load the namecache plugin.
53  *
54  * @param cfg configuration to pass
55  * @return NULL on error
56  */
57 static struct GNUNET_NAMECACHE_PluginFunctions *
58 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
59 {
60   struct GNUNET_NAMECACHE_PluginFunctions *ret;
61   char *libname;
62
63   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namecache plugin\n"),
64               plugin_name);
65   GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
66   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
67   {
68     FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
69     GNUNET_free (libname);
70     return NULL;
71   }
72   GNUNET_free (libname);
73   return ret;
74 }
75
76
77 static void
78 run (void *cls, char *const *args, const char *cfgfile,
79      const struct GNUNET_CONFIGURATION_Handle *cfg)
80 {
81   struct GNUNET_NAMECACHE_PluginFunctions *nsp;
82
83   ok = 0;
84   nsp = load_plugin (cfg);
85   if (NULL == nsp)
86   {
87     FPRINTF (stderr,
88              "%s",
89              "Failed to initialize namecache.  Database likely not setup, skipping test.\n");
90     return;
91   }
92
93   unload_plugin (nsp);
94 }
95
96
97 int
98 main (int argc, char *argv[])
99 {
100   char cfg_name[128];
101   char *const xargv[] = {
102     "test-plugin-namecache",
103     "-c",
104     cfg_name,
105     NULL
106   };
107   struct GNUNET_GETOPT_CommandLineOption options[] = {
108     GNUNET_GETOPT_OPTION_END
109   };
110
111   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
112   GNUNET_log_setup ("test-plugin-namecache",
113                     "WARNING",
114                     NULL);
115   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
116   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namecache_%s.conf",
117                    plugin_name);
118   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
119                       "test-plugin-namecache", "nohelp", options, &run, NULL);
120   if (ok != 0)
121     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
122   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
123   return ok;
124 }
125
126 /* end of test_plugin_namecache.c */