Skip to content
Snippets Groups Projects
Commit 658945b1 authored by Simon McVittie's avatar Simon McVittie
Browse files

tests: Move some tests for utility code out of pressure-vessel


The code under test was already moved, but the tests couldn't follow
until their executable was statically linked to libsteam-runtime-tools,
which it now is.

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent 7ce7a395
Branches
Tags
1 merge request!156Disable GIO modules without provoking scary warnings (#32)
...@@ -81,29 +81,6 @@ test_arbitrary_key (Fixture *f, ...@@ -81,29 +81,6 @@ test_arbitrary_key (Fixture *f,
g_assert_cmpstr (k, ==, "world"); g_assert_cmpstr (k, ==, "world");
} }
static void
test_avoid_gvfs (Fixture *f,
gconstpointer context)
{
/* This doesn't actually call _srt_setenv_disable_gio_modules(),
* because that's documented
* to have to happen as early as possible in main(). Instead, we do that
* in main() as documented, and in this function we just assert that
* we did. */
GVfs *vfs = g_vfs_get_default ();
GVfs *local = g_vfs_get_local ();
g_test_message ("Default VFS: %s at %p", G_OBJECT_TYPE_NAME (vfs), vfs);
g_test_message ("Local VFS: %s at %p", G_OBJECT_TYPE_NAME (local), local);
/* We compare by string equality to have a better message if this
* assertion fails. We can't assert that the pointers are the same,
* because GLib currently uses two instances of the same class. */
g_assert_cmpstr (G_OBJECT_TYPE_NAME (vfs), ==,
G_OBJECT_TYPE_NAME (local));
g_assert_cmpuint (G_OBJECT_TYPE (vfs), ==,
G_OBJECT_TYPE (local));
}
static void static void
test_capture_output (Fixture *f, test_capture_output (Fixture *f,
gconstpointer context) gconstpointer context)
...@@ -280,108 +257,6 @@ test_search_path_append (Fixture *f, ...@@ -280,108 +257,6 @@ test_search_path_append (Fixture *f,
g_assert_cmpstr (str->str, ==, "/bin:/usr/bin:/usr/bin"); g_assert_cmpstr (str->str, ==, "/bin:/usr/bin:/usr/bin");
} }
typedef struct
{
const char *name;
mode_t mode;
} File;
typedef struct
{
const char *name;
const char *target;
} Symlink;
typedef struct
{
const char *path;
GFileTest test;
gboolean expected_result;
} InSysrootTest;
static void
test_file_in_sysroot (Fixture *f,
gconstpointer context)
{
static const char * const prepare_dirs[] =
{
"dir1/dir2/dir3",
};
static const File prepare_files[] =
{
{ "dir1/file1", 0600 },
{ "dir1/dir2/file2", 0600 },
{ "dir1/exec1", 0700 },
};
static const Symlink prepare_symlinks[] =
{
{ "dir1/dir2/symlink_to_dir3", "dir3" },
{ "dir1/dir2/symlink_to_file2", "file2" },
{ "dir1/dir2/sym_to_sym_to_file2", "symlink_to_file2" },
{ "dir1/abs_symlink_to_run", "/run" },
};
static const InSysrootTest tests[] =
{
{ "dir1", G_FILE_TEST_IS_DIR, TRUE },
{ "dir1", G_FILE_TEST_EXISTS, TRUE },
{ "/dir1", G_FILE_TEST_EXISTS, TRUE },
{ "dir1/dir2", G_FILE_TEST_IS_DIR, TRUE },
/* These gets solved in sysroot, following symlinks too */
{ "dir1/dir2/symlink_to_dir3", G_FILE_TEST_IS_DIR, TRUE },
{ "dir1/dir2/sym_to_sym_to_file2", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/abs_symlink_to_run", G_FILE_TEST_IS_DIR, FALSE },
{ "dir1/missing", G_FILE_TEST_EXISTS, FALSE },
{ "dir1/file1", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/file1", (G_FILE_TEST_IS_DIR | G_FILE_TEST_IS_EXECUTABLE), FALSE },
{ "dir1/exec1", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/exec1", G_FILE_TEST_IS_EXECUTABLE, TRUE },
};
g_autoptr(GError) error = NULL;
g_auto(GLnxTmpDir) tmpdir = { FALSE };
gsize i;
glnx_mkdtemp ("test-XXXXXX", 0700, &tmpdir, &error);
g_assert_no_error (error);
for (i = 0; i < G_N_ELEMENTS (prepare_dirs); i++)
{
const char *it = prepare_dirs[i];
glnx_shutil_mkdir_p_at (tmpdir.fd, it, 0700, NULL, &error);
g_assert_no_error (error);
}
for (i = 0; i < G_N_ELEMENTS (prepare_files); i++)
{
const File *it = &prepare_files[i];
glnx_autofd int fd = openat (tmpdir.fd, it->name, O_WRONLY|O_CREAT, it->mode);
if (fd == -1)
g_error ("openat %s: %s", it->name, g_strerror (errno));
}
for (i = 0; i < G_N_ELEMENTS (prepare_symlinks); i++)
{
const Symlink *it = &prepare_symlinks[i];
if (symlinkat (it->target, tmpdir.fd, it->name) != 0)
g_error ("symlinkat %s: %s", it->name, g_strerror (errno));
}
for (i = 0; i < G_N_ELEMENTS (tests); i++)
{
const InSysrootTest *it = &tests[i];
g_assert_cmpint (_srt_file_test_in_sysroot (tmpdir.path, -1,
it->path, it->test),
==, it->expected_result);
}
}
int int
main (int argc, main (int argc,
char **argv) char **argv)
...@@ -391,15 +266,12 @@ main (int argc, ...@@ -391,15 +266,12 @@ main (int argc,
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add ("/arbitrary-key", Fixture, NULL, g_test_add ("/arbitrary-key", Fixture, NULL,
setup, test_arbitrary_key, teardown); setup, test_arbitrary_key, teardown);
g_test_add ("/avoid-gvfs", Fixture, NULL, setup, test_avoid_gvfs, teardown);
g_test_add ("/capture-output", Fixture, NULL, g_test_add ("/capture-output", Fixture, NULL,
setup, test_capture_output, teardown); setup, test_capture_output, teardown);
g_test_add ("/envp-cmp", Fixture, NULL, setup, test_envp_cmp, teardown); g_test_add ("/envp-cmp", Fixture, NULL, setup, test_envp_cmp, teardown);
g_test_add ("/same-file", Fixture, NULL, setup, test_same_file, teardown); g_test_add ("/same-file", Fixture, NULL, setup, test_same_file, teardown);
g_test_add ("/search-path-append", Fixture, NULL, g_test_add ("/search-path-append", Fixture, NULL,
setup, test_search_path_append, teardown); setup, test_search_path_append, teardown);
g_test_add ("/test-file-in-sysroot", Fixture, NULL,
setup, test_file_in_sysroot, teardown);
return g_test_run (); return g_test_run ();
} }
...@@ -56,6 +56,131 @@ teardown (Fixture *f, ...@@ -56,6 +56,131 @@ teardown (Fixture *f,
G_GNUC_UNUSED const Config *config = context; G_GNUC_UNUSED const Config *config = context;
} }
static void
test_avoid_gvfs (Fixture *f,
gconstpointer context)
{
/* This doesn't actually call _srt_setenv_disable_gio_modules(),
* because that's documented
* to have to happen as early as possible in main(). Instead, we do that
* in main() as documented, and in this function we just assert that
* we did. */
GVfs *vfs = g_vfs_get_default ();
GVfs *local = g_vfs_get_local ();
g_test_message ("Default VFS: %s at %p", G_OBJECT_TYPE_NAME (vfs), vfs);
g_test_message ("Local VFS: %s at %p", G_OBJECT_TYPE_NAME (local), local);
/* We compare by string equality to have a better message if this
* assertion fails. We can't assert that the pointers are the same,
* because GLib currently uses two instances of the same class. */
g_assert_cmpstr (G_OBJECT_TYPE_NAME (vfs), ==,
G_OBJECT_TYPE_NAME (local));
g_assert_cmpuint (G_OBJECT_TYPE (vfs), ==,
G_OBJECT_TYPE (local));
}
typedef struct
{
const char *name;
mode_t mode;
} File;
typedef struct
{
const char *name;
const char *target;
} Symlink;
typedef struct
{
const char *path;
GFileTest test;
gboolean expected_result;
} InSysrootTest;
static void
test_file_in_sysroot (Fixture *f,
gconstpointer context)
{
static const char * const prepare_dirs[] =
{
"dir1/dir2/dir3",
};
static const File prepare_files[] =
{
{ "dir1/file1", 0600 },
{ "dir1/dir2/file2", 0600 },
{ "dir1/exec1", 0700 },
};
static const Symlink prepare_symlinks[] =
{
{ "dir1/dir2/symlink_to_dir3", "dir3" },
{ "dir1/dir2/symlink_to_file2", "file2" },
{ "dir1/dir2/sym_to_sym_to_file2", "symlink_to_file2" },
{ "dir1/abs_symlink_to_run", "/run" },
};
static const InSysrootTest tests[] =
{
{ "dir1", G_FILE_TEST_IS_DIR, TRUE },
{ "dir1", G_FILE_TEST_EXISTS, TRUE },
{ "/dir1", G_FILE_TEST_EXISTS, TRUE },
{ "dir1/dir2", G_FILE_TEST_IS_DIR, TRUE },
/* These gets solved in sysroot, following symlinks too */
{ "dir1/dir2/symlink_to_dir3", G_FILE_TEST_IS_DIR, TRUE },
{ "dir1/dir2/sym_to_sym_to_file2", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/abs_symlink_to_run", G_FILE_TEST_IS_DIR, FALSE },
{ "dir1/missing", G_FILE_TEST_EXISTS, FALSE },
{ "dir1/file1", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/file1", (G_FILE_TEST_IS_DIR | G_FILE_TEST_IS_EXECUTABLE), FALSE },
{ "dir1/exec1", G_FILE_TEST_IS_REGULAR, TRUE },
{ "dir1/exec1", G_FILE_TEST_IS_EXECUTABLE, TRUE },
};
g_autoptr(GError) error = NULL;
g_auto(GLnxTmpDir) tmpdir = { FALSE };
gsize i;
glnx_mkdtemp ("test-XXXXXX", 0700, &tmpdir, &error);
g_assert_no_error (error);
for (i = 0; i < G_N_ELEMENTS (prepare_dirs); i++)
{
const char *it = prepare_dirs[i];
glnx_shutil_mkdir_p_at (tmpdir.fd, it, 0700, NULL, &error);
g_assert_no_error (error);
}
for (i = 0; i < G_N_ELEMENTS (prepare_files); i++)
{
const File *it = &prepare_files[i];
glnx_autofd int fd = openat (tmpdir.fd, it->name, O_WRONLY|O_CREAT, it->mode);
if (fd == -1)
g_error ("openat %s: %s", it->name, g_strerror (errno));
}
for (i = 0; i < G_N_ELEMENTS (prepare_symlinks); i++)
{
const Symlink *it = &prepare_symlinks[i];
if (symlinkat (it->target, tmpdir.fd, it->name) != 0)
g_error ("symlinkat %s: %s", it->name, g_strerror (errno));
}
for (i = 0; i < G_N_ELEMENTS (tests); i++)
{
const InSysrootTest *it = &tests[i];
g_assert_cmpint (_srt_file_test_in_sysroot (tmpdir.path, -1,
it->path, it->test),
==, it->expected_result);
}
}
/* /*
* Test _srt_filter_gameoverlayrenderer function. * Test _srt_filter_gameoverlayrenderer function.
*/ */
...@@ -95,7 +220,12 @@ int ...@@ -95,7 +220,12 @@ int
main (int argc, main (int argc,
char **argv) char **argv)
{ {
_srt_setenv_disable_gio_modules ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add ("/utils/avoid-gvfs", Fixture, NULL, setup, test_avoid_gvfs, teardown);
g_test_add ("/utils/test-file-in-sysroot", Fixture, NULL,
setup, test_file_in_sysroot, teardown);
g_test_add ("/utils/filter_gameoverlayrenderer", Fixture, NULL, setup, g_test_add ("/utils/filter_gameoverlayrenderer", Fixture, NULL, setup,
filter_gameoverlayrenderer, teardown); filter_gameoverlayrenderer, teardown);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment