librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Jul 06 18:19:13 2009 +0100 (2009-07-06)
changeset 375 c903635ae422
child 376 d15a16347c77
permissions -rw-r--r--
Set RPM_INSTALL_PREFIX{n} in preun as well
ali@352
     1
/*
ali@352
     2
 * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
ali@352
     3
 *
ali@352
     4
 * This program is free software; you can redistribute it and/or modify
ali@352
     5
 * it under the terms of the GNU General Public License as published by
ali@352
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@352
     7
 * (at your option) any later version.
ali@352
     8
 *
ali@352
     9
 * This program is distributed in the hope that it will be useful,
ali@352
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@352
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@352
    12
 * GNU General Public License for more details.
ali@352
    13
 *
ali@352
    14
 * You should have received a copy of the GNU General Public License along
ali@352
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@352
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@352
    17
 */
ali@352
    18
ali@352
    19
#include "config.h"
ali@352
    20
ali@352
    21
#include <stdlib.h>
ali@352
    22
#include <stdio.h>
ali@352
    23
#include <string.h>
ali@352
    24
#include <sys/types.h>
ali@352
    25
#include <sys/stat.h>
ali@352
    26
#include <dirent.h>
ali@352
    27
#include <unistd.h>
ali@352
    28
#include <math.h>
ali@352
    29
#include "razor-internal.h"
ali@352
    30
ali@352
    31
/*
ali@352
    32
 * This kludge is to work around an apparent bug in Fedora 10's lua package
ali@352
    33
 * (it appears to require libm but not to include a DT_NEEDED).
ali@352
    34
 */
ali@352
    35
ali@352
    36
void (*kludge)() = (void (*)())sin;
ali@352
    37
ali@352
    38
static void recursive_remove(const char *directory)
ali@352
    39
{
ali@352
    40
	DIR *dp;
ali@352
    41
	struct dirent *dirp;
ali@352
    42
	char *buf;
ali@352
    43
ali@352
    44
	dp = opendir(directory);
ali@352
    45
	while((dirp = readdir(dp))) {
ali@352
    46
		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
ali@352
    47
			buf = malloc(strlen(directory) + strlen(dirp->d_name)
ali@352
    48
				     + 2);
ali@352
    49
			sprintf(buf, "%s/%s", directory, dirp->d_name);
ali@352
    50
			if (remove(buf) < 0)
ali@352
    51
				recursive_remove(buf);
ali@352
    52
			free(buf);
ali@352
    53
		}
ali@352
    54
	}
ali@352
    55
ali@352
    56
	rmdir(directory);
ali@352
    57
}
ali@352
    58
ali@352
    59
int main(int argc, char *argv[])
ali@352
    60
{
ali@352
    61
	char root[] = "/tmp/razor.XXXXXX";
ali@352
    62
	int r;
ali@352
    63
	void *script;
ali@352
    64
	size_t len;
ali@352
    65
	char *s, *test_file;
ali@352
    66
	FILE *fp;
ali@352
    67
ali@352
    68
	if (argc > 2) {
ali@352
    69
		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
ali@352
    70
		exit(1);
ali@352
    71
	}
ali@352
    72
	if (argc == 2)
ali@352
    73
		test_file = argv[1];
ali@352
    74
	else
ali@352
    75
		test_file = "test.lua";
ali@352
    76
ali@352
    77
	if (!mkdtemp(root)) {
ali@352
    78
		perror(root);
ali@352
    79
		exit(1);
ali@352
    80
	}
ali@352
    81
ali@352
    82
	s = malloc(strlen(root) + strlen("/testfile") + 1);
ali@352
    83
	strcpy(s, root);
ali@352
    84
	strcat(s, "/testfile");
ali@352
    85
	fp = fopen(s, "w");
ali@352
    86
	if (!fp) {
ali@352
    87
		perror(s);
ali@352
    88
		exit(1);
ali@352
    89
	}
ali@352
    90
	fprintf(fp, "#!" LUA_BINARY "\n"
ali@352
    91
	  "print('Abracadabra!')\n");
ali@352
    92
	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
ali@352
    93
	fclose(fp);
ali@352
    94
	free(s);
ali@352
    95
ali@352
    96
	script = razor_file_get_contents(test_file, &len);
ali@352
    97
	r = run_lua_script(root, test_file, script, len);
ali@352
    98
	razor_file_free_contents(script, len);
ali@352
    99
ali@352
   100
	recursive_remove(root);
ali@352
   101
	exit(r ? 1 : 0);
ali@352
   102
}