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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2015, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "config.h"
  24. #include "main.h"
  25. #include "util.h"
  26. #include "lua/lua_common.h"
  27. static const char *lua_src = BUILDROOT "/test/lua/tests.lua";
  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 (NULL);
  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_printf ("Starting lua tests\n");
  58. if ((rp = realpath (lua_src, rp_buf)) == NULL) {
  59. msg_err ("cannot find path %s: %s",
  60. lua_src, strerror (errno));
  61. g_assert (0);
  62. }
  63. tmp = g_strdup (rp);
  64. dir = dirname (tmp);
  65. /* Set lua path */
  66. lua_getglobal (L, "package");
  67. lua_getfield (L, -1, "path");
  68. old_path = luaL_checkstring (L, -1);
  69. rspamd_snprintf (path_buf, sizeof (path_buf), "%s;%s/?.lua;%s/unit/?.lua",
  70. old_path, dir, dir);
  71. lua_pop (L, 1);
  72. lua_pushstring (L, path_buf);
  73. lua_setfield (L, -2, "path");
  74. lua_pop (L, 1);
  75. lua_newtable (L);
  76. globbuf.gl_offs = 0;
  77. len = strlen (dir) + sizeof ("/unit/") + sizeof ("*.lua");
  78. pattern = g_malloc (len);
  79. rspamd_snprintf (pattern, len, "%s/unit/%s", dir, "*.lua");
  80. if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) {
  81. for (i = 0; i < (gint)globbuf.gl_pathc; i++) {
  82. lua_pushinteger (L, i + 1);
  83. lua_pushstring (L, globbuf.gl_pathv[i]);
  84. lua_settable (L, -3);
  85. }
  86. globfree (&globbuf);
  87. g_free (pattern);
  88. }
  89. else {
  90. msg_err ("pattern %s doesn't match: %s", pattern,
  91. strerror (errno));
  92. g_assert (0);
  93. }
  94. lua_setglobal (L, "tests_list");
  95. lua_pushcfunction (L, traceback);
  96. luaL_loadfile (L, rp);
  97. if (lua_pcall (L, 0, 0, lua_gettop (L) - 1) != 0) {
  98. msg_err ("run test failed: %s", lua_tostring (L, -1));
  99. g_assert (0);
  100. }
  101. exit (EXIT_SUCCESS);
  102. }