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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3. #include <sys/wait.h>
  4. #include <sys/param.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <netdb.h>
  8. #include <syslog.h>
  9. #include <fcntl.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <event.h>
  13. #include "../config.h"
  14. #include "../main.h"
  15. #include "../cfg_file.h"
  16. #include "../memcached.h"
  17. #include "tests.h"
  18. u_char *buf = "test";
  19. static void
  20. memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
  21. {
  22. struct timeval tv;
  23. switch (ctx->op) {
  24. case CMD_CONNECT:
  25. g_assert (error == OK);
  26. msg_debug ("Connect ok");
  27. memc_set (ctx, ctx->param, 60);
  28. break;
  29. case CMD_READ:
  30. g_assert (error == OK);
  31. g_assert (!memcmp(ctx->param->buf, buf, ctx->param->bufsize));
  32. msg_debug ("Read ok");
  33. memc_close_ctx (ctx);
  34. tv.tv_sec = 0;
  35. tv.tv_usec = 0;
  36. event_loopexit (&tv);
  37. break;
  38. case CMD_WRITE:
  39. g_assert (error == OK);
  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. }
  46. }
  47. void
  48. rspamd_memcached_test_func ()
  49. {
  50. memcached_ctx_t *ctx;
  51. memcached_param_t *param;
  52. struct in_addr addr;
  53. ctx = g_malloc (sizeof (memcached_ctx_t));
  54. param = g_malloc (sizeof (memcached_param_t));
  55. bzero (ctx, sizeof (memcached_ctx_t));
  56. bzero (param, sizeof (memcached_param_t));
  57. event_init ();
  58. ctx->callback = memcached_callback;
  59. ctx->callback_data = (void *)param;
  60. ctx->protocol = TCP_TEXT;
  61. inet_aton ("127.0.0.1", &addr);
  62. memcpy (&ctx->addr, &addr, sizeof (struct in_addr));
  63. ctx->port = htons (11211);
  64. ctx->timeout.tv_sec = 1;
  65. ctx->timeout.tv_usec = 0;
  66. ctx->sock = -1;
  67. ctx->options = MEMC_OPT_DEBUG;
  68. strlcpy (param->key, buf, sizeof (param->key));
  69. param->buf = buf;
  70. param->bufsize = strlen (buf);
  71. ctx->param = param;
  72. g_assert (memc_init_ctx (ctx) != -1);
  73. event_loop (0);
  74. }