tests: Expand g_file_copy() tests to test DEFAULT_PERMS flag

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-09-30 14:23:51 +01:00
parent 67772b6a70
commit eb2125770d

View File

@ -850,52 +850,94 @@ static void
test_copy_preserve_mode (void)
{
#ifdef G_OS_UNIX
GFile *tmpfile;
GFile *dest_tmpfile;
GFileInfo *dest_info;
GFileIOStream *iostream;
GError *local_error = NULL;
GError **error = &local_error;
guint32 romode = S_IFREG | 0600;
guint32 dest_mode;
mode_t current_umask = umask (0);
const struct
{
guint32 source_mode;
guint32 expected_destination_mode;
gboolean create_destination_before_copy;
GFileCopyFlags copy_flags;
}
vectors[] =
{
/* Overwriting the destination file should copy the permissions from the
* source file, even if %G_FILE_COPY_ALL_METADATA is set: */
{ 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
{ 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
/* The same behaviour should hold if the destination file is not being
* overwritten because it doesnt already exist: */
{ 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
{ 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS },
/* Anything with %G_FILE_COPY_TARGET_DEFAULT_PERMS should use the current
* umask for the destination file: */
{ 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
{ 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
{ 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
{ 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS },
};
gsize i;
tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
&iostream, error);
g_assert_no_error (local_error);
g_io_stream_close ((GIOStream*)iostream, NULL, error);
g_assert_no_error (local_error);
g_clear_object (&iostream);
/* Reset the umask after querying it above. Theres no way to query it without
* changing it. */
umask (current_umask);
g_test_message ("Current umask: %u", current_umask);
g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
&romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL, error);
g_assert_no_error (local_error);
for (i = 0; i < G_N_ELEMENTS (vectors); i++)
{
GFile *tmpfile;
GFile *dest_tmpfile;
GFileInfo *dest_info;
GFileIOStream *iostream;
GError *local_error = NULL;
guint32 romode = vectors[i].source_mode;
guint32 dest_mode;
dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
&iostream, error);
g_assert_no_error (local_error);
g_io_stream_close ((GIOStream*)iostream, NULL, error);
g_assert_no_error (local_error);
g_clear_object (&iostream);
g_test_message ("Vector %" G_GSIZE_FORMAT, i);
g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
NULL, NULL, NULL, error);
g_assert_no_error (local_error);
tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
&iostream, &local_error);
g_assert_no_error (local_error);
g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
g_assert_no_error (local_error);
g_clear_object (&iostream);
dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL, error);
g_assert_no_error (local_error);
g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
&romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL, &local_error);
g_assert_no_error (local_error);
dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
g_assert_cmpint (dest_mode, ==, romode);
dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
&iostream, &local_error);
g_assert_no_error (local_error);
g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
g_assert_no_error (local_error);
g_clear_object (&iostream);
(void) g_file_delete (tmpfile, NULL, NULL);
(void) g_file_delete (dest_tmpfile, NULL, NULL);
g_clear_object (&tmpfile);
g_clear_object (&dest_tmpfile);
g_clear_object (&dest_info);
if (!vectors[i].create_destination_before_copy)
{
g_file_delete (dest_tmpfile, NULL, &local_error);
g_assert_no_error (local_error);
}
g_file_copy (tmpfile, dest_tmpfile, vectors[i].copy_flags,
NULL, NULL, NULL, &local_error);
g_assert_no_error (local_error);
dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL, &local_error);
g_assert_no_error (local_error);
dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
g_assert_cmpint (dest_mode, ==, vectors[i].expected_destination_mode);
(void) g_file_delete (tmpfile, NULL, NULL);
(void) g_file_delete (dest_tmpfile, NULL, NULL);
g_clear_object (&tmpfile);
g_clear_object (&dest_tmpfile);
g_clear_object (&dest_info);
}
#else /* if !G_OS_UNIX */
g_test_skip ("File permissions tests can only be run on Unix")
#endif