You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamd_cxx_unit.cxx 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*-
  2. * Copyright 2021 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include <memory>
  19. #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  20. #include "doctest/doctest.h"
  21. #include "rspamd_cxx_unit_utils.hxx"
  22. static gboolean verbose = false;
  23. static const GOptionEntry entries[] =
  24. {
  25. {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
  26. "Enable verbose logging", NULL},
  27. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  28. };
  29. int
  30. main(int argc, char **argv) {
  31. struct rspamd_main *rspamd_main;
  32. rspamd_mempool_t *pool;
  33. struct rspamd_config *cfg;
  34. GOptionContext *options_context;
  35. pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0);
  36. rspamd_main = (struct rspamd_main *) rspamd_mempool_alloc0(pool, sizeof(*rspamd_main));
  37. rspamd_main->server_pool = pool;
  38. cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT);
  39. cfg->libs_ctx = rspamd_init_libs();
  40. rspamd_main->cfg = cfg;
  41. cfg->cfg_pool = pool;
  42. options_context = g_option_context_new("- run rspamd cxx test");
  43. g_option_context_add_main_entries(options_context, entries, NULL);
  44. g_option_context_set_ignore_unknown_options(options_context, true);
  45. g_option_context_set_help_enabled(options_context, false);
  46. GError *error = NULL;
  47. if (!g_option_context_parse(options_context, &argc, &argv, &error)) {
  48. fprintf(stderr, "option parsing failed: %s\n", error->message);
  49. g_option_context_free(options_context);
  50. exit(1);
  51. }
  52. if (verbose) {
  53. rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool,
  54. RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM);
  55. rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG);
  56. }
  57. else {
  58. rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool,
  59. RSPAMD_LOG_FLAG_RSPAMADM);
  60. rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE);
  61. }
  62. doctest::Context context(argc, argv);
  63. int res = context.run();
  64. if (context.shouldExit()) {
  65. return res;
  66. }
  67. rspamd_mempool_delete(pool);
  68. return res;
  69. }