From 1517375e5b33fb42d6cd50fcbbc8d990ddd5b81a Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Sun, 31 Mar 2013 20:03:26 +0000 Subject: - ARM based peer start stop --- src/testing/Makefile.am | 12 ++- src/testing/test_testing_peerstartup2.c | 176 ++++++++++++++++++++++++++++++++ src/testing/testing.c | 170 +++++++++++++++++++++++++++++- 3 files changed, 353 insertions(+), 5 deletions(-) create mode 100644 src/testing/test_testing_peerstartup2.c (limited to 'src') diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am index 2b0cb9879..02a62e57f 100644 --- a/src/testing/Makefile.am +++ b/src/testing/Makefile.am @@ -21,6 +21,7 @@ libgnunettesting_la_SOURCES = \ testing.c libgnunettesting_la_LIBADD = \ $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/arm/libgnunetarm.la \ $(LTLIBINTL) libgnunettesting_la_LDFLAGS = \ $(GN_LIB_LDFLAGS) \ @@ -53,14 +54,17 @@ gnunet_testing_DEPENDENCIES = \ check_PROGRAMS = \ test_testing_portreservation \ + test_testing_servicestartup \ test_testing_peerstartup \ - test_testing_servicestartup + test_testing_peerstartup2 if ENABLE_TEST_RUN TESTS = \ test_testing_portreservation \ test_testing_peerstartup \ + test_testing_peerstartup2 \ test_testing_servicestartup + endif test_testing_portreservation_SOURCES = \ @@ -75,6 +79,12 @@ test_testing_peerstartup_LDADD = \ $(top_builddir)/src/testing/libgnunettesting.la \ $(top_builddir)/src/util/libgnunetutil.la +test_testing_peerstartup2_SOURCES = \ + test_testing_peerstartup2.c +test_testing_peerstartup2_LDADD = \ + $(top_builddir)/src/testing/libgnunettesting.la \ + $(top_builddir)/src/util/libgnunetutil.la + test_testing_servicestartup_SOURCES = \ test_testing_servicestartup.c test_testing_servicestartup_LDADD = \ diff --git a/src/testing/test_testing_peerstartup2.c b/src/testing/test_testing_peerstartup2.c new file mode 100644 index 000000000..45d2280c7 --- /dev/null +++ b/src/testing/test_testing_peerstartup2.c @@ -0,0 +1,176 @@ +/* + This file is part of GNUnet + (C) 2008, 2009, 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + */ + +/** + * @file testing/test_testing_new_peerstartup.c + * @brief test case for testing peer startup and shutdown using new testing + * library + * @author Sree Harsha Totakura + */ + +#include "platform.h" +#include "gnunet_configuration_lib.h" +#include "gnunet_os_lib.h" +#include "gnunet_testing_lib.h" + +#define LOG(kind,...) \ + GNUNET_log (kind, __VA_ARGS__) + +/** + * The status of the test + */ +int status; + +/** + * The testing context + */ +struct TestingContext +{ + /** + * The testing system + */ + struct GNUNET_TESTING_System *system; + + /** + * The peer which has been started by the testing system + */ + struct GNUNET_TESTING_Peer *peer; + + /** + * The running configuration of the peer + */ + struct GNUNET_CONFIGURATION_Handle *cfg; +}; + + +static void +do_shutdown2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct TestingContext *test_ctx = cls; + + if (NULL != test_ctx->peer) + GNUNET_TESTING_peer_destroy (test_ctx->peer); + if (NULL != test_ctx->cfg) + GNUNET_CONFIGURATION_destroy (test_ctx->cfg); + if (NULL != test_ctx->system) + GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES); + GNUNET_free (test_ctx); + +} + +static void +peer_stop_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success) +{ + GNUNET_break (GNUNET_NO == success); + status = GNUNET_OK; + GNUNET_SCHEDULER_add_now (&do_shutdown2, cls); +} + + +/** + * Task for shutdown + * + * @param cls the testing context + * @param tc the tast context + */ +static void +do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct TestingContext *test_ctx = cls; + + GNUNET_assert (NULL != test_ctx); + if (NULL != test_ctx->peer) + { + GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_stop2 (test_ctx->peer, + &peer_stop_cb, + test_ctx)); + } + else + do_shutdown (test_ctx, tc); +} + +static void +ps_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success) +{ + struct TestingContext *test_ctx = cls; + + GNUNET_break (GNUNET_YES == success); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &do_shutdown, test_ctx); +} + + +/** + * Main point of test execution + */ +static void +run (void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + struct TestingContext *test_ctx; + char *emsg; + struct GNUNET_PeerIdentity id; + + test_ctx = GNUNET_malloc (sizeof (struct TestingContext)); + test_ctx->system = + GNUNET_TESTING_system_create ("test-gnunet-testing", + "127.0.0.1", NULL); + emsg = NULL; + if (NULL == test_ctx->system) + goto end; + test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg); + test_ctx->peer = + GNUNET_TESTING_peer_configure (test_ctx->system, + test_ctx->cfg, + 0, &id, &emsg); + if (NULL == test_ctx->peer) + { + if (NULL != emsg) + printf ("Test failed upon error: %s", emsg); + goto end; + } + if (GNUNET_OK != GNUNET_TESTING_peer_start2 (test_ctx->peer, + &ps_cb, test_ctx)) + goto end; + return; + + end: + GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx); + GNUNET_free_non_null (emsg); +} + + +int main (int argc, char *argv[]) +{ + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + + status = GNUNET_SYSERR; + if (GNUNET_OK != + GNUNET_PROGRAM_run (argc, argv, + "test_testing_new_peerstartup", + "test case for peerstartup using new testing library", + options, &run, NULL)) + return 1; + return (GNUNET_OK == status) ? 0 : 1; +} + +/* end of test_testing_peerstartup.c */ diff --git a/src/testing/testing.c b/src/testing/testing.c index 1711531c4..216dddfea 100644 --- a/src/testing/testing.c +++ b/src/testing/testing.c @@ -31,6 +31,7 @@ */ #include "platform.h" #include "gnunet_util_lib.h" +#include "gnunet_arm_service.h" #include "gnunet_testing_lib.h" #define LOG(kind,...) \ @@ -179,6 +180,22 @@ struct GNUNET_TESTING_Peer */ struct GNUNET_OS_Process *main_process; + /** + * The handle to the peer's ARM service + */ + struct GNUNET_ARM_Handle *ah; + + /** + * The config of the peer + */ + struct GNUNET_CONFIGURATION_Handle *cfg; + + GNUNET_TESTING_PeerStartCallback cb; + + void *cb_cls; + + struct GNUNET_ARM_MonitorHandle *mh; + /** * The cached identity of this peer. Will be populated on call to * GNUNET_TESTING_peer_get_identity() @@ -819,6 +836,8 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system, GNUNET_asprintf (&default_config, "%s/config", uc.service_home); GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", default_config); + GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", + default_config); GNUNET_free (default_config); GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "SERVICEHOME", uc.service_home); @@ -876,7 +895,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system, else GNUNET_free (emsg_); return NULL; - } + } if (key_number >= system->total_hostkeys) { GNUNET_asprintf (&emsg_, @@ -960,6 +979,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system, } peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer)); peer->cfgfile = config_filename; /* Free in peer_destroy */ + peer->cfg = GNUNET_CONFIGURATION_dup (cfg); libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm"); if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "arm", "PREFIX", &peer->main_binary)) { @@ -1035,6 +1055,92 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer) } +void +GNUNET_TESTING_peer_service_start (struct GNUNET_TESTING_Peer *peer, + const char *service_name, + GNUNET_ARM_ResultCallback cont, + void *cont_cls) +{ + GNUNET_ARM_request_service_start (peer->ah, + service_name, + GNUNET_OS_INHERIT_STD_ALL, + GNUNET_TIME_UNIT_MINUTES, + cont, + cont_cls); + +} + + +void GNUNET_TESTING_peer_service_stop (struct GNUNET_TESTING_Peer *peer, + const char *service_name, + GNUNET_ARM_ResultCallback cont, + void *cont_cls) +{ + GNUNET_ARM_request_service_stop (peer->ah, service_name, + GNUNET_TIME_UNIT_MINUTES, + cont, cont_cls); +} + +static void +arm_start_result_cb (void *cls, + struct GNUNET_ARM_Handle *arm, + enum GNUNET_ARM_RequestStatus rs, + const char *service, + enum GNUNET_ARM_Result result) +{ + struct GNUNET_TESTING_Peer *peer = cls; + + if ((GNUNET_ARM_REQUEST_SENT_OK != rs) + || ! ((GNUNET_ARM_RESULT_STARTING == result) + || (GNUNET_ARM_RESULT_IS_STARTING_ALREADY == result) + || (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result))) + { + peer->cb (peer->cb_cls, peer, GNUNET_NO); + return; + } + peer->cb (peer->cb_cls, peer, GNUNET_OK); +} + +/** + * Function called whenever we connect to or disconnect from ARM. + * + * @param cls closure + * @param arm handle to the ARM connection + * @param connected GNUNET_YES if connected, GNUNET_NO if disconnected, + * GNUNET_SYSERR on error. + */ +static void +conn_status (void *cls, struct GNUNET_ARM_Handle *arm, + int connected) +{ + struct GNUNET_TESTING_Peer *peer = cls; + + peer->cb (peer->cb_cls, peer, connected); +} + + +int +GNUNET_TESTING_peer_start2 (struct GNUNET_TESTING_Peer *peer, + GNUNET_TESTING_PeerStartCallback cb, + void *cb_cls) +{ + if (NULL != peer->ah) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + GNUNET_assert (NULL != (peer->cb = cb)); + peer->cb_cls = cb_cls; + peer->ah = GNUNET_ARM_connect (peer->cfg, &conn_status, peer); + if (NULL == peer->ah) + return GNUNET_SYSERR; + //GNUNET_TESTING_peer_service_start (peer, "arm", &arm_start_result_cb, peer); + GNUNET_TESTING_peer_service_start (peer, "arm", NULL, NULL); + return GNUNET_OK; +} + + + /** * Sends SIGTERM to the peer's main process * @@ -1094,6 +1200,60 @@ GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer) return GNUNET_SYSERR; return GNUNET_OK; } + +static void +arm_stop_result_cb (void *cls, + struct GNUNET_ARM_Handle *arm, + enum GNUNET_ARM_RequestStatus rs, + const char *service, + enum GNUNET_ARM_Result result) +{ + struct GNUNET_TESTING_Peer *peer = cls; + + if ((GNUNET_ARM_REQUEST_SENT_OK != rs) + || ! ((GNUNET_ARM_RESULT_STOPPED == result) + || (GNUNET_ARM_RESULT_STOPPING == result) + || (GNUNET_ARM_RESULT_IS_STOPPING_ALREADY == result) + || (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result))) + { + peer->cb (peer->cb_cls, peer, GNUNET_NO); + return; + } + peer->cb (peer->cb_cls, peer, GNUNET_OK); +} + + +static void +arm_service_monitor (void *cls, + struct GNUNET_ARM_MonitorHandle *arm, + const char *service, + enum GNUNET_ARM_ServiceStatus status) +{ + struct GNUNET_TESTING_Peer *peer = cls; + + peer->mh = arm; + if (GNUNET_ARM_SERVICE_STOPPED != status) + return; + if (0 != strcasecmp (service, "arm")) + return; + peer->cb (peer->cb_cls, peer, GNUNET_OK); +} + + +int +GNUNET_TESTING_peer_stop2 (struct GNUNET_TESTING_Peer *peer, + GNUNET_TESTING_PeerStartCallback cb, + void *cb_cls) +{ + if (NULL == peer->ah) + return GNUNET_SYSERR; + GNUNET_assert (NULL != (peer->cb = cb)); + peer->cb_cls = cb_cls; + /* if (NULL == peer->mh) */ + /* peer->mh = GNUNET_ARM_monitor (peer->cfg, &arm_service_monitor, peer); */ + GNUNET_TESTING_peer_service_stop (peer, "arm", NULL, NULL); + return GNUNET_OK; +} /** @@ -1107,11 +1267,13 @@ void GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer) { if (NULL != peer->main_process) - { - GNUNET_break (0); GNUNET_TESTING_peer_stop (peer); - } + if (NULL != peer->mh) + GNUNET_ARM_monitor_disconnect_and_free (peer->mh); + if (NULL != peer->ah) + GNUNET_ARM_disconnect_and_free (peer->ah); GNUNET_free (peer->cfgfile); + GNUNET_CONFIGURATION_destroy (peer->cfg); GNUNET_free (peer->main_binary); GNUNET_free (peer->args); GNUNET_free_non_null (peer->id); -- cgit v1.2.3