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
, 0)
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
, 0)
117 swfdec_test_socket_receive (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
118 SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
120 SwfdecTestSocket
*sock
;
121 SwfdecBuffer
*buffer
;
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_test_buffer_new (cx
, buffer
));
145 SWFDEC_TEST_FUNCTION ("Socket_error", swfdec_test_socket_error
, 0)
147 swfdec_test_socket_error (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
148 SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
150 SwfdecTestSocket
*sock
;
152 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET
, &sock
, "");
154 swfdec_test_socket_do_close (sock
, FALSE
);
157 SWFDEC_TEST_FUNCTION ("Socket_close", swfdec_test_socket_close_as
, 0)
159 swfdec_test_socket_close_as (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
160 SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
162 SwfdecTestSocket
*sock
;
164 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET
, &sock
, "");
166 swfdec_test_socket_do_close (sock
, TRUE
);
169 SWFDEC_TEST_FUNCTION ("Socket_get_closed", swfdec_test_socket_get_closed
, 0)
171 swfdec_test_socket_get_closed (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
172 SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
174 SwfdecTestSocket
*sock
;
176 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_SOCKET
, &sock
, "");
178 SWFDEC_AS_VALUE_SET_BOOLEAN (retval
, sock
->plugin
== NULL
);
182 swfdec_test_socket_plugin_send (SwfdecTestPluginSocket
*plugin
, unsigned char *data
,
183 unsigned long length
)
185 SwfdecTestSocket
*sock
= plugin
->data
;
186 SwfdecBuffer
*buffer
;
188 buffer
= swfdec_buffer_new_for_data (g_memdup (data
, length
), length
);
189 swfdec_buffer_queue_push (sock
->receive_queue
, buffer
);
193 swfdec_test_socket_plugin_close (SwfdecTestPluginSocket
*plugin
)
195 swfdec_test_socket_do_close (plugin
->data
, TRUE
);
199 swfdec_test_socket_new (SwfdecTestTest
*test
, SwfdecTestPluginSocket
*plugin
)
205 g_return_val_if_fail (SWFDEC_IS_TEST_TEST (test
), NULL
);
206 g_return_val_if_fail (plugin
!= NULL
, NULL
);
208 cx
= SWFDEC_AS_OBJECT (test
)->context
;
210 if (!swfdec_as_context_use_mem (cx
, sizeof (SwfdecTestSocket
)))
212 new = g_object_new (SWFDEC_TYPE_TEST_SOCKET
, NULL
);
213 swfdec_as_object_add (new, cx
, sizeof (SwfdecTestSocket
));
214 swfdec_as_object_get_variable (cx
->global
,
215 swfdec_as_context_get_string (cx
, "Socket"), &val
);
216 if (SWFDEC_AS_VALUE_IS_OBJECT (&val
))
217 swfdec_as_object_set_constructor (new, SWFDEC_AS_VALUE_GET_OBJECT (&val
));
219 plugin
->send
= swfdec_test_socket_plugin_send
;
220 plugin
->close
= swfdec_test_socket_plugin_close
;
222 SWFDEC_TEST_SOCKET (new)->test
= test
;
223 SWFDEC_TEST_SOCKET (new)->plugin
= plugin
;
224 test
->sockets
= g_list_append (test
->sockets
, new);
226 SWFDEC_AS_VALUE_SET_STRING (&val
,
227 swfdec_as_context_get_string (cx
, plugin
->host
));
228 swfdec_as_object_set_variable (new,
229 swfdec_as_context_get_string (cx
, "host"), &val
);
230 SWFDEC_AS_VALUE_SET_INT (&val
, plugin
->port
);
231 swfdec_as_object_set_variable (new,
232 swfdec_as_context_get_string (cx
, "port"), &val
);
238 swfdec_test_socket_close (SwfdecTestSocket
*sock
)
240 g_return_if_fail (SWFDEC_IS_TEST_SOCKET (sock
));
242 return swfdec_test_socket_do_close (sock
, FALSE
);