gstreamer/tests/check/libs/rtp.c
Wim Taymans 86ab51207b gst-libs/gst/rtp/gstbasertpdepayload.c: Validate the RTP packet before further processing it. It's just too dangerous...
Original commit message from CVS:
* gst-libs/gst/rtp/gstbasertpdepayload.c:
(gst_base_rtp_depayload_chain):
Validate the RTP packet before further processing it. It's just too
dangerous to accept random packets and people are not forced to use a
jitterbuffer or session manager to filter out the bad packets.
* gst-libs/gst/rtp/gstrtpbuffer.c:
(gst_rtp_buffer_set_extension_data),
(gst_rtp_buffer_get_payload_subbuffer):
Small cleanups.
When setting extension data in a buffer that is too small, we fail and
we should not set the extension bit.
Change GST_WARNINGS into g_warning because they really are
programming errors.
* tests/check/libs/rtp.c: (GST_START_TEST):
Catch the g_warnings now in the unit tests and that fact that failing to
set extension data left the extension bit untouched.
2008-05-14 20:28:02 +00:00

292 lines
9.9 KiB
C

/* GStreamer unit tests for the RTP support library
*
* Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/check/gstcheck.h>
#include <gst/rtp/gstrtpbuffer.h>
#include <string.h>
#define RTP_HEADER_LEN 12
GST_START_TEST (test_rtp_buffer)
{
GstBuffer *buf;
guint8 *data;
/* check GstRTPHeader structure alignment and packing */
buf = gst_rtp_buffer_new_allocate (16, 4, 0);
fail_unless (buf != NULL);
fail_unless_equals_int (GST_BUFFER_SIZE (buf), RTP_HEADER_LEN + 16 + 4);
data = GST_BUFFER_DATA (buf);
/* check defaults */
fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 2);
fail_unless (gst_rtp_buffer_get_padding (buf) == FALSE);
fail_unless (gst_rtp_buffer_get_extension (buf) == FALSE);
fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 0);
fail_unless (gst_rtp_buffer_get_marker (buf) == FALSE);
fail_unless (gst_rtp_buffer_get_payload_type (buf) == 0);
fail_unless_equals_int (GST_READ_UINT16_BE (data), 0x8000);
/* check version in bitfield */
gst_rtp_buffer_set_version (buf, 3);
fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 3);
fail_unless_equals_int ((data[0] & 0xC0) >> 6, 3);
gst_rtp_buffer_set_version (buf, 2);
fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 2);
fail_unless_equals_int ((data[0] & 0xC0) >> 6, 2);
/* check padding bit */
gst_rtp_buffer_set_padding (buf, TRUE);
fail_unless (gst_rtp_buffer_get_padding (buf) == TRUE);
fail_unless_equals_int ((data[0] & 0x20) >> 5, 1);
gst_rtp_buffer_set_padding (buf, FALSE);
fail_unless (gst_rtp_buffer_get_padding (buf) == FALSE);
fail_unless_equals_int ((data[0] & 0x20) >> 5, 0);
/* check marker bit */
gst_rtp_buffer_set_marker (buf, TRUE);
fail_unless (gst_rtp_buffer_get_marker (buf) == TRUE);
fail_unless_equals_int ((data[1] & 0x80) >> 7, 1);
gst_rtp_buffer_set_marker (buf, FALSE);
fail_unless (gst_rtp_buffer_get_marker (buf) == FALSE);
fail_unless_equals_int ((data[1] & 0x80) >> 7, 0);
/* check sequence offset */
gst_rtp_buffer_set_seq (buf, 0xF2C9);
fail_unless_equals_int (gst_rtp_buffer_get_seq (buf), 0xF2C9);
fail_unless_equals_int (GST_READ_UINT16_BE (data + 2), 0xF2C9);
gst_rtp_buffer_set_seq (buf, 0);
fail_unless_equals_int (gst_rtp_buffer_get_seq (buf), 0);
fail_unless_equals_int (GST_READ_UINT16_BE (data + 2), 0);
/* check timestamp offset */
gst_rtp_buffer_set_timestamp (buf, 432191);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 4), 432191);
fail_unless_equals_int (gst_rtp_buffer_get_timestamp (buf), 432191);
gst_rtp_buffer_set_timestamp (buf, 0);
fail_unless_equals_int (gst_rtp_buffer_get_timestamp (buf), 0);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 4), 0);
/* check ssrc offset */
gst_rtp_buffer_set_ssrc (buf, 0xf04043C2);
fail_unless_equals_int (gst_rtp_buffer_get_ssrc (buf), (gint) 0xf04043c2);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 4 + 4), (gint) 0xf04043c2);
gst_rtp_buffer_set_ssrc (buf, 0);
fail_unless_equals_int (gst_rtp_buffer_get_ssrc (buf), 0);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 4 + 4), 0);
/* check csrc bits */
fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 0);
ASSERT_CRITICAL (gst_rtp_buffer_get_csrc (buf, 0));
fail_unless_equals_int (data[0] & 0xf, 0);
gst_buffer_unref (buf);
/* and again, this time with CSRCs */
buf = gst_rtp_buffer_new_allocate (16, 4, 3);
fail_unless (buf != NULL);
fail_unless_equals_int (GST_BUFFER_SIZE (buf),
RTP_HEADER_LEN + 16 + 4 + 4 * 3);
data = GST_BUFFER_DATA (buf);
fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 3);
ASSERT_CRITICAL (gst_rtp_buffer_get_csrc (buf, 3));
fail_unless_equals_int (data[0] & 0xf, 3);
fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 0), 0);
fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 1), 0);
fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 2), 0);
data += RTP_HEADER_LEN; /* skip the other header stuff */
gst_rtp_buffer_set_csrc (buf, 0, 0xf7c0);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 0 * 4), 0xf7c0);
gst_rtp_buffer_set_csrc (buf, 1, 0xf7c1);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 1 * 4), 0xf7c1);
gst_rtp_buffer_set_csrc (buf, 2, 0xf7c2);
fail_unless_equals_int (GST_READ_UINT32_BE (data + 2 * 4), 0xf7c2);
ASSERT_CRITICAL (gst_rtp_buffer_set_csrc (buf, 3, 0xf123));
gst_buffer_unref (buf);
}
GST_END_TEST;
GST_START_TEST (test_rtp_buffer_set_extension_data)
{
GstBuffer *buf;
guint8 *data;
guint16 bits;
guint size;
gpointer pointer;
/* check GstRTPHeader structure alignment and packing */
buf = gst_rtp_buffer_new_allocate (4, 0, 0);
data = GST_BUFFER_DATA (buf);
/* should be impossible to set the extension data */
ASSERT_WARNING (fail_unless (gst_rtp_buffer_set_extension_data (buf, 0,
4) == FALSE));
fail_unless (gst_rtp_buffer_get_extension (buf) == FALSE);
/* should be possible to set the extension data */
fail_unless (gst_rtp_buffer_set_extension_data (buf, 270, 0) == TRUE);
fail_unless (gst_rtp_buffer_get_extension (buf) == TRUE);
gst_rtp_buffer_get_extension_data (buf, &bits, &pointer, &size);
fail_unless (bits == 270);
fail_unless (size == 0);
fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
pointer = gst_rtp_buffer_get_payload (buf);
fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
gst_buffer_unref (buf);
buf = gst_rtp_buffer_new_allocate (20, 0, 0);
data = GST_BUFFER_DATA (buf);
fail_unless (gst_rtp_buffer_get_extension (buf) == FALSE);
fail_unless (gst_rtp_buffer_set_extension_data (buf, 333, 2) == TRUE);
fail_unless (gst_rtp_buffer_get_extension (buf) == TRUE);
gst_rtp_buffer_get_extension_data (buf, &bits, &pointer, &size);
fail_unless (bits == 333);
fail_unless (size == 2);
fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
pointer = gst_rtp_buffer_get_payload (buf);
fail_unless (pointer == GST_BUFFER_DATA (buf) + 24);
gst_buffer_unref (buf);
}
GST_END_TEST;
GST_START_TEST (test_rtp_seqnum_compare)
{
#define ASSERT_COMP(a,b,c) fail_unless (gst_rtp_buffer_compare_seqnum ((guint16)a,(guint16)b) == c);
ASSERT_COMP (0xfffe, 0xfffd, -1);
ASSERT_COMP (0xffff, 0xfffe, -1);
ASSERT_COMP (0x0000, 0xffff, -1);
ASSERT_COMP (0x0001, 0x0000, -1);
ASSERT_COMP (0x0002, 0x0001, -1);
ASSERT_COMP (0xffff, 0xfffd, -2);
ASSERT_COMP (0x0000, 0xfffd, -3);
ASSERT_COMP (0x0001, 0xfffd, -4);
ASSERT_COMP (0x0002, 0xfffd, -5);
ASSERT_COMP (0x7ffe, 0x7ffd, -1);
ASSERT_COMP (0x7fff, 0x7ffe, -1);
ASSERT_COMP (0x8000, 0x7fff, -1);
ASSERT_COMP (0x8001, 0x8000, -1);
ASSERT_COMP (0x8002, 0x8001, -1);
ASSERT_COMP (0x7fff, 0x7ffd, -2);
ASSERT_COMP (0x8000, 0x7ffd, -3);
ASSERT_COMP (0x8001, 0x7ffd, -4);
ASSERT_COMP (0x8002, 0x7ffd, -5);
ASSERT_COMP (0x7ffd, 0xffff, -0x7ffe);
ASSERT_COMP (0x7ffe, 0x0000, -0x7ffe);
ASSERT_COMP (0x7fff, 0x0001, -0x7ffe);
ASSERT_COMP (0x7fff, 0x0000, -0x7fff);
ASSERT_COMP (0x8000, 0x0001, -0x7fff);
ASSERT_COMP (0x8001, 0x0002, -0x7fff);
ASSERT_COMP (0xfffd, 0x7ffe, -0x7fff);
ASSERT_COMP (0xfffe, 0x7fff, -0x7fff);
ASSERT_COMP (0xffff, 0x8000, -0x7fff);
ASSERT_COMP (0x0000, 0x8001, -0x7fff);
ASSERT_COMP (0x0001, 0x8002, -0x7fff);
ASSERT_COMP (0xfffe, 0x7ffe, -0x8000);
ASSERT_COMP (0xffff, 0x7fff, -0x8000);
ASSERT_COMP (0x0000, 0x8000, -0x8000);
ASSERT_COMP (0x0001, 0x8001, -0x8000);
ASSERT_COMP (0x7ffe, 0xfffe, -0x8000);
ASSERT_COMP (0x7fff, 0xffff, -0x8000);
ASSERT_COMP (0x8000, 0x0000, -0x8000);
ASSERT_COMP (0x8001, 0x0001, -0x8000);
ASSERT_COMP (0x0001, 0x0002, 1);
ASSERT_COMP (0x0000, 0x0001, 1);
ASSERT_COMP (0xffff, 0x0000, 1);
ASSERT_COMP (0xfffe, 0xffff, 1);
ASSERT_COMP (0xfffd, 0xfffe, 1);
ASSERT_COMP (0x0000, 0x0002, 2);
ASSERT_COMP (0xffff, 0x0002, 3);
ASSERT_COMP (0xfffe, 0x0002, 4);
ASSERT_COMP (0xfffd, 0x0002, 5);
ASSERT_COMP (0x8001, 0x8002, 1);
ASSERT_COMP (0x8000, 0x8001, 1);
ASSERT_COMP (0x7fff, 0x8000, 1);
ASSERT_COMP (0x7ffe, 0x7fff, 1);
ASSERT_COMP (0x7ffd, 0x7ffe, 1);
ASSERT_COMP (0x8000, 0x8002, 2);
ASSERT_COMP (0x7fff, 0x8002, 3);
ASSERT_COMP (0x7ffe, 0x8002, 4);
ASSERT_COMP (0x7ffd, 0x8002, 5);
ASSERT_COMP (0xfffe, 0x7ffd, 0x7fff);
ASSERT_COMP (0xffff, 0x7ffe, 0x7fff);
ASSERT_COMP (0x0000, 0x7fff, 0x7fff);
ASSERT_COMP (0x0001, 0x8000, 0x7fff);
ASSERT_COMP (0x0002, 0x8001, 0x7fff);
ASSERT_COMP (0x7ffe, 0xfffd, 0x7fff);
ASSERT_COMP (0x7fff, 0xfffe, 0x7fff);
ASSERT_COMP (0x8000, 0xffff, 0x7fff);
ASSERT_COMP (0x8001, 0x0000, 0x7fff);
ASSERT_COMP (0x8002, 0x0001, 0x7fff);
#undef ASSERT_COMP
}
GST_END_TEST;
static Suite *
rtp_suite (void)
{
Suite *s = suite_create ("rtp support library");
TCase *tc_chain = tcase_create ("general");
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_rtp_buffer);
tcase_add_test (tc_chain, test_rtp_buffer_set_extension_data);
tcase_add_test (tc_chain, test_rtp_seqnum_compare);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = rtp_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}