[vlc-devel] [PATCH 5/5] keystore: add unit tests
Maxime ...
mmeisson at outlook.fr
Tue Jul 30 13:01:40 CEST 2019
in order to test option handling
---
test/Makefile.am | 3 +
test/modules/keystore/test_options.c | 267 +++++++++++++++++++++++++++
2 files changed, 270 insertions(+)
create mode 100644 test/modules/keystore/test_options.c
diff --git a/test/Makefile.am b/test/Makefile.am
index 625f8d3453..abdd78a6b0 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -36,6 +36,7 @@ check_PROGRAMS = \
test_modules_packetizer_helpers \
test_modules_packetizer_hxxx \
test_modules_keystore \
+ test_modules_keystore_instantiate \
test_modules_demux_dashuri
if ENABLE_SOUT
check_PROGRAMS += test_modules_tls
@@ -140,6 +141,8 @@ test_modules_packetizer_hxxx_SOURCES = modules/packetizer/hxxx.c
test_modules_packetizer_hxxx_LDADD = $(LIBVLCCORE) $(LIBVLC)
test_modules_keystore_SOURCES = modules/keystore/test.c
test_modules_keystore_LDADD = $(LIBVLCCORE) $(LIBVLC)
+test_modules_keystore_instantiate_SOURCES = modules/keystore/test_options.c
+test_modules_keystore_instantiate_LDADD = $(LIBVLCCORE) $(LIBVLC)
test_modules_tls_SOURCES = modules/misc/tls.c
test_modules_tls_LDADD = $(LIBVLCCORE) $(LIBVLC)
test_modules_demux_dashuri_SOURCES = modules/demux/dashuri.cpp
diff --git a/test/modules/keystore/test_options.c b/test/modules/keystore/test_options.c
new file mode 100644
index 0000000000..056abaa349
--- /dev/null
+++ b/test/modules/keystore/test_options.c
@@ -0,0 +1,267 @@
+/*****************************************************************************
+ * 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_fs.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
+ */
+
+#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
+
+static void should_fail(int argc, const char * const *argv, vlc_keystore *keystore)
+{
+ VLC_UNUSED(argc);
+ VLC_UNUSED(argv);
+
+ fprintf(stderr, "testing that %s doesn't create a keystore\n", argv[1]);
+ assert(keystore == NULL);
+}
+
+static void shouldnt_fail(int argc, const char * const *argv, vlc_keystore *keystore)
+{
+ VLC_UNUSED(argc);
+ VLC_UNUSED(argv);
+ fprintf(stderr, "testing that %s creates a keystore\n", argv[1]);
+ assert(keystore != NULL);
+}
+
+static const struct
+{
+ int argc;
+ const char * const argv[3];
+ void (*cb)(int argc, const char * const *argv, vlc_keystore *);
+} tests_set[] = {
+ { .argc = 1,
+ .argv = {
+ "vlc",
+ NULL,
+ },
+ should_fail,
+ },
+ { .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with an unknown keystore type
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=garbage",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with an empty list
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=:",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with a long empty list
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=:::",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with spaces only
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore= ",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with a bad option without arg for type file
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file{gfsg}",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with a bad option with arg for type file
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file{opt=gsdgfd}",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create fails with an option that is supposed to be reserved
+ // for a file keystore
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=memory{file=%s}",
+ NULL,
+ },
+ should_fail,
+ },
+ { // Test that keystore_create works with type file with a valid arg
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file{file=%s}",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+ { // Test that keystore_create works with type memory without arg
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=memory",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+#ifdef CRYPTFILE
+ { // Test that keystore_create works with type file_crypt with a valid arg
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file_crypt{file=%s}",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+#endif
+ { // Test that keystore_create fallbacks after a unknow type
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=garbage:file{file=%s}",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+ { // Test that keystore_create fallbacks after a empty type
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=:file{file=%s}",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+ { // Test that keystore_create fallbacks after a known type that received bag args
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file{fdshfd}:memory",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+ { // Test that keystore_create fallbacks with args after a known type that received
+ // bag args
+ .argc = 2,
+ .argv = {
+ "vlc",
+ "--keystore=file{fdshfd}:file{file=%s}",
+ NULL,
+ },
+ shouldnt_fail,
+ },
+};
+
+
+int main(void)
+{
+ setenv("VLC_PLUGIN_PATH", "../modules", 1);
+
+
+ for ( size_t i = 0; i < ARRAY_LENGTH(tests_set); i++ )
+ {
+ char **argv;
+
+ argv = calloc(tests_set[i].argc + 1, sizeof(char *));
+ assert(argv != NULL);
+
+ for ( int j = 0; j < tests_set[i].argc; j++ )
+ {
+ if ( strstr(tests_set[i].argv[j], "file=%s") != NULL )
+ {
+ char tmp_path[] = "/tmp/libvlc_XXXXXX";
+ int tmp_fd = vlc_mkstemp(tmp_path);
+
+ assert(tmp_fd != -1);
+
+ asprintf(argv + j, tests_set[i].argv[j], tmp_path);
+
+ vlc_close(tmp_fd);
+ unlink(tmp_path);
+ }
+ else
+ {
+ argv[j] = strdup(tests_set[i].argv[j]);
+ }
+ assert(argv[j] != NULL);
+ }
+
+
+ libvlc_instance_t *libvlc = libvlc_new(tests_set[i].argc, argv);
+ assert(libvlc != NULL);
+
+ vlc_keystore *keystore = vlc_keystore_create(libvlc->p_libvlc_int);
+
+ tests_set[i].cb(tests_set[i].argc, argv, keystore);
+
+ for ( int j = 0; j < tests_set[i].argc; j++ )
+ free(argv[j]);
+ free(argv);
+ if ( keystore != NULL )
+ libvlc_release(libvlc);
+ }
+
+ return 0;
+}
--
2.17.1
More information about the vlc-devel
mailing list