migrate more tests to new transmission API
[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
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_api_disconnect.c
22  * @brief base test case for transport implementations
23  *
24  * This test case tests disconnect notifications in peer shutdown.
25  * Starts two peers, has them connect, sends a message in between,
26  * stops one peer, expects the others to send a disconnect notification.
27  */
28 #include "platform.h"
29 #include "gnunet_transport_service.h"
30 #include "transport-testing.h"
31
32 /**
33  * How long until we give up on transmitting the message?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
41
42 #define MTYPE 12345
43
44 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
45
46 static struct GNUNET_TRANSPORT_TransmitHandle *th;
47
48 static int shutdown_;
49
50
51 static void
52 custom_shutdown (void *cls)
53 {
54   if (th != NULL)
55   {
56     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
57     th = NULL;
58   }
59 }
60
61
62 static void
63 notify_disconnect (void *cls,
64                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
65                    const struct GNUNET_PeerIdentity *other)
66 {
67   if (me != ccc->p[0])
68     return;
69   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
70                                            me,
71                                            other);
72   if (th != NULL)
73   {
74     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
75     th = NULL;
76   }
77   if (shutdown_ == GNUNET_YES)
78   {
79     ccc->global_ret = GNUNET_OK;
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81                 "Test good, shutting down...\n");
82     GNUNET_SCHEDULER_shutdown ();
83   }
84 }
85
86
87 static void
88 stop_peer (void *cls)
89 {
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91               "Shutting down peer %u (`%s')\n",
92               ccc->p[1]->no,
93               GNUNET_i2s (&ccc->p[1]->id));
94   shutdown_ = GNUNET_YES;
95   GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[1]);
96   ccc->p[1] = NULL;
97 }
98
99
100 static void
101 notify_receive (void *cls,
102                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
103                 const struct GNUNET_PeerIdentity *sender,
104                 const struct GNUNET_MessageHeader *message)
105 {
106   {
107     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
108
109     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110                 "Peer %u (`%4s') received message of type %d and size %u size from peer %s!\n",
111                 receiver->no,
112                 ps,
113                 ntohs (message->type),
114                 ntohs (message->size),
115                 GNUNET_i2s (sender));
116     GNUNET_free (ps);
117   }
118   if ((MTYPE == ntohs (message->type)) &&
119       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
120   {
121     GNUNET_SCHEDULER_add_now (&stop_peer,
122                               NULL);
123     return;
124   }
125 }
126
127
128 static size_t
129 notify_ready (void *cls,
130               size_t size,
131               void *buf)
132 {
133   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
134   struct GNUNET_MessageHeader *hdr;
135
136   th = NULL;
137
138   if (buf == NULL)
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141                 "Transmission error occurred in transmit_ready\n");
142     GNUNET_SCHEDULER_shutdown ();
143     return 0;
144   }
145
146   GNUNET_assert (size >= 256);
147
148   if (NULL != buf)
149   {
150     hdr = buf;
151     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
152     hdr->type = htons (MTYPE);
153   }
154   {
155     char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
156
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
159                 ccc->p[1]->no,
160                 ps,
161                 ntohs (hdr->type),
162                 ntohs (hdr->size),
163                 p->no,
164                 GNUNET_i2s (&p->id));
165     GNUNET_free (ps);
166   }
167   return sizeof (struct GNUNET_MessageHeader);
168 }
169
170
171 static void
172 sendtask (void *cls)
173 {
174   ccc->global_ret = GNUNET_SYSERR;
175   {
176     char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
177
178     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179                 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
180                 ccc->p[1]->no,
181                 GNUNET_i2s (&ccc->p[1]->id),
182                 ccc->p[0]->no,
183                 receiver_s);
184     GNUNET_free (receiver_s);
185   }
186   th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
187                                                &ccc->p[0]->id, 256,
188                                                TIMEOUT_TRANSMIT,
189                                                &notify_ready,
190                                                ccc->p[0]);
191 }
192
193
194 int
195 main (int argc, char *argv[])
196 {
197   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
198     .connect_continuation = &sendtask,
199     .config_file = "test_transport_api_data.conf",
200     .rec = &notify_receive,
201     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
202     .nd = &notify_disconnect,
203     .shutdown_task = &custom_shutdown,
204     .timeout = TIMEOUT
205   };
206   ccc = &my_ccc;
207
208   if (GNUNET_OK !=
209       GNUNET_TRANSPORT_TESTING_main (2,
210                                      &GNUNET_TRANSPORT_TESTING_connect_check,
211                                      ccc))
212     return 1;
213   return 0;
214 }
215
216 /* end of test_transport_api_disconnect.c */