librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Mar 05 09:43:14 2018 +0000 (2018-03-05)
changeset 493 06f70d662e39
parent 455 df914f383f5c
permissions -rw-r--r--
Add a test for relocations that are valid paths but would be invalid URIs if mis-interpreted
     1 /*
     2  * Copyright (C) 2009, 2016  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #include <stdlib.h>
    22 #include <stdio.h>
    23 #include <string.h>
    24 #include <errno.h>
    25 #include <dirent.h>
    26 #include <unistd.h>
    27 #include <math.h>
    28 #include "razor-internal.h"
    29 
    30 /*
    31  * This kludge is to work around an apparent bug in Fedora 10's lua package
    32  * (it appears to require libm but not to include a DT_NEEDED).
    33  */
    34 
    35 void (*kludge)() = (void (*)())sin;
    36 
    37 static void recursive_remove(const char *directory)
    38 {
    39 	DIR *dp;
    40 	struct dirent *dirp;
    41 	char *buf;
    42 
    43 	dp = opendir(directory);
    44 	while((dirp = readdir(dp))) {
    45 		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
    46 			buf = malloc(strlen(directory) + strlen(dirp->d_name)
    47 				     + 2);
    48 			sprintf(buf, "%s/%s", directory, dirp->d_name);
    49 			if (remove(buf) < 0)
    50 				recursive_remove(buf);
    51 			free(buf);
    52 		}
    53 	}
    54 
    55 	rmdir(directory);
    56 }
    57 
    58 int main(int argc, char *argv[])
    59 {
    60 	char root[] = "/tmp/razor.XXXXXX";
    61 	int r;
    62 	void *script;
    63 	size_t len;
    64 	char *s, *test_uri, *srcdir, *suffix, *cwd;
    65 	FILE *fp;
    66 	struct razor_error *error = NULL;
    67 	struct razor_uri base_ru, test_ru, ru;
    68 
    69 	if (argc > 2) {
    70 		fprintf(stderr, "usage: %s [TESTS-URI]\n", argv[0]);
    71 		exit(1);
    72 	}
    73 	if (argc == 2)
    74 		test_uri = argv[1];
    75 	else
    76 		test_uri = "test.lua";
    77 
    78 	if (!mkdtemp(root)) {
    79 		perror(root);
    80 		exit(1);
    81 	}
    82 
    83 	s = malloc(strlen(root) + strlen("/testfile") + 1);
    84 	strcpy(s, root);
    85 	strcat(s, "/testfile");
    86 	fp = fopen(s, "w");
    87 	if (!fp) {
    88 		perror(s);
    89 		exit(1);
    90 	}
    91 	fprintf(fp, "#!" LUA_BINARY "\n"
    92 	  "print('Abracadabra!')\n");
    93 	fclose(fp);
    94 	chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
    95 	free(s);
    96 
    97 	for (len = 32;; len *= 2) {
    98 	    cwd = malloc(len);
    99 	    if (getcwd(cwd, len))
   100 		    break;
   101 	    free(cwd);
   102 	}
   103 	cwd = realloc(cwd, strlen(cwd) + 1);
   104 
   105 	s = razor_concat("file:", cwd, NULL);
   106 	if (razor_uri_parse(&test_ru, test_uri, &error) ||
   107 	    razor_uri_parse(&base_ru, s, &error)) {
   108 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   109 		razor_error_free(error);
   110 		exit(1);
   111 	}
   112 	free(s);
   113 
   114 	razor_uri_resolve(&ru, &base_ru, &test_ru);
   115 	razor_uri_destroy(&base_ru);
   116 	s = razor_uri_recompose(&ru);
   117 	razor_uri_destroy(&ru);
   118 	script = razor_uri_get_contents(s, &len, 0, &error);
   119 	free(s);
   120 	if (!script) {
   121 		srcdir = getenv("srcdir");
   122 		if (srcdir && errno == ENOENT) {
   123 			razor_error_free(error);
   124 			error = NULL;
   125 			suffix = srcdir[strlen(srcdir) - 1] == '/' ? NULL : "/";
   126 			if (*srcdir == '/')
   127 				s = razor_concat("file:", srcdir, suffix, NULL);
   128 			else
   129 				s = razor_concat("file:", cwd, "/", srcdir,
   130 						 suffix, NULL);
   131 			razor_uri_parse(&base_ru, s, &error);
   132 			free(s);
   133 			if (!error) {
   134 				razor_uri_resolve(&ru, &base_ru, &test_ru);
   135 				razor_uri_destroy(&base_ru);
   136 				s = razor_uri_recompose(&ru);
   137 				razor_uri_destroy(&ru);
   138 				script = razor_uri_get_contents(s, &len, 0,
   139 								&error);
   140 				free(s);
   141 			}
   142 			if (error) {
   143 				fprintf(stderr, "%s\n",
   144 					razor_error_get_msg(error));
   145 				razor_error_free(error);
   146 				exit(1);
   147 			}
   148 		} else {
   149 			fprintf(stderr, "%s\n", razor_error_get_msg(error));
   150 			razor_error_free(error);
   151 			exit(1);
   152 		}
   153 	}
   154 	razor_uri_destroy(&test_ru);
   155 	free(cwd);
   156 
   157 	r = run_lua_script(root, test_uri, script, len, -1);
   158 	razor_uri_free_contents(script, len);
   159 
   160 	recursive_remove(root);
   161 	exit(r ? 1 : 0);
   162 }