missing check
[oweals/gnunet.git] / src / testing / test_testing_peerstartup2.c
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
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_configuration_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_testing_lib.h"
32
33 #define LOG(kind,...)                           \
34   GNUNET_log (kind, __VA_ARGS__)
35
36
37 #define FAIL_TEST(cond)                         \
38   do {                                          \
39     if (!(cond)) {                              \
40       GNUNET_break (0);                         \
41       if (GNUNET_OK == status) {                \
42         status = GNUNET_SYSERR;                 \
43       }                                         \
44     }                                           \
45   } while (0)                                   \
46     
47
48 /**
49  * The status of the test
50  */
51 int status;
52
53 /**
54  * The testing context
55  */
56 struct TestingContext
57 {
58   /**
59    * The testing system
60    */
61   struct GNUNET_TESTING_System *system;
62   
63   /**
64    * The peer which has been started by the testing system
65    */
66   struct GNUNET_TESTING_Peer *peer;
67
68   /**
69    * The running configuration of the peer
70    */
71   struct GNUNET_CONFIGURATION_Handle *cfg;
72
73   /**
74    * State
75    */
76   enum {
77     PEER_INIT,
78
79     PEER_STARTED,
80
81     PEER_STOPPED
82   } state;
83 };
84
85
86 static void
87 do_shutdown2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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 /**
103  * Task for shutdown
104  *
105  * @param cls the testing context
106  * @param tc the tast context
107  */
108 static void
109 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
110
111
112 static void
113 peer_status_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success)
114 {
115   struct TestingContext *test_ctx = cls;
116
117   switch (test_ctx->state)
118   {
119   case PEER_INIT:
120     FAIL_TEST (0);
121     break;
122   case PEER_STARTED:
123     FAIL_TEST (GNUNET_YES == success);
124     test_ctx->state = PEER_STOPPED;
125     GNUNET_SCHEDULER_add_now (&do_shutdown2, cls);
126     break;
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  * @param tc the tast context
138  */
139 static void
140 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   struct TestingContext *test_ctx = cls;
143
144   GNUNET_assert (NULL != test_ctx);
145   if (NULL != test_ctx->peer)
146   {
147     FAIL_TEST (GNUNET_OK == 
148                GNUNET_TESTING_peer_stop_async (test_ctx->peer,
149                                                &peer_status_cb,
150                                                test_ctx));
151   }
152   else
153     do_shutdown2 (test_ctx, tc);
154 }
155
156
157 /**
158  * Main point of test execution
159  */
160 static void
161 run (void *cls, char *const *args, const char *cfgfile,
162      const struct GNUNET_CONFIGURATION_Handle *cfg)
163 {
164   struct TestingContext *test_ctx;
165   char *emsg;
166   struct GNUNET_PeerIdentity id;
167
168   test_ctx = GNUNET_malloc (sizeof (struct TestingContext));
169   test_ctx->system = 
170       GNUNET_TESTING_system_create ("test-gnunet-testing",
171                                     "127.0.0.1", NULL, NULL);
172   emsg = NULL;
173   if (NULL == test_ctx->system)
174     goto end;
175   test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
176   test_ctx->peer = 
177       GNUNET_TESTING_peer_configure (test_ctx->system,
178                                      test_ctx->cfg,
179                                      0, &id, &emsg);
180   if (NULL == test_ctx->peer)
181   {
182     if (NULL != emsg)
183       printf ("Test failed upon error: %s", emsg);
184     goto end;
185   }
186   if (GNUNET_OK != GNUNET_TESTING_peer_start (test_ctx->peer))
187     goto end;
188   test_ctx->state = PEER_STARTED;
189   FAIL_TEST (GNUNET_OK == 
190              GNUNET_TESTING_peer_stop_async (test_ctx->peer,
191                                              &peer_status_cb,
192                                              test_ctx));
193   GNUNET_TESTING_peer_stop_async_cancel (test_ctx->peer);
194   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
195                                 &do_shutdown, test_ctx);
196   return;
197   
198  end:
199   FAIL_TEST (0);
200   GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
201   GNUNET_free_non_null (emsg);
202 }
203
204
205 int main (int argc, char *argv[])
206 {
207   struct GNUNET_GETOPT_CommandLineOption options[] = {
208     GNUNET_GETOPT_OPTION_END
209   };
210
211   status = GNUNET_OK;
212   if (GNUNET_OK !=
213       GNUNET_PROGRAM_run (argc, argv,
214                           "test_testing_new_peerstartup",
215                           "test case for peerstartup using new testing library",
216                           options, &run, NULL))
217     return 1;
218   return (GNUNET_OK == status) ? 0 : 1;
219 }
220
221 /* end of test_testing_peerstartup.c */