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