diff options
Diffstat (limited to 'src/util/test_network_receive_cancel.c')
-rw-r--r-- | src/util/test_network_receive_cancel.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/util/test_network_receive_cancel.c b/src/util/test_network_receive_cancel.c new file mode 100644 index 000000000..e22e24d8c --- /dev/null +++ b/src/util/test_network_receive_cancel.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
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 2, 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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/test_network_receive_cancel.c | ||
22 | * @brief tests for network.c | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_common.h" | ||
26 | #include "gnunet_network_lib.h" | ||
27 | #include "gnunet_scheduler_lib.h" | ||
28 | #include "gnunet_time_lib.h" | ||
29 | |||
30 | #define VERBOSE GNUNET_NO | ||
31 | |||
32 | #define PORT 12435 | ||
33 | |||
34 | |||
35 | static struct GNUNET_NETWORK_SocketHandle *csock; | ||
36 | |||
37 | static struct GNUNET_NETWORK_SocketHandle *asock; | ||
38 | |||
39 | static struct GNUNET_NETWORK_SocketHandle *lsock; | ||
40 | |||
41 | static int ls; | ||
42 | |||
43 | static GNUNET_SCHEDULER_TaskIdentifier receive_task; | ||
44 | |||
45 | |||
46 | |||
47 | |||
48 | /** | ||
49 | * Create and initialize a listen socket for the server. | ||
50 | * | ||
51 | * @return -1 on error, otherwise the listen socket | ||
52 | */ | ||
53 | static int | ||
54 | open_listen_socket () | ||
55 | { | ||
56 | const static int on = 1; | ||
57 | struct sockaddr_in sa; | ||
58 | int fd; | ||
59 | |||
60 | memset (&sa, 0, sizeof (sa)); | ||
61 | sa.sin_port = htons (PORT); | ||
62 | fd = SOCKET (AF_INET, SOCK_STREAM, 0); | ||
63 | GNUNET_assert (fd >= 0); | ||
64 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | ||
65 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
66 | "setsockopt"); | ||
67 | GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0); | ||
68 | LISTEN (fd, 5); | ||
69 | return fd; | ||
70 | } | ||
71 | |||
72 | |||
73 | |||
74 | static void | ||
75 | dead_receive (void *cls, | ||
76 | const void *buf, | ||
77 | size_t available, | ||
78 | const struct sockaddr *addr, socklen_t addrlen, int errCode) | ||
79 | { | ||
80 | GNUNET_assert (0); | ||
81 | } | ||
82 | |||
83 | |||
84 | static void | ||
85 | run_accept_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
86 | { | ||
87 | |||
88 | asock = GNUNET_NETWORK_socket_create_from_accept (tc->sched, | ||
89 | NULL, NULL, ls, 1024); | ||
90 | GNUNET_assert (asock != NULL); | ||
91 | GNUNET_assert (GNUNET_YES == GNUNET_NETWORK_socket_check (asock)); | ||
92 | GNUNET_NETWORK_socket_destroy (lsock); | ||
93 | receive_task | ||
94 | = GNUNET_NETWORK_receive (asock, | ||
95 | 1024, | ||
96 | GNUNET_TIME_relative_multiply | ||
97 | (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, | ||
98 | cls); | ||
99 | } | ||
100 | |||
101 | |||
102 | static void | ||
103 | receive_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
104 | { | ||
105 | int *ok = cls; | ||
106 | GNUNET_NETWORK_receive_cancel (asock, receive_task); | ||
107 | GNUNET_NETWORK_socket_destroy (csock); | ||
108 | GNUNET_NETWORK_socket_destroy (asock); | ||
109 | *ok = 0; | ||
110 | } | ||
111 | |||
112 | |||
113 | |||
114 | static void | ||
115 | task_receive_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
116 | { | ||
117 | ls = open_listen_socket (); | ||
118 | lsock = GNUNET_NETWORK_socket_create_from_existing (tc->sched, ls, 0); | ||
119 | GNUNET_assert (lsock != NULL); | ||
120 | csock = GNUNET_NETWORK_socket_create_from_connect (tc->sched, | ||
121 | "localhost", PORT, 1024); | ||
122 | GNUNET_assert (csock != NULL); | ||
123 | GNUNET_SCHEDULER_add_read (tc->sched, | ||
124 | GNUNET_NO, | ||
125 | GNUNET_SCHEDULER_PRIORITY_HIGH, | ||
126 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
127 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
128 | ls, &run_accept_cancel, cls); | ||
129 | GNUNET_SCHEDULER_add_delayed (tc->sched, | ||
130 | GNUNET_NO, | ||
131 | GNUNET_SCHEDULER_PRIORITY_KEEP, | ||
132 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
133 | GNUNET_TIME_UNIT_SECONDS, | ||
134 | &receive_cancel_task, cls); | ||
135 | } | ||
136 | |||
137 | |||
138 | |||
139 | /** | ||
140 | * Main method, starts scheduler with task_timeout. | ||
141 | */ | ||
142 | static int | ||
143 | check_receive_cancel () | ||
144 | { | ||
145 | int ok; | ||
146 | |||
147 | ok = 1; | ||
148 | GNUNET_SCHEDULER_run (&task_receive_cancel, &ok); | ||
149 | return ok; | ||
150 | } | ||
151 | |||
152 | |||
153 | int | ||
154 | main (int argc, char *argv[]) | ||
155 | { | ||
156 | int ret = 0; | ||
157 | |||
158 | GNUNET_log_setup ("test_network_receive_cancel", "WARNING", NULL); | ||
159 | ret += check_receive_cancel (); | ||
160 | |||
161 | return ret; | ||
162 | } | ||
163 | |||
164 | /* end of test_network.c */ | ||