file_test.c 10.7 KB
Newer Older
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1
/*****************************************************************************
2
 * file_test.c: HTTP file download test
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
3 4 5 6 7 8 9 10 11 12 13
 *****************************************************************************
 * Copyright (C) 2015 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program 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
14
 * GNU Lesser General Public License for more details.
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#undef NDEBUG

#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>

#include <vlc_common.h>
#include "file.h"
#include "message.h"

static const char url[] = "https://www.example.com:8443/dir/file.ext?a=b";
static const char ua[] = PACKAGE_NAME "/" PACKAGE_VERSION " (test suite)";

static const char *replies[2] = { NULL, NULL };
40
static uintmax_t offset = 0;
41
static bool etags = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
42 43 44 45 46 47 48 49 50 51 52 53

int main(void)
{
    struct vlc_http_file *f;
    char *str;

    /* Request failure test */
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    vlc_http_file_seek(f, 0);
    assert(vlc_http_file_get_status(f) < 0);
    assert(vlc_http_file_get_redirect(f) == NULL);
54
    assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
55 56 57 58 59 60 61 62 63 64 65 66
    assert(!vlc_http_file_can_seek(f));
    assert(vlc_http_file_get_type(f) == NULL);
    assert(vlc_http_file_read(f) == NULL);
    vlc_http_file_destroy(f);

    /* Non-seekable stream test */
    replies[0] = "HTTP/1.1 200 OK\r\n"
                 "ETag: \"foobar42\"\r\n"
                 "Content-Type: video/mpeg\r\n"
                 "\r\n";

    offset = 0;
67
    etags = true;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
68 69 70 71
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(vlc_http_file_get_status(f) == 200);
    assert(!vlc_http_file_can_seek(f));
72
    assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    str = vlc_http_file_get_type(f);
    assert(str != NULL && !strcmp(str, "video/mpeg"));
    free(str);

    /* Seek failure */
    replies[0] = "HTTP/1.1 200 OK\r\nETag: \"foobar42\"\r\n\r\n";

    assert(vlc_http_file_seek(f, offset = 1234) < 0);
    vlc_http_file_destroy(f);

    /* Seekable file test */
    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: bytes 0-2344/2345\r\n"
                 "ETag: W/\"foobar42\"\r\n"
87
                 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88 89 90 91 92 93 94
                 "\r\n";

    offset = 0;
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(vlc_http_file_can_seek(f));
    assert(vlc_http_file_get_size(f) == 2345);
95
    assert(vlc_http_file_read(f) == NULL);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
96 97 98 99 100

    /* Seek success */
    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: bytes 1234-3455/3456\r\n"
                 "ETag: W/\"foobar42\"\r\n"
101
                 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102 103 104 105
                 "\r\n";
    assert(vlc_http_file_seek(f, offset = 1234) == 0);
    assert(vlc_http_file_can_seek(f));
    assert(vlc_http_file_get_size(f) == 3456);
106
    assert(vlc_http_file_read(f) == NULL);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
107 108 109 110 111

    /* Seek too far */
    replies[0] = "HTTP/1.1 416 Range Not Satisfiable\r\n"
                 "Content-Range: bytes */4567\r\n"
                 "ETag: W/\"foobar42\"\r\n"
112
                 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                 "\r\n";
    vlc_http_file_seek(f, offset = 5678);
    assert(vlc_http_file_can_seek(f));
    assert(vlc_http_file_get_size(f) == 4567);
    assert(vlc_http_file_read(f) == NULL);
    vlc_http_file_destroy(f);

    /* Redirect */
    replies[0] = "HTTP/1.1 301 Permanent Redirect\r\n"
                 "Location: /somewhere/else/#here\r\n"
                 "\r\n";

    offset = 0;
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(!vlc_http_file_can_seek(f));
129
    assert(vlc_http_file_get_size(f) == (uintmax_t)-1);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    str = vlc_http_file_get_redirect(f);
    assert(str != NULL
        && !strcmp(str, "https://www.example.com:8443/somewhere/else/"));
    free(str);
    vlc_http_file_destroy(f);

    /* Continuation */
    replies[0] = "HTTP/1.1 100 Standby\r\n"
                 "\r\n";
    replies[1] = "HTTP/1.1 200 OK\r\n"
                 "Content-Length: 9999\r\n"
                 "\r\n";
    offset = 0;
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(vlc_http_file_get_size(f) == 9999);
    assert(vlc_http_file_get_redirect(f) == NULL);
    vlc_http_file_destroy(f);

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    /* No entity tags */
    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: bytes 0-2344/2345\r\n"
                 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
                 "\r\n";

    offset = 0;
    etags = false;
    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(vlc_http_file_can_seek(f));

    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: bytes 1234-3455/3456\r\n"
                 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
                 "\r\n";
    assert(vlc_http_file_seek(f, offset = 1234) == 0);
    vlc_http_file_destroy(f);

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    /* Invalid responses */
    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Type: multipart/byteranges\r\n"
                 "\r\n";
    offset = 0;

    f = vlc_http_file_create(NULL, url, ua, NULL);
    assert(f != NULL);
    assert(vlc_http_file_get_size(f) == (uintmax_t)-1);

    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: seconds 60-120/180\r\n"
                 "\r\n";
    assert(vlc_http_file_seek(f, 0) == -1);

    /* Incomplete range */
    replies[0] = "HTTP/1.1 206 Partial Content\r\n"
                 "Content-Range: bytes 0-1233/*\r\n"
                 "\r\n";
    assert(vlc_http_file_seek(f, 0) == 0);
    assert(vlc_http_file_get_size(f) == 1234);

    /* Extraneous range */
    replies[0] = "HTTP/1.1 200 OK\r\n"
                 "Content-Range: bytes 0-1233/1234\r\n"
                 "\r\n";
    assert(vlc_http_file_seek(f, 0) == 0);
    assert(vlc_http_file_get_size(f) == (uintmax_t)-1);

    vlc_http_file_destroy(f);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
198 199 200 201

    /* Dummy API calls */
    f = vlc_http_file_create(NULL, "ftp://localhost/foo", NULL, NULL);
    assert(f == NULL);
202 203 204 205 206
    f = vlc_http_file_create(NULL, "/foo", NULL, NULL);
    assert(f == NULL);
    f = vlc_http_file_create(NULL, "http://www.example.com", NULL, NULL);
    assert(f != NULL);
    vlc_http_file_destroy(f);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    return 0;
}

/* Callback for vlc_http_msg_h2_frame */
#include "h2frame.h"

struct vlc_h2_frame *
vlc_h2_frame_headers(uint_fast32_t id, uint_fast32_t mtu, bool eos,
                     unsigned count, const char *const tab[][2])
{
    (void) id; (void) mtu; (void) count, (void) tab;
    assert(!eos);
    return NULL;
}

/* Callback for the HTTP request */
#include "connmgr.h"

static struct vlc_http_stream stream;

static struct vlc_http_msg *stream_read_headers(struct vlc_http_stream *s)
{
    assert(s == &stream);

    /* return next reply */
    struct vlc_http_msg *m = NULL;
    const char *answer = replies[0];

    if (answer != NULL)
    {
        m = vlc_http_msg_headers(answer);
        assert(m != NULL);
        vlc_http_msg_attach(m, s);
    }

    memmove(replies, replies + 1, sizeof (replies) - sizeof (replies[0]));
    replies[(sizeof (replies) / sizeof (replies[0])) - 1] = NULL;

    return m;
}

static struct block_t *stream_read(struct vlc_http_stream *s)
{
    assert(s == &stream);
    return NULL;
}

255
static void stream_close(struct vlc_http_stream *s, bool abort)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
256 257
{
    assert(s == &stream);
258
    assert(!abort);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
259 260 261 262 263 264 265 266 267 268 269
}

static const struct vlc_http_stream_cbs stream_callbacks =
{
    stream_read_headers,
    stream_read,
    stream_close,
};

static struct vlc_http_stream stream = { &stream_callbacks };

270 271 272
struct vlc_http_msg *vlc_http_mgr_request(struct vlc_http_mgr *mgr, bool https,
                                          const char *host, unsigned port,
                                          const struct vlc_http_msg *req)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
273 274 275 276
{
    const char *str;
    char *end;

277
    assert(https);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    assert(mgr == NULL);
    assert(!strcmp(host, "www.example.com"));
    assert(port == 8443);

    str = vlc_http_msg_get_method(req);
    assert(!strcmp(str, "GET"));
    str = vlc_http_msg_get_scheme(req);
    assert(!strcmp(str, "https"));
    str = vlc_http_msg_get_authority(req);
    assert(!strcmp(str, "www.example.com:8443"));
    str = vlc_http_msg_get_path(req);
    assert(!strcmp(str, "/dir/file.ext?a=b"));
    str = vlc_http_msg_get_agent(req);
    assert(!strcmp(str, ua));
    str = vlc_http_msg_get_header(req, "Referer");
    assert(str == NULL);
    str = vlc_http_msg_get_header(req, "Accept");
    assert(str == NULL || strstr(str, "*/*") != NULL);
    str = vlc_http_msg_get_header(req, "Accept-Language");
    assert(str == NULL || strstr(str, "*") != NULL);

    str = vlc_http_msg_get_header(req, "Range");
    assert(str != NULL && !strncmp(str, "bytes=", 6)
        && strtoul(str + 6, &end, 10) == offset && *end == '-');

303
    time_t mtime = vlc_http_msg_get_time(req, "If-Unmodified-Since");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
304
    str = vlc_http_msg_get_header(req, "If-Match");
305 306 307 308 309 310 311 312 313

    if (etags)
    {
        if (offset != 0)
            assert(str != NULL && !strcmp(str, "\"foobar42\""));
        else
        if (str != NULL)
            assert(strcmp(str, "*") || strcmp(str, "\"foobar42\""));
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
314
    else
315 316 317 318
    {
        if (offset != 0)
            assert(mtime == 1382386402);
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
319 320 321

    return vlc_http_stream_read_headers(&stream);
}
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

int vlc_http_mgr_send_cookies(struct vlc_http_mgr *mgr, bool https,
                              const char *host, const char *path,
                              struct vlc_http_msg *req)
{
    assert(https);
    assert(!strcmp(host, "www.example.com"));
    assert(!strcmp(path, "/dir/file.ext?a=b"));
    assert(mgr == NULL);
    (void) req;
    return 0;
}

void vlc_http_mgr_recv_cookies(struct vlc_http_mgr *mgr, bool https,
                               const char *host, const char *path,
                               const struct vlc_http_msg *resp)
{
    assert(https);
    assert(!strcmp(host, "www.example.com"));
    assert(!strcmp(path, "/dir/file.ext?a=b"));
    assert(mgr == NULL);
    (void) resp;
}