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