xref: /third_party/curl/tests/libtest/lib568.c (revision 13498266)
1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "test.h"
25
26#ifdef HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
29#ifdef HAVE_FCNTL_H
30#include <fcntl.h>
31#endif
32
33#include "memdebug.h"
34
35/* build request url */
36static char *suburl(const char *base, int i)
37{
38  return curl_maprintf("%s%.4d", base, i);
39}
40
41/*
42 * Test the Client->Server ANNOUNCE functionality (PUT style)
43 */
44int test(char *URL)
45{
46  int res;
47  CURL *curl;
48  int sdp;
49  FILE *sdpf = NULL;
50  struct_stat file_info;
51  char *stream_uri = NULL;
52  int request = 1;
53  struct curl_slist *custom_headers = NULL;
54
55  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
56    fprintf(stderr, "curl_global_init() failed\n");
57    return TEST_ERR_MAJOR_BAD;
58  }
59
60  curl = curl_easy_init();
61  if(!curl) {
62    fprintf(stderr, "curl_easy_init() failed\n");
63    curl_global_cleanup();
64    return TEST_ERR_MAJOR_BAD;
65  }
66
67  test_setopt(curl, CURLOPT_HEADERDATA, stdout);
68  test_setopt(curl, CURLOPT_WRITEDATA, stdout);
69
70  test_setopt(curl, CURLOPT_URL, URL);
71
72  stream_uri = suburl(URL, request++);
73  if(!stream_uri) {
74    res = TEST_ERR_MAJOR_BAD;
75    goto test_cleanup;
76  }
77  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
78  curl_free(stream_uri);
79  stream_uri = NULL;
80
81  sdp = open(libtest_arg2, O_RDONLY);
82  fstat(sdp, &file_info);
83  close(sdp);
84
85  sdpf = fopen(libtest_arg2, "rb");
86  if(!sdpf) {
87    fprintf(stderr, "can't open %s\n", libtest_arg2);
88    res = TEST_ERR_MAJOR_BAD;
89    goto test_cleanup;
90  }
91  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_ANNOUNCE);
92
93  test_setopt(curl, CURLOPT_READDATA, sdpf);
94  test_setopt(curl, CURLOPT_UPLOAD, 1L);
95  test_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) file_info.st_size);
96  test_setopt(curl, CURLOPT_VERBOSE, 1L);
97
98  /* Do the ANNOUNCE */
99  res = curl_easy_perform(curl);
100  if(res)
101    goto test_cleanup;
102
103  test_setopt(curl, CURLOPT_UPLOAD, 0L);
104  fclose(sdpf);
105  sdpf = NULL;
106
107  /* Make sure we can do a normal request now */
108  stream_uri = suburl(URL, request++);
109  if(!stream_uri) {
110    res = TEST_ERR_MAJOR_BAD;
111    goto test_cleanup;
112  }
113  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
114  curl_free(stream_uri);
115  stream_uri = NULL;
116
117  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
118  res = curl_easy_perform(curl);
119  if(res)
120    goto test_cleanup;
121
122  /* Now do a POST style one */
123
124  stream_uri = suburl(URL, request++);
125  if(!stream_uri) {
126    res = TEST_ERR_MAJOR_BAD;
127    goto test_cleanup;
128  }
129  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
130  curl_free(stream_uri);
131  stream_uri = NULL;
132
133  custom_headers = curl_slist_append(custom_headers,
134                                     "Content-Type: posty goodness");
135  if(!custom_headers) {
136    res = TEST_ERR_MAJOR_BAD;
137    goto test_cleanup;
138  }
139  test_setopt(curl, CURLOPT_RTSPHEADER, custom_headers);
140  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_ANNOUNCE);
141  test_setopt(curl, CURLOPT_POSTFIELDS,
142              "postyfield=postystuff&project=curl\n");
143
144  res = curl_easy_perform(curl);
145  if(res)
146    goto test_cleanup;
147
148  test_setopt(curl, CURLOPT_POSTFIELDS, NULL);
149  test_setopt(curl, CURLOPT_RTSPHEADER, NULL);
150  curl_slist_free_all(custom_headers);
151  custom_headers = NULL;
152
153  /* Make sure we can do a normal request now */
154  stream_uri = suburl(URL, request++);
155  if(!stream_uri) {
156    res = TEST_ERR_MAJOR_BAD;
157    goto test_cleanup;
158  }
159  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
160  curl_free(stream_uri);
161  stream_uri = NULL;
162
163  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS);
164  res = curl_easy_perform(curl);
165
166test_cleanup:
167
168  if(sdpf)
169    fclose(sdpf);
170
171  curl_free(stream_uri);
172
173  if(custom_headers)
174    curl_slist_free_all(custom_headers);
175
176  curl_easy_cleanup(curl);
177  curl_global_cleanup();
178
179  return res;
180}
181