6f92f285ebe732a63a584195eb61ba3aece50595
[platform/upstream/libnice.git] / tests / test-io-stream-closing-write.c
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
55 read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
56 {
57   gpointer tmp;
58   guint stream_id;
59   GError *error = NULL;
60   gssize len;
61   guint8 buf[MESSAGE_SIZE];
62
63   /* Block on receiving some data. */
64   len = g_input_stream_read (input_stream, buf, sizeof (buf), NULL, &error);
65   g_assert_cmpint (len, ==, sizeof(buf));
66
67   g_mutex_lock (&count_lock);
68   count++;
69   g_cond_broadcast (&count_cond);
70   if (data->user_data) {
71     g_mutex_unlock (&count_lock);
72     return;
73   }
74
75   while (count != 4)
76     g_cond_wait (&count_cond, &count_lock);
77   g_mutex_unlock (&count_lock);
78
79   /* Now we remove the stream, lets see how the writer handles that */
80
81   tmp = g_object_get_data (G_OBJECT (data->other->agent), "stream-id");
82   stream_id = GPOINTER_TO_UINT (tmp);
83
84   nice_agent_remove_stream (data->other->agent, stream_id);
85 }
86
87 static void
88 write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
89 {
90   gchar buf[MESSAGE_SIZE] = {0};
91   gssize ret;
92   GError *error = NULL;
93
94   g_mutex_lock (&count_lock);
95   count++;
96   g_cond_broadcast (&count_cond);
97   g_mutex_unlock (&count_lock);
98
99   do {
100     g_assert_no_error (error);
101     ret = g_output_stream_write (output_stream, buf, sizeof (buf), NULL,
102         &error);
103
104     if (!data->user_data) {
105       g_assert_cmpint (ret, ==, sizeof (buf));
106       return;
107     }
108   } while (ret > 0);
109   g_assert_cmpint (ret, ==, -1);
110
111   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
112   g_clear_error (&error);
113
114   stop_main_loop (data->error_loop);
115 }
116
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 }