glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_send_tcp.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_api_manipulation_send_tcp.c
17  * @brief base test case for transport traffic manipulation implementation
18  *
19  * This test case will setup 2 peers and connect them, the first message
20  * will be sent without manipulation, then a send delay of 1 second will
21  * be configured and 1 more message will be sent. Time will be measured.
22  *
23  * In addition the distance on receiver side will be manipulated to be 10
24  */
25 #include "platform.h"
26 #include "gnunet_transport_service.h"
27 #include "transport-testing.h"
28
29 /**
30  * How long until we give up on transmitting the message?
31  */
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
33
34 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
35
36 static int messages_recv;
37
38 static struct GNUNET_TIME_Absolute start_normal;
39
40 static struct GNUNET_TIME_Relative dur_normal;
41
42 static struct GNUNET_TIME_Absolute start_delayed;
43
44 static struct GNUNET_TIME_Relative dur_delayed;
45
46
47 static void
48 do_free (void *cls)
49 {
50   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
51   
52   GNUNET_free (sc);
53 }
54
55
56 static void
57 delayed_transmit (void *cls)
58 {
59   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
60   
61   start_delayed = GNUNET_TIME_absolute_get ();
62   GNUNET_TRANSPORT_TESTING_large_send (sc);
63 }
64
65
66 static void
67 sendtask (void *cls)
68 {
69   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc;
70   struct GNUNET_TIME_Relative delay;
71   struct GNUNET_ATS_Properties prop;
72
73   sc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_SendClosure);
74   sc->num_messages = 1;
75   sc->ccc = ccc;
76   sc->cont = &do_free;
77   sc->cont_cls = sc;
78   if (0 == messages_recv)
79   {
80     start_normal = GNUNET_TIME_absolute_get ();
81   }
82   if (1 == messages_recv)
83   {
84     memset (&prop,
85             0,
86             sizeof (prop));
87     delay = GNUNET_TIME_UNIT_SECONDS;
88     GNUNET_TRANSPORT_manipulation_set (ccc->p[0]->tmh,
89                                        &ccc->p[1]->id,
90                                        &prop,
91                                        GNUNET_TIME_UNIT_ZERO,
92                                        delay);
93     /* wait 1s to allow manipulation to go into effect */
94     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
95                                   &delayed_transmit,
96                                   sc);
97     return;
98   }
99   GNUNET_TRANSPORT_TESTING_large_send (sc);
100 }
101
102
103 static void
104 notify_receive (void *cls,
105                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
106                 const struct GNUNET_PeerIdentity *sender,
107                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
108 {
109   {
110     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
111
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
114                 receiver->no,
115                 ps,
116                 ntohs (message->header.type),
117                 ntohs (message->header.size),
118                 GNUNET_i2s (sender));
119     GNUNET_free (ps);
120   }
121
122   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
123        (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (message->header.size)) )
124   {
125     GNUNET_break (0);
126     ccc->global_ret = GNUNET_SYSERR;
127     GNUNET_SCHEDULER_shutdown ();
128     return;
129   }
130
131   if (0 == messages_recv)
132   {
133     /* Received non-delayed message */
134     dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
135     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136                 "Received non-delayed message %u after %s\n",
137                 messages_recv,
138                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
139                                                         GNUNET_YES));
140     GNUNET_SCHEDULER_add_now (&sendtask,
141                               NULL);
142     messages_recv++;
143     return;
144   }
145   /* Received manipulated message */
146   dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148               "Received delayed message %u after %s\n",
149               messages_recv,
150               GNUNET_STRINGS_relative_time_to_string (dur_delayed,
151                                                       GNUNET_YES));
152   if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
153   {
154     GNUNET_break (0);
155     ccc->global_ret = GNUNET_SYSERR;
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157                 "Delayed message was not delayed correctly: took only %s\n",
158                 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
159                                                         GNUNET_YES));
160   }
161   else
162   {
163     ccc->global_ret = GNUNET_OK;
164   }
165   GNUNET_SCHEDULER_shutdown ();
166 }
167
168
169 int
170 main (int argc,
171       char *argv[])
172 {
173   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
174     .connect_continuation = &sendtask,
175     .config_file = "test_transport_api_data.conf",
176     .rec = &notify_receive,
177     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
178     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
179     .timeout = TIMEOUT,
180     .global_ret = GNUNET_NO
181   };
182
183   ccc = &my_ccc;
184   if (GNUNET_OK !=
185       GNUNET_TRANSPORT_TESTING_main (2,
186                                      &GNUNET_TRANSPORT_TESTING_connect_check,
187                                      ccc))
188     return 1;
189   return 0;
190 }
191
192 /* end of test_transport_api_manipulation_send_tcp.c */