diff options
Diffstat (limited to 'src/util/test_connection.c')
-rw-r--r-- | src/util/test_connection.c | 167 |
1 files changed, 0 insertions, 167 deletions
diff --git a/src/util/test_connection.c b/src/util/test_connection.c deleted file mode 100644 index eaca75c2e..000000000 --- a/src/util/test_connection.c +++ /dev/null | |||
@@ -1,167 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009 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 util/test_connection.c | ||
22 | * @brief tests for connection.c | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | |||
27 | #define PORT 12435 | ||
28 | |||
29 | |||
30 | static struct GNUNET_CONNECTION_Handle *csock; | ||
31 | |||
32 | static struct GNUNET_CONNECTION_Handle *asock; | ||
33 | |||
34 | static struct GNUNET_CONNECTION_Handle *lsock; | ||
35 | |||
36 | static size_t sofar; | ||
37 | |||
38 | static struct GNUNET_NETWORK_Handle *ls; | ||
39 | |||
40 | static struct GNUNET_CONFIGURATION_Handle *cfg; | ||
41 | |||
42 | /** | ||
43 | * Create and initialize a listen socket for the server. | ||
44 | * | ||
45 | * @return -1 on error, otherwise the listen socket | ||
46 | */ | ||
47 | static struct GNUNET_NETWORK_Handle * | ||
48 | open_listen_socket () | ||
49 | { | ||
50 | const static int on = 1; | ||
51 | struct sockaddr_in sa; | ||
52 | struct GNUNET_NETWORK_Handle *desc; | ||
53 | |||
54 | memset (&sa, 0, sizeof (sa)); | ||
55 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
56 | sa.sin_len = sizeof (sa); | ||
57 | #endif | ||
58 | sa.sin_port = htons (PORT); | ||
59 | sa.sin_family = AF_INET; | ||
60 | desc = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); | ||
61 | GNUNET_assert (desc != NULL); | ||
62 | if (GNUNET_NETWORK_socket_setsockopt | ||
63 | (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) | ||
64 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); | ||
65 | GNUNET_assert (GNUNET_OK == | ||
66 | GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, | ||
67 | sizeof (sa))); | ||
68 | GNUNET_NETWORK_socket_listen (desc, 5); | ||
69 | return desc; | ||
70 | } | ||
71 | |||
72 | static void | ||
73 | receive_check (void *cls, const void *buf, size_t available, | ||
74 | const struct sockaddr *addr, socklen_t addrlen, int errCode) | ||
75 | { | ||
76 | int *ok = cls; | ||
77 | |||
78 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n"); | ||
79 | GNUNET_assert (buf != NULL); /* no timeout */ | ||
80 | if (0 == memcmp (&"Hello World"[sofar], buf, available)) | ||
81 | sofar += available; | ||
82 | if (sofar < 12) | ||
83 | { | ||
84 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); | ||
85 | GNUNET_CONNECTION_receive (asock, 1024, | ||
86 | GNUNET_TIME_relative_multiply | ||
87 | (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, | ||
88 | cls); | ||
89 | } | ||
90 | else | ||
91 | { | ||
92 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n"); | ||
93 | *ok = 0; | ||
94 | GNUNET_CONNECTION_destroy (asock); | ||
95 | GNUNET_CONNECTION_destroy (csock); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | run_accept (void *cls) | ||
102 | { | ||
103 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test accepts connection\n"); | ||
104 | asock = GNUNET_CONNECTION_create_from_accept (NULL, NULL, ls); | ||
105 | GNUNET_assert (asock != NULL); | ||
106 | GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); | ||
107 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys listen socket\n"); | ||
108 | GNUNET_CONNECTION_destroy (lsock); | ||
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
110 | "Test asks to receive on accepted socket\n"); | ||
111 | GNUNET_CONNECTION_receive (asock, 1024, | ||
112 | GNUNET_TIME_relative_multiply | ||
113 | (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, | ||
114 | cls); | ||
115 | } | ||
116 | |||
117 | |||
118 | static size_t | ||
119 | make_hello (void *cls, size_t size, void *buf) | ||
120 | { | ||
121 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
122 | "Test prepares to transmit on connect socket\n"); | ||
123 | GNUNET_assert (size >= 12); | ||
124 | strcpy ((char *) buf, "Hello World"); | ||
125 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys client socket\n"); | ||
126 | return 12; | ||
127 | } | ||
128 | |||
129 | |||
130 | static void | ||
131 | task (void *cls) | ||
132 | { | ||
133 | ls = open_listen_socket (); | ||
134 | lsock = GNUNET_CONNECTION_create_from_existing (ls); | ||
135 | GNUNET_assert (lsock != NULL); | ||
136 | csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); | ||
137 | GNUNET_assert (csock != NULL); | ||
138 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n"); | ||
139 | GNUNET_assert (NULL != | ||
140 | GNUNET_CONNECTION_notify_transmit_ready (csock, 12, | ||
141 | GNUNET_TIME_UNIT_SECONDS, | ||
142 | &make_hello, NULL)); | ||
143 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); | ||
144 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept, | ||
145 | cls); | ||
146 | } | ||
147 | |||
148 | |||
149 | int | ||
150 | main (int argc, char *argv[]) | ||
151 | { | ||
152 | int ok; | ||
153 | |||
154 | GNUNET_log_setup ("test_connection", | ||
155 | "WARNING", | ||
156 | NULL); | ||
157 | |||
158 | ok = 1; | ||
159 | cfg = GNUNET_CONFIGURATION_create (); | ||
160 | GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", | ||
161 | "localhost"); | ||
162 | GNUNET_SCHEDULER_run (&task, &ok); | ||
163 | GNUNET_CONFIGURATION_destroy (cfg); | ||
164 | return ok; | ||
165 | } | ||
166 | |||
167 | /* end of test_connection.c */ | ||