test_plugin_namecache.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2012 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. * @file namecache/test_plugin_namecache.c
  19. * @brief Test for the namecache plugins
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_namecache_plugin.h"
  25. #include "gnunet_testing_lib.h"
  26. static int ok;
  27. /**
  28. * Name of plugin under test.
  29. */
  30. static const char *plugin_name;
  31. /**
  32. * Function called when the service shuts down. Unloads our namecache
  33. * plugin.
  34. *
  35. * @param api api to unload
  36. */
  37. static void
  38. unload_plugin (struct GNUNET_NAMECACHE_PluginFunctions *api)
  39. {
  40. char *libname;
  41. GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
  42. GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
  43. GNUNET_free (libname);
  44. }
  45. /**
  46. * Load the namecache plugin.
  47. *
  48. * @param cfg configuration to pass
  49. * @return NULL on error
  50. */
  51. static struct GNUNET_NAMECACHE_PluginFunctions *
  52. load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
  53. {
  54. struct GNUNET_NAMECACHE_PluginFunctions *ret;
  55. char *libname;
  56. GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namecache plugin\n"),
  57. plugin_name);
  58. GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
  59. if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
  60. {
  61. FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
  62. GNUNET_free (libname);
  63. return NULL;
  64. }
  65. GNUNET_free (libname);
  66. return ret;
  67. }
  68. static void
  69. run (void *cls, char *const *args, const char *cfgfile,
  70. const struct GNUNET_CONFIGURATION_Handle *cfg)
  71. {
  72. struct GNUNET_NAMECACHE_PluginFunctions *nsp;
  73. ok = 0;
  74. nsp = load_plugin (cfg);
  75. if (NULL == nsp)
  76. {
  77. FPRINTF (stderr,
  78. "%s",
  79. "Failed to initialize namecache. Database likely not setup, skipping test.\n");
  80. return;
  81. }
  82. unload_plugin (nsp);
  83. }
  84. int
  85. main (int argc, char *argv[])
  86. {
  87. char cfg_name[128];
  88. char *const xargv[] = {
  89. "test-plugin-namecache",
  90. "-c",
  91. cfg_name,
  92. NULL
  93. };
  94. struct GNUNET_GETOPT_CommandLineOption options[] = {
  95. GNUNET_GETOPT_OPTION_END
  96. };
  97. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
  98. GNUNET_log_setup ("test-plugin-namecache",
  99. "WARNING",
  100. NULL);
  101. plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
  102. GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namecache_%s.conf",
  103. plugin_name);
  104. GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
  105. "test-plugin-namecache", "nohelp", options, &run, NULL);
  106. if (ok != 0)
  107. FPRINTF (stderr, "Missed some testcases: %d\n", ok);
  108. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
  109. return ok;
  110. }
  111. /* end of test_plugin_namecache.c */