<commit_before>/*****************************************************************************
 * 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>

#include "../../../lib/libvlc_internal.h"

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

#undef NDEBUG
#include <assert.h>

/*
 * Build and exec all keystore tests:
 * $ cd vlc/build-<name>/test
 * $ make test_modules_keystore
 * $ ./test_modules_keystore -a
 */

static const struct
{
    const char *psz_module;
    bool b_test_default;
} keystore_args[] =
{
    { "plaintext", true },
    /* Following keystores are tested only when asked explicitly by the tester
     * (with "-a" argv) */
    { "secret", false },
    { "kwallet", false }
};

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);
        }
    }
    vlc_keystore_release_entries(p_entries, i_entries);<commit_msg>keystore test: fix invalid free<commit_after>/*****************************************************************************
 * 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>

#include "../../../lib/libvlc_internal.h"

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

#undef NDEBUG
#include <assert.h>

/*
 * Build and exec all keystore tests:
 * $ cd vlc/build-<name>/test
 * $ make test_modules_keystore
 * $ ./test_modules_keystore -a
 */

static const struct
{
    const char *psz_module;
    bool b_test_default;
} keystore_args[] =
{
    { "plaintext", true },
    /* Following keystores are tested only when asked explicitly by the tester
     * (with "-a" argv) */
    { "secret", false },
    { "kwallet", false }
};

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);
        }
    }
    if (i_entries > 0)
        vlc_keystore_release_entries(p_entries, i_entries);
{'fraction_non_alphanumeric': 0.17398735258930098, 'fraction_numerical': 0.011451034011280123, 'mean_word_length': 3.43669446550417, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717820', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1764, 'n_words': 538}