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_lua_test.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*-
  2. * Copyright 2016 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 "util.h"
  19. #include "lua/lua_common.h"
  20. #include "unix-std.h"
  21. #ifdef HAVE_GLOB_H
  22. #include <glob.h>
  23. #endif
  24. static const char *lua_src_name = "lua/tests.lua";
  25. extern gchar *lua_test;
  26. extern gchar *lua_test_case;
  27. extern gchar *argv0_dirname;
  28. extern struct rspamd_main *rspamd_main;
  29. static int
  30. traceback (lua_State *L)
  31. {
  32. if (!lua_isstring (L, 1)) {
  33. return 1;
  34. }
  35. lua_getglobal (L, "debug");
  36. if (!lua_istable(L, -1)) {
  37. lua_pop(L, 1);
  38. return 1;
  39. }
  40. lua_getfield (L, -1, "traceback");
  41. if (!lua_isfunction(L, -1)) {
  42. lua_pop(L, 2);
  43. return 1;
  44. }
  45. lua_pushvalue (L, 1);
  46. lua_pushinteger (L, 2);
  47. lua_call(L, 2, 1);
  48. return 1;
  49. }
  50. _Noreturn void
  51. rspamd_lua_test_func (void)
  52. {
  53. lua_State *L = (lua_State *)rspamd_main->cfg->lua_state;
  54. gchar *lua_src, *rp, rp_buf[PATH_MAX], path_buf[PATH_MAX], *tmp, *dir, *pattern;
  55. const gchar *old_path;
  56. glob_t globbuf;
  57. gint i, len;
  58. rspamd_lua_set_env (L, NULL, NULL, NULL);
  59. rspamd_lua_set_globals (rspamd_main->cfg, L);
  60. rspamd_lua_start_gc (rspamd_main->cfg);
  61. if (lua_test_case) {
  62. lua_pushstring (L, lua_test_case);
  63. lua_setglobal (L, "test_pattern");
  64. }
  65. rspamd_printf ("Starting lua tests\n");
  66. lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL);
  67. if ((rp = realpath (lua_src, rp_buf)) == NULL) {
  68. msg_err ("cannot find path %s: %s",
  69. lua_src, strerror (errno));
  70. g_assert (0);
  71. }
  72. g_free (lua_src);
  73. tmp = g_strdup (rp);
  74. dir = dirname (tmp);
  75. /* Set lua path */
  76. lua_getglobal (L, "package");
  77. lua_getfield (L, -1, "path");
  78. old_path = luaL_checkstring (L, -1);
  79. rspamd_snprintf (path_buf, sizeof (path_buf), "%s;%s/?.lua;%s/unit/?.lua",
  80. old_path, dir, dir);
  81. lua_pop (L, 1);
  82. lua_pushstring (L, path_buf);
  83. lua_setfield (L, -2, "path");
  84. lua_pop (L, 1);
  85. lua_newtable (L);
  86. globbuf.gl_offs = 0;
  87. len = strlen (dir) + sizeof ("/unit/") + sizeof ("*.lua");
  88. pattern = g_malloc (len);
  89. rspamd_snprintf (pattern, len, "%s/unit/%s", dir, "*.lua");
  90. gint lua_test_len = 0;
  91. gint inserted_file = 1;
  92. gint path_start;
  93. if (lua_test) {
  94. lua_test_len = strlen (lua_test);
  95. }
  96. if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) {
  97. for (i = 0; i < (gint)globbuf.gl_pathc; i++) {
  98. if (lua_test) {
  99. path_start = strlen (globbuf.gl_pathv[i]) - lua_test_len;
  100. if (path_start < 0 ||
  101. strncmp (globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) {
  102. continue;
  103. }
  104. }
  105. lua_pushinteger (L, inserted_file);
  106. lua_pushstring (L, globbuf.gl_pathv[i]);
  107. lua_settable (L, -3);
  108. inserted_file ++;
  109. }
  110. globfree (&globbuf);
  111. g_free (pattern);
  112. }
  113. else {
  114. msg_err ("pattern %s doesn't match: %s", pattern,
  115. strerror (errno));
  116. g_assert (0);
  117. }
  118. lua_setglobal (L, "tests_list");
  119. rspamd_lua_set_path (L, NULL, NULL);
  120. lua_pushcfunction (L, traceback);
  121. luaL_loadfile (L, rp);
  122. if (lua_pcall (L, 0, 0, lua_gettop (L) - 1) != 0) {
  123. msg_err ("run test failed: %s", lua_tostring (L, -1));
  124. g_assert (0);
  125. }
  126. exit (EXIT_SUCCESS);
  127. }