-towards nicer transport-testing lib
[oweals/gnunet.git] / src / transport / test_transport_api.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.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves as a base for tcp, udp, and udp-nat
25  * transport test cases.  Based on the executable being run
26  * the correct test case will be performed.  Conservation of
27  * C code apparently.
28  */
29 #include "platform.h"
30 #include "gnunet_transport_service.h"
31 #include "transport-testing.h"
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43 #define TEST_MESSAGE_SIZE 2600
44
45 #define TEST_MESSAGE_TYPE 12345
46
47
48 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
49
50 static struct GNUNET_TRANSPORT_TransmitHandle *th;
51
52
53 static void
54 custom_shutdown (void *cls)
55 {
56   if (NULL != th)
57   {
58     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
59     th = NULL;
60   }
61   else
62     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63                 "Peers were not ready to send data\n");
64 }
65
66
67 static void
68 notify_receive (void *cls,
69                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
70                 const struct GNUNET_PeerIdentity *sender,
71                 const struct GNUNET_MessageHeader *message)
72 {
73   {
74     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
75
76     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
77                 "Peer %u (`%4s') received message of type %d and size %u size from peer %s!\n",
78                 receiver->no,
79                 ps,
80                 ntohs (message->type),
81                 ntohs (message->size),
82                 GNUNET_i2s (sender));
83     GNUNET_free (ps);
84   }
85
86   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
87       (TEST_MESSAGE_SIZE == ntohs (message->size)))
88   {
89     ccc->global_ret = GNUNET_OK;
90     GNUNET_SCHEDULER_shutdown ();
91   }
92   else
93   {
94     GNUNET_break (0);
95     ccc->global_ret = GNUNET_SYSERR;
96     GNUNET_SCHEDULER_shutdown ();
97   }
98 }
99
100
101 static size_t
102 notify_ready (void *cls,
103               size_t size,
104               void *buf)
105 {
106   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
107   struct GNUNET_MessageHeader *hdr;
108
109   th = NULL;
110   if (buf == NULL)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113                 "Timeout occurred while waiting for transmit_ready\n");
114     GNUNET_SCHEDULER_shutdown ();
115     ccc->global_ret = 42;
116     return 0;
117   }
118
119   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
120   if (NULL != buf)
121   {
122     memset (buf, '\0', TEST_MESSAGE_SIZE);
123     hdr = buf;
124     hdr->size = htons (TEST_MESSAGE_SIZE);
125     hdr->type = htons (TEST_MESSAGE_TYPE);
126   }
127
128   {
129     char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
130     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
132                 ccc->p[1]->no,
133                 ps,
134                 ntohs (hdr->type),
135                 ntohs (hdr->size),
136                 p->no,
137                 GNUNET_i2s (&p->id));
138     GNUNET_free (ps);
139   }
140   return TEST_MESSAGE_SIZE;
141 }
142
143
144 static void
145 sendtask (void *cls)
146 {
147   {
148     char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
149
150     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
151                 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
152                 ccc->p[1]->no,
153                 GNUNET_i2s (&ccc->p[1]->id),
154                 ccc->p[0]->no,
155                 receiver_s);
156     GNUNET_free (receiver_s);
157   }
158   ccc->global_ret = GNUNET_SYSERR;
159   th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
160                                                &ccc->p[0]->id,
161                                                TEST_MESSAGE_SIZE,
162                                                TIMEOUT_TRANSMIT,
163                                                &notify_ready,
164                                                ccc->p[0]);
165 }
166
167
168 static void
169 notify_disconnect (void *cls,
170                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
171                    const struct GNUNET_PeerIdentity *other)
172 {
173   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
174                                            me,
175                                            other);
176   if (NULL != th)
177   {
178     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
179     th = NULL;
180   }
181 }
182
183
184 int
185 main (int argc,
186       char *argv[])
187 {
188   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
189     .connect_continuation = &sendtask,
190     .config_file = "test_transport_api_data.conf",
191     .rec = &notify_receive,
192     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
193     .nd = &notify_disconnect,
194     .shutdown_task = &custom_shutdown,
195     .timeout = TIMEOUT
196   };
197
198   ccc = &my_ccc;
199   if (GNUNET_OK !=
200       GNUNET_TRANSPORT_TESTING_main (2,
201                                      &GNUNET_TRANSPORT_TESTING_connect_check,
202                                      ccc))
203     return 1;
204   return 0;
205 }
206
207 /* end of test_transport_api.c */