fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_api_limited_sockets.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 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_limited_sockets.c
20  * @brief base test case for transport implementations
21  *
22  * This test case serves as a base for tcp, udp, and udp-nat
23  * transport test cases.  Based on the executable being run
24  * the correct test case will be performed.  Conservation of
25  * C code apparently.
26  */
27 #include "platform.h"
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
30
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36 #define MAX_FILES 50
37
38
39 #if HAVE_SETRLIMIT
40
41 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
42
43
44 static void
45 notify_receive (void *cls, 
46                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
47                 const struct GNUNET_PeerIdentity *sender,
48                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
49 {
50   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
51               "Received message of type %d from peer %s!\n",
52               ntohs (message->header.type),
53               GNUNET_i2s (sender));
54   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE ==
55         ntohs (message->header.type)) &&
56        (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) ==
57         ntohs (message->header.size)) )
58   {
59     ccc->global_ret = GNUNET_OK;
60   }
61   else
62   {
63     GNUNET_break (0);
64   }
65   GNUNET_SCHEDULER_shutdown ();
66 }
67
68
69 int
70 main (int argc, char *argv[])
71 {
72   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
73     .num_messages = 1
74   };
75   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
76     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
77     .connect_continuation_cls = &sc,
78     .config_file = "test_transport_api_data.conf",
79     .rec = &notify_receive,
80     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
81     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
82     .timeout = TIMEOUT,
83     .global_ret = GNUNET_SYSERR
84   };
85   struct rlimit r_file_old;
86   struct rlimit r_file_new;
87   int res;
88
89   sc.ccc = &my_ccc;
90   res = getrlimit (RLIMIT_NOFILE,
91                    &r_file_old);
92   r_file_new.rlim_cur = MAX_FILES;
93   r_file_new.rlim_max = r_file_old.rlim_max;
94   res = setrlimit (RLIMIT_NOFILE,
95                    &r_file_new);
96   if (0 != res)
97   {
98     fprintf (stderr,
99              "Setting limit failed: %s\n",
100              strerror (errno));
101     return 77;
102   }
103
104   ccc = &my_ccc;
105   ccc->global_ret = GNUNET_SYSERR;
106   if (GNUNET_OK !=
107       GNUNET_TRANSPORT_TESTING_main (2,
108                                      &GNUNET_TRANSPORT_TESTING_connect_check,
109                                      ccc))
110     return 1;
111   return 0;
112 }
113
114 #else
115 /* cannot setrlimit */
116
117
118 int
119 main (int argc, char *argv[])
120 {
121   fprintf (stderr,
122            "Cannot run test on this system\n");
123   return 77;
124 }
125
126 #endif
127
128 /* end of test_transport_api_limited_sockets.c */