#include "hybbx/instance.h" #include "hybbx/networks.h" #include #include #include static int failures; static void expect_int(const char *label, int got, int want) { if (got != want) { fprintf(stderr, "FAIL %s: got %d want %d\n", label, got, want); failures++; } } static void test_enforce_hub(void) { hybbx_networks_config_t net; hybbx_networks_config_defaults(&net); net.telnet = 1; net.ax25 = 1; net.baycom = 1; net.ardop = 1; net.crdop = 1; net.websocket = 0; net.mains_proxy = 0; if (hybbx_networks_enforce_instance(&net) != HYBBX_OK) { fprintf(stderr, "FAIL enforce returned error\n"); failures++; return; } switch (hybbx_instance_role()) { case HYBBX_INSTANCE_MAIN: expect_int("main ax25 off", net.ax25, 0); expect_int("main baycom off", net.baycom, 0); expect_int("main ardop off", net.ardop, 0); expect_int("main crdop off", net.crdop, 0); expect_int("main telnet off", net.telnet, 0); expect_int("main websocket on", net.websocket, 1); expect_int("main circuit on", net.circuit, 1); break; case HYBBX_INSTANCE_SECONDARY: expect_int("secondary ax25 kept", net.ax25, 1); expect_int("secondary baycom kept", net.baycom, 1); expect_int("secondary telnet off", net.telnet, 0); expect_int("secondary websocket off", net.websocket, 0); expect_int("secondary mains_proxy on", net.mains_proxy, 1); break; case HYBBX_INSTANCE_PROXY: expect_int("proxy ax25 kept", net.ax25, 1); expect_int("proxy baycom kept", net.baycom, 1); expect_int("proxy ardop kept", net.ardop, 1); expect_int("proxy crdop kept", net.crdop, 1); expect_int("proxy websocket off", net.websocket, 0); expect_int("proxy circuit on", net.circuit, 1); break; default: fprintf(stderr, "FAIL unknown role\n"); failures++; break; } } static void test_plugin_allow_hub(void) { switch (hybbx_instance_role()) { case HYBBX_INSTANCE_MAIN: expect_int("main packet_radio denied", hybbx_instance_plugin_allowed("packet_radio"), 0); expect_int("main baycom denied", hybbx_instance_plugin_allowed("baycom"), 0); expect_int("main ardop denied", hybbx_instance_plugin_allowed("ardop"), 0); expect_int("main crdop denied", hybbx_instance_plugin_allowed("crdop"), 0); expect_int("main websocket", hybbx_instance_plugin_allowed("websocket"), 1); expect_int("main telnet", hybbx_instance_plugin_allowed("telnet"), 0); expect_int("main user_bbx", hybbx_instance_offers_user_bbx(), 1); break; case HYBBX_INSTANCE_SECONDARY: expect_int("secondary packet_radio", hybbx_instance_plugin_allowed("packet_radio"), 1); expect_int("secondary baycom", hybbx_instance_plugin_allowed("baycom"), 1); expect_int("secondary mains_proxy", hybbx_instance_plugin_allowed("mains_proxy"), 1); expect_int("secondary telnet", hybbx_instance_plugin_allowed("telnet"), 0); expect_int("secondary websocket", hybbx_instance_plugin_allowed("websocket"), 0); expect_int("secondary user_bbx", hybbx_instance_offers_user_bbx(), 0); break; case HYBBX_INSTANCE_PROXY: expect_int("proxy packet_radio", hybbx_instance_plugin_allowed("packet_radio"), 1); expect_int("proxy baycom", hybbx_instance_plugin_allowed("baycom"), 1); expect_int("proxy telnet", hybbx_instance_plugin_allowed("telnet"), 1); expect_int("proxy ssh", hybbx_instance_plugin_allowed("ssh"), 1); expect_int("proxy websocket", hybbx_instance_plugin_allowed("websocket"), 0); expect_int("proxy mains_proxy", hybbx_instance_plugin_allowed("mains_proxy"), 1); expect_int("proxy user_bbx", hybbx_instance_offers_user_bbx(), 0); break; default: break; } } static void test_standalone_main(void) { hybbx_networks_config_t net; if (hybbx_instance_role() != HYBBX_INSTANCE_MAIN) { return; } hybbx_instance_set_standalone(1); expect_int("standalone baycom allowed", hybbx_instance_plugin_allowed("baycom"), 1); expect_int("standalone packet_radio allowed", hybbx_instance_plugin_allowed("packet_radio"), 1); expect_int("standalone ardop allowed", hybbx_instance_plugin_allowed("ardop"), 1); hybbx_networks_config_defaults(&net); net.telnet = 1; net.ax25 = 1; net.baycom = 1; net.ardop = 1; net.crdop = 1; if (hybbx_networks_enforce_instance(&net) != HYBBX_OK) { fprintf(stderr, "FAIL standalone enforce returned error\n"); failures++; hybbx_instance_set_standalone(0); return; } expect_int("standalone ax25 kept", net.ax25, 1); expect_int("standalone baycom kept", net.baycom, 1); expect_int("standalone ardop kept", net.ardop, 1); expect_int("standalone crdop kept", net.crdop, 1); hybbx_instance_set_standalone(0); } int main(void) { printf("instance tests role=%s binary=%s\n", hybbx_instance_role_name(), hybbx_instance_binary_name()); test_plugin_allow_hub(); test_enforce_hub(); test_standalone_main(); return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }