Example #1
0
File: gc.c Project: fengye/swfdec
static guint
check_object_variables (void)
{
  SwfdecAsObject *o, *o2;
  guint errors = 0;
  SwfdecAsContext *context;
  const char *s;
  gpointer check = GUINT_TO_POINTER (-1); /* NOT NULL */
  gpointer check2 = GUINT_TO_POINTER (-1); /* NOT NULL */
  SwfdecAsValue v1, v2;
  
  context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
  swfdec_as_context_startup (context);
  g_assert (check != NULL);

  o = swfdec_as_object_new (context);
  o2 = swfdec_as_object_new (context);
  g_object_add_weak_pointer (G_OBJECT (o), &check);
  g_object_add_weak_pointer (G_OBJECT (o2), &check2);
  s = swfdec_as_context_get_string (context, "foo");
  /* step one: set a variable */
  SWFDEC_AS_VALUE_SET_OBJECT (&v1, o);
  swfdec_as_object_set_variable (context->global, s, &v1);
  SWFDEC_AS_VALUE_SET_OBJECT (&v2, o2);
  swfdec_as_object_set_variable (o, s, &v2);
  SWFDEC_AS_VALUE_SET_UNDEFINED (&v2);
  swfdec_as_object_get_variable (o, s, &v2);
  if (!SWFDEC_AS_VALUE_IS_OBJECT (&v2)) {
    ERROR ("variable changed type");
  } else if (o2 != SWFDEC_AS_VALUE_GET_OBJECT (&v2)) {
    ERROR ("variable changed value");
  }
  /* step 2: gc */
  swfdec_as_context_gc (context);
  if (check == NULL || check2 == NULL) {
    ERROR ("GC collected a used object, bailing");
    g_object_unref (context);
    return errors;
  }
  /* step 3: unset root reference and set cyclic variable */
  swfdec_as_object_delete_variable (context->global, s);
  swfdec_as_object_set_variable (o2, s, &v1);
  SWFDEC_AS_VALUE_SET_UNDEFINED (&v1);
  swfdec_as_object_get_variable (o2, s, &v1);
  if (!SWFDEC_AS_VALUE_IS_OBJECT (&v1)) {
    ERROR ("variable changed type");
  } else if (o != SWFDEC_AS_VALUE_GET_OBJECT (&v1)) {
    ERROR ("variable changed value");
  }
  /* step 4: gc, ensure that both objects disappears */
  swfdec_as_context_gc (context);
  if (check != NULL || check2 != NULL) {
    ERROR ("GC didn't collect unused object");
  }

  g_object_unref (context);
  return errors;

}
static void
swfdec_test_test_request_socket (SwfdecTestPlugin *plugin,
    SwfdecTestPluginSocket *psock)
{
  SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
  SwfdecAsContext *cx = swfdec_gc_object_get_context (test);
  SwfdecTestSocket *sock;
  SwfdecAsValue val;

  sock = swfdec_test_socket_new (test, psock);
  SWFDEC_AS_VALUE_SET_OBJECT (&val, swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (sock)));
  swfdec_as_relay_call (SWFDEC_AS_RELAY (test), swfdec_as_context_get_string (cx, "onSocket"),
      1, &val, NULL);
}
Example #3
0
File: gc.c Project: fengye/swfdec
static guint
check_strings (void)
{
  const char *s;
  guint errors = 0;
  SwfdecAsContext *context;
  
  context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
  swfdec_as_context_startup (context);

  s = swfdec_as_context_get_string (context, "hi mom");
  if (!g_str_equal (s, "hi mom")) {
    ERROR ("swfdec_as_context_get_string returns different string from input");
  }

  g_object_unref (context);
  return errors;
}
static gboolean
swfdec_xml_socket_stream_target_parse (SwfdecStreamTarget *target,
    SwfdecStream *stream)
{
  SwfdecXmlSocket *xml = SWFDEC_XML_SOCKET (target);
  SwfdecBufferQueue *queue;
  SwfdecBuffer *buffer;
  gsize len;

  /* parse until next 0 byte or take everything */
  queue = swfdec_stream_get_queue (stream);
  while ((buffer = swfdec_buffer_queue_peek_buffer (queue))) {
    guchar *nul = memchr (buffer->data, 0, buffer->length);
    
    len = nul ? (gsize) (nul - buffer->data + 1) : buffer->length;
    g_assert (len > 0);
    swfdec_buffer_unref (buffer);
    buffer = swfdec_buffer_queue_pull (queue, len);
    swfdec_buffer_queue_push (xml->queue, buffer);
    if (nul) {
      len = swfdec_buffer_queue_get_depth (xml->queue);
      g_assert (len > 0);
      buffer = swfdec_buffer_queue_pull (xml->queue, len);
      if (!g_utf8_validate ((char *) buffer->data, len, NULL)) {
	SWFDEC_FIXME ("invalid utf8 sent through socket, what now?");
      } else {
	SwfdecAsValue val;

	SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (
	      swfdec_gc_object_get_context (xml), (char *) buffer->data));
	swfdec_sandbox_use (xml->sandbox);
	swfdec_as_object_call (xml->target, SWFDEC_AS_STR_onData, 1, &val, NULL);
	swfdec_sandbox_unuse (xml->sandbox);
      }
    }
  }
  return FALSE;
}
static void
swfdec_system_player_type (SwfdecPlayer *player, SwfdecAsValue *ret)
{
  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_get_string (
	SWFDEC_AS_CONTEXT (player), player->priv->system->player_type));
}
Example #6
0
int
main (int argc, char **argv)
{
    char *script_filename = NULL;
    GError *error = NULL;
    SwfdecAsContext *context;
    SwfdecAsObject *array;
    SwfdecScript *script;
    SwfdecAsValue val;
    int i, ret;
    gboolean dump = FALSE;
    gboolean no_check = FALSE, only_check = FALSE;

    GOptionEntry options[] = {
        { "dump", 'd', 0, G_OPTION_ARG_NONE, &dump, "dump informative output on failure", FALSE },
        { "no-check", 0, 0, G_OPTION_ARG_NONE, &no_check, "don't check if the system is ok for running the testsuite", FALSE },
        { "self-check", 0, 0, G_OPTION_ARG_NONE, &only_check, "run a system check and exit", FALSE },
        { "player", 'p', 0, G_OPTION_ARG_STRING, &swfdec_test_plugin_name, "player to test", "NAME" },
        { "script", 's', 0, G_OPTION_ARG_STRING, &script_filename, "script to execute if not ./default.sts", "FILENAME" },
        { NULL }
    };
    GOptionContext *ctx;

    /* set the right warning levels */
    g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);
    /* by default get rid of the loads of warnings the tests produce */
    g_setenv ("SWFDEC_DEBUG", "2", FALSE);

    g_thread_init (NULL);
    swfdec_init ();

    ctx = g_option_context_new ("");
    g_option_context_add_main_entries (ctx, options, "options");
    g_option_context_parse (ctx, &argc, &argv, &error);
    g_option_context_free (ctx);

    if (error) {
        g_printerr ("ERROR: wrong command line arguments: %s\n", error->message);
        g_error_free (error);
        return EXIT_FAILURE;
    }

    if (only_check || !no_check) {
        gboolean result = check_system (only_check);
        if (!result) {
            g_print ("ERROR: System checked failed, aborting. Use --no-check to disable.\n");
            return 1;
        } else if (only_check) {
            return 0;
        }
    }
    g_assert (!only_check);

    /* allow env vars instead of options - eases running make check with different settings */
    if (swfdec_test_plugin_name == NULL)
        swfdec_test_plugin_name = g_strdup (g_getenv ("SWFDEC_TEST_PLAYER"));

    script = load_script (script_filename);
    g_free (script_filename);
    if (script == NULL)
        return EXIT_FAILURE;

    context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
    swfdec_as_context_startup (context);

    SWFDEC_AS_VALUE_SET_BOOLEAN (&val, dump);
    swfdec_as_object_set_variable (context->global,
                                   swfdec_as_context_get_string (context, "dump"), &val);

    swfdec_test_function_init_context (context);
    swfdec_as_context_run_init_script (context, swfdec_test_initialize,
                                       sizeof (swfdec_test_initialize), SWFDEC_TEST_VERSION);

    array = swfdec_as_array_new (context);
    if (array == NULL) {
        g_print ("ERROR: Not enough memory");
        return EXIT_FAILURE;
    }
    if (argc < 2) {
        GDir *dir;
        const char *file;
        dir = g_dir_open (".", 0, NULL);
        while ((file = g_dir_read_name (dir))) {
            if (!g_str_has_suffix (file, ".swf"))
                continue;
            SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (context, file));
            swfdec_as_array_push (SWFDEC_AS_ARRAY (array), &val);
        }
        g_dir_close (dir);
    } else {
        for (i = 1; i < argc; i++) {
            SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (context, argv[i]));
            swfdec_as_array_push (SWFDEC_AS_ARRAY (array), &val);
        }
    }
    SWFDEC_AS_VALUE_SET_OBJECT (&val, array);
    swfdec_as_object_set_variable (context->global,
                                   swfdec_as_context_get_string (context, "filenames"), &val);
    swfdec_as_object_run (context->global, script);
    if (swfdec_as_context_catch (context, &val)) {
        g_print ("ERROR: %s\n", swfdec_as_value_to_string (context, &val));
        ret = EXIT_FAILURE;
    } else {
        g_print ("SUCCESS\n");
        ret = EXIT_SUCCESS;
    }

    swfdec_script_unref (script);
    g_object_unref (context);

    return ret;
}