librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Sep 09 15:27:12 2014 +0100 (2014-09-09)
changeset 447 0a5e583393e1
parent 378 ee438b1e94c0
child 455 df914f383f5c
permissions -rw-r--r--
Add error domains and codes
     1 /*
     2  * Copyright (C) 2009  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 <sys/types.h>
    26 #include <sys/stat.h>
    27 #include <dirent.h>
    28 #include <unistd.h>
    29 #include <math.h>
    30 #include "razor-internal.h"
    31 
    32 /*
    33  * This kludge is to work around an apparent bug in Fedora 10's lua package
    34  * (it appears to require libm but not to include a DT_NEEDED).
    35  */
    36 
    37 void (*kludge)() = (void (*)())sin;
    38 
    39 static void recursive_remove(const char *directory)
    40 {
    41 	DIR *dp;
    42 	struct dirent *dirp;
    43 	char *buf;
    44 
    45 	dp = opendir(directory);
    46 	while((dirp = readdir(dp))) {
    47 		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
    48 			buf = malloc(strlen(directory) + strlen(dirp->d_name)
    49 				     + 2);
    50 			sprintf(buf, "%s/%s", directory, dirp->d_name);
    51 			if (remove(buf) < 0)
    52 				recursive_remove(buf);
    53 			free(buf);
    54 		}
    55 	}
    56 
    57 	rmdir(directory);
    58 }
    59 
    60 int main(int argc, char *argv[])
    61 {
    62 	char root[] = "/tmp/razor.XXXXXX";
    63 	int r;
    64 	void *script;
    65 	size_t len;
    66 	char *s, *test_file, *srcdir;
    67 	FILE *fp;
    68 	struct razor_error *error = NULL;
    69 
    70 	if (argc > 2) {
    71 		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
    72 		exit(1);
    73 	}
    74 	if (argc == 2)
    75 		test_file = argv[1];
    76 	else
    77 		test_file = "test.lua";
    78 
    79 	if (!mkdtemp(root)) {
    80 		perror(root);
    81 		exit(1);
    82 	}
    83 
    84 	s = malloc(strlen(root) + strlen("/testfile") + 1);
    85 	strcpy(s, root);
    86 	strcat(s, "/testfile");
    87 	fp = fopen(s, "w");
    88 	if (!fp) {
    89 		perror(s);
    90 		exit(1);
    91 	}
    92 	fprintf(fp, "#!" LUA_BINARY "\n"
    93 	  "print('Abracadabra!')\n");
    94 	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
    95 	fclose(fp);
    96 	free(s);
    97 
    98 	script = razor_file_get_contents(test_file, &len, 0, &error);
    99 	if (!script) {
   100 		srcdir = getenv("srcdir");
   101 		if (srcdir && errno == ENOENT && *test_file != '/') {
   102 			razor_error_free(error);
   103 			s = malloc(strlen(srcdir) + strlen(test_file) + 2);
   104 			strcpy(s, srcdir);
   105 			strcat(s, "/");
   106 			strcat(s, test_file);
   107 			script = razor_file_get_contents(s, &len, 0, &error);
   108 			if (!script) {
   109 				fprintf(stderr, "%s\n",
   110 					razor_error_get_msg(error));
   111 				razor_error_free(error);
   112 				exit(1);
   113 			}
   114 			free(s);
   115 		} else {
   116 			fprintf(stderr, "%s\n", razor_error_get_msg(error));
   117 			razor_error_free(error);
   118 			exit(1);
   119 		}
   120 	}
   121 	r = run_lua_script(root, test_file, script, len, -1);
   122 	razor_file_free_contents(script, len);
   123 
   124 	recursive_remove(root);
   125 	exit(r ? 1 : 0);
   126 }