Imported Upstream version 0.1.17
[platform/upstream/libnice.git] / tests / test-io-stream-thread.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2013 Collabora Ltd.
5  *  Contact: Philip Withnall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is the Nice GLib ICE library.
18  *
19  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20  * Corporation. All Rights Reserved.
21  *
22  * Contributors:
23  *   Philip Withnall, Collabora Ltd.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27  * case the provisions of LGPL are applicable instead of those above. If you
28  * wish to allow use of your version of this file only under the terms of the
29  * LGPL and not to allow others to use your version of this file under the
30  * MPL, indicate your decision by deleting the provisions above and replace
31  * them with the notice and other provisions required by the LGPL. If you do
32  * not delete the provisions above, a recipient may use your version of this
33  * file under either the MPL or the LGPL.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include "agent.h"
40 #include "test-io-stream-common.h"
41
42 #include <stdlib.h>
43 #include <string.h>
44 #ifndef G_OS_WIN32
45 #include <unistd.h>
46 #endif
47
48 typedef struct {
49   guint cand_count;
50   guint *other_cand_count;
51
52   gsize recv_count;
53   gsize *other_recv_count;
54
55   gsize send_count;
56   gsize *other_send_count;
57 } ThreadData;
58
59 static void
60 read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
61 {
62   ThreadData *user_data = data->user_data;
63
64   for (user_data->recv_count = 0;
65        user_data->recv_count < 10;
66        user_data->recv_count++) {
67     guint8 expected_data[MESSAGE_SIZE];
68     GError *error = NULL;
69     guint8 buf[MESSAGE_SIZE];
70     gssize len;
71
72     /* Block on receiving some data. */
73     len = g_input_stream_read (input_stream, buf, sizeof (buf), NULL, &error);
74     g_assert_no_error (error);
75     g_assert_cmpint (len, ==, sizeof (buf));
76
77     memset (expected_data, user_data->recv_count + '1', sizeof (expected_data));
78     g_assert_cmpmem (buf, sizeof (expected_data), expected_data,
79         sizeof (expected_data));
80   }
81
82   check_for_termination (data, &user_data->recv_count,
83       user_data->other_recv_count, &user_data->send_count, 10);
84 }
85
86 static void
87 new_selected_pair_cb (NiceAgent *agent, guint stream_id, guint component_id,
88     gchar *lfoundation, gchar *rfoundation, TestIOStreamThreadData *data)
89 {
90   ThreadData *user_data = data->user_data;
91
92   g_atomic_int_inc (&user_data->cand_count);
93 }
94
95 static void
96 write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
97 {
98   ThreadData *user_data = data->user_data;
99   guint8 buf[MESSAGE_SIZE];
100
101   for (user_data->send_count = 0;
102        user_data->send_count < 10;
103        user_data->send_count++) {
104     GError *error = NULL;
105
106     memset (buf, user_data->send_count + '1', MESSAGE_SIZE);
107
108     g_output_stream_write (output_stream, buf, sizeof (buf), NULL, &error);
109     g_assert_no_error (error);
110   }
111 }
112
113 int main (void)
114 {
115   ThreadData *l_data, *r_data;
116
117   const TestIOStreamCallbacks callbacks = {
118     read_thread_cb,
119     write_thread_cb,
120     NULL,
121     new_selected_pair_cb,
122   };
123
124 #ifdef G_OS_WIN32
125   WSADATA w;
126   WSAStartup (0x0202, &w);
127 #endif
128
129   l_data = g_malloc0 (sizeof (ThreadData));
130   r_data = g_malloc0 (sizeof (ThreadData));
131
132   l_data->cand_count = 0;
133   l_data->other_cand_count = &r_data->cand_count;
134   l_data->recv_count = 0;
135   l_data->other_recv_count = &r_data->recv_count;
136   l_data->send_count = 0;
137   l_data->other_send_count = &r_data->send_count;
138
139   r_data->cand_count = 0;
140   r_data->other_cand_count = &l_data->cand_count;
141   r_data->recv_count = 0;
142   r_data->other_recv_count = &l_data->recv_count;
143   r_data->send_count = 0;
144   r_data->other_send_count = &l_data->send_count;
145
146   run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
147
148   /* Verify that correct number of local candidates were reported. */
149   g_assert_cmpuint (l_data->cand_count, ==, 1);
150   g_assert_cmpuint (r_data->cand_count, ==, 1);
151
152   g_free (r_data);
153   g_free (l_data);
154
155 #ifdef G_OS_WIN32
156   WSACleanup ();
157 #endif
158   return 0;
159 }