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