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) test_copy_preserve_mode (void)
{ {
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
GFile *tmpfile; mode_t current_umask = umask (0);
GFile *dest_tmpfile; const struct
GFileInfo *dest_info; {
GFileIOStream *iostream; guint32 source_mode;
GError *local_error = NULL; guint32 expected_destination_mode;
GError **error = &local_error; gboolean create_destination_before_copy;
guint32 romode = S_IFREG | 0600; GFileCopyFlags copy_flags;
guint32 dest_mode; }
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", /* Reset the umask after querying it above. Theres no way to query it without
&iostream, error); * changing it. */
g_assert_no_error (local_error); umask (current_umask);
g_io_stream_close ((GIOStream*)iostream, NULL, error); g_test_message ("Current umask: %u", current_umask);
g_assert_no_error (local_error);
g_clear_object (&iostream);
g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32, for (i = 0; i < G_N_ELEMENTS (vectors); i++)
&romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, {
NULL, error); GFile *tmpfile;
g_assert_no_error (local_error); 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", g_test_message ("Vector %" G_GSIZE_FORMAT, i);
&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_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
NULL, NULL, NULL, error); &iostream, &local_error);
g_assert_no_error (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, g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
NULL, error); &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
g_assert_no_error (local_error); NULL, &local_error);
g_assert_no_error (local_error);
dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE); dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
&iostream, &local_error);
g_assert_cmpint (dest_mode, ==, romode); 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); if (!vectors[i].create_destination_before_copy)
(void) g_file_delete (dest_tmpfile, NULL, NULL); {
g_file_delete (dest_tmpfile, NULL, &local_error);
g_clear_object (&tmpfile); g_assert_no_error (local_error);
g_clear_object (&dest_tmpfile); }
g_clear_object (&dest_info);
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 */ #else /* if !G_OS_UNIX */
g_test_skip ("File permissions tests can only be run on Unix") g_test_skip ("File permissions tests can only be run on Unix")
#endif #endif