1 #include "parser.h"
2 #include <gst/check/gstcheck.h>
3 #include <gst/check/gstharness.h>
4 
5 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
6     GST_PAD_SRC,
7     GST_PAD_ALWAYS,
8     GST_STATIC_CAPS ("raw/x-pcap"));
9 
10 static GstStaticPadTemplate sinktemplate_rtp = GST_STATIC_PAD_TEMPLATE ("sink",
11     GST_PAD_SINK,
12     GST_PAD_ALWAYS,
13     GST_STATIC_CAPS ("application/x-rtp"));
14 
15 static guint8 pcap_header[] = {
16   0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
17   0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
18 };
19 
20 static const guint pcap_frame_with_eth_padding_offset = 16 + 14 + 20 + 8;
21 static guint8 pcap_frame_with_eth_padding[] = {
22   0x5f, 0x12, 0x4e, 0x54, 0x57, 0x70, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
23   0x3c, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x29, 0xa6, 0x13, 0x41, 0x00, 0x0c,
24   0x29, 0xb2, 0x93, 0x7d, 0x08, 0x00, 0x45, 0x00, 0x00, 0x2c, 0x00, 0x00,
25   0x40, 0x00, 0x32, 0x11, 0x25, 0xb9, 0x52, 0xc5, 0x4d, 0xd6, 0xb9, 0x23,
26   0xc9, 0x49, 0x44, 0x66, 0x9f, 0xf2, 0x00, 0x18, 0x75, 0xe8, 0x80, 0xe3,
27   0x7c, 0xca, 0x79, 0xba, 0x09, 0xc0, 0x70, 0x6e, 0x8b, 0x33, 0x05, 0x0a,
28   0x00, 0xa0, 0x00, 0x00
29 };
30 
31 static gboolean
verify_buffer(buffer_verify_data_s * vdata,GstBuffer * buffer)32 verify_buffer (buffer_verify_data_s * vdata, GstBuffer * buffer)
33 {
34   guint offset = 0;
35   guint size = 0;
36 
37   if (vdata->data_to_verify == pcap_frame_with_eth_padding) {
38     offset = pcap_frame_with_eth_padding_offset;
39     size = sizeof (pcap_frame_with_eth_padding) -
40         pcap_frame_with_eth_padding_offset - 2;
41   }
42 
43   fail_unless_equals_int (gst_buffer_get_size (buffer), size);
44   fail_unless (gst_buffer_memcmp (buffer, 0, vdata->data_to_verify + offset,
45           size) == 0);
46 
47   return TRUE;
48 }
49 
50 static GstElement *
setup_element(const gchar * desc)51 setup_element (const gchar * desc)
52 {
53   GstElement *element;
54   GstCaps *caps;
55 
56   (void) desc;
57 
58   caps = gst_caps_from_string ("application/x-rtp");
59   element = gst_check_setup_element ("pcapparse");
60   g_object_set (G_OBJECT (element), "caps", caps, NULL);
61   gst_caps_unref (caps);
62 
63   return element;
64 }
65 
GST_START_TEST(test_parse_frames_with_eth_padding)66 GST_START_TEST (test_parse_frames_with_eth_padding)
67 {
68   gst_parser_test_split (pcap_frame_with_eth_padding,
69       sizeof (pcap_frame_with_eth_padding));
70 }
71 
72 GST_END_TEST;
73 
74 static const guint8 zerosize_data[] = {
75   0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
76   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77   0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00,
78   0xd3, 0xff, 0x7a, 0x56, 0xbb, 0xd8, 0x0e, 0x00,
79   0x2a, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00,
80   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81   0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00,
82   0x00, 0x1c, 0x06, 0xe7, 0x40, 0x00, 0x40, 0x11,
83   0x35, 0xe8, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00,
84   0x00, 0x01, 0xd2, 0xa3, 0x13, 0x8c, 0x00, 0x08,
85   0xfe, 0x1b
86 };
87 
GST_START_TEST(test_parse_zerosize_frames)88 GST_START_TEST (test_parse_zerosize_frames)
89 {
90   GstBuffer *in_buf, *out_buf;
91   GstHarness *h;
92   gsize data_size;
93 
94   h = gst_harness_new ("pcapparse");
95 
96   gst_harness_set_src_caps_str (h, "raw/x-pcap");
97 
98   data_size = sizeof (zerosize_data);
99 
100   in_buf = gst_buffer_new_wrapped (g_memdup (zerosize_data, data_size),
101       data_size);
102 
103   gst_harness_push (h, in_buf);
104   gst_harness_play (h);
105   gst_harness_push_event (h, gst_event_new_eos ());
106 
107   /* check that a buffer comes out and that it is 0 bytes in size */
108   out_buf = gst_harness_pull (h);
109 
110   fail_unless (gst_buffer_get_size (out_buf) == 0);
111 
112   gst_buffer_unref (out_buf);
113   gst_harness_teardown (h);
114 }
115 
116 GST_END_TEST;
117 
118 static Suite *
pcapparse_suite(void)119 pcapparse_suite (void)
120 {
121   Suite *s = suite_create ("pcapparse");
122   TCase *tc_chain = tcase_create ("general");
123 
124   ctx_factory = "pcapparse";
125   ctx_setup = setup_element;
126   ctx_sink_template = &sinktemplate_rtp;
127   ctx_src_template = &srctemplate;
128   ctx_headers[0].data = pcap_header;
129   ctx_headers[0].size = sizeof (pcap_header);
130   ctx_no_metadata = TRUE;
131   ctx_verify_buffer = verify_buffer;
132 
133   suite_add_tcase (s, tc_chain);
134   tcase_add_test (tc_chain, test_parse_frames_with_eth_padding);
135   tcase_add_test (tc_chain, test_parse_zerosize_frames);
136 
137   return s;
138 }
139 
140 GST_CHECK_MAIN (pcapparse);
141