Blame tests/check/gst/gstprotection.c

Packit a6ee4b
/* GStreamer
Packit a6ee4b
 *
Packit a6ee4b
 * Unit tests for protection library.
Packit a6ee4b
 *
Packit a6ee4b
 * Copyright (C) <2015> YouView TV Ltd.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
#ifdef HAVE_CONFIG_H
Packit a6ee4b
#include "config.h"
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
#include <gst/check/gstcheck.h>
Packit a6ee4b
#include <gst/gstprotection.h>
Packit a6ee4b
Packit a6ee4b
#ifndef GST_PACKAGE_NAME
Packit a6ee4b
#define GST_PACKAGE_NAME "gstreamer"
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
#ifndef GST_PACKAGE_ORIGIN
Packit a6ee4b
#define GST_PACKAGE_ORIGIN "https://developer.gnome.org/gstreamer/"
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
static GType gst_protection_test_get_type (void);
Packit a6ee4b
Packit a6ee4b
#define GST_TYPE_PROTECTION_TEST            (gst_protection_test_get_type ())
Packit a6ee4b
#define GST_PROTECTION_TEST(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PROTECTION_TEST, GstProtectionTest))
Packit a6ee4b
#define GST_PROTECTION_TEST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PROTECTION_TEST, GstProtectionTestClass))
Packit a6ee4b
#define GST_IS_PROTECTION_TEST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PROTECTION_TEST))
Packit a6ee4b
#define GST_IS_PROTECTION_TEST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PROTECTION_TEST))
Packit a6ee4b
#define GST_PROTECTION_TEST_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PROTECTION_TEST, GstProtectionTestClass))
Packit a6ee4b
#define GST_PROTECTION_TEST_NAME            "protection-test"
Packit a6ee4b
Packit a6ee4b
#define CLEARKEY_SYSTEM_ID "78f32170-d883-11e0-9572-0800200c9a66"
Packit a6ee4b
Packit a6ee4b
typedef struct _GstProtectionTest
Packit a6ee4b
{
Packit a6ee4b
  GstElement parent;
Packit a6ee4b
Packit a6ee4b
  gint test;
Packit a6ee4b
} GstProtectionTest;
Packit a6ee4b
Packit a6ee4b
typedef struct _GstProtectionTestClass
Packit a6ee4b
{
Packit a6ee4b
  GstElementClass parent_class;
Packit a6ee4b
} GstProtectionTestClass;
Packit a6ee4b
Packit a6ee4b
typedef struct _PluginInitContext
Packit a6ee4b
{
Packit a6ee4b
  const gchar *name;
Packit a6ee4b
  guint rank;
Packit a6ee4b
  GType type;
Packit a6ee4b
} PluginInitContext;
Packit a6ee4b
Packit a6ee4b
static GstStaticPadTemplate gst_decrypt_sink_template =
Packit a6ee4b
GST_STATIC_PAD_TEMPLATE ("sink",
Packit a6ee4b
    GST_PAD_SINK,
Packit a6ee4b
    GST_PAD_ALWAYS,
Packit a6ee4b
    GST_STATIC_CAPS
Packit a6ee4b
    ("application/x-cenc, original-media-type=(string)video/x-h264, "
Packit a6ee4b
        GST_PROTECTION_SYSTEM_ID_CAPS_FIELD "=(string)" CLEARKEY_SYSTEM_ID)
Packit a6ee4b
    );
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_protection_test_class_init (GObjectClass * klass)
Packit a6ee4b
{
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_protection_test_base_init (GstProtectionTestClass * klass)
Packit a6ee4b
{
Packit a6ee4b
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
Packit a6ee4b
Packit a6ee4b
  gst_element_class_add_static_pad_template (element_class,
Packit a6ee4b
      &gst_decrypt_sink_template);
Packit a6ee4b
Packit a6ee4b
  gst_element_class_set_metadata (element_class,
Packit a6ee4b
      "Decryptor element for unit tests",
Packit a6ee4b
      GST_ELEMENT_FACTORY_KLASS_DECRYPTOR,
Packit a6ee4b
      "Use in unit tests", "Alex Ashley <alex.ashley@youview.com>");
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static GType
Packit a6ee4b
gst_protection_test_get_type (void)
Packit a6ee4b
{
Packit a6ee4b
  static volatile gsize protection_test_type = 0;
Packit a6ee4b
Packit a6ee4b
  if (g_once_init_enter (&protection_test_type)) {
Packit a6ee4b
    GType type;
Packit a6ee4b
    const GTypeInfo info = {
Packit a6ee4b
      sizeof (GstProtectionTestClass),
Packit a6ee4b
      (GBaseInitFunc) gst_protection_test_base_init,    /* base_init */
Packit a6ee4b
      NULL,                     /* base_finalize */
Packit a6ee4b
      (GClassInitFunc) gst_protection_test_class_init,  /* class_init */
Packit a6ee4b
      NULL,                     /* class_finalize */
Packit a6ee4b
      NULL,                     /* class_data */
Packit a6ee4b
      sizeof (GstProtectionTest),
Packit a6ee4b
      0,                        /* n_preallocs */
Packit a6ee4b
      NULL,                     /* instance_init */
Packit a6ee4b
      NULL                      /* value_table */
Packit a6ee4b
    };
Packit a6ee4b
    type =
Packit a6ee4b
        g_type_register_static (GST_TYPE_ELEMENT, "GstProtectionTest", &info,
Packit a6ee4b
        0);
Packit a6ee4b
    g_once_init_leave (&protection_test_type, type);
Packit a6ee4b
  }
Packit a6ee4b
  return protection_test_type;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
protection_plugin_init_func (GstPlugin * plugin, gpointer user_data)
Packit a6ee4b
{
Packit a6ee4b
  PluginInitContext *context = (PluginInitContext *) user_data;
Packit a6ee4b
  gboolean ret;
Packit a6ee4b
Packit a6ee4b
  ret =
Packit a6ee4b
      gst_element_register (plugin, context->name, context->rank,
Packit a6ee4b
      context->type);
Packit a6ee4b
  return ret;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
protection_create_plugin (GstRegistry * registry, const gchar * name,
Packit a6ee4b
    GType type)
Packit a6ee4b
{
Packit a6ee4b
  gboolean ret;
Packit a6ee4b
  PluginInitContext context;
Packit a6ee4b
Packit a6ee4b
  context.name = name;
Packit a6ee4b
  context.rank = GST_RANK_MARGINAL;
Packit a6ee4b
  context.type = type;
Packit a6ee4b
  ret = gst_plugin_register_static_full (GST_VERSION_MAJOR,     /* version */
Packit a6ee4b
      GST_VERSION_MINOR,        /* version */
Packit a6ee4b
      name,                     /* name */
Packit a6ee4b
      "Protection unit test",   /* description */
Packit a6ee4b
      protection_plugin_init_func,      /* init function */
Packit a6ee4b
      "0.0.0",                  /* version string */
Packit a6ee4b
      GST_LICENSE_UNKNOWN,      /* license */
Packit a6ee4b
      __FILE__,                 /* source */
Packit a6ee4b
      GST_PACKAGE_NAME,         /* package */
Packit a6ee4b
      GST_PACKAGE_ORIGIN,       /* origin */
Packit a6ee4b
      &context                  /* user_data */
Packit a6ee4b
      );
Packit a6ee4b
  return ret;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
test_setup (void)
Packit a6ee4b
{
Packit a6ee4b
  GstRegistry *registry;
Packit a6ee4b
Packit a6ee4b
  registry = gst_registry_get ();
Packit a6ee4b
  protection_create_plugin (registry, GST_PROTECTION_TEST_NAME,
Packit a6ee4b
      GST_TYPE_PROTECTION_TEST);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
test_teardown (void)
Packit a6ee4b
{
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
Packit a6ee4b
GST_START_TEST (test_decryptor_element_class)
Packit a6ee4b
{
Packit a6ee4b
  GstElement *elem;
Packit a6ee4b
  const gchar *selected_id;
Packit a6ee4b
  const gchar *sys_ids[] = {
Packit a6ee4b
    CLEARKEY_SYSTEM_ID,
Packit a6ee4b
    "69f908af-4816-46ea-910c-cd5dcccb0a3a",
Packit a6ee4b
    "5e629af5-38da-4063-8977-97ffbd9902d4",
Packit a6ee4b
    NULL
Packit a6ee4b
  };
Packit a6ee4b
Packit a6ee4b
#ifdef DEBUG_PLUGINS
Packit a6ee4b
  GList *list, *walk;
Packit a6ee4b
Packit a6ee4b
  list = gst_registry_get_plugin_list (gst_registry_get ());
Packit a6ee4b
  for (walk = list; walk; walk = g_list_next (walk)) {
Packit a6ee4b
    GstPlugin *plugin = (GstPlugin *) walk->data;
Packit a6ee4b
    g_print ("Element %s\n", gst_plugin_get_name (plugin));
Packit a6ee4b
  }
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
  elem = gst_element_factory_make (GST_PROTECTION_TEST_NAME, NULL);
Packit a6ee4b
  fail_unless (GST_IS_ELEMENT (elem));
Packit a6ee4b
Packit a6ee4b
  selected_id = gst_protection_select_system (sys_ids);
Packit a6ee4b
  fail_if (selected_id == NULL);
Packit a6ee4b
Packit a6ee4b
  selected_id = gst_protection_select_system (&sys_ids[1]);
Packit a6ee4b
  fail_unless (selected_id == NULL);
Packit a6ee4b
Packit a6ee4b
  selected_id = gst_protection_select_system (&sys_ids[3]);
Packit a6ee4b
  fail_unless (selected_id == NULL);
Packit a6ee4b
Packit a6ee4b
  gst_object_unref (elem);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_END_TEST;
Packit a6ee4b
Packit a6ee4b
GST_START_TEST (test_protection_metadata)
Packit a6ee4b
{
Packit a6ee4b
  GstBuffer *buf = NULL;
Packit a6ee4b
  GstBuffer *iv, *kid;
Packit a6ee4b
  GstBuffer *fetched_iv = NULL, *fetched_key_id = NULL;
Packit a6ee4b
  GstStructure *meta_info;
Packit a6ee4b
  GstProtectionMeta *meta = NULL;
Packit a6ee4b
  const GstMetaInfo *info = NULL;
Packit a6ee4b
  const GValue *value;
Packit a6ee4b
Packit a6ee4b
  /* Check correct type info is returned */
Packit a6ee4b
  info = gst_protection_meta_get_info ();
Packit a6ee4b
  fail_unless (info != NULL);
Packit a6ee4b
  fail_unless (info->api == GST_PROTECTION_META_API_TYPE);
Packit a6ee4b
Packit a6ee4b
  iv = gst_buffer_new_allocate (NULL, 16, NULL);
Packit a6ee4b
  gst_buffer_memset (iv, 0, 'i', 16);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (iv, "iv", 1);
Packit a6ee4b
  kid = gst_buffer_new_allocate (NULL, 16, NULL);
Packit a6ee4b
  gst_buffer_memset (kid, 0, 'k', 16);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (kid, "kid", 1);
Packit a6ee4b
  meta_info = gst_structure_new ("application/x-cenc",
Packit a6ee4b
      "encrypted", G_TYPE_BOOLEAN, TRUE,
Packit a6ee4b
      "iv", GST_TYPE_BUFFER, iv,
Packit a6ee4b
      "iv_size", G_TYPE_UINT, 16, "kid", GST_TYPE_BUFFER, kid, NULL);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (kid, "kid", 2);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (iv, "iv", 2);
Packit a6ee4b
Packit a6ee4b
  buf = gst_buffer_new_allocate (NULL, 1024, NULL);
Packit a6ee4b
  /* Test attaching protection metadata to buffer */
Packit a6ee4b
  meta = gst_buffer_add_protection_meta (buf, meta_info);
Packit a6ee4b
  fail_unless (meta != NULL);
Packit a6ee4b
  /* gst_buffer_new_allocate takes ownership of info GstStructure */
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (buf, "Buffer", 1);
Packit a6ee4b
Packit a6ee4b
  /* Test detaching protection metadata from buffer, and check that
Packit a6ee4b
   * contained data is correct */
Packit a6ee4b
  meta = NULL;
Packit a6ee4b
  meta = gst_buffer_get_protection_meta (buf);
Packit a6ee4b
  fail_unless (meta != NULL);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (buf, "Buffer", 1);
Packit a6ee4b
  value = gst_structure_get_value (meta->info, "iv");
Packit a6ee4b
  fail_unless (value != NULL);
Packit a6ee4b
  fetched_iv = gst_value_get_buffer (value);
Packit a6ee4b
  fail_unless (fetched_iv != NULL);
Packit a6ee4b
  fail_unless (gst_buffer_get_size (fetched_iv) == 16);
Packit a6ee4b
  value = gst_structure_get_value (meta->info, "kid");
Packit a6ee4b
  fail_unless (value != NULL);
Packit a6ee4b
  fetched_key_id = gst_value_get_buffer (value);
Packit a6ee4b
  fail_unless (fetched_key_id != NULL);
Packit a6ee4b
  fail_unless (gst_buffer_get_size (fetched_key_id) == 16);
Packit a6ee4b
Packit a6ee4b
  gst_buffer_remove_meta (buf, (GstMeta *) meta);
Packit a6ee4b
Packit a6ee4b
  /* Check that refcounts are decremented after metadata is freed */
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (buf, "Buffer", 1);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (iv, "IV", 1);
Packit a6ee4b
  ASSERT_MINI_OBJECT_REFCOUNT (kid, "KID", 1);
Packit a6ee4b
Packit a6ee4b
  gst_buffer_unref (buf);
Packit a6ee4b
  gst_buffer_unref (iv);
Packit a6ee4b
  gst_buffer_unref (kid);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_END_TEST;
Packit a6ee4b
Packit a6ee4b
static Suite *
Packit a6ee4b
protection_suite (void)
Packit a6ee4b
{
Packit a6ee4b
  Suite *s = suite_create ("protection library");
Packit a6ee4b
  TCase *tc_chain = tcase_create ("general");
Packit a6ee4b
Packit a6ee4b
  suite_add_tcase (s, tc_chain);
Packit a6ee4b
  tcase_add_test (tc_chain, test_decryptor_element_class);
Packit a6ee4b
  tcase_add_test (tc_chain, test_protection_metadata);
Packit a6ee4b
  tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
Packit a6ee4b
Packit a6ee4b
  return s;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_CHECK_MAIN (protection);