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

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