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.
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
24 #include "swfdec_test_socket.h"
25 #include "swfdec_test_buffer.h"
26 #include "swfdec_test_function.h"
29 swfdec_test_throw (SwfdecAsContext
*cx
, const char *message
, ...)
33 if (!swfdec_as_context_catch (cx
, &val
)) {
37 va_start (varargs
, message
);
38 s
= g_strdup_vprintf (message
, 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
)
52 swfdec_test_socket_do_close (SwfdecTestSocket
*sock
, gboolean success
)
54 if (sock
->plugin
== NULL
)
57 sock
->plugin
->finish (sock
->plugin
, success
? 0 : 1);
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
);
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
);
81 swfdec_test_socket_class_init (SwfdecTestSocketClass
*klass
)
83 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
85 object_class
->dispose
= swfdec_test_socket_dispose
;
89 swfdec_test_socket_init (SwfdecTestSocket
*sock
)
91 sock
->receive_queue
= swfdec_buffer_queue_new ();
96 SWFDEC_TEST_FUNCTION ("Socket_send", swfdec_test_socket_send
)
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");
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
)
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
);
129 swfdec_test_throw (cx
, "only %zu bytes available", depth
);
132 buffer
= swfdec_buffer_queue_pull (sock
->receive_queue
, len
);
136 SWFDEC_AS_VALUE_SET_NULL (retval
);
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
)
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
)
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
)
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
);
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
);
194 swfdec_test_socket_plugin_close (SwfdecTestPluginSocket
*plugin
)
196 swfdec_test_socket_do_close (plugin
->data
, TRUE
);
200 swfdec_test_socket_new (SwfdecTestTest
*test
, SwfdecTestPluginSocket
*plugin
)
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
;
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
);
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
);