#include <atf-c.h>
#include <errno.h>
#include <stdio.h>
#include "util.h"
ATF_TC(basic);
ATF_TC_HEAD(basic, conf)
{
atf_tc_set_md_var(conf, "require.user", "root");
}
ATF_TC_BODY(basic, dummy)
{
char msg[] = "test";
ng_counter_t r;
ng_errors(PASS);
ng_shutdown("hub:");
ng_errors(FAIL);
ng_init();
ng_mkpeer(".", "a", "hub", "a");
ng_name("a", "hub");
ng_connect(".", "b", "hub:", "b");
ng_connect(".", "c", "hub:", "c");
ng_register_data("a", get_data0);
ng_counter_clear(r);
ng_send_data("a", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 0);
ng_register_data("b", get_data0);
ng_register_data("c", get_data0);
ng_counter_clear(r);
ng_send_data("a", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 2);
ng_counter_clear(r);
ng_send_data("b", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 2);
ng_counter_clear(r);
ng_send_data("c", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 2);
ng_rmhook(".", "b");
ng_counter_clear(r);
ng_send_data("a", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 1);
ng_shutdown("hub:");
}
ATF_TC(persistence);
ATF_TC_HEAD(persistence, conf)
{
atf_tc_set_md_var(conf, "require.user", "root");
}
ATF_TC_BODY(persistence, dummy)
{
ng_errors(PASS);
ng_shutdown("hub:");
ng_errors(FAIL);
ng_init();
ng_mkpeer(".", "a", "hub", "a");
ng_name("a", "hub");
ng_send_msg("hub:", "setpersistent");
ng_rmhook(".", "a");
ng_shutdown("hub:");
}
ATF_TC(loop);
ATF_TC_HEAD(loop, conf)
{
atf_tc_set_md_var(conf, "require.user", "root");
}
ATF_TC_BODY(loop, dummy)
{
ng_counter_t r;
int i;
char msg[] = "LOOP Alert!";
ng_errors(PASS);
ng_shutdown("hub1:");
ng_shutdown("hub2:");
ng_errors(FAIL);
ng_init();
ng_mkpeer(".", "a", "hub", "a");
ng_name("a", "hub1");
ng_mkpeer(".", "b", "hub", "b");
ng_name("b", "hub2");
ng_register_data("a", get_data0);
ng_register_data("b", get_data0);
ng_connect("hub1:", "xc1", "hub2:", "xc1");
ng_counter_clear(r);
ng_send_data("a", msg, sizeof(msg));
ng_handle_events(50, r);
ATF_CHECK(r[0] == 1);
ng_connect("hub1:", "xc2", "hub2:", "xc2");
ng_counter_clear(r);
ng_send_data("a", msg, sizeof(msg));
for (i = 0; i < 10; i++)
if (!ng_handle_event(50, r))
break;
ATF_CHECK(r[0] > 7);
ng_shutdown("hub1:");
ng_shutdown("hub2:");
}
ATF_TC(many_hooks);
ATF_TC_HEAD(many_hooks, conf)
{
atf_tc_set_md_var(conf, "require.user", "root");
}
ATF_TC_BODY(many_hooks, dummy)
{
ng_counter_t r;
int i;
char msg[] = "test";
const int HOOKS = 1000;
ng_errors(PASS);
ng_shutdown("hub:");
ng_errors(FAIL);
ng_init();
ng_mkpeer(".", "a", "hub", "a");
ng_name("a", "hub");
ng_register_data("a", get_data0);
ng_counter_clear(r);
for (i = 0; i < HOOKS; i++)
{
char hook[20];
snprintf(hook, sizeof(hook), "hook%d", i);
ng_connect(".", hook, "hub:", hook);
ng_errors(PASS);
ng_send_data(hook, msg, sizeof(msg));
ng_errors(FAIL);
if (errno != 0)
break;
ng_handle_events(50, r);
}
ATF_CHECK(r[0] > 100);
atf_tc_expect_fail("Implementation limitation (%d)", i);
ATF_CHECK(r[0] == HOOKS);
atf_tc_expect_pass();
ng_shutdown("hub:");
}
ATF_TP_ADD_TCS(hub)
{
ATF_TP_ADD_TC(hub, basic);
ATF_TP_ADD_TC(hub, loop);
ATF_TP_ADD_TC(hub, persistence);
ATF_TP_ADD_TC(hub, many_hooks);
return atf_no_error();
}