2 This file is part of GNUnet
3 Copyright (C) 2008, 2009, 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file testing/test_testing_new_peerstartup.c
18 * @brief test case for testing peer startup and shutdown using new testing
20 * @author Sree Harsha Totakura
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
27 #define LOG(kind,...) \
28 GNUNET_log (kind, __VA_ARGS__)
31 #define FAIL_TEST(cond) \
35 if (GNUNET_OK == status) { \
36 status = GNUNET_SYSERR; \
43 * The status of the test
55 struct GNUNET_TESTING_System *system;
58 * The peer which has been started by the testing system
60 struct GNUNET_TESTING_Peer *peer;
63 * The running configuration of the peer
65 struct GNUNET_CONFIGURATION_Handle *cfg;
81 do_shutdown2 (void *cls)
83 struct TestingContext *test_ctx = cls;
85 if (NULL != test_ctx->peer)
86 GNUNET_TESTING_peer_destroy (test_ctx->peer);
87 if (NULL != test_ctx->cfg)
88 GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
89 if (NULL != test_ctx->system)
90 GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
91 GNUNET_free (test_ctx);
99 * @param cls the testing context
102 do_shutdown (void *cls);
106 peer_status_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success)
108 struct TestingContext *test_ctx = cls;
110 switch (test_ctx->state)
116 FAIL_TEST (GNUNET_YES == success);
117 test_ctx->state = PEER_STOPPED;
118 GNUNET_SCHEDULER_add_now (&do_shutdown2, cls);
129 * @param cls the testing context
132 do_shutdown (void *cls)
134 struct TestingContext *test_ctx = cls;
136 GNUNET_assert (NULL != test_ctx);
137 if (NULL != test_ctx->peer)
139 FAIL_TEST (GNUNET_OK ==
140 GNUNET_TESTING_peer_stop_async (test_ctx->peer,
145 do_shutdown2 (test_ctx);
150 * Main point of test execution
153 run (void *cls, char *const *args, const char *cfgfile,
154 const struct GNUNET_CONFIGURATION_Handle *cfg)
156 struct TestingContext *test_ctx;
158 struct GNUNET_PeerIdentity id;
160 test_ctx = GNUNET_new (struct TestingContext);
162 GNUNET_TESTING_system_create ("test-gnunet-testing",
163 "127.0.0.1", NULL, NULL);
165 if (NULL == test_ctx->system)
167 test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
169 GNUNET_TESTING_peer_configure (test_ctx->system,
172 if (NULL == test_ctx->peer)
175 printf ("Test failed upon error: %s", emsg);
178 if (GNUNET_OK != GNUNET_TESTING_peer_start (test_ctx->peer))
180 test_ctx->state = PEER_STARTED;
181 FAIL_TEST (GNUNET_OK ==
182 GNUNET_TESTING_peer_stop_async (test_ctx->peer,
185 GNUNET_TESTING_peer_stop_async_cancel (test_ctx->peer);
186 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
187 &do_shutdown, test_ctx);
192 GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
193 GNUNET_free_non_null (emsg);
197 int main (int argc, char *argv[])
199 struct GNUNET_GETOPT_CommandLineOption options[] = {
200 GNUNET_GETOPT_OPTION_END
205 GNUNET_PROGRAM_run (argc, argv,
206 "test_testing_new_peerstartup",
207 "test case for peerstartup using new testing library",
208 options, &run, NULL))
210 return (GNUNET_OK == status) ? 0 : 1;
213 /* end of test_testing_peerstartup.c */