more refactoring of tests for new send API
[oweals/gnunet.git] / src / transport / test_transport_testing_restart.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 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 #include "platform.h"
26 #include "gnunet_transport_service.h"
27 #include "transport-testing.h"
28
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
30
31
32 static struct GNUNET_SCHEDULER_Task *timeout_task;
33
34 static struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
35
36 static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
37
38 static int ret;
39
40
41 static void
42 end ()
43 {
44   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45               "Stopping peers\n");
46   if (NULL != timeout_task)
47     GNUNET_SCHEDULER_cancel (timeout_task);
48   if (NULL != p)
49     GNUNET_TRANSPORT_TESTING_stop_peer (p);
50   if (NULL != tth)
51     GNUNET_TRANSPORT_TESTING_done (tth);
52 }
53
54
55 static void
56 end_badly ()
57 {
58   timeout_task = NULL;
59   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
60               "Timeout!\n");
61   end ();
62   ret = GNUNET_SYSERR;
63 }
64
65
66 static void
67 restart_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
68             void *cls)
69 {
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71               "Peer %u (`%s') successfully restarted\n",
72               p->no,
73               GNUNET_i2s (&p->id));
74   ret = 0;
75   end ();
76 }
77
78
79 static void
80 restart_task ()
81 {
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83               "Peer %u (`%s') restarting\n",
84               p->no,
85               GNUNET_i2s (&p->id));
86   GNUNET_TRANSPORT_TESTING_restart_peer (p,
87                                          &restart_cb,
88                                          p);
89 }
90
91
92 static void
93 start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
94           void *cls)
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97               "Peer %u (`%s') successfully started\n",
98               p->no,
99               GNUNET_i2s (&p->id));
100   GNUNET_SCHEDULER_add_now (&restart_task,
101                             NULL);
102 }
103
104
105 static void
106 run (void *cls,
107      char *const *args,
108      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 (TIMEOUT,
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                                           NULL, /* nc/nd closure */
126                                           start_cb, /* startup cb */
127                                           NULL); /* closure */
128   if (NULL == p)
129   {
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131                 "Failed to start peer\n");
132     end ();
133     ret = 1;
134   }
135 }
136
137
138 int
139 main (int argc,
140       char *argv[])
141 {
142   char *const argv_1[] = { "test_transport_testing_restart",
143     "-c",
144     "test_transport_api_data.conf",
145     NULL
146   };
147   struct GNUNET_GETOPT_CommandLineOption options[] = {
148     GNUNET_GETOPT_OPTION_END
149   };
150
151   GNUNET_log_setup ("test_transport_testing_restart",
152                     "WARNING",
153                     NULL);
154   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1,
155                       argv_1,
156                       "test_transport_testing_restart",
157                       "nohelp",
158                       options,
159                       &run,
160                       NULL);
161   return ret;
162 }
163
164 /* end of test_transport_testing_restart.c */