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