librazor/test-lua.c
changeset 360 c798d6db076b
child 376 d15a16347c77
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/librazor/test-lua.c	Wed Apr 22 16:27:11 2009 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/*
     1.5 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +
    1.24 +#include <stdlib.h>
    1.25 +#include <stdio.h>
    1.26 +#include <string.h>
    1.27 +#include <sys/types.h>
    1.28 +#include <sys/stat.h>
    1.29 +#include <dirent.h>
    1.30 +#include <unistd.h>
    1.31 +#include <math.h>
    1.32 +#include "razor-internal.h"
    1.33 +
    1.34 +/*
    1.35 + * This kludge is to work around an apparent bug in Fedora 10's lua package
    1.36 + * (it appears to require libm but not to include a DT_NEEDED).
    1.37 + */
    1.38 +
    1.39 +void (*kludge)() = (void (*)())sin;
    1.40 +
    1.41 +static void recursive_remove(const char *directory)
    1.42 +{
    1.43 +	DIR *dp;
    1.44 +	struct dirent *dirp;
    1.45 +	char *buf;
    1.46 +
    1.47 +	dp = opendir(directory);
    1.48 +	while((dirp = readdir(dp))) {
    1.49 +		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
    1.50 +			buf = malloc(strlen(directory) + strlen(dirp->d_name)
    1.51 +				     + 2);
    1.52 +			sprintf(buf, "%s/%s", directory, dirp->d_name);
    1.53 +			if (remove(buf) < 0)
    1.54 +				recursive_remove(buf);
    1.55 +			free(buf);
    1.56 +		}
    1.57 +	}
    1.58 +
    1.59 +	rmdir(directory);
    1.60 +}
    1.61 +
    1.62 +int main(int argc, char *argv[])
    1.63 +{
    1.64 +	char root[] = "/tmp/razor.XXXXXX";
    1.65 +	int r;
    1.66 +	void *script;
    1.67 +	size_t len;
    1.68 +	char *s, *test_file;
    1.69 +	FILE *fp;
    1.70 +
    1.71 +	if (argc > 2) {
    1.72 +		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
    1.73 +		exit(1);
    1.74 +	}
    1.75 +	if (argc == 2)
    1.76 +		test_file = argv[1];
    1.77 +	else
    1.78 +		test_file = "test.lua";
    1.79 +
    1.80 +	if (!mkdtemp(root)) {
    1.81 +		perror(root);
    1.82 +		exit(1);
    1.83 +	}
    1.84 +
    1.85 +	s = malloc(strlen(root) + strlen("/testfile") + 1);
    1.86 +	strcpy(s, root);
    1.87 +	strcat(s, "/testfile");
    1.88 +	fp = fopen(s, "w");
    1.89 +	if (!fp) {
    1.90 +		perror(s);
    1.91 +		exit(1);
    1.92 +	}
    1.93 +	fprintf(fp, "#!" LUA_BINARY "\n"
    1.94 +	  "print('Abracadabra!')\n");
    1.95 +	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
    1.96 +	fclose(fp);
    1.97 +	free(s);
    1.98 +
    1.99 +	script = razor_file_get_contents(test_file, &len);
   1.100 +	r = run_lua_script(root, test_file, script, len);
   1.101 +	razor_file_free_contents(script, len);
   1.102 +
   1.103 +	recursive_remove(root);
   1.104 +	exit(r ? 1 : 0);
   1.105 +}