Merge branch '0.8'
[swfdec.git] / test / swfdec_test_socket.c
blobbd2f5a50cc4f624d7089a456cad504b88239e9be
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_test_socket.h"
25 #include "swfdec_test_buffer.h"
26 #include "swfdec_test_function.h"
28 static void
29 swfdec_test_throw (SwfdecAsContext *cx, const char *message, ...)
31 SwfdecAsValue val;
33 if (!swfdec_as_context_catch (cx, &val)) {
34 va_list varargs;
35 char *s;
37 va_start (varargs, message);
38 s = g_strdup_vprintf (message, varargs);
39 va_end (varargs);
41 /* FIXME: Throw a real object here? */
42 SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_give_string (cx, s));
44 swfdec_as_context_throw (cx, &val);
47 /*** SWFDEC_TEST_SOCKET ***/
49 G_DEFINE_TYPE (SwfdecTestSocket, swfdec_test_socket, SWFDEC_TYPE_AS_OBJECT)
51 static void
52 swfdec_test_socket_do_close (SwfdecTestSocket *sock, gboolean success)
54 if (sock->plugin == NULL)
55 return;
57 sock->plugin->finish (sock->plugin, success ? 0 : 1);
58 sock->plugin = NULL;
60 if (sock->test->pending_sockets &&
61 sock->test->pending_sockets->data == sock) {
62 sock->test->pending_sockets = sock->test->pending_sockets->prev;
64 sock->test->sockets = g_list_remove (sock->test->sockets, sock);
65 sock->test = NULL;
68 static void
69 swfdec_test_socket_dispose (GObject *object)
71 SwfdecTestSocket *sock = SWFDEC_TEST_SOCKET (object);
73 swfdec_test_socket_do_close (sock, FALSE);
74 swfdec_buffer_queue_unref (sock->receive_queue);
75 sock->receive_queue = NULL;
77 G_OBJECT_CLASS (swfdec_test_socket_parent_class)->dispose (object);
80 static void
81 swfdec_test_socket_class_init (SwfdecTestSocketClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 object_class->dispose = swfdec_test_socket_dispose;
88 static void
89 swfdec_test_socket_init (SwfdecTestSocket *sock)
91 sock->receive_queue = swfdec_buffer_queue_new ();
94 /*** AS CODE ***/
96 SWFDEC_TEST_FUNCTION ("Socket_send", swfdec_test_socket_send)
97 void
98 swfdec_test_socket_send (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
99 SwfdecAsValue *argv, SwfdecAsValue *retval)
101 SwfdecTestSocket *sock;
102 SwfdecBuffer *buffer;
104 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET, &sock, "");
106 if (sock->plugin == NULL) {
107 swfdec_test_throw (cx, "socket is closed");
108 return;
110 buffer = swfdec_test_buffer_from_args (cx, argc, argv);
111 sock->plugin->receive (sock->plugin, buffer->data, buffer->length);
112 swfdec_buffer_unref (buffer);
115 SWFDEC_TEST_FUNCTION ("Socket_receive", swfdec_test_socket_receive)
116 void
117 swfdec_test_socket_receive (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
118 SwfdecAsValue *argv, SwfdecAsValue *retval)
120 SwfdecTestSocket *sock;
121 SwfdecBuffer *buffer;
122 gsize len = 0, depth;
124 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET, &sock, "|i", &len);
126 depth = swfdec_buffer_queue_get_depth (sock->receive_queue);
127 if (len > 0) {
128 if (depth < len) {
129 swfdec_test_throw (cx, "only %zu bytes available", depth);
130 return;
131 } else {
132 buffer = swfdec_buffer_queue_pull (sock->receive_queue, len);
134 } else {
135 if (depth == 0) {
136 SWFDEC_AS_VALUE_SET_NULL (retval);
137 return;
138 } else {
139 buffer = swfdec_buffer_queue_pull (sock->receive_queue, depth);
142 SWFDEC_AS_VALUE_SET_OBJECT (retval, swfdec_as_relay_get_as_object (
143 SWFDEC_AS_RELAY (swfdec_test_buffer_new (cx, buffer))));
146 SWFDEC_TEST_FUNCTION ("Socket_error", swfdec_test_socket_error)
147 void
148 swfdec_test_socket_error (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
149 SwfdecAsValue *argv, SwfdecAsValue *retval)
151 SwfdecTestSocket *sock;
153 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET, &sock, "");
155 swfdec_test_socket_do_close (sock, FALSE);
158 SWFDEC_TEST_FUNCTION ("Socket_close", swfdec_test_socket_close_as)
159 void
160 swfdec_test_socket_close_as (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
161 SwfdecAsValue *argv, SwfdecAsValue *retval)
163 SwfdecTestSocket *sock;
165 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET, &sock, "");
167 swfdec_test_socket_do_close (sock, TRUE);
170 SWFDEC_TEST_FUNCTION ("Socket_get_closed", swfdec_test_socket_get_closed)
171 void
172 swfdec_test_socket_get_closed (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
173 SwfdecAsValue *argv, SwfdecAsValue *retval)
175 SwfdecTestSocket *sock;
177 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET, &sock, "");
179 SWFDEC_AS_VALUE_SET_BOOLEAN (retval, sock->plugin == NULL);
182 static void
183 swfdec_test_socket_plugin_send (SwfdecTestPluginSocket *plugin, unsigned char *data,
184 unsigned long length)
186 SwfdecTestSocket *sock = plugin->data;
187 SwfdecBuffer *buffer;
189 buffer = swfdec_buffer_new_for_data (g_memdup (data, length), length);
190 swfdec_buffer_queue_push (sock->receive_queue, buffer);
193 static void
194 swfdec_test_socket_plugin_close (SwfdecTestPluginSocket *plugin)
196 swfdec_test_socket_do_close (plugin->data, TRUE);
199 SwfdecAsObject *
200 swfdec_test_socket_new (SwfdecTestTest *test, SwfdecTestPluginSocket *plugin)
202 SwfdecAsValue val;
203 SwfdecAsContext *cx;
204 SwfdecAsObject *new;
206 g_return_val_if_fail (SWFDEC_IS_TEST_TEST (test), NULL);
207 g_return_val_if_fail (plugin != NULL, NULL);
209 cx = swfdec_gc_object_get_context (test);
211 new = g_object_new (SWFDEC_TYPE_TEST_SOCKET, "context", cx, NULL);
212 swfdec_as_object_get_variable (cx->global,
213 swfdec_as_context_get_string (cx, "Socket"), &val);
214 if (SWFDEC_AS_VALUE_IS_OBJECT (&val))
215 swfdec_as_object_set_constructor (new, SWFDEC_AS_VALUE_GET_OBJECT (&val));
217 plugin->send = swfdec_test_socket_plugin_send;
218 plugin->close = swfdec_test_socket_plugin_close;
219 plugin->data = new;
220 SWFDEC_TEST_SOCKET (new)->test = test;
221 SWFDEC_TEST_SOCKET (new)->plugin = plugin;
222 test->sockets = g_list_append (test->sockets, new);
224 SWFDEC_AS_VALUE_SET_STRING (&val,
225 swfdec_as_context_get_string (cx, plugin->host));
226 swfdec_as_object_set_variable (new,
227 swfdec_as_context_get_string (cx, "host"), &val);
228 swfdec_as_value_set_integer (cx, &val, plugin->port);
229 swfdec_as_object_set_variable (new,
230 swfdec_as_context_get_string (cx, "port"), &val);
232 return new;
235 void
236 swfdec_test_socket_close (SwfdecTestSocket *sock)
238 g_return_if_fail (SWFDEC_IS_TEST_SOCKET (sock));
240 return swfdec_test_socket_do_close (sock, FALSE);