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_memcached_test.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "../src/config.h"
  2. #include "../src/main.h"
  3. #include "../src/cfg_file.h"
  4. #include "../src/memcached.h"
  5. #include "tests.h"
  6. static const u_char *buf = "test";
  7. static void
  8. memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
  9. {
  10. struct timeval tv;
  11. switch (ctx->op) {
  12. case CMD_CONNECT:
  13. if (error != OK) {
  14. msg_warn ("Connect failed, skipping test");
  15. memc_close_ctx (ctx);
  16. tv.tv_sec = 0;
  17. tv.tv_usec = 0;
  18. event_loopexit (&tv);
  19. }
  20. msg_debug ("Connect ok");
  21. memc_set (ctx, ctx->param, 60);
  22. break;
  23. case CMD_READ:
  24. g_assert (error == OK);
  25. g_assert (!memcmp(ctx->param->buf, buf, ctx->param->bufsize));
  26. msg_debug ("Read ok");
  27. memc_close_ctx (ctx);
  28. tv.tv_sec = 0;
  29. tv.tv_usec = 0;
  30. event_loopexit (&tv);
  31. break;
  32. case CMD_WRITE:
  33. if (error != OK) {
  34. msg_warn ("Connect failed, skipping test");
  35. memc_close_ctx (ctx);
  36. tv.tv_sec = 0;
  37. tv.tv_usec = 0;
  38. event_loopexit (&tv);
  39. }
  40. msg_debug ("Write ok");
  41. ctx->param->buf = g_malloc (sizeof (buf));
  42. bzero (ctx->param->buf, sizeof (buf));
  43. memc_get (ctx, ctx->param);
  44. break;
  45. default:
  46. return;
  47. }
  48. }
  49. void
  50. rspamd_memcached_test_func ()
  51. {
  52. memcached_ctx_t *ctx;
  53. memcached_param_t *param;
  54. struct in_addr addr;
  55. ctx = g_malloc (sizeof (memcached_ctx_t));
  56. param = g_malloc (sizeof (memcached_param_t));
  57. bzero (ctx, sizeof (memcached_ctx_t));
  58. bzero (param, sizeof (memcached_param_t));
  59. event_init ();
  60. ctx->callback = memcached_callback;
  61. ctx->callback_data = (void *)param;
  62. ctx->protocol = TCP_TEXT;
  63. inet_aton ("127.0.0.1", &addr);
  64. memcpy (&ctx->addr, &addr, sizeof (struct in_addr));
  65. ctx->port = htons (11211);
  66. ctx->timeout.tv_sec = 1;
  67. ctx->timeout.tv_usec = 0;
  68. ctx->sock = -1;
  69. ctx->options = MEMC_OPT_DEBUG;
  70. rspamd_strlcpy (param->key, buf, sizeof (param->key));
  71. param->buf = buf;
  72. param->bufsize = strlen (buf);
  73. ctx->param = param;
  74. g_assert (memc_init_ctx (ctx) != -1);
  75. event_loop (0);
  76. }