/* * Copyright (C) 2009, 2016 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 "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_uri, *srcdir, *suffix, *cwd; FILE *fp; struct razor_error *error = NULL; struct razor_uri base_ru, test_ru, ru; if (argc > 2) { fprintf(stderr, "usage: %s [TESTS-URI]\n", argv[0]); exit(1); } if (argc == 2) test_uri = argv[1]; else test_uri = "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"); fclose(fp); chmod(s, S_IRUSR | S_IWUSR | S_IXUSR); free(s); for (len = 32;; len *= 2) { cwd = malloc(len); if (getcwd(cwd, len)) break; free(cwd); } cwd = realloc(cwd, strlen(cwd) + 1); s = razor_concat("file:", cwd, NULL); if (razor_uri_parse(&test_ru, test_uri, &error) || razor_uri_parse(&base_ru, s, &error)) { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); exit(1); } free(s); razor_uri_resolve(&ru, &base_ru, &test_ru); razor_uri_destroy(&base_ru); s = razor_uri_recompose(&ru); razor_uri_destroy(&ru); script = razor_uri_get_contents(s, &len, 0, &error); free(s); if (!script) { srcdir = getenv("srcdir"); if (srcdir && errno == ENOENT) { razor_error_free(error); error = NULL; suffix = srcdir[strlen(srcdir) - 1] == '/' ? NULL : "/"; if (*srcdir == '/') s = razor_concat("file:", srcdir, suffix, NULL); else s = razor_concat("file:", cwd, "/", srcdir, suffix, NULL); razor_uri_parse(&base_ru, s, &error); free(s); if (!error) { razor_uri_resolve(&ru, &base_ru, &test_ru); razor_uri_destroy(&base_ru); s = razor_uri_recompose(&ru); razor_uri_destroy(&ru); script = razor_uri_get_contents(s, &len, 0, &error); free(s); } if (error) { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); exit(1); } } else { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); exit(1); } } razor_uri_destroy(&test_ru); free(cwd); r = run_lua_script(root, test_uri, script, len, -1); razor_uri_free_contents(script, len); recursive_remove(root); exit(r ? 1 : 0); }