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