porting transport testing to new testing lib (mantis 2453)
[oweals/gnunet.git] / src / transport / test_transport_testing_restart.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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  * @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_common.h"
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
30
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35
36 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
37
38 static struct PeerContext *p;
39
40 struct GNUNET_TRANSPORT_TESTING_handle *tth;
41
42 static int ret = 0;
43
44 static void
45 end ()
46 {
47   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
48
49   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
50     GNUNET_SCHEDULER_cancel (timeout_task);
51
52   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
53   GNUNET_TRANSPORT_TESTING_done (tth);
54 }
55
56 static void
57 end_badly ()
58 {
59   timeout_task = GNUNET_SCHEDULER_NO_TASK;
60   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
61
62   if (NULL != p)
63     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
64
65   if (NULL != tth)
66     GNUNET_TRANSPORT_TESTING_done (tth);
67
68   ret = GNUNET_SYSERR;
69 }
70
71 static void
72 restart_cb (struct PeerContext *p, void *cls)
73 {
74   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') successfully restarted\n",
75               p->no,
76               GNUNET_i2s (&p->id));
77
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, "Peer %u (`%s') restarting, \n",
87               p->no,
88               GNUNET_i2s (&p->id));
89   GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, NULL, restart_cb, p);
90 }
91
92 static void
93 start_cb (struct PeerContext *p, void *cls)
94 {
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') successfully started\n",
96               p->no,
97               GNUNET_i2s (&p->id));
98
99   GNUNET_SCHEDULER_add_now (&restart_task, NULL);
100 }
101
102
103 static void
104 run (void *cls, char *const *args, const char *cfgfile,
105      const struct GNUNET_CONFIGURATION_Handle *cfg)
106 {
107   ret = 1;
108   tth = GNUNET_TRANSPORT_TESTING_init ();
109   GNUNET_assert (NULL != tth);
110
111   timeout_task =
112       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
113
114   p = GNUNET_TRANSPORT_TESTING_start_peer(tth, cfgfile, 1,
115                                           NULL, /* receive cb */
116                                           NULL, /* connect cb */
117                                           NULL, /* disconnect cb */
118                                           start_cb, /* startup cb */
119                                           NULL); /* closure */
120   if (NULL == p)
121   {
122     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start peer\n");
123     if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
124       GNUNET_SCHEDULER_cancel (timeout_task);
125     timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
126   }
127 }
128
129 int
130 main (int argc, char *argv[])
131 {
132   GNUNET_log_setup ("test_transport_testing_restart",
133                     "WARNING",
134                     NULL);
135
136   char *const argv_1[] = { "test_transport_testing_restart",
137     "-c",
138     "test_transport_api_data.conf",
139     NULL
140   };
141
142   struct GNUNET_GETOPT_CommandLineOption options[] = {
143     GNUNET_GETOPT_OPTION_END
144   };
145
146   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
147                       "test_transport_testing_restart", "nohelp", options, &run, &ret);
148
149   return ret;
150 }
151
152 /* end of test_transport_testing_restart.c */