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 2.7KB

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