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