summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2021-05-21 17:41:31 +0100
committerAlexander Larsson <alexander.larsson@gmail.com>2021-05-25 11:11:03 +0200
commit0a44aaff8d2adff52b0d322cb25f52b241962de1 (patch)
treefbde2e84606841b2abd4617f58ba320bb2b0cb8c
parent10a4cd7e08cacefe1213b4fe7f1ff10e8074a13c (diff)
downloadflatpak-0a44aaff8d2adff52b0d322cb25f52b241962de1.tar.gz
tests: Test environment block parsing
Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--tests/Makefile.am.inc5
-rw-r--r--tests/test-context.c128
2 files changed, 133 insertions, 0 deletions
diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc
index 4232386b..c9a73dd1 100644
--- a/tests/Makefile.am.inc
+++ b/tests/Makefile.am.inc
@@ -82,6 +82,10 @@ testcommon_LDADD = \
$(NULL)
testcommon_SOURCES = tests/testcommon.c
+test_context_CFLAGS = $(testcommon_CFLAGS)
+test_context_LDADD = $(testcommon_LDADD) libtestlib.la
+test_context_SOURCES = tests/test-context.c
+
test_exports_CFLAGS = $(testcommon_CFLAGS)
test_exports_LDADD = $(testcommon_LDADD) libtestlib.la
test_exports_SOURCES = tests/test-exports.c
@@ -281,6 +285,7 @@ dist_test_scripts = ${TEST_MATRIX_DIST}
dist_installed_test_extra_scripts += ${TEST_MATRIX_EXTRA_DIST}
test_programs = \
+ test-context \
test-exports \
test-instance \
testcommon \
diff --git a/tests/test-context.c b/tests/test-context.c
new file mode 100644
index 00000000..b4626288
--- /dev/null
+++ b/tests/test-context.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright © 2021 Collabora Ltd.
+ *
+ * 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 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 Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include "flatpak.h"
+#include "flatpak-context-private.h"
+
+#include "tests/testlib.h"
+
+static void
+test_context_env (void)
+{
+ g_autoptr(FlatpakContext) context = flatpak_context_new ();
+ g_autoptr(GError) error = NULL;
+ gboolean ok;
+ const char env[] = "ONE=one\0TWO=two\0THREE=three\0EMPTY=\0X=x";
+
+ ok = flatpak_context_parse_env_block (context, env, sizeof (env), &error);
+ g_assert_no_error (error);
+ g_assert_true (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "ONE"), ==, "one");
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "EMPTY"), ==, "");
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "nope"), ==, NULL);
+
+ ok = flatpak_context_parse_env_block (context,
+ "FOO=barnstorming past the end",
+ strlen ("FOO=bar"),
+ &error);
+ g_assert_no_error (error);
+ g_assert_true (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "FOO"), ==, "bar");
+
+ ok = flatpak_context_parse_env_block (context,
+ "BAD=barnstorming past the end",
+ strlen ("BA"),
+ &error);
+ g_assert_nonnull (error);
+ g_test_message ("Got error as expected: %s #%d: %s",
+ g_quark_to_string (error->domain), error->code,
+ error->message);
+ g_assert_false (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "BA"), ==, NULL);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "BAD"), ==, NULL);
+ g_clear_error (&error);
+
+ ok = flatpak_context_parse_env_block (context, "=x", strlen ("=x"), &error);
+ g_assert_nonnull (error);
+ g_test_message ("Got error as expected: %s #%d: %s",
+ g_quark_to_string (error->domain), error->code,
+ error->message);
+ g_assert_false (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, ""), ==, NULL);
+ g_clear_error (&error);
+
+ ok = flatpak_context_parse_env_block (context, "\0", 1, &error);
+ g_assert_nonnull (error);
+ g_test_message ("Got error as expected: %s #%d: %s",
+ g_quark_to_string (error->domain), error->code,
+ error->message);
+ g_assert_false (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, ""), ==, NULL);
+ g_clear_error (&error);
+
+ ok = flatpak_context_parse_env_block (context, "", 0, &error);
+ g_assert_no_error (error);
+ g_assert_true (ok);
+}
+
+static void
+test_context_env_fd (void)
+{
+ g_autoptr(FlatpakContext) context = flatpak_context_new ();
+ g_autoptr(GError) error = NULL;
+ gboolean ok;
+ const char env[] = "ONE=one\0TWO=two\0THREE=three\0EMPTY=\0X=x";
+ glnx_autofd int fd = -1;
+ g_autofree char *path = NULL;
+ int bad_fd;
+
+ path = g_strdup_printf ("/tmp/flatpak-test.XXXXXX");
+ fd = g_mkstemp (path);
+ g_assert_no_errno (glnx_loop_write (fd, env, sizeof (env)));
+ g_assert_no_errno (lseek (fd, 0, SEEK_SET));
+
+ ok = flatpak_context_parse_env_fd (context, fd, &error);
+ g_assert_no_error (error);
+ g_assert_true (ok);
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "ONE"), ==, "one");
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "EMPTY"), ==, "");
+ g_assert_cmpstr (g_hash_table_lookup (context->env_vars, "nope"), ==, NULL);
+
+ bad_fd = fd;
+ glnx_close_fd (&fd);
+ ok = flatpak_context_parse_env_fd (context, bad_fd, &error);
+ g_assert_nonnull (error);
+ g_test_message ("Got error as expected: %s #%d: %s",
+ g_quark_to_string (error->domain), error->code,
+ error->message);
+ g_assert_false (ok);
+ g_clear_error (&error);
+}
+
+int
+main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/context/env", test_context_env);
+ g_test_add_func ("/context/env-fd", test_context_env_fd);
+
+ return g_test_run ();
+}