test.c 9.76 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
/*****************************************************************************
 * test.c: test keystore module
 *****************************************************************************
 * Copyright © 2015-2016 VLC authors, VideoLAN and VideoLabs
 *
 * 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 <vlc/vlc.h>

26
#include "../../../lib/libvlc_internal.h"
Thomas Guillem's avatar
Thomas Guillem committed
27 28 29 30 31 32 33

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_modules.h>
#include <vlc_interrupt.h>
#include <vlc_keystore.h>

34
#undef NDEBUG
Thomas Guillem's avatar
Thomas Guillem committed
35 36
#include <assert.h>

37 38 39 40 41 42 43
/*
 * Build and exec all keystore tests:
 * $ cd vlc/build-<name>/test
 * $ make test_modules_keystore
 * $ ./test_modules_keystore -a
 */

Thomas Guillem's avatar
Thomas Guillem committed
44 45 46
static const struct
{
    const char *psz_module;
47
    bool b_test_default;
Thomas Guillem's avatar
Thomas Guillem committed
48 49
} keystore_args[] =
{
50
    { "memory", true },
51 52 53
    /* Following keystores are tested only when asked explicitly by the tester
     * (with "-a" argv) */
    { "secret", false },
54 55
    { "kwallet", false },
    { "keychain", false }
Thomas Guillem's avatar
Thomas Guillem committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
};

static void
values_reinit(const char * ppsz_values[KEY_MAX])
{
    VLC_KEYSTORE_VALUES_INIT(ppsz_values);
    ppsz_values[KEY_REALM] = "libVLC TEST";
}

static unsigned int
ks_find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
        const char* psz_cmp_secret)
{
    vlc_keystore_entry *p_entries;
    unsigned int i_entries = vlc_keystore_find(p_keystore, ppsz_values,
                                               &p_entries);
    for (unsigned int i = 0; i < i_entries; ++i)
    {
        vlc_keystore_entry *p_entry = &p_entries[i];
        assert(p_entry->p_secret[p_entry->i_secret_len - 1] == '\0');
        assert(strcmp((const char *)p_entry->p_secret, psz_cmp_secret) == 0);

        for (unsigned int j = 0; j < KEY_MAX; ++j)
        {
            const char *psz_value1 = ppsz_values[j];
            const char *psz_value2 = p_entry->ppsz_values[j];

            if (!psz_value1)
                continue;
            assert(psz_value2);
            assert(strcmp(psz_value1, psz_value2) == 0);
        }
    }
89 90
    if (i_entries > 0)
        vlc_keystore_release_entries(p_entries, i_entries);
Thomas Guillem's avatar
Thomas Guillem committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    return i_entries;
}

static unsigned int
ks_remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
{
    return vlc_keystore_remove(p_keystore, ppsz_values);
}

static void
ks_store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
         const uint8_t* p_secret, size_t i_secret_len)
{
    assert(vlc_keystore_store(p_keystore, ppsz_values,
                              (const uint8_t *)p_secret,
                              i_secret_len,
                              "libVLC TEST") == VLC_SUCCESS);
}

static void
111 112
test_module(const char *psz_module, bool b_test_all,
            int argc, const char * const *argv)
Thomas Guillem's avatar
Thomas Guillem committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 198 199 200 201 202 203 204 205 206 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
{
#define VALUES_INSERT(i_key, psz_value) ppsz_values[i_key] = psz_value
#define VALUES_REINIT() values_reinit(ppsz_values)
#define KS_FIND() do { \
    i_entries = ks_find(p_keystore, ppsz_values, psz_secret); \
} while (0)
#define KS_REMOVE() do { \
    i_entries = ks_remove(p_keystore, ppsz_values); \
} while(0)
#define KS_STORE() ks_store(p_keystore, ppsz_values, (const uint8_t *)psz_secret, -1)

    printf("\n== Testing %s keystore module ==\n\n", psz_module);

    printf("creating libvlc\n");
    libvlc_instance_t *p_libvlc = libvlc_new(argc, argv);
    assert(p_libvlc != NULL);

    vlc_interrupt_t *ctx = vlc_interrupt_create();
    assert(ctx != NULL);

    printf("creating %s keystore\n", psz_module);
    vlc_keystore *p_keystore = vlc_keystore_create(p_libvlc->p_libvlc_int);
    assert(p_keystore);

    const char *psz_secret = "libvlc test secret";
    unsigned int i_entries;
    const char *ppsz_values[KEY_MAX];

    printf("testing that there is no entry\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "http");
    VALUES_INSERT(KEY_SERVER, "www.example.com");
    VALUES_INSERT(KEY_PATH, "/example/example.mkv");
    VALUES_INSERT(KEY_PORT, "88");
    VALUES_INSERT(KEY_USER, "user1");
    KS_FIND();
    assert(i_entries == 0);

    printf("testing adding an entry\n");
    KS_STORE();
    KS_FIND();
    assert(i_entries == 1);

    printf("testing adding an other entry\n");
    VALUES_INSERT(KEY_USER, "user2");
    KS_FIND();
    assert(i_entries == 0);
    KS_STORE();
    KS_FIND();
    assert(i_entries == 1);

    printf("testing finding the 2 previous entries\n");
    VALUES_INSERT(KEY_USER, NULL);
    KS_FIND();
    assert(i_entries == 2);

    printf("testing that we can't store 2 duplicate entries\n");
    VALUES_INSERT(KEY_USER, "user2");
    KS_STORE();
    VALUES_INSERT(KEY_USER, NULL);
    KS_FIND();
    assert(i_entries == 2);

    printf("testing that entries are still present after a module unload\n");
    vlc_keystore_release(p_keystore);
    p_keystore = vlc_keystore_create(p_libvlc->p_libvlc_int);
    assert(p_keystore);
    KS_FIND();
    assert(i_entries == 2);

    printf("testing adding a third entry from a second running instance\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "smb");
    VALUES_INSERT(KEY_SERVER, "example");
    VALUES_INSERT(KEY_PATH, "/example.mkv");
    VALUES_INSERT(KEY_USER, "user1");
    KS_FIND();
    assert(i_entries == 0);

    vlc_keystore *old_keystore = p_keystore;
    p_keystore = vlc_keystore_create(p_libvlc->p_libvlc_int);
    assert(p_keystore);

    KS_STORE();
    KS_FIND();
    assert(i_entries == 1);

    vlc_keystore_release(p_keystore);
    p_keystore = old_keystore;
    KS_FIND();
    assert(i_entries == 1);

    printf("testing adding a fourth entry (without user/path)\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "ftp");
    VALUES_INSERT(KEY_SERVER, "example.com");
    KS_FIND();
    assert(i_entries == 0);
    KS_STORE();
    KS_FIND();
    assert(i_entries == 1);

    printf("testing finding an entry only by its protocol\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "smb");
    KS_FIND();
    assert(i_entries == 1);
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "http");
    KS_FIND();
    assert(i_entries == 2);
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "ftp");
    KS_FIND();
    assert(i_entries == 1);

    printf("testing finding all previous entries\n");
    VALUES_REINIT();
    KS_FIND();
    assert(i_entries == 4);

234 235 236 237 238
    if (b_test_all)
    {
        printf("\nPress ENTER to remove entries\n");
        getchar();
    }
Thomas Guillem's avatar
Thomas Guillem committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    printf("testing removing entries that match user => user1\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_USER, "user1");
    KS_REMOVE();
    assert(i_entries == 2);

    printf("testing removing entries that match user => user2\n");
    VALUES_INSERT(KEY_USER, "user2");
    KS_REMOVE();
    assert(i_entries == 1);

    printf("testing removing entries that match protocol => ftp\n");
    VALUES_REINIT();
    VALUES_INSERT(KEY_PROTOCOL, "ftp");
    KS_REMOVE();
    assert(i_entries == 1);

    printf("testing that all entries are deleted\n");
    VALUES_REINIT();
    KS_FIND();
    assert(i_entries == 0);

    vlc_keystore_release(p_keystore);

    vlc_interrupt_destroy(ctx);

    libvlc_release(p_libvlc);
}

int
270
main(int i_argc, char *ppsz_argv[])
Thomas Guillem's avatar
Thomas Guillem committed
271
{
272 273 274 275 276
    /* If b_test_all is true, this test could pollute the developer´s keystores */
    bool b_test_all = i_argc > 1 && strcmp(ppsz_argv[1], "-a") == 0;

    if (!b_test_all)
        alarm(3);
Thomas Guillem's avatar
Thomas Guillem committed
277 278

    setenv("VLC_PLUGIN_PATH", "../modules", 1);
279 280 281 282

    /* Create a dummy libvlc to initialize module bank, needed by module_exists */
    libvlc_instance_t *p_libvlc = libvlc_new(0, NULL);
    assert(p_libvlc != NULL);
Thomas Guillem's avatar
Thomas Guillem committed
283 284 285 286 287

    for (unsigned int i = 0; i < sizeof(keystore_args)/sizeof(*keystore_args); ++i)
    {
        const char *psz_module = keystore_args[i].psz_module;

288 289 290 291 292 293 294 295 296 297 298
        if ((b_test_all || keystore_args[i].b_test_default)
         && module_exists(psz_module))
        {
            int i_vlc_argc = 1;
            char *ppsz_vlc_argv[2] = { 0 };
            int i_tmp_fd = -1;
            char psz_tmp_path[] = "/tmp/libvlc_XXXXXX";

            assert(asprintf(&ppsz_vlc_argv[0], "--keystore=%s,none",
                   psz_module) != -1);

299
            if (strcmp(psz_module, "memory") == 0)
300 301 302 303
            {
                assert((i_tmp_fd = mkstemp(psz_tmp_path)) != -1);
                printf("plaintext tmp file: '%s'\n", psz_tmp_path);
                assert(asprintf(&ppsz_vlc_argv[1],
304
                       "--keystore-file=%s", psz_tmp_path) != -1);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
                i_vlc_argc++;
            }
            else if (strcmp(psz_module, "kwallet") == 0)
            {
                /* See TODO in kwallet.cpp, VLCKWallet::connect() */
                assert(libvlc_InternalAddIntf(p_libvlc->p_libvlc_int, "qt") == VLC_SUCCESS);
            }

            test_module(psz_module, b_test_all, i_vlc_argc,
                        (const char * const *)ppsz_vlc_argv);

            if (i_tmp_fd != -1)
            {
                close(i_tmp_fd);
                unlink(psz_tmp_path);
            }
            free(ppsz_vlc_argv[0]);
            free(ppsz_vlc_argv[1]);
        }
Thomas Guillem's avatar
Thomas Guillem committed
324
        else
325
            printf("not testing %s\n", psz_module);
Thomas Guillem's avatar
Thomas Guillem committed
326
    }
327
    libvlc_release(p_libvlc);
Thomas Guillem's avatar
Thomas Guillem committed
328 329 330

    return 0;
}