fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_api_disconnect.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_api_disconnect.c
20  * @brief base test case for transport implementations
21  *
22  * This test case tests disconnect notifications in peer shutdown.
23  * Starts two peers, has them connect, sends a message in between,
24  * stops one peer, expects the others to send a disconnect notification.
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, 120)
34
35
36 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
37
38 static int shutdown_;
39
40
41 static void
42 notify_disconnect (void *cls,
43                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
44                    const struct GNUNET_PeerIdentity *other)
45 {
46   if (me != ccc->p[0])
47     return;
48   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
49                                            me,
50                                            other);
51   if (GNUNET_YES == shutdown_)
52   {
53     ccc->global_ret = GNUNET_OK;
54     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
55                 "Test good, shutting down...\n");
56     GNUNET_SCHEDULER_shutdown ();
57   }
58 }
59
60
61 static void
62 stop_peer (void *cls)
63 {
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65               "Shutting down peer %u (`%s')\n",
66               ccc->p[1]->no,
67               GNUNET_i2s (&ccc->p[1]->id));
68   shutdown_ = GNUNET_YES;
69   GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[1]);
70   ccc->p[1] = NULL;
71 }
72
73
74 static void
75 notify_receive (void *cls,
76                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
77                 const struct GNUNET_PeerIdentity *sender,
78                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
79 {
80   {
81     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
82
83     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
85                 receiver->no,
86                 ps,
87                 ntohs (message->header.type),
88                 ntohs (message->header.size),
89                 GNUNET_i2s (sender));
90     GNUNET_free (ps);
91   }
92   if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
93       (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->header.size)))
94   {
95     GNUNET_SCHEDULER_add_now (&stop_peer,
96                               NULL);
97     return;
98   }
99 }
100
101
102 int
103 main (int argc,
104       char *argv[])
105 {
106   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
107     .num_messages = 1
108   };
109   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
110     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
111     .connect_continuation_cls = &sc,
112     .config_file = "test_transport_api_data.conf",
113     .rec = &notify_receive,
114     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
115     .nd = &notify_disconnect,
116     .timeout = TIMEOUT,
117     .global_ret = GNUNET_SYSERR
118   };
119   
120   ccc = &my_ccc;
121   sc.ccc = ccc;
122   if (GNUNET_OK !=
123       GNUNET_TRANSPORT_TESTING_main (2,
124                                      &GNUNET_TRANSPORT_TESTING_connect_check,
125                                      ccc))
126     return 1;
127   return 0;
128 }
129
130 /* end of test_transport_api_disconnect.c */