diff -r 000000000000 -r d842feb64ecb librazor/test-lua.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/librazor/test-lua.c Thu Mar 12 20:52:12 2009 +0000 @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2009 J. Ali Harlow + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "razor-internal.h" + +/* + * This kludge is to work around an apparent bug in Fedora 10's lua package + * (it appears to require libm but not to include a DT_NEEDED). + */ + +void (*kludge)() = (void (*)())sin; + +static void recursive_remove(const char *directory) +{ + DIR *dp; + struct dirent *dirp; + char *buf; + + dp = opendir(directory); + while((dirp = readdir(dp))) { + if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) { + buf = malloc(strlen(directory) + strlen(dirp->d_name) + + 2); + sprintf(buf, "%s/%s", directory, dirp->d_name); + if (remove(buf) < 0) + recursive_remove(buf); + free(buf); + } + } + + rmdir(directory); +} + +int main(int argc, char *argv[]) +{ + char root[] = "/tmp/razor.XXXXXX"; + int r; + void *script; + size_t len; + char *s, *test_file; + FILE *fp; + + if (argc > 2) { + fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]); + exit(1); + } + if (argc == 2) + test_file = argv[1]; + else + test_file = "test.lua"; + + if (!mkdtemp(root)) { + perror(root); + exit(1); + } + + s = malloc(strlen(root) + strlen("/testfile") + 1); + strcpy(s, root); + strcat(s, "/testfile"); + fp = fopen(s, "w"); + if (!fp) { + perror(s); + exit(1); + } + fprintf(fp, "#!" LUA_BINARY "\n" + "print('Abracadabra!')\n"); + fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR); + fclose(fp); + free(s); + + script = razor_file_get_contents(test_file, &len); + r = run_lua_script(root, test_file, script, len); + razor_file_free_contents(script, len); + + recursive_remove(root); + exit(r ? 1 : 0); +}