librazor/test-lua.c
changeset 498 5a49f274ab2d
parent 455 df914f383f5c
     1.1 --- a/librazor/test-lua.c	Thu Oct 09 17:27:41 2014 +0100
     1.2 +++ b/librazor/test-lua.c	Tue Jun 05 11:07:53 2018 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + * Copyright (C) 2009, 2016  J. Ali Harlow <ali@juiblex.co.uk>
     1.7   *
     1.8   * This program is free software; you can redistribute it and/or modify
     1.9   * it under the terms of the GNU General Public License as published by
    1.10 @@ -22,8 +22,6 @@
    1.11  #include <stdio.h>
    1.12  #include <string.h>
    1.13  #include <errno.h>
    1.14 -#include <sys/types.h>
    1.15 -#include <sys/stat.h>
    1.16  #include <dirent.h>
    1.17  #include <unistd.h>
    1.18  #include <math.h>
    1.19 @@ -63,18 +61,19 @@
    1.20  	int r;
    1.21  	void *script;
    1.22  	size_t len;
    1.23 -	char *s, *test_file, *srcdir;
    1.24 +	char *s, *test_uri, *srcdir, *suffix, *cwd;
    1.25  	FILE *fp;
    1.26  	struct razor_error *error = NULL;
    1.27 +	struct razor_uri base_ru, test_ru, ru;
    1.28  
    1.29  	if (argc > 2) {
    1.30 -		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
    1.31 +		fprintf(stderr, "usage: %s [TESTS-URI]\n", argv[0]);
    1.32  		exit(1);
    1.33  	}
    1.34  	if (argc == 2)
    1.35 -		test_file = argv[1];
    1.36 +		test_uri = argv[1];
    1.37  	else
    1.38 -		test_file = "test.lua";
    1.39 +		test_uri = "test.lua";
    1.40  
    1.41  	if (!mkdtemp(root)) {
    1.42  		perror(root);
    1.43 @@ -95,31 +94,68 @@
    1.44  	chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
    1.45  	free(s);
    1.46  
    1.47 -	script = razor_file_get_contents(test_file, &len, 0, &error);
    1.48 +	for (len = 32;; len *= 2) {
    1.49 +	    cwd = malloc(len);
    1.50 +	    if (getcwd(cwd, len))
    1.51 +		    break;
    1.52 +	    free(cwd);
    1.53 +	}
    1.54 +	cwd = realloc(cwd, strlen(cwd) + 1);
    1.55 +
    1.56 +	s = razor_concat("file:", cwd, NULL);
    1.57 +	if (razor_uri_parse(&test_ru, test_uri, &error) ||
    1.58 +	    razor_uri_parse(&base_ru, s, &error)) {
    1.59 +		fprintf(stderr, "%s\n", razor_error_get_msg(error));
    1.60 +		razor_error_free(error);
    1.61 +		exit(1);
    1.62 +	}
    1.63 +	free(s);
    1.64 +
    1.65 +	razor_uri_resolve(&ru, &base_ru, &test_ru);
    1.66 +	razor_uri_destroy(&base_ru);
    1.67 +	s = razor_uri_recompose(&ru);
    1.68 +	razor_uri_destroy(&ru);
    1.69 +	script = razor_uri_get_contents(s, &len, 0, &error);
    1.70 +	free(s);
    1.71  	if (!script) {
    1.72  		srcdir = getenv("srcdir");
    1.73 -		if (srcdir && errno == ENOENT && *test_file != '/') {
    1.74 +		if (srcdir && errno == ENOENT) {
    1.75  			razor_error_free(error);
    1.76 -			s = malloc(strlen(srcdir) + strlen(test_file) + 2);
    1.77 -			strcpy(s, srcdir);
    1.78 -			strcat(s, "/");
    1.79 -			strcat(s, test_file);
    1.80 -			script = razor_file_get_contents(s, &len, 0, &error);
    1.81 -			if (!script) {
    1.82 +			error = NULL;
    1.83 +			suffix = srcdir[strlen(srcdir) - 1] == '/' ? NULL : "/";
    1.84 +			if (*srcdir == '/')
    1.85 +				s = razor_concat("file:", srcdir, suffix, NULL);
    1.86 +			else
    1.87 +				s = razor_concat("file:", cwd, "/", srcdir,
    1.88 +						 suffix, NULL);
    1.89 +			razor_uri_parse(&base_ru, s, &error);
    1.90 +			free(s);
    1.91 +			if (!error) {
    1.92 +				razor_uri_resolve(&ru, &base_ru, &test_ru);
    1.93 +				razor_uri_destroy(&base_ru);
    1.94 +				s = razor_uri_recompose(&ru);
    1.95 +				razor_uri_destroy(&ru);
    1.96 +				script = razor_uri_get_contents(s, &len, 0,
    1.97 +								&error);
    1.98 +				free(s);
    1.99 +			}
   1.100 +			if (error) {
   1.101  				fprintf(stderr, "%s\n",
   1.102  					razor_error_get_msg(error));
   1.103  				razor_error_free(error);
   1.104  				exit(1);
   1.105  			}
   1.106 -			free(s);
   1.107  		} else {
   1.108  			fprintf(stderr, "%s\n", razor_error_get_msg(error));
   1.109  			razor_error_free(error);
   1.110  			exit(1);
   1.111  		}
   1.112  	}
   1.113 -	r = run_lua_script(root, test_file, script, len, -1);
   1.114 -	razor_file_free_contents(script, len);
   1.115 +	razor_uri_destroy(&test_ru);
   1.116 +	free(cwd);
   1.117 +
   1.118 +	r = run_lua_script(root, test_uri, script, len, -1);
   1.119 +	razor_uri_free_contents(script, len);
   1.120  
   1.121  	recursive_remove(root);
   1.122  	exit(r ? 1 : 0);