Verify the cache dir is correctly set with different combination of variables

This commit is contained in:
Jakub Jelen 2020-11-10 14:46:06 +01:00 committed by Frank Morgner
parent 3eae6a031c
commit d0e5d62bf5
2 changed files with 110 additions and 2 deletions

View File

@ -6,8 +6,8 @@ include $(top_srcdir)/aminclude_static.am
clean-local: code-coverage-clean
distclean-local: code-coverage-dist-clean
noinst_PROGRAMS = asn1 simpletlv
TESTS = asn1 simpletlv
noinst_PROGRAMS = asn1 simpletlv cachedir
TESTS = asn1 simpletlv cachedir
noinst_HEADERS = torture.h
@ -22,6 +22,7 @@ LDADD = $(top_builddir)/src/libopensc/libopensc.la \
asn1_SOURCES = asn1.c
simpletlv_SOURCES = simpletlv.c
cachedir_SOURCES = cachedir.c
if ENABLE_ZLIB
noinst_PROGRAMS += compression

View File

@ -0,0 +1,107 @@
/*
* cachedir.c: Test various options how cache dir is evaluated
*
* Copyright (C) 2020 Red Hat, Inc.
*
* Author: Jakub Jelen <jjelen@redhat.com>
*
* This library 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 library 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <limits.h>
#include "torture.h"
#include "libopensc/opensc.h"
static void torture_cachedir_default_empty_home(void **state)
{
sc_context_t *ctx = NULL;
char buf[PATH_MAX] = {0};
size_t buflen = sizeof(buf);
int rv;
rv = sc_establish_context(&ctx, "cachedir");
assert_int_equal(rv, SC_SUCCESS);
assert_non_null(ctx);
/* Keep configuration empty */
setenv("OPENSC_CONF", "/nonexistent", 1);
setenv("XDG_CACHE_HOME", "", 1);
setenv("HOME", "", 1);
rv = sc_get_cache_dir(ctx, buf, buflen);
assert_int_equal(rv, SC_ERROR_INTERNAL);
sc_release_context(ctx);
}
static void torture_cachedir_default_empty(void **state)
{
sc_context_t *ctx = NULL;
char buf[PATH_MAX] = {0};
size_t buflen = sizeof(buf);
int rv;
rv = sc_establish_context(&ctx, "cachedir");
assert_int_equal(rv, SC_SUCCESS);
assert_non_null(ctx);
/* Keep configuration empty */
setenv("OPENSC_CONF", "/nonexistent", 1);
setenv("XDG_CACHE_HOME", "", 1);
setenv("HOME", "/home/test", 1);
rv = sc_get_cache_dir(ctx, buf, buflen);
assert_int_equal(rv, SC_SUCCESS);
assert_string_equal(buf, "/home/test/.cache/opensc");
sc_release_context(ctx);
}
static void torture_cachedir_default_cache_home(void **state)
{
sc_context_t *ctx = NULL;
char buf[PATH_MAX] = {0};
size_t buflen = sizeof(buf);
int rv;
rv = sc_establish_context(&ctx, "cachedir");
assert_int_equal(rv, SC_SUCCESS);
assert_non_null(ctx);
/* Keep configuration empty */
setenv("OPENSC_CONF", "/nonexistent", 1);
setenv("XDG_CACHE_HOME", "/home/test2/.cache", 1);
setenv("HOME", "/home/test", 1);
rv = sc_get_cache_dir(ctx, buf, buflen);
assert_int_equal(rv, SC_SUCCESS);
assert_string_equal(buf, "/home/test2/.cache/opensc");
sc_release_context(ctx);
}
int main(void)
{
int rc;
struct CMUnitTest tests[] = {
cmocka_unit_test(torture_cachedir_default_empty_home),
cmocka_unit_test(torture_cachedir_default_empty),
cmocka_unit_test(torture_cachedir_default_cache_home),
};
rc = cmocka_run_group_tests(tests, NULL, NULL);
return rc;
}