mrl_helpers.c 3.28 KB
Newer Older
Thomas Guillem's avatar
Thomas Guillem committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*****************************************************************************
 * mrl_helpers.c: test src/input/mrl_helpers.h
 *****************************************************************************
 * Copyright (C) 2016 VLC authors and VideoLAN
 *
 * 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
 * GNU Lesser General Public License for more details.
 *
 * 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

#include <stdio.h>
#include <string.h>
#undef NDEBUG
#include <assert.h>

#include <vlc_common.h>
#include "../input/mrl_helpers.h"

#define MAX_RESULT 10

static const struct {
    const char *payload;
    const char *results[MAX_RESULT];
    const char *extra;
    bool success;
} testcase[] = {
    /* successful tests: */
    { "!/hello.zip!/goodbye.rar",
      { "hello.zip", "goodbye.rar" }, NULL, true },

    { "!/hello.zip!/goodbye.rar?t=0&s=0",
      { "hello.zip", "goodbye.rar" }, "t=0&s=0", true },

    { "!/hello.zip!/goodbye.rar?",
      { "hello.zip", "goodbye.rar" }, "", true },

    { "!/he%20%25""llo.zip!/good%2520bye.rar",
      { "he %llo.zip", "good%20bye.rar" }, NULL, true },

    { "",
      {}, NULL, true },

    { "?extra",
      {}, "?extra", true },

    /* failing tests: */

    { "!/he!llo.zip!/goodbye.rar",
      {}, NULL, false },

    { "!/hello.zip!/!",
      {}, NULL, false },
};

int main (void)
{
    for (size_t i = 0; i < ARRAY_SIZE(testcase); ++i)
    {
73
        vlc_array_t out;
Thomas Guillem's avatar
Thomas Guillem committed
74
75
76
77
78
79
80
81
82
83
84
85
86
        const char *extra = NULL;
        int ret = mrl_FragmentSplit(&out, &extra, testcase[i].payload);
        if (testcase[i].success)
        {
            assert(ret == VLC_SUCCESS);
            if (extra != NULL)
                assert(strcmp(extra, testcase[i].extra) == 0);
            else
                assert(testcase[i].extra == NULL);

            const char *p = testcase[i].payload + 2;
            for (int j = 0; testcase[i].results[j] != NULL; ++j)
            {
87
88
                assert(j < vlc_array_count(&out) && j < MAX_RESULT);
                char *res = vlc_array_item_at_index(&out, j);
Thomas Guillem's avatar
Thomas Guillem committed
89
90
91
92
93
94
95
96
97
98
99
100

                assert(strcmp(testcase[i].results[j], res) == 0);

                char *res_escaped = NULL;
                ret = mrl_EscapeFragmentIdentifier(&res_escaped, res);
                assert(ret == VLC_SUCCESS && res_escaped != NULL);
                assert(strncmp(p, res_escaped, strlen(res_escaped)) == 0);
                p += strlen(res_escaped) + 2;

                free(res_escaped);
                free(res);
            }
101
            vlc_array_clear(&out);
Thomas Guillem's avatar
Thomas Guillem committed
102
103
104
105
106
107
108
109
110
        }
        else
        {
            assert(ret != VLC_SUCCESS);
        }
    }
    return 0;
}