test_plugin_namecache.c 3.5 KB

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