Files
flexnbd-c/tests/unit/check_util.c

174 lines
3.1 KiB
C
Raw Normal View History

2012-06-21 18:01:56 +01:00
#include "util.h"
#include "self_pipe.h"
#include <check.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
struct cleanup_bucket {
struct self_pipe *called_signal;
2012-06-21 18:01:56 +01:00
};
struct cleanup_bucket bkt;
void bucket_init(void){
if ( bkt.called_signal ) {
self_pipe_destroy( bkt.called_signal );
}
bkt.called_signal = self_pipe_create();
2012-06-21 18:01:56 +01:00
}
void setup(void)
{
bkt.called_signal = NULL;
2012-06-21 18:01:56 +01:00
}
int handler_called(void)
{
return self_pipe_signal_clear( bkt.called_signal );
2012-06-21 18:01:56 +01:00
}
void dummy_cleanup( struct cleanup_bucket * foo, int fatal __attribute__((unused)) )
2012-06-21 18:01:56 +01:00
{
if (NULL != foo){
self_pipe_signal( foo->called_signal );
}
2012-06-21 18:01:56 +01:00
}
void trigger_fatal(void)
{
error_init();
error_set_handler( (cleanup_handler*) dummy_cleanup, &bkt );
2012-06-21 18:01:56 +01:00
log_level = 5;
2012-06-21 18:01:56 +01:00
fatal("Expected fatal error");
2012-06-21 18:01:56 +01:00
}
void trigger_error( void )
2012-06-21 18:01:56 +01:00
{
error_init();
error_set_handler( (cleanup_handler *) dummy_cleanup, &bkt);
log_level = 4;
error("Expected error");
2012-06-21 18:01:56 +01:00
}
START_TEST( test_fatal_kills_process )
2012-06-21 18:01:56 +01:00
{
pid_t pid;
pid = fork();
if ( pid == 0 ) {
trigger_fatal();
/* If we get here, just block so the test timeout fails
* us */
sleep(10);
}
else {
int kidret, kidstatus, result;
result = waitpid( pid, &kidret, 0 );
fail_if( result < 0, "Wait failed." );
fail_unless( WIFSIGNALED( kidret ), "Process didn't exit via signal" );
kidstatus = WTERMSIG( kidret );
ck_assert_int_eq( kidstatus, SIGABRT );
}
2012-06-21 18:01:56 +01:00
}
END_TEST
2012-06-21 18:01:56 +01:00
void * error_thread( void *nothing __attribute__((unused)) )
2012-06-21 18:01:56 +01:00
{
trigger_error();
return NULL;
2012-06-21 18:01:56 +01:00
}
START_TEST( test_error_doesnt_kill_process )
2012-06-21 18:01:56 +01:00
{
bucket_init();
pthread_attr_t attr;
pthread_t tid;
2012-06-21 18:01:56 +01:00
pthread_attr_init( &attr );
2012-06-21 18:01:56 +01:00
pthread_create( &tid, &attr, error_thread, NULL );
pthread_join( tid, NULL );
2012-06-21 18:01:56 +01:00
}
END_TEST
2012-06-21 18:01:56 +01:00
START_TEST( test_error_calls_handler )
2012-06-21 18:01:56 +01:00
{
bucket_init();
2012-06-21 18:01:56 +01:00
pthread_attr_t attr;
pthread_t tid;
2012-06-21 18:01:56 +01:00
pthread_attr_init( &attr );
2012-06-21 18:01:56 +01:00
pthread_create( &tid, &attr, error_thread, NULL );
pthread_join( tid, NULL );
fail_unless( handler_called(), "Handler wasn't called." );
2012-06-21 18:01:56 +01:00
}
END_TEST
2012-06-21 18:01:56 +01:00
START_TEST( test_fatal_doesnt_call_handler )
2012-06-21 18:01:56 +01:00
{
bucket_init();
2012-06-21 18:01:56 +01:00
pid_t kidpid;
2012-06-21 18:01:56 +01:00
kidpid = fork();
if ( kidpid == 0 ) {
trigger_fatal();
}
else {
int kidstatus;
int result = waitpid( kidpid, &kidstatus, 0 );
fail_if( result < 0, "Wait failed" );
fail_if( handler_called(), "Handler was called.");
}
2012-06-21 18:01:56 +01:00
}
END_TEST
2012-06-21 18:01:56 +01:00
Suite* util_suite(void)
{
Suite *s = suite_create("util");
TCase *tc_process = tcase_create("process");
TCase *tc_handler = tcase_create("handler");
2012-06-21 18:01:56 +01:00
tcase_add_checked_fixture( tc_process, setup, NULL );
2012-06-21 18:01:56 +01:00
tcase_add_test(tc_process, test_fatal_kills_process);
tcase_add_test(tc_process, test_error_doesnt_kill_process);
tcase_add_test(tc_handler, test_error_calls_handler );
tcase_add_test(tc_handler, test_fatal_doesnt_call_handler);
2018-02-20 10:05:35 +00:00
suite_add_tcase(s, tc_process);
suite_add_tcase(s, tc_handler);
return s;
2012-06-21 18:01:56 +01:00
}
int main(void)
{
int number_failed;
Suite *s = util_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
2012-06-21 18:01:56 +01:00
}