box-o-sand/lcthw-remnants-2/ex29/tests/ex29_tests.c

66 lines
1.4 KiB
C
Raw Normal View History

2016-04-16 20:41:20 +00:00
#include "minunit.h"
2016-04-16 20:00:17 +00:00
#include <dlfcn.h>
typedef int (*lib_function)(const char *data, int count);
2016-04-16 20:41:20 +00:00
char *lib_file = "build/libex29.so";
void *lib = NULL;
2016-04-16 20:00:17 +00:00
2016-04-16 20:41:20 +00:00
int check_function(const char *func_to_run, const char *data, int expected)
2016-04-16 20:00:17 +00:00
{
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());
2016-04-16 20:41:20 +00:00
int rc = func(data, (int)strlen(data));
check(rc == expected, "Function %s return %d for data: %s", func_to_run, rc, data);
2016-04-16 20:00:17 +00:00
2016-04-16 20:41:20 +00:00
return 1;
error:
2016-04-16 20:00:17 +00:00
return 0;
2016-04-16 20:41:20 +00:00
}
2016-04-16 20:00:17 +00:00
2016-04-16 20:41:20 +00:00
char *test_dlopen()
{
lib = dlopen(lib_file, RTLD_NOW);
mu_assert(lib != NULL, "Failed to open the library to test.");
return NULL;
2016-04-16 20:00:17 +00:00
}
2016-04-16 20:41:20 +00:00
char *test_functions()
2016-04-16 20:00:17 +00:00
{
2016-04-16 20:41:20 +00:00
mu_assert(check_function("print_a_message", "Hello", 0), "print_a_message failed.");
mu_assert(check_function("uppercase", "Hello", 0), "uppercase failed.");
mu_assert(check_function("lowercase", "Hello", 0), "lowercase failed.");
2016-04-16 20:00:17 +00:00
2016-04-16 20:41:20 +00:00
return NULL;
2016-04-16 20:00:17 +00:00
}
2016-04-16 20:41:20 +00:00
char *test_failures()
{
mu_assert(check_function("fail_on_purpose", "Hello", 1), "fail_on_purpose should fail.");
return NULL;
}
char *test_dlclose()
{
int rc = dlclose(lib);
mu_assert(rc == 0, "Failed to close lib.");
return NULL;
}
char *all_tests()
{
mu_suite_start();
mu_run_test(test_dlopen);
mu_run_test(test_functions);
mu_run_test(test_failures);
mu_run_test(test_dlclose);
return NULL;
}
RUN_TESTS(all_tests);