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_rcl_test.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (c) 2013, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "../src/config.h"
  24. #include "../src/rcl/rcl.h"
  25. #include "../src/main.h"
  26. #include "tests.h"
  27. const gchar *rcl_test_valid[] = {
  28. /* Json like */
  29. "{"
  30. "\"key1\": value;"
  31. "\"key1\": value2;"
  32. "\"key1\": \"value;\""
  33. "}\n",
  34. /* Nginx like */
  35. "section1 { param1 = value; param2 = value, "
  36. "section3 {param = value; param2 = value, param3 = [\"value1\", value2, 100500]}}\n"
  37. "section2 { param1 = {key = value}, param1 = [\"key\"]}",
  38. /* Numbers */
  39. "key = 1s\n"
  40. "key2 = 1min\n"
  41. "key3 = 1kb\n"
  42. "key4 = 5M\n"
  43. "key5 = 10mS\n"
  44. "key6 = 10y\n",
  45. /* Strings */
  46. "key = \"some string\";"
  47. "key1 = /some/path;"
  48. "key3 = 111some,"
  49. "key4: s1,"
  50. "\"key5\": \"\\n\\r123\"",
  51. /* Macros */
  52. "section1 {key = value; section {\n"
  53. "param = \"value\";\n"
  54. "param2 = value\n"
  55. "array = [ 1, 1mb, test]}\n"
  56. ".includes \"./test.cfg\"}",
  57. NULL
  58. };
  59. static const gchar test_pubkey[] = ""
  60. "-----BEGIN PUBLIC KEY-----\n"
  61. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlhk2u5nbTVgEskmS+qZcAj339\n"
  62. "bLwEK/TXdd0G3d4BVKpF712frw+YwetRdmRRYL5EdjiF01Bv3s6QmsThAJX/li/c\n"
  63. "Q15YFxhvq9DZ0qJmL7e1NzORo6m/WLRK9wxWA+PXSvSUKrlZ3kt9ygD4z5QZ3/td\n"
  64. "qil9VM6Mz7P1HJ0KywIDAQAB\n"
  65. "-----END PUBLIC KEY-----\n";
  66. void
  67. rspamd_rcl_test_func (void)
  68. {
  69. struct rspamd_cl_parser *parser, *parser2;
  70. rspamd_cl_object_t *obj;
  71. const gchar **cur;
  72. guchar *emitted;
  73. GError *err = NULL;
  74. struct timespec start, end;
  75. gdouble seconds;
  76. cur = rcl_test_valid;
  77. while (*cur != NULL) {
  78. parser = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
  79. rspamd_cl_pubkey_add (parser, test_pubkey, sizeof (test_pubkey) - 1, &err);
  80. g_assert_no_error (err);
  81. g_assert (parser != NULL);
  82. rspamd_cl_parser_add_chunk (parser, *cur, strlen (*cur), &err);
  83. g_assert_no_error (err);
  84. obj = rspamd_cl_parser_get_object (parser, &err);
  85. g_assert_no_error (err);
  86. /* Test config emitting */
  87. emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_CONFIG);
  88. g_assert (emitted != NULL);
  89. msg_debug ("got config output: %s", emitted);
  90. parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
  91. g_assert (parser2 != NULL);
  92. rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
  93. g_assert_no_error (err);
  94. rspamd_cl_parser_free (parser2);
  95. g_free (emitted);
  96. /* Test json emitted */
  97. emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_JSON);
  98. g_assert (emitted != NULL);
  99. msg_debug ("got json output: %s", emitted);
  100. parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
  101. g_assert (parser2 != NULL);
  102. rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
  103. g_assert_no_error (err);
  104. rspamd_cl_parser_free (parser2);
  105. g_free (emitted);
  106. /* Compact json */
  107. emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_JSON_COMPACT);
  108. g_assert (emitted != NULL);
  109. msg_debug ("got json compacted output: %s", emitted);
  110. parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
  111. g_assert (parser2 != NULL);
  112. rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
  113. g_assert_no_error (err);
  114. rspamd_cl_parser_free (parser2);
  115. g_free (emitted);
  116. /* Cleanup */
  117. rspamd_cl_parser_free (parser);
  118. rspamd_cl_obj_unref (obj);
  119. cur ++;
  120. }
  121. /* Load a big json */
  122. parser = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
  123. clock_gettime (CLOCK_MONOTONIC, &start);
  124. rspamd_cl_parser_add_file (parser, "./rcl_test.json", &err);
  125. g_assert_no_error (err);
  126. obj = rspamd_cl_parser_get_object (parser, &err);
  127. g_assert_no_error (err);
  128. clock_gettime (CLOCK_MONOTONIC, &end);
  129. seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.;
  130. msg_info ("parsed json in %.4f seconds", seconds);
  131. /* Test config emitting */
  132. clock_gettime (CLOCK_MONOTONIC, &start);
  133. emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_CONFIG);
  134. g_assert (emitted != NULL);
  135. clock_gettime (CLOCK_MONOTONIC, &end);
  136. seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.;
  137. msg_info ("emitted object in %.4f seconds", seconds);
  138. rspamd_cl_parser_free (parser);
  139. rspamd_cl_obj_unref (obj);
  140. }