contrib: soxr: enable by default
[vlc.git] / modules / access / http / file_test.c
blob64fab0711bc41ea7f60d86970be98b557cad358d
1 /*****************************************************************************
2 * file_test.c: HTTP file download test
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #undef NDEBUG
27 #include <assert.h>
28 #include <inttypes.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <vlc_common.h>
33 #include <vlc_http.h>
34 #include "resource.h"
35 #include "file.h"
36 #include "message.h"
38 static const char url[] = "https://www.example.com:8443/dir/file.ext?a=b";
39 static const char url_http[] = "http://www.example.com:8443/dir/file.ext?a=b";
40 static const char url_mmsh[] = "mmsh://www.example.com:8443/dir/file.ext?a=b";
41 static const char url_icyx[] = "icyx://www.example.com:8443/dir/file.ext?a=b";
42 static const char ua[] = PACKAGE_NAME "/" PACKAGE_VERSION " (test suite)";
44 static const char *replies[2] = { NULL, NULL };
45 static uintmax_t offset = 0;
46 static bool secure = true;
47 static bool etags = false;
48 static int lang = -1;
50 static vlc_http_cookie_jar_t *jar;
52 int main(void)
54 struct vlc_http_resource *f;
55 char *str;
57 jar = vlc_http_cookies_new();
59 /* Request failure test */
60 f = vlc_http_file_create(NULL, url, ua, NULL);
61 assert(f != NULL);
62 vlc_http_res_set_login(f, NULL, NULL);
63 vlc_http_res_set_login(f, "john", NULL);
64 vlc_http_res_set_login(f, NULL, NULL);
65 vlc_http_res_set_login(f, "john", "secret");
66 vlc_http_res_set_login(f, NULL, NULL);
67 vlc_http_file_seek(f, 0);
68 assert(vlc_http_file_get_status(f) < 0);
69 assert(vlc_http_file_get_redirect(f) == NULL);
70 assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
71 assert(!vlc_http_file_can_seek(f));
72 assert(vlc_http_file_get_type(f) == NULL);
73 assert(vlc_http_file_read(f) == NULL);
74 vlc_http_res_destroy(f);
76 /* Non-seekable stream test */
77 replies[0] = "HTTP/1.1 200 OK\r\n"
78 "ETag: \"foobar42\"\r\n"
79 "Content-Type: video/mpeg\r\n"
80 "\r\n";
82 offset = 0;
83 etags = true;
84 f = vlc_http_file_create(NULL, url, ua, NULL);
85 assert(f != NULL);
86 assert(vlc_http_file_get_status(f) == 200);
87 assert(!vlc_http_file_can_seek(f));
88 assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
89 str = vlc_http_file_get_type(f);
90 assert(str != NULL && !strcmp(str, "video/mpeg"));
91 free(str);
93 /* Seek failure */
94 replies[0] = "HTTP/1.1 200 OK\r\nETag: \"foobar42\"\r\n\r\n";
96 assert(vlc_http_file_seek(f, offset = 1234) < 0);
97 vlc_http_file_destroy(f);
99 /* Seekable file test */
100 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
101 "Content-Range: bytes 0-2344/2345\r\n"
102 "ETag: W/\"foobar42\"\r\n"
103 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
104 "\r\n";
106 offset = 0;
107 f = vlc_http_file_create(NULL, url, ua, NULL);
108 assert(f != NULL);
109 assert(vlc_http_file_can_seek(f));
110 assert(vlc_http_file_get_size(f) == 2345);
111 assert(vlc_http_file_read(f) == NULL);
113 /* Seek success */
114 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
115 "Content-Range: bytes 1234-3455/3456\r\n"
116 "ETag: W/\"foobar42\"\r\n"
117 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
118 "\r\n";
119 assert(vlc_http_file_seek(f, offset = 1234) == 0);
120 assert(vlc_http_file_can_seek(f));
121 assert(vlc_http_file_get_size(f) == 3456);
122 assert(vlc_http_file_read(f) == NULL);
124 /* Seek too far */
125 replies[0] = "HTTP/1.1 416 Range Not Satisfiable\r\n"
126 "Content-Range: bytes */4567\r\n"
127 "ETag: W/\"foobar42\"\r\n"
128 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
129 "\r\n";
130 vlc_http_file_seek(f, offset = 5678);
131 assert(vlc_http_file_can_seek(f));
132 assert(vlc_http_file_get_size(f) == 4567);
133 assert(vlc_http_file_read(f) == NULL);
134 vlc_http_file_destroy(f);
136 /* Redirect */
137 replies[0] = "HTTP/1.1 301 Permanent Redirect\r\n"
138 "Location: /somewhere/else/#here\r\n"
139 "\r\n";
141 offset = 0;
142 f = vlc_http_file_create(NULL, url, ua, NULL);
143 assert(f != NULL);
144 assert(!vlc_http_file_can_seek(f));
145 assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
146 str = vlc_http_file_get_redirect(f);
147 assert(str != NULL);
148 assert(!strcmp(str, "https://www.example.com:8443/somewhere/else/"));
149 free(str);
150 vlc_http_file_destroy(f);
152 /* Continuation */
153 replies[0] = "HTTP/1.1 100 Standby\r\n"
154 "\r\n";
155 replies[1] = "HTTP/1.1 200 OK\r\n"
156 "Content-Length: 9999\r\n"
157 "\r\n";
158 offset = 0;
159 f = vlc_http_file_create(NULL, url, ua, NULL);
160 assert(f != NULL);
161 assert(vlc_http_file_get_size(f) == 9999);
162 assert(vlc_http_file_get_redirect(f) == NULL);
163 vlc_http_file_destroy(f);
165 /* No entity tags */
166 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
167 "Content-Range: bytes 0-2344/2345\r\n"
168 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
169 "\r\n";
171 offset = 0;
172 etags = false;
173 f = vlc_http_file_create(NULL, url, ua, NULL);
174 assert(f != NULL);
175 assert(vlc_http_file_can_seek(f));
177 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
178 "Content-Range: bytes 1234-3455/3456\r\n"
179 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
180 "\r\n";
181 assert(vlc_http_file_seek(f, offset = 1234) == 0);
182 vlc_http_file_destroy(f);
184 /* Invalid responses */
185 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
186 "Content-Type: multipart/byteranges\r\n"
187 "\r\n";
188 offset = 0;
190 f = vlc_http_file_create(NULL, url, ua, NULL);
191 assert(f != NULL);
192 assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
194 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
195 "Content-Range: seconds 60-120/180\r\n"
196 "\r\n";
197 assert(vlc_http_file_seek(f, 0) == -1);
199 /* Incomplete range */
200 replies[0] = "HTTP/1.1 206 Partial Content\r\n"
201 "Content-Range: bytes 0-1233/*\r\n"
202 "\r\n";
203 assert(vlc_http_file_seek(f, 0) == 0);
204 assert(vlc_http_file_get_size(f) == 1234);
206 /* Extraneous range */
207 replies[0] = "HTTP/1.1 200 OK\r\n"
208 "Content-Range: bytes 0-1233/1234\r\n"
209 "\r\n";
210 assert(vlc_http_file_seek(f, 0) == 0);
211 assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
213 /* Non-negotiable language */
214 replies[0] = "HTTP/1.1 406 Not Acceptable\r\n"
215 "\r\n";
216 replies[1] = "HTTP/1.1 206 OK\r\n"
217 "Content-Range: bytes 0-1/2\r\n"
218 "\r\n";
219 lang = 1;
220 assert(vlc_http_file_seek(f, 0) == 0);
221 assert(vlc_http_file_can_seek(f));
222 assert(vlc_http_file_get_size(f) == 2);
224 /* Protocol redirect hacks - not over TLS */
225 replies[0] = "HTTP/1.1 200 OK\r\n"
226 "Pragma: features\r\n"
227 "\r\n";
228 assert(vlc_http_file_seek(f, 0) == 0);
229 assert(vlc_http_file_get_redirect(f) == NULL);
231 replies[0] = "HTTP/1.1 200 OK\r\n"
232 "Icy-Name:CraptasticRadio\r\n"
233 "\r\n";
234 assert(vlc_http_file_seek(f, 0) == 0);
235 assert(vlc_http_file_get_redirect(f) == NULL);
237 vlc_http_file_destroy(f);
239 secure = false;
240 lang = -1;
241 f = vlc_http_file_create(NULL, url_http, ua, NULL);
242 assert(f != NULL);
244 /* Protocol redirect hacks - over insecure HTTP */
245 replies[0] = "HTTP/1.1 200 OK\r\n"
246 "Pragma: features\r\n"
247 "\r\n";
248 str = vlc_http_file_get_redirect(f);
249 assert(str != NULL && strcmp(str, url_mmsh) == 0);
250 free(str);
252 replies[0] = "HTTP/1.1 200 OK\r\n"
253 "Icy-Name:CraptasticRadio\r\n"
254 "\r\n";
255 vlc_http_file_seek(f, 0);
256 str = vlc_http_file_get_redirect(f);
257 assert(str != NULL && strcmp(str, url_icyx) == 0);
258 free(str);
260 vlc_http_file_destroy(f);
262 /* Dummy API calls */
263 f = vlc_http_file_create(NULL, "ftp://localhost/foo", NULL, NULL);
264 assert(f == NULL);
265 f = vlc_http_file_create(NULL, "/foo", NULL, NULL);
266 assert(f == NULL);
267 f = vlc_http_file_create(NULL, "http://www.example.com", NULL, NULL);
268 assert(f != NULL);
269 vlc_http_file_destroy(f);
271 vlc_http_cookies_destroy(jar);
272 return 0;
275 /* Callback for vlc_http_msg_h2_frame */
276 #include "h2frame.h"
278 struct vlc_h2_frame *
279 vlc_h2_frame_headers(uint_fast32_t id, uint_fast32_t mtu, bool eos,
280 unsigned count, const char *const tab[][2])
282 (void) id; (void) mtu; (void) count, (void) tab;
283 assert(!eos);
284 return NULL;
287 /* Callback for the HTTP request */
288 #include "connmgr.h"
290 static struct vlc_http_stream stream;
292 static struct vlc_http_msg *stream_read_headers(struct vlc_http_stream *s)
294 assert(s == &stream);
296 /* return next reply */
297 struct vlc_http_msg *m = NULL;
298 const char *answer = replies[0];
300 if (answer != NULL)
302 m = vlc_http_msg_headers(answer);
303 assert(m != NULL);
304 vlc_http_msg_attach(m, s);
307 memmove(replies, replies + 1, sizeof (replies) - sizeof (replies[0]));
308 replies[(sizeof (replies) / sizeof (replies[0])) - 1] = NULL;
310 return m;
313 static struct block_t *stream_read(struct vlc_http_stream *s)
315 assert(s == &stream);
316 return NULL;
319 static void stream_close(struct vlc_http_stream *s, bool abort)
321 assert(s == &stream);
322 assert(!abort);
325 static const struct vlc_http_stream_cbs stream_callbacks =
327 stream_read_headers,
328 stream_read,
329 stream_close,
332 static struct vlc_http_stream stream = { &stream_callbacks };
334 struct vlc_http_msg *vlc_http_mgr_request(struct vlc_http_mgr *mgr, bool https,
335 const char *host, unsigned port,
336 const struct vlc_http_msg *req)
338 const char *str;
339 char *end;
341 assert(https == secure);
342 assert(mgr == NULL);
343 assert(!strcmp(host, "www.example.com"));
344 assert(port == 8443);
346 str = vlc_http_msg_get_method(req);
347 assert(!strcmp(str, "GET"));
348 str = vlc_http_msg_get_scheme(req);
349 assert(!strcmp(str, secure ? "https" : "http"));
350 str = vlc_http_msg_get_authority(req);
351 assert(!strcmp(str, "www.example.com:8443"));
352 str = vlc_http_msg_get_path(req);
353 assert(!strcmp(str, "/dir/file.ext?a=b"));
354 str = vlc_http_msg_get_agent(req);
355 assert(!strcmp(str, ua));
356 str = vlc_http_msg_get_header(req, "Referer");
357 assert(str == NULL);
358 str = vlc_http_msg_get_header(req, "Accept");
359 assert(str == NULL || strstr(str, "*/*") != NULL);
361 str = vlc_http_msg_get_header(req, "Accept-Language");
362 /* This test case does not call setlocale(), so en_US can be assumed. */
363 if (lang != 0)
365 assert(str != NULL && strncmp(str, "en_US", 5) == 0);
366 if (lang > 0)
367 lang--;
369 else
370 assert(str == NULL);
372 str = vlc_http_msg_get_header(req, "Range");
373 assert(str != NULL && !strncmp(str, "bytes=", 6)
374 && strtoul(str + 6, &end, 10) == offset && *end == '-');
376 time_t mtime = vlc_http_msg_get_time(req, "If-Unmodified-Since");
377 str = vlc_http_msg_get_header(req, "If-Match");
379 if (etags)
381 if (offset != 0)
382 assert(str != NULL && !strcmp(str, "\"foobar42\""));
383 else
384 if (str != NULL)
385 assert(strcmp(str, "*") || strcmp(str, "\"foobar42\""));
387 else
389 if (offset != 0)
390 assert(mtime == 1382386402);
393 return vlc_http_msg_get_initial(&stream);
396 struct vlc_http_cookie_jar_t *vlc_http_mgr_get_jar(struct vlc_http_mgr *mgr)
398 assert(mgr == NULL);
399 return jar;