librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Apr 29 17:00:01 2009 +0100 (2009-04-29)
changeset 361 2523d03a840e
child 376 d15a16347c77
permissions -rw-r--r--
Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
     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 <sys/types.h>
    25 #include <sys/stat.h>
    26 #include <dirent.h>
    27 #include <unistd.h>
    28 #include <math.h>
    29 #include "razor-internal.h"
    30 
    31 /*
    32  * This kludge is to work around an apparent bug in Fedora 10's lua package
    33  * (it appears to require libm but not to include a DT_NEEDED).
    34  */
    35 
    36 void (*kludge)() = (void (*)())sin;
    37 
    38 static void recursive_remove(const char *directory)
    39 {
    40 	DIR *dp;
    41 	struct dirent *dirp;
    42 	char *buf;
    43 
    44 	dp = opendir(directory);
    45 	while((dirp = readdir(dp))) {
    46 		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
    47 			buf = malloc(strlen(directory) + strlen(dirp->d_name)
    48 				     + 2);
    49 			sprintf(buf, "%s/%s", directory, dirp->d_name);
    50 			if (remove(buf) < 0)
    51 				recursive_remove(buf);
    52 			free(buf);
    53 		}
    54 	}
    55 
    56 	rmdir(directory);
    57 }
    58 
    59 int main(int argc, char *argv[])
    60 {
    61 	char root[] = "/tmp/razor.XXXXXX";
    62 	int r;
    63 	void *script;
    64 	size_t len;
    65 	char *s, *test_file;
    66 	FILE *fp;
    67 
    68 	if (argc > 2) {
    69 		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
    70 		exit(1);
    71 	}
    72 	if (argc == 2)
    73 		test_file = argv[1];
    74 	else
    75 		test_file = "test.lua";
    76 
    77 	if (!mkdtemp(root)) {
    78 		perror(root);
    79 		exit(1);
    80 	}
    81 
    82 	s = malloc(strlen(root) + strlen("/testfile") + 1);
    83 	strcpy(s, root);
    84 	strcat(s, "/testfile");
    85 	fp = fopen(s, "w");
    86 	if (!fp) {
    87 		perror(s);
    88 		exit(1);
    89 	}
    90 	fprintf(fp, "#!" LUA_BINARY "\n"
    91 	  "print('Abracadabra!')\n");
    92 	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
    93 	fclose(fp);
    94 	free(s);
    95 
    96 	script = razor_file_get_contents(test_file, &len);
    97 	r = run_lua_script(root, test_file, script, len);
    98 	razor_file_free_contents(script, len);
    99 
   100 	recursive_remove(root);
   101 	exit(r ? 1 : 0);
   102 }