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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = rspamd_lua_init (false);
  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. if (lua_test_case) {
  60. lua_pushstring (L, lua_test_case);
  61. lua_setglobal (L, "test_pattern");
  62. }
  63. rspamd_printf ("Starting lua tests\n");
  64. if ((rp = realpath (lua_src, rp_buf)) == NULL) {
  65. msg_err ("cannot find path %s: %s",
  66. lua_src, strerror (errno));
  67. g_assert (0);
  68. }
  69. tmp = g_strdup (rp);
  70. dir = dirname (tmp);
  71. /* Set lua path */
  72. lua_getglobal (L, "package");
  73. lua_getfield (L, -1, "path");
  74. old_path = luaL_checkstring (L, -1);
  75. rspamd_snprintf (path_buf, sizeof (path_buf), "%s;%s/?.lua;%s/unit/?.lua",
  76. old_path, dir, dir);
  77. lua_pop (L, 1);
  78. lua_pushstring (L, path_buf);
  79. lua_setfield (L, -2, "path");
  80. lua_pop (L, 1);
  81. lua_newtable (L);
  82. globbuf.gl_offs = 0;
  83. len = strlen (dir) + sizeof ("/unit/") + sizeof ("*.lua");
  84. pattern = g_malloc (len);
  85. rspamd_snprintf (pattern, len, "%s/unit/%s", dir, "*.lua");
  86. gint lua_test_len = 0;
  87. gint inserted_file = 1;
  88. gint path_start;
  89. if (lua_test) {
  90. lua_test_len = strlen (lua_test);
  91. }
  92. if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) {
  93. for (i = 0; i < (gint)globbuf.gl_pathc; i++) {
  94. if (lua_test) {
  95. path_start = strlen (globbuf.gl_pathv[i]) - lua_test_len;
  96. if (path_start < 0 ||
  97. strncmp (globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) {
  98. continue;
  99. }
  100. }
  101. lua_pushinteger (L, inserted_file);
  102. lua_pushstring (L, globbuf.gl_pathv[i]);
  103. lua_settable (L, -3);
  104. inserted_file ++;
  105. }
  106. globfree (&globbuf);
  107. g_free (pattern);
  108. }
  109. else {
  110. msg_err ("pattern %s doesn't match: %s", pattern,
  111. strerror (errno));
  112. g_assert (0);
  113. }
  114. lua_setglobal (L, "tests_list");
  115. rspamd_lua_set_path (L, NULL, NULL);
  116. lua_pushcfunction (L, traceback);
  117. luaL_loadfile (L, rp);
  118. if (lua_pcall (L, 0, 0, lua_gettop (L) - 1) != 0) {
  119. msg_err ("run test failed: %s", lua_tostring (L, -1));
  120. g_assert (0);
  121. }
  122. exit (EXIT_SUCCESS);
  123. }