tolerate additional IPv4 address now available for gnunet.org
[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      SPDX-License-Identifier: AGPL3.0-or-later
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 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
39
40 static int shutdown_;
41
42
43 static void
44 notify_disconnect (void *cls,
45                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
46                    const struct GNUNET_PeerIdentity *other)
47 {
48   if (me != ccc->p[0])
49     return;
50   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
51                                            me,
52                                            other);
53   if (GNUNET_YES == shutdown_)
54   {
55     ccc->global_ret = GNUNET_OK;
56     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57                 "Test good, shutting down...\n");
58     GNUNET_SCHEDULER_shutdown ();
59   }
60 }
61
62
63 static void
64 stop_peer (void *cls)
65 {
66   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67               "Shutting down peer %u (`%s')\n",
68               ccc->p[1]->no,
69               GNUNET_i2s (&ccc->p[1]->id));
70   shutdown_ = GNUNET_YES;
71   GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[1]);
72   ccc->p[1] = NULL;
73 }
74
75
76 static void
77 notify_receive (void *cls,
78                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
79                 const struct GNUNET_PeerIdentity *sender,
80                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
81 {
82   {
83     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
84
85     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86                 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
87                 receiver->no,
88                 ps,
89                 ntohs (message->header.type),
90                 ntohs (message->header.size),
91                 GNUNET_i2s (sender));
92     GNUNET_free (ps);
93   }
94   if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
95       (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->header.size)))
96   {
97     GNUNET_SCHEDULER_add_now (&stop_peer,
98                               NULL);
99     return;
100   }
101 }
102
103
104 int
105 main (int argc,
106       char *argv[])
107 {
108   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
109     .num_messages = 1
110   };
111   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
112     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
113     .connect_continuation_cls = &sc,
114     .config_file = "test_transport_api_data.conf",
115     .rec = &notify_receive,
116     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
117     .nd = &notify_disconnect,
118     .timeout = TIMEOUT,
119     .global_ret = GNUNET_SYSERR
120   };
121   
122   ccc = &my_ccc;
123   sc.ccc = ccc;
124   if (GNUNET_OK !=
125       GNUNET_TRANSPORT_TESTING_main (2,
126                                      &GNUNET_TRANSPORT_TESTING_connect_check,
127                                      ccc))
128     return 1;
129   return 0;
130 }
131
132 /* end of test_transport_api_disconnect.c */