cleaning up set handlers, eliminating 2nd level demultiplexing and improving use...
[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  * @author Christian Grothoff
24  *
25  * This test case serves as a base for tcp, udp, and udp-nat
26  * transport test cases.  Based on the executable being run
27  * the correct test case will be performed.  Conservation of
28  * C code apparently.
29  */
30 #include "platform.h"
31 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
33
34 /**
35  * How long until we give up on transmitting the message?
36  */
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38
39 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
40
41
42 static void
43 notify_receive (void *cls,
44                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
45                 const struct GNUNET_PeerIdentity *sender,
46                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
47 {
48   {
49     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
50
51     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
52                 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
53                 receiver->no,
54                 ps,
55                 ntohs (message->header.type),
56                 ntohs (message->header.size),
57                 GNUNET_i2s (sender));
58     GNUNET_free (ps);
59   }
60
61   if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
62       (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE == ntohs (message->header.size)))
63   {
64     ccc->global_ret = GNUNET_OK;
65     GNUNET_SCHEDULER_shutdown ();
66   }
67   else
68   {
69     GNUNET_break (0);
70     ccc->global_ret = GNUNET_SYSERR;
71     GNUNET_SCHEDULER_shutdown ();
72   }
73 }
74
75
76 /**
77  * Runs the test.
78  *
79  * @param argv the argv argument from main()
80  * @param bi_directional should we try to establish connections
81  *        in both directions simultaneously?
82  */
83 static int
84 test (char *argv[],
85       int bi_directional)
86 {
87   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
88     .num_messages = 1
89   };
90   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
91     .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
92     .connect_continuation_cls = &sc,
93     .config_file = "test_transport_api_data.conf",
94     .rec = &notify_receive,
95     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
96     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
97     .timeout = TIMEOUT,
98     .bi_directional = bi_directional
99   };
100
101   ccc = &my_ccc;
102   sc.ccc = ccc;
103   if (GNUNET_OK !=
104       GNUNET_TRANSPORT_TESTING_main (2,
105                                      &GNUNET_TRANSPORT_TESTING_connect_check,
106                                      ccc))
107     return 1;
108   return 0;
109 }
110
111
112 int
113 main (int argc,
114       char *argv[])
115 {
116   if ( (0 != test (argv,
117                    GNUNET_NO)) ||
118        (0 != test (argv,
119                    GNUNET_YES) ) )
120     return 1;
121   return 0;
122 }
123
124 /* end of test_transport_api.c */