use correct plugin name; thanks Corvus Corax
[oweals/gnunet.git] / src / testing / test_testing_peerstartup2.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008, 2009, 2012 GNUnet e.V.
4
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.
9
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.
14
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file testing/test_testing_new_peerstartup.c
23  * @brief test case for testing peer startup and shutdown using new testing
24  *          library
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31
32 #define LOG(kind, ...)                           \
33   GNUNET_log (kind, __VA_ARGS__)
34
35
36 #define FAIL_TEST(cond)                         \
37   do {                                          \
38     if (! (cond)) {                              \
39       GNUNET_break (0);                         \
40       if (GNUNET_OK == status) {                \
41         status = GNUNET_SYSERR;                 \
42       }                                         \
43     }                                           \
44   } while (0)                                   \
45
46
47 /**
48  * The status of the test
49  */
50 int status;
51
52 /**
53  * The testing context
54  */
55 struct TestingContext
56 {
57   /**
58    * The testing system
59    */
60   struct GNUNET_TESTING_System *system;
61
62   /**
63    * The peer which has been started by the testing system
64    */
65   struct GNUNET_TESTING_Peer *peer;
66
67   /**
68    * The running configuration of the peer
69    */
70   struct GNUNET_CONFIGURATION_Handle *cfg;
71
72   /**
73    * State
74    */
75   enum
76   {
77     PEER_INIT,
78
79     PEER_STARTED,
80
81     PEER_STOPPED
82   } state;
83 };
84
85
86 static void
87 do_shutdown2 (void *cls)
88 {
89   struct TestingContext *test_ctx = cls;
90
91   if (NULL != test_ctx->peer)
92     GNUNET_TESTING_peer_destroy (test_ctx->peer);
93   if (NULL != test_ctx->cfg)
94     GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
95   if (NULL != test_ctx->system)
96     GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
97   GNUNET_free (test_ctx);
98 }
99
100
101 /**
102  * Task for shutdown
103  *
104  * @param cls the testing context
105  */
106 static void
107 do_shutdown (void *cls);
108
109
110 static void
111 peer_status_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success)
112 {
113   struct TestingContext *test_ctx = cls;
114
115   switch (test_ctx->state)
116   {
117   case PEER_INIT:
118     FAIL_TEST (0);
119     break;
120
121   case PEER_STARTED:
122     FAIL_TEST (GNUNET_YES == success);
123     test_ctx->state = PEER_STOPPED;
124     GNUNET_SCHEDULER_add_now (&do_shutdown2, cls);
125     break;
126
127   case PEER_STOPPED:
128     FAIL_TEST (0);
129   }
130 }
131
132
133 /**
134  * Task for shutdown
135  *
136  * @param cls the testing context
137  */
138 static void
139 do_shutdown (void *cls)
140 {
141   struct TestingContext *test_ctx = cls;
142
143   GNUNET_assert (NULL != test_ctx);
144   if (NULL != test_ctx->peer)
145   {
146     FAIL_TEST (GNUNET_OK ==
147                GNUNET_TESTING_peer_stop_async (test_ctx->peer,
148                                                &peer_status_cb,
149                                                test_ctx));
150   }
151   else
152     do_shutdown2 (test_ctx);
153 }
154
155
156 /**
157  * Main point of test execution
158  */
159 static void
160 run (void *cls, char *const *args, const char *cfgfile,
161      const struct GNUNET_CONFIGURATION_Handle *cfg)
162 {
163   struct TestingContext *test_ctx;
164   char *emsg;
165   struct GNUNET_PeerIdentity id;
166
167   test_ctx = GNUNET_new (struct TestingContext);
168   test_ctx->system =
169     GNUNET_TESTING_system_create ("test-gnunet-testing",
170                                   "127.0.0.1", NULL, NULL);
171   emsg = NULL;
172   if (NULL == test_ctx->system)
173     goto end;
174   test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
175   test_ctx->peer =
176     GNUNET_TESTING_peer_configure (test_ctx->system,
177                                    test_ctx->cfg,
178                                    0, &id, &emsg);
179   if (NULL == test_ctx->peer)
180   {
181     if (NULL != emsg)
182       printf ("Test failed upon error: %s", emsg);
183     goto end;
184   }
185   if (GNUNET_OK != GNUNET_TESTING_peer_start (test_ctx->peer))
186     goto end;
187   test_ctx->state = PEER_STARTED;
188   FAIL_TEST (GNUNET_OK ==
189              GNUNET_TESTING_peer_stop_async (test_ctx->peer,
190                                              &peer_status_cb,
191                                              test_ctx));
192   GNUNET_TESTING_peer_stop_async_cancel (test_ctx->peer);
193   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
194                                 &do_shutdown, test_ctx);
195   return;
196
197 end:
198   FAIL_TEST (0);
199   GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
200   GNUNET_free_non_null (emsg);
201 }
202
203
204 int main (int argc, char *argv[])
205 {
206   struct GNUNET_GETOPT_CommandLineOption options[] = {
207     GNUNET_GETOPT_OPTION_END
208   };
209
210   status = GNUNET_OK;
211   if (GNUNET_OK !=
212       GNUNET_PROGRAM_run (argc, argv,
213                           "test_testing_new_peerstartup",
214                           "test case for peerstartup using new testing library",
215                           options, &run, NULL))
216     return 1;
217   return (GNUNET_OK == status) ? 0 : 1;
218 }
219
220 /* end of test_testing_peerstartup.c */