1 /*
2 * This file is part of the Nice GLib ICE library.
3 *
4 * (C) 2014 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 #define NUM_MESSAGES 10
49
50 guint count = 0;
51 GMutex count_lock;
52 GCond count_cond;
53
54 static void
read_thread_cb(GInputStream * input_stream,TestIOStreamThreadData * data)55 read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
56 {
57 GError *error = NULL;
58 gssize len;
59 guint8 buf[MESSAGE_SIZE];
60
61
62 g_mutex_lock (&count_lock);
63 count++;
64 g_cond_broadcast (&count_cond);
65 g_mutex_unlock (&count_lock);
66
67 /* Block on receiving some data. */
68 do {
69 len = g_input_stream_read (input_stream, buf, sizeof (buf), NULL, &error);
70 if (!data->user_data) {
71 g_assert_cmpint (len, ==, sizeof(buf));
72 return;
73 }
74 } while (len > 0);
75 g_assert_cmpint (len, ==, -1);
76
77 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE);
78 g_clear_error (&error);
79
80 stop_main_loop (data->error_loop);
81 }
82
83 static void
write_thread_cb(GOutputStream * output_stream,TestIOStreamThreadData * data)84 write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
85 {
86 gchar buf[MESSAGE_SIZE] = {0};
87 gssize ret;
88 GError *error = NULL;
89 gpointer tmp;
90 guint stream_id;
91
92 ret = g_output_stream_write (output_stream, buf, sizeof (buf), NULL,
93 &error);
94
95 g_mutex_lock (&count_lock);
96 count++;
97 g_cond_broadcast (&count_cond);
98 if (data->user_data) {
99 g_assert_cmpint (ret, ==, sizeof(buf));
100 g_mutex_unlock (&count_lock);
101 return;
102 }
103
104 while (count != 4)
105 g_cond_wait (&count_cond, &count_lock);
106 g_mutex_unlock (&count_lock);
107
108
109 /* Now we remove the stream, lets see how the writer handles that */
110
111 tmp = g_object_get_data (G_OBJECT (data->other->agent), "stream-id");
112 stream_id = GPOINTER_TO_UINT (tmp);
113
114 nice_agent_remove_stream (data->other->agent, stream_id);
115 }
116
main(void)117 int main (void)
118 {
119 const TestIOStreamCallbacks callbacks = {
120 read_thread_cb,
121 write_thread_cb,
122 NULL,
123 NULL,
124 };
125
126 #ifdef G_OS_WIN32
127 WSADATA w;
128 WSAStartup (0x0202, &w);
129 #endif
130
131 run_io_stream_test (30, TRUE, &callbacks, (gpointer) TRUE, NULL, NULL, NULL);
132
133 #ifdef G_OS_WIN32
134 WSACleanup ();
135 #endif
136
137 return 0;
138 }
139