2 * This file is part of the Nice GLib ICE library.
4 * (C) 2018 Jakub Adam <jakub.adam@ktknet.cz>
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Nice GLib ICE library.
18 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
19 * Corporation. All Rights Reserved.
21 * Alternatively, the contents of this file may be used under the terms of the
22 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
23 * case the provisions of LGPL are applicable instead of those above. If you
24 * wish to allow use of your version of this file only under the terms of the
25 * LGPL and not to allow others to use your version of this file under the
26 * MPL, indicate your decision by deleting the provisions above and replace
27 * them with the notice and other provisions required by the LGPL. If you do
28 * not delete the provisions above, a recipient may use your version of this
29 * file under either the MPL or the LGPL.
35 #include <gio/gnetworking.h>
37 #include "agent-priv.h"
43 generate_test_messages(void)
46 GSList *result = NULL;
48 for (i = 0; i != 100; ++i) {
49 GInputVector *msg_data = g_new (GInputVector, 1);
50 gsize msg_size = g_rand_int_range (randg, 0, G_MAXUINT16);
53 msg_data->size = msg_size + sizeof (guint16);
54 msg_data->buffer = g_malloc (msg_data->size);
55 *(guint16 *)(msg_data->buffer) = htons (msg_size);
57 for (j = 2; j != msg_data->size; ++j) {
58 ((guint8 *)msg_data->buffer)[j] = g_rand_int(randg);
61 result = g_slist_append(result, msg_data);
71 guint8 send_buffer[G_MAXUINT16 + sizeof (guint16)];
75 test_socket_recv_messages (NiceSocket *sock, NiceInputMessage *recv_messages,
76 guint n_recv_messages) {
77 TestSocketPriv *priv = sock->priv;
80 for (i = 0; priv->current_msg && i != n_recv_messages; ++i) {
81 gsize msg_size = g_rand_int_range (randg, 0, G_MAXUINT16) + sizeof (guint16);
85 while (priv->current_msg && j < msg_size) {
86 GInputVector *msg = priv->current_msg->data;
87 gsize cpylen = MIN (msg->size - priv->offset, msg_size - j);
88 memcpy (priv->send_buffer + j, (guint8 *)msg->buffer + priv->offset,
90 priv->offset += cpylen;
93 if (priv->offset == msg->size) {
94 priv->current_msg = priv->current_msg->next;
100 *(guint16 *)(priv->send_buffer) = htons (msg_size - sizeof (guint16));
102 memcpy_buffer_to_input_message (&recv_messages[i], priv->send_buffer, msg_size);
103 nice_address_set_from_string (recv_messages[i].from, "127.0.0.1");
110 test_socket_is_reliable (NiceSocket *sock) {
115 test_socket_close (NiceSocket *sock) {
120 test_socket_new (GSList *msg_data)
122 NiceSocket *sock = g_slice_new0 (NiceSocket);
123 TestSocketPriv *priv = g_new0 (TestSocketPriv, 1);
124 priv->msg_data = msg_data;
125 priv->current_msg = msg_data;
128 sock->type = NICE_SOCKET_TYPE_UDP_TURN_OVER_TCP;
129 sock->recv_messages = test_socket_recv_messages;
130 sock->is_reliable = test_socket_is_reliable;
131 sock->close = test_socket_close;
132 sock->priv = (void *) priv;
137 #define N_RECV_MESSAGES 7
140 tcp_turn_fragmentation (void)
142 /* Generate some RFC4571-framed test messages. A dummy base socket will split
143 * them randomly into TCP-TURN messages. Test that tcp-turn socket can
144 * correctly extract and reassemble the original test data out of the TURN
146 GSList *test_messages = generate_test_messages ();
148 NiceSocket *turnsock;
149 NiceSocket *testsock;
151 NiceInputMessage recv_messages[N_RECV_MESSAGES];
152 GInputVector recv_vectors[N_RECV_MESSAGES];
153 NiceAddress recv_addr[N_RECV_MESSAGES];
154 guint8 recv_buffers[N_RECV_MESSAGES][G_MAXUINT16 + sizeof (guint16)];
160 for (i = 0; i != N_RECV_MESSAGES; ++i) {
161 recv_messages[i].buffers = &recv_vectors[i];
162 recv_messages[i].from = &recv_addr[i];
163 recv_messages[i].n_buffers = 1;
164 recv_messages[i].length = 0;
165 recv_vectors[i].buffer = &recv_buffers[i];
166 recv_vectors[i].size = sizeof (recv_buffers[i]);
169 nice_address_set_from_string (&addr, "127.0.0.1");
171 testsock = test_socket_new (test_messages);
173 turnsock = nice_udp_turn_socket_new (NULL, &addr,
174 testsock, &addr, "", "",
175 NICE_TURN_SOCKET_COMPATIBILITY_OC2007);
179 n_messages = nice_socket_recv_messages (turnsock, recv_messages,
182 for (i = 0; i != (guint)n_messages; ++i) {
183 NiceInputMessage *message = &recv_messages[i];
184 GInputVector *vec = li->data;
185 if (message->length == 0) {
188 g_assert_cmpint (message->length, ==, vec->size);
189 g_assert_cmpmem (message->buffers->buffer, message->length,
190 vec->buffer, message->length);
196 for (li = test_messages; li; li = li->next) {
197 GInputVector *v = li->data;
201 g_slist_free (test_messages);
203 nice_socket_free (turnsock);
204 nice_socket_free (testsock);
208 main (int argc, char *argv[])
212 g_networking_init ();
214 randg = g_rand_new();
215 g_test_init (&argc, &argv, NULL);
217 mainloop = g_main_loop_new (NULL, TRUE);
219 g_test_add_func ("/udp-turn/tcp-fragmentation", tcp_turn_fragmentation);
225 g_main_loop_unref (mainloop);