box-o-sand/lcthw-remnants-2/ex29/tests/ex29_tests.c
2016-04-16 16:00:17 -04:00

49 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "dbg.h"
#include <dlfcn.h>
typedef int (*lib_function)(const char *data, int count);
int test_ex29(char *lib_file, char *func_to_run, char *data)
{
int rc = 0;
void *lib = dlopen(lib_file, RTLD_NOW);
check(lib != NULL, "Failed to open the library %s: %s", lib_file, dlerror());
lib_function func = dlsym(lib, func_to_run);
check(func != NULL, "Did not find %s function in the library %s: %s", func_to_run, lib_file, dlerror());
rc = func(data, (int)strlen(data));
check(rc == 0, "Function %s return %d for data: %s", func_to_run, rc, data);
rc = dlclose(lib);
check(rc == 0, "Failed to close %s", lib_file);
return 0;
error:
return 1;
}
int main()
{
char *dllib = getenv("EX29_DLLIB");
if(dllib == NULL) {
dllib = "build/libex29.so";
}
check(test_ex29(dllib, "print_a_message", "hello there") == 0, "print failed");
check(test_ex29(dllib, "uppercase", "hello there") == 0, "uppercase failed");
check(test_ex29(dllib, "lowercase", "HELLO tHeRe") == 0, "lowercase failed");
check(test_ex29(dllib, "fail_on_purpose", "i fail") == 1, "fail failed to fail");
check(test_ex29(dllib, "fail_on_purpose", "") == 1, "fail failed to fail");
check(test_ex29(dllib, "adfasfasdf", "asdfadff") == 1, "adfasfasdf failed to fail");
check(test_ex29("libex.so", "adfasfasdf", "asdfadff") == 1, "libex failed to fail");
return 0;
error:
return 1;
}