test: add test for session in options request
[platform/upstream/gstreamer.git] / tests / check / gst / client.c
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 #include <rtsp-client.h>
23
24 static gint cseq;
25
26 static gboolean
27 test_option_response_200 (GstRTSPClient * client, GstRTSPMessage * response,
28     gboolean close, gpointer user_data)
29 {
30   GstRTSPStatusCode code;
31   const gchar *reason;
32   GstRTSPVersion version;
33   gchar *str;
34   GstRTSPMethod methods;
35
36   fail_unless (gst_rtsp_message_get_type (response) ==
37       GST_RTSP_MESSAGE_RESPONSE);
38
39   gst_rtsp_message_dump (response);
40   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
41           &version)
42       == GST_RTSP_OK);
43   fail_unless (code == GST_RTSP_STS_OK);
44   fail_unless (g_str_equal (reason, "OK"));
45   fail_unless (version == GST_RTSP_VERSION_1_0);
46
47   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
48           0) == GST_RTSP_OK);
49   fail_unless (atoi (str) == cseq++);
50
51   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_PUBLIC, &str,
52           0) == GST_RTSP_OK);
53
54   methods = gst_rtsp_options_from_text (str);
55   fail_if (methods == 0);
56   fail_unless (methods == (GST_RTSP_DESCRIBE |
57           GST_RTSP_OPTIONS |
58           GST_RTSP_PAUSE |
59           GST_RTSP_PLAY |
60           GST_RTSP_SETUP |
61           GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN));
62
63   return TRUE;
64 }
65
66 static gboolean
67 test_option_response_454 (GstRTSPClient * client, GstRTSPMessage * response,
68     gboolean close, gpointer user_data)
69 {
70   GstRTSPStatusCode code;
71   const gchar *reason;
72   GstRTSPVersion version;
73
74   fail_unless (gst_rtsp_message_get_type (response) ==
75       GST_RTSP_MESSAGE_RESPONSE);
76   gst_rtsp_message_dump (response);
77
78   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
79           &version)
80       == GST_RTSP_OK);
81   fail_unless (code == GST_RTSP_STS_SESSION_NOT_FOUND);
82   fail_unless (g_str_equal (reason, "Session Not Found"));
83   fail_unless (version == GST_RTSP_VERSION_1_0);
84
85   return TRUE;
86 }
87
88 GST_START_TEST (test_options)
89 {
90   GstRTSPClient *client;
91   GstRTSPMessage request = { 0, };
92   gchar *str;
93
94   client = gst_rtsp_client_new ();
95
96   /* simple OPTIONS */
97   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
98           "rtsp://localhost/test") == GST_RTSP_OK);
99   str = g_strdup_printf ("%d", cseq);
100   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
101   g_free (str);
102
103   gst_rtsp_client_set_send_func (client, test_option_response_200, NULL, NULL);
104   gst_rtsp_message_dump (&request);
105   fail_unless (gst_rtsp_client_handle_message (client,
106           &request) == GST_RTSP_OK);
107   gst_rtsp_message_unset (&request);
108
109   /* OPTIONS with unknown session id */
110   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
111           "rtsp://localhost/test") == GST_RTSP_OK);
112   str = g_strdup_printf ("%d", cseq);
113   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
114   g_free (str);
115   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SESSION, "foobar");
116
117   gst_rtsp_client_set_send_func (client, test_option_response_454, NULL, NULL);
118   gst_rtsp_message_dump (&request);
119   fail_unless (gst_rtsp_client_handle_message (client,
120           &request) == GST_RTSP_OK);
121
122   gst_rtsp_message_unset (&request);
123
124   g_object_unref (client);
125 }
126
127 GST_END_TEST;
128
129 static Suite *
130 rtspclient_suite (void)
131 {
132   Suite *s = suite_create ("rtspclient");
133   TCase *tc = tcase_create ("general");
134
135   suite_add_tcase (s, tc);
136   tcase_set_timeout (tc, 20);
137   tcase_add_test (tc, test_options);
138
139   return s;
140 }
141
142 GST_CHECK_MAIN (rtspclient);