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

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