-start to refactor testcases for sanity
[oweals/gnunet.git] / src / transport / test_transport_testing_restart.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file transport/test_transport_testing_restart.c
22  * @brief test case for transport testing library:
23  * start the peer, get the HELLO message, restart and stop the peer
24  *
25  */
26 #include "platform.h"
27 #include "gnunet_transport_service.h"
28 #include "transport-testing.h"
29
30 /**
31  * How long until we give up on transmitting the message?
32  */
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
34
35 struct GNUNET_SCHEDULER_Task * timeout_task;
36
37 static struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
38
39 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
40
41 static int ret = 0;
42
43 static void
44 end ()
45 {
46   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
47
48   if (timeout_task != NULL)
49     GNUNET_SCHEDULER_cancel (timeout_task);
50
51   GNUNET_TRANSPORT_TESTING_stop_peer (p);
52   GNUNET_TRANSPORT_TESTING_done (tth);
53 }
54
55 static void
56 end_badly ()
57 {
58   timeout_task = NULL;
59   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
60
61   if (NULL != p)
62     GNUNET_TRANSPORT_TESTING_stop_peer (p);
63
64   if (NULL != tth)
65     GNUNET_TRANSPORT_TESTING_done (tth);
66
67   ret = GNUNET_SYSERR;
68 }
69
70 static void
71 restart_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
72             void *cls)
73 {
74   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75               "Peer %u (`%s') successfully restarted\n",
76               p->no,
77               GNUNET_i2s (&p->id));
78   ret = 0;
79   GNUNET_SCHEDULER_add_now (&end, NULL);
80 }
81
82
83 static void
84 restart_task ()
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87               "Peer %u (`%s') restarting, \n",
88               p->no,
89               GNUNET_i2s (&p->id));
90   GNUNET_TRANSPORT_TESTING_restart_peer (p,
91                                          &restart_cb,
92                                          p);
93 }
94
95
96 static void
97 start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
98 {
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') successfully started\n",
100               p->no,
101               GNUNET_i2s (&p->id));
102
103   GNUNET_SCHEDULER_add_now (&restart_task, NULL);
104 }
105
106
107 static void
108 run (void *cls, char *const *args, const char *cfgfile,
109      const struct GNUNET_CONFIGURATION_Handle *cfg)
110 {
111   ret = 1;
112   tth = GNUNET_TRANSPORT_TESTING_init ();
113   GNUNET_assert (NULL != tth);
114
115   timeout_task =
116       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
117                                     &end_badly,
118                                     NULL);
119   p = GNUNET_TRANSPORT_TESTING_start_peer(tth,
120                                           cfgfile,
121                                           1,
122                                           NULL, /* receive cb */
123                                           NULL, /* connect cb */
124                                           NULL, /* disconnect cb */
125                                           start_cb, /* startup cb */
126                                           NULL); /* closure */
127   if (NULL == p)
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start peer\n");
130     if (timeout_task != NULL)
131       GNUNET_SCHEDULER_cancel (timeout_task);
132     timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
133   }
134 }
135
136 int
137 main (int argc, char *argv[])
138 {
139   GNUNET_log_setup ("test_transport_testing_restart",
140                     "WARNING",
141                     NULL);
142
143   char *const argv_1[] = { "test_transport_testing_restart",
144     "-c",
145     "test_transport_api_data.conf",
146     NULL
147   };
148
149   struct GNUNET_GETOPT_CommandLineOption options[] = {
150     GNUNET_GETOPT_OPTION_END
151   };
152
153   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
154                       "test_transport_testing_restart", "nohelp", options, &run, &ret);
155
156   return ret;
157 }
158
159 /* end of test_transport_testing_restart.c */