tests: tracerrecord: Fix messages glist memory leak
[platform/upstream/gstreamer.git] / tests / check / gst / gsttracerrecord.c
1 /* GStreamer
2  *
3  * Unit tests for GstTracerRecord
4  *
5  * Copyright (C) 2016 Stefan Sauer <ensonic@users.sf.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <gst/check/gstcheck.h>
24 #include <gst/gsttracerrecord.h>
25
26 static GList *messages;         /* NULL */
27 static gboolean save_messages;  /* FALSE */
28
29 static void
30 tracer_log_func (GstDebugCategory * category,
31     GstDebugLevel level, const gchar * file, const gchar * function,
32     gint line, GObject * object, GstDebugMessage * message, gpointer unused)
33 {
34   const gchar *dbg_msg;
35
36   if (!save_messages || level != GST_LEVEL_TRACE ||
37       !g_str_equal (category->name, "GST_TRACER")) {
38     return;
39   }
40
41   dbg_msg = gst_debug_message_get (message);
42   fail_unless (dbg_msg != NULL);
43
44   messages = g_list_append (messages, g_strdup (dbg_msg));
45 }
46
47 static void
48 setup (void)
49 {
50   gst_debug_remove_log_function (gst_debug_log_default);
51   gst_debug_add_log_function (tracer_log_func, NULL, NULL);
52   gst_debug_set_threshold_for_name ("GST_TRACER", GST_LEVEL_TRACE);
53   messages = NULL;
54   save_messages = FALSE;
55 }
56
57 static void
58 cleanup (void)
59 {
60   save_messages = FALSE;
61   gst_debug_set_threshold_for_name ("GST_TRACER", GST_LEVEL_NONE);
62   gst_debug_add_log_function (gst_debug_log_default, NULL, NULL);
63   gst_debug_remove_log_function (tracer_log_func);
64   g_list_free_full (messages, (GDestroyNotify) g_free);
65   messages = NULL;
66 }
67
68
69 GST_START_TEST (serialize_message_logging)
70 {
71   GstTracerRecord *tr;
72   gchar *str;
73
74   /* *INDENT-OFF* */
75   tr = gst_tracer_record_new (gst_structure_new ("test.class",
76       "string", GST_TYPE_STRUCTURE, gst_structure_new ("value",
77           "type", G_TYPE_GTYPE, G_TYPE_STRING,
78           NULL),
79       NULL));
80   /* *INDENT-ON* */
81
82   save_messages = TRUE;
83   gst_tracer_record_log (tr, "test");
84   save_messages = FALSE;
85
86   fail_unless_equals_int (g_list_length (messages), 1);
87   str = (gchar *) messages->data;
88   fail_unless (str != NULL);
89 }
90
91 GST_END_TEST;
92
93
94 GST_START_TEST (serialize_static_record)
95 {
96   GstTracerRecord *tr;
97   GstStructure *s;
98   gchar *str;
99   gchar *str_val;
100   gint int_val;
101   gboolean bool_val;
102   GstPadDirection enum_val;
103
104   /* *INDENT-OFF* */
105   tr = gst_tracer_record_new (gst_structure_new ("test.class",
106       "string", GST_TYPE_STRUCTURE, gst_structure_new ("value",
107           "type", G_TYPE_GTYPE, G_TYPE_STRING,
108           NULL),
109       "int", GST_TYPE_STRUCTURE, gst_structure_new ("value",
110           "type", G_TYPE_GTYPE, G_TYPE_INT,
111           NULL),
112       "bool", GST_TYPE_STRUCTURE, gst_structure_new ("value",
113           "type", G_TYPE_GTYPE, G_TYPE_BOOLEAN,
114           NULL),
115       "enum", GST_TYPE_STRUCTURE, gst_structure_new ("value",
116           "type", G_TYPE_GTYPE, GST_TYPE_PAD_DIRECTION,
117           NULL),
118       NULL));
119   /* *INDENT-ON* */
120
121   save_messages = TRUE;
122   gst_tracer_record_log (tr, "test", 1, TRUE, GST_PAD_SRC);
123   save_messages = FALSE;
124
125   str = (gchar *) messages->data;
126   GST_INFO ("serialized to '%s'", str);
127
128   s = gst_structure_from_string (str, NULL);
129   fail_unless (s != NULL);
130
131   fail_unless_equals_string (gst_structure_get_name (s), "test");
132
133   fail_unless (gst_structure_get (s,
134           "string", G_TYPE_STRING, &str_val,
135           "int", G_TYPE_INT, &int_val,
136           "bool", G_TYPE_BOOLEAN, &bool_val,
137           "enum", GST_TYPE_PAD_DIRECTION, &enum_val, NULL));
138   fail_unless_equals_int (int_val, 1);
139   fail_unless_equals_string (str_val, "test");
140   fail_unless_equals_int (bool_val, TRUE);
141   fail_unless_equals_int (enum_val, GST_PAD_SRC);
142
143   gst_structure_free (s);
144 }
145
146 GST_END_TEST;
147
148
149 static Suite *
150 gst_tracer_record_suite (void)
151 {
152   Suite *s = suite_create ("GstTracerRecord");
153   TCase *tc_chain = tcase_create ("record");
154
155   suite_add_tcase (s, tc_chain);
156   tcase_add_checked_fixture (tc_chain, setup, cleanup);
157   tcase_add_test (tc_chain, serialize_message_logging);
158   tcase_add_test (tc_chain, serialize_static_record);
159
160   /* FIXME: add more tests, e.g. enums, pointer types and optional fields */
161
162   return s;
163 }
164
165 GST_CHECK_MAIN (gst_tracer_record);