paragraph for gnunet devs that don't know how to use the web
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /*
19  * @file namecache/test_plugin_namecache.c
20  * @brief Test for the namecache plugins
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_namecache_plugin.h"
26 #include "gnunet_testing_lib.h"
27
28
29 static int ok;
30
31 /**
32  * Name of plugin under test.
33  */
34 static const char *plugin_name;
35
36
37 /**
38  * Function called when the service shuts down.  Unloads our namecache
39  * plugin.
40  *
41  * @param api api to unload
42  */
43 static void
44 unload_plugin (struct GNUNET_NAMECACHE_PluginFunctions *api)
45 {
46   char *libname;
47
48   GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
49   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
50   GNUNET_free (libname);
51 }
52
53
54 /**
55  * Load the namecache plugin.
56  *
57  * @param cfg configuration to pass
58  * @return NULL on error
59  */
60 static struct GNUNET_NAMECACHE_PluginFunctions *
61 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
62 {
63   struct GNUNET_NAMECACHE_PluginFunctions *ret;
64   char *libname;
65
66   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namecache plugin\n"),
67               plugin_name);
68   GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
69   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
70   {
71     FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
72     GNUNET_free (libname);
73     return NULL;
74   }
75   GNUNET_free (libname);
76   return ret;
77 }
78
79
80 static void
81 run (void *cls, char *const *args, const char *cfgfile,
82      const struct GNUNET_CONFIGURATION_Handle *cfg)
83 {
84   struct GNUNET_NAMECACHE_PluginFunctions *nsp;
85
86   ok = 0;
87   nsp = load_plugin (cfg);
88   if (NULL == nsp)
89   {
90     FPRINTF (stderr,
91              "%s",
92              "Failed to initialize namecache.  Database likely not setup, skipping test.\n");
93     return;
94   }
95
96   unload_plugin (nsp);
97 }
98
99
100 int
101 main (int argc, char *argv[])
102 {
103   char cfg_name[128];
104   char *const xargv[] = {
105     "test-plugin-namecache",
106     "-c",
107     cfg_name,
108     NULL
109   };
110   struct GNUNET_GETOPT_CommandLineOption options[] = {
111     GNUNET_GETOPT_OPTION_END
112   };
113
114   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
115   GNUNET_log_setup ("test-plugin-namecache",
116                     "WARNING",
117                     NULL);
118   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
119   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namecache_%s.conf",
120                    plugin_name);
121   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
122                       "test-plugin-namecache", "nohelp", options, &run, NULL);
123   if (ok != 0)
124     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
125   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
126   return ok;
127 }
128
129 /* end of test_plugin_namecache.c */