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