From 849d3812c905a8c10b67a131093e6e2b51b8392e Mon Sep 17 00:00:00 2001 From: Emmanuel Fleury Date: Thu, 16 Dec 2021 22:52:47 +0100 Subject: [PATCH 1/2] Move tests/file-test.c to glib/tests/fileutils.c --- glib/tests/fileutils.c | 142 +++++++++++++++++++++++-- tests/file-test.c | 233 ----------------------------------------- tests/meson.build | 1 - 3 files changed, 135 insertions(+), 241 deletions(-) delete mode 100644 tests/file-test.c diff --git a/glib/tests/fileutils.c b/glib/tests/fileutils.c index f11a855b9..490415c25 100644 --- a/glib/tests/fileutils.c +++ b/glib/tests/fileutils.c @@ -967,9 +967,15 @@ test_file_open_tmp (void) static void test_mkstemp (void) { - gchar *name; gint fd; + gint result; + gchar *name; + char chars[62]; + char template[32]; + const char hello[] = "Hello, World"; + const int hellolen = sizeof (hello) - 1; + /* Test normal case */ name = g_strdup ("testXXXXXXtest"), fd = g_mkstemp (name); g_assert_cmpint (fd, !=, -1); @@ -978,17 +984,48 @@ test_mkstemp (void) close (fd); g_free (name); - name = g_strdup ("testYYYYYYtest"), - fd = g_mkstemp (name); - g_assert_cmpint (fd, ==, -1); - g_free (name); + /* g_mkstemp() must not work if template doesn't contain XXXXXX */ + strcpy (template, "foobar"); + g_assert_cmpint (g_mkstemp (template), ==, -1); + + /* g_mkstemp() must not work if template doesn't contain six X */ + strcpy (template, "foobarXXX"); + g_assert_cmpint (g_mkstemp (template), ==, -1); + + strcpy (template, "fooXXXXXX"); + fd = g_mkstemp (template); + g_assert_cmpint (fd, !=, -1); + result = write (fd, hello, hellolen); + g_assert_cmpint (result, !=, -1); + g_assert_cmpint (result, ==, hellolen); + + lseek (fd, 0, 0); + result = read (fd, chars, sizeof (chars)); + g_assert_cmpint (result, !=, -1); + g_assert_cmpint (result, ==, hellolen); + + chars[result] = '\0'; + g_assert_cmpstr (chars, ==, hello); + + close (fd); + remove (template); + + /* Check that is does not work for "fooXXXXXX.pdf" */ + strcpy (template, "fooXXXXXX.pdf"); + fd = g_mkstemp (template); + g_assert_cmpint (fd, !=, -1); + + close (fd); + remove (template); } static void test_mkdtemp (void) { - gchar *name; + gint fd; gchar *ret; + gchar *name; + char template[32]; name = g_strdup ("testXXXXXXtest"), ret = g_mkdtemp (name); @@ -1001,6 +1038,56 @@ test_mkdtemp (void) ret = g_mkdtemp (name); g_assert_null (ret); g_free (name); + + strcpy (template, "foodir"); + g_assert_null (g_mkdtemp (template)); + + strcpy (template, "foodir"); + g_assert_null (g_mkdtemp (template)); + + strcpy (template, "fooXXXXXX"); + ret = g_mkdtemp (template); + g_assert_nonnull (ret); + g_assert_true (ret == template); + g_assert_false (g_file_test (template, G_FILE_TEST_IS_REGULAR)); + g_assert_true (g_file_test (template, G_FILE_TEST_IS_DIR)); + + strcat (template, "/abc"); + fd = g_open (template, O_WRONLY | O_CREAT, 0600); + g_assert_cmpint (fd, !=, -1); + close (fd); + g_assert_true (g_file_test (template, G_FILE_TEST_IS_REGULAR)); + g_assert_cmpint (g_unlink (template), !=, -1); + + template[9] = '\0'; + g_assert_cmpint (g_rmdir (template), !=, -1); + + strcpy (template, "fooXXXXXX.dir"); + g_assert_nonnull (g_mkdtemp (template)); + g_assert_true (g_file_test (template, G_FILE_TEST_IS_DIR)); + g_rmdir (template); +} + +static void +test_get_contents (void) +{ + FILE *f; + gsize len; + gchar *contents; + GError *error = NULL; + const gchar *text = "abcdefghijklmnopqrstuvwxyz"; + const gchar *filename = "file-test-get-contents"; + + f = g_fopen (filename, "w"); + fwrite (text, 1, strlen (text), f); + fclose (f); + + g_assert_true (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); + + g_assert_true (g_file_get_contents (filename, &contents, &len, &error)); + g_assert_cmpstr (text, ==, contents); + + g_free (contents); } static void @@ -1318,12 +1405,18 @@ test_read_link (void) #ifdef HAVE_READLINK #ifdef G_OS_UNIX int ret; - const gchar *oldpath; + FILE *file; gchar *cwd; + gchar *data; gchar *newpath; gchar *badpath; gchar *path; GError *error = NULL; + const gchar *oldpath; + const gchar *filename = "file-test-data"; + const gchar *link1 = "file-test-link1"; + const gchar *link2 = "file-test-link2"; + const gchar *link3 = "file-test-link3"; cwd = g_get_current_dir (); @@ -1356,6 +1449,40 @@ test_read_link (void) g_free (newpath); g_free (badpath); + file = fopen (filename, "w"); + g_assert_nonnull (file); + fclose (file); + + g_assert_cmpint (symlink (filename, link1), ==, 0); + g_assert_cmpint (symlink (link1, link2), ==, 0); + + error = NULL; + data = g_file_read_link (link1, &error); + g_assert_nonnull (data); + g_assert_cmpstr (data, ==, filename); + g_free (data); + + error = NULL; + data = g_file_read_link (link2, &error); + g_assert_nonnull (data); + g_assert_cmpstr (data, ==, link1); + g_free (data); + + error = NULL; + data = g_file_read_link (link3, &error); + g_assert_null (data); + g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); + g_error_free (error); + + error = NULL; + data = g_file_read_link (filename, &error); + g_assert_null (data); + g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL); + g_error_free (error); + + remove (filename); + remove (link1); + remove (link2); #endif #else g_test_skip ("Symbolic links not supported"); @@ -1905,6 +2032,7 @@ main (int argc, g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp); g_test_add_func ("/fileutils/mkstemp", test_mkstemp); g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp); + g_test_add_func ("/fileutils/get-contents", test_get_contents); g_test_add_func ("/fileutils/set-contents", test_set_contents); g_test_add_func ("/fileutils/set-contents-full", test_set_contents_full); g_test_add_func ("/fileutils/set-contents-full/read-only-file", test_set_contents_full_read_only_file); diff --git a/tests/file-test.c b/tests/file-test.c deleted file mode 100644 index 67e7e87ed..000000000 --- a/tests/file-test.c +++ /dev/null @@ -1,233 +0,0 @@ -/* GLIB - Library of useful routines for C programming - * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald - * - * This library 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 . - */ - -/* - * Modified by the GLib Team and others 1997-2000. See the AUTHORS - * file for a list of people on the GLib Team. See the ChangeLog - * files for a list of changes. These files are distributed with - * GLib at ftp://ftp.gtk.org/pub/gtk/. - */ - -#undef G_DISABLE_ASSERT -#undef G_LOG_DOMAIN - -#ifdef GLIB_COMPILATION -#undef GLIB_COMPILATION -#endif - -#include - -#include - -#include - -#include /* For open() */ - -#ifdef G_OS_UNIX -#include -#endif -#ifdef G_OS_WIN32 -#include /* For read(), write() etc */ -#endif - -static void -test_mkstemp (void) -{ - char template[32]; - int fd; - int i; - const char hello[] = "Hello, World"; - const int hellolen = sizeof (hello) - 1; - char chars[62]; - - strcpy (template, "foobar"); - fd = g_mkstemp (template); - if (fd != -1) - { - g_warning ("g_mkstemp works even if template doesn't contain XXXXXX"); - close (fd); - } - - strcpy (template, "foobarXXX"); - fd = g_mkstemp (template); - if (fd != -1) - { - g_warning ("g_mkstemp works even if template contains less than six X"); - close (fd); - } - - strcpy (template, "fooXXXXXX"); - fd = g_mkstemp (template); - g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX"); - i = write (fd, hello, hellolen); - g_assert (i != -1 && "write() failed"); - g_assert (i == hellolen && "write() has written too few bytes"); - - lseek (fd, 0, 0); - i = read (fd, chars, sizeof (chars)); - g_assert (i != -1 && "read() failed: %s"); - g_assert (i == hellolen && "read() has got wrong number of bytes"); - - chars[i] = 0; - g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back"); - - close (fd); - remove (template); - - strcpy (template, "fooXXXXXX.pdf"); - fd = g_mkstemp (template); - g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX.pdf"); - - close (fd); - remove (template); -} - -static void -test_mkdtemp (void) -{ - char template[32], *retval; - int fd; - int i; - - strcpy (template, "foodir"); - retval = g_mkdtemp (template); - if (retval != NULL) - { - g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX"); - g_rmdir (retval); - } - - strcpy (template, "foodir"); - retval = g_mkdtemp (template); - if (retval != NULL) - { - g_warning ("g_mkdtemp works even if template contains less than six X"); - g_rmdir (retval); - } - - strcpy (template, "fooXXXXXX"); - retval = g_mkdtemp (template); - g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX"); - g_assert (retval == template && "g_mkdtemp allocated the resulting string?"); - g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR)); - g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); - - strcat (template, "/abc"); - fd = g_open (template, O_WRONLY | O_CREAT, 0600); - g_assert (fd != -1 && "couldn't open file in temporary directory"); - close (fd); - g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR)); - i = g_unlink (template); - g_assert (i != -1 && "couldn't unlink file in temporary directory"); - - template[9] = '\0'; - i = g_rmdir (template); - g_assert (i != -1 && "couldn't remove temporary directory"); - - strcpy (template, "fooXXXXXX.dir"); - retval = g_mkdtemp (template); - g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir"); - g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); - g_rmdir (template); -} - -static void -test_readlink (void) -{ -#ifdef HAVE_SYMLINK - FILE *file; - int result; - char *filename = "file-test-data"; - char *link1 = "file-test-link1"; - char *link2 = "file-test-link2"; - char *link3 = "file-test-link3"; - char *data; - GError *error; - - file = fopen (filename, "w"); - g_assert (file != NULL && "fopen() failed"); - fclose (file); - - result = symlink (filename, link1); - g_assert (result == 0 && "symlink() failed"); - result = symlink (link1, link2); - g_assert (result == 0 && "symlink() failed"); - - error = NULL; - data = g_file_read_link (link1, &error); - g_assert (data != NULL && "couldn't read link1"); - g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data"); - g_free (data); - - error = NULL; - data = g_file_read_link (link2, &error); - g_assert (data != NULL && "couldn't read link2"); - g_assert (strcmp (data, link1) == 0 && "link2 contains wrong data"); - g_free (data); - - error = NULL; - data = g_file_read_link (link3, &error); - g_assert (data == NULL && "could read link3"); - g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); - g_error_free (error); - - error = NULL; - data = g_file_read_link (filename, &error); - g_assert (data == NULL && "could read regular file as link"); - g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL); - g_error_free (error); - - remove (filename); - remove (link1); - remove (link2); -#endif -} - -static void -test_get_contents (void) -{ - const gchar *text = "abcdefghijklmnopqrstuvwxyz"; - const gchar *filename = "file-test-get-contents"; - gchar *contents; - gsize len; - FILE *f; - GError *error = NULL; - - f = g_fopen (filename, "w"); - fwrite (text, 1, strlen (text), f); - fclose (f); - - g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); - - if (! g_file_get_contents (filename, &contents, &len, &error)) - g_error ("g_file_get_contents() failed: %s", error->message); - - g_assert (strcmp (text, contents) == 0 && "content mismatch"); - - g_free (contents); -} - -int -main (int argc, char *argv[]) -{ - test_mkstemp (); - test_mkdtemp (); - test_readlink (); - test_get_contents (); - - return 0; -} diff --git a/tests/meson.build b/tests/meson.build index 2527a832c..8130dc5ed 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -18,7 +18,6 @@ subdir('refcount') tests = { 'testglib' : {'tap' : true}, 'testgdate' : {}, - 'file-test' : {}, 'gio-test' : {}, 'mainloop-test' : {}, 'mapping-test' : {}, From b3d0752f3d6c2599582735a8292968c3502e30a6 Mon Sep 17 00:00:00 2001 From: Emmanuel Fleury Date: Sat, 18 Dec 2021 00:45:08 +0100 Subject: [PATCH 2/2] Improve test coverage of glib/gfileutils.c --- glib/tests/fileutils.c | 113 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/glib/tests/fileutils.c b/glib/tests/fileutils.c index 490415c25..bb3b51648 100644 --- a/glib/tests/fileutils.c +++ b/glib/tests/fileutils.c @@ -70,6 +70,14 @@ check_string (gchar *str, const gchar *expected) static void test_build_path (void) { + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_null (g_build_path (NULL, "x", "y", NULL)); + g_test_assert_expected_messages (); + } + /* check_string (g_build_path ("", NULL), "");*/ check_string (g_build_path ("", "", NULL), ""); check_string (g_build_path ("", "x", NULL), "x"); @@ -255,7 +263,6 @@ test_build_pathv (void) static void test_build_filename (void) { -/* check_string (g_build_filename (NULL), "");*/ check_string (g_build_filename (S, NULL), S); check_string (g_build_filename (S"x", NULL), S"x"); check_string (g_build_filename ("x"S, NULL), "x"S); @@ -742,6 +749,8 @@ test_format_size_for_display (void) static void test_file_errors (void) { + g_assert_cmpint (g_file_error_from_errno (-1), ==, G_FILE_ERROR_FAILED); + #ifdef EEXIST g_assert_cmpint (g_file_error_from_errno (EEXIST), ==, G_FILE_ERROR_EXIST); #endif @@ -821,6 +830,19 @@ test_basename (void) { gchar *b; + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_null (g_basename (NULL)); + g_test_assert_expected_messages (); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_null (g_path_get_basename (NULL)); + g_test_assert_expected_messages (); + } + b = g_path_get_basename (""); g_assert_cmpstr (b, ==, "."); g_free (b); @@ -885,6 +907,14 @@ test_dirname (void) #endif }; + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_null (g_path_get_dirname (NULL)); + g_test_assert_expected_messages (); + } + for (i = 0; i < G_N_ELEMENTS (dirname_checks); i++) { gchar *dirname; @@ -973,7 +1003,7 @@ test_mkstemp (void) char chars[62]; char template[32]; const char hello[] = "Hello, World"; - const int hellolen = sizeof (hello) - 1; + const gsize hellolen = sizeof (hello) - 1; /* Test normal case */ name = g_strdup ("testXXXXXXtest"), @@ -1082,14 +1112,65 @@ test_get_contents (void) fwrite (text, 1, strlen (text), f); fclose (f); + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_false (g_file_get_contents (NULL, &contents, &len, &error)); + g_test_assert_expected_messages (); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + g_assert_false (g_file_get_contents (filename, NULL, &len, &error)); + g_test_assert_expected_messages (); + } + g_assert_true (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); g_assert_true (g_file_get_contents (filename, &contents, &len, &error)); g_assert_cmpstr (text, ==, contents); + g_assert_no_error (error); g_free (contents); } +static void +test_file_test (void) +{ + GError *error = NULL; + gboolean result; + gchar *name; + gint fd; + + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + result = g_file_test (NULL, G_FILE_TEST_EXISTS); + g_assert_false (result); + g_test_assert_expected_messages (); + } + + fd = g_file_open_tmp (NULL, &name, &error); + g_assert_no_error (error); + write (fd, "a", 1); + g_assert_cmpint (g_fsync (fd), ==, 0); + close (fd); + +#ifndef G_OS_WIN32 + result = g_file_test (name, G_FILE_TEST_IS_SYMLINK); + g_assert_false (result); + + symlink (name, "symlink"); + result = g_file_test ("symlink", G_FILE_TEST_IS_SYMLINK); + g_assert_true (result); + unlink ("symlink"); +#endif + + /* Cleaning */ + g_remove (name); +} + static void test_set_contents (void) { @@ -1320,6 +1401,23 @@ test_set_contents_full_read_only_file (void) close (fd); g_assert_no_errno (chmod (file_name, 0200)); + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + ret = g_file_set_contents_full (NULL, "b", 1, + G_FILE_SET_CONTENTS_NONE, 0644, &error); + g_assert_false (ret); + g_test_assert_expected_messages (); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + ret = g_file_set_contents_full (file_name, NULL, 1, + G_FILE_SET_CONTENTS_NONE, 0644, &error); + g_assert_false (ret); + g_test_assert_expected_messages (); + } + /* Set the file contents */ ret = g_file_set_contents_full (file_name, "b", 1, G_FILE_SET_CONTENTS_NONE, 0644, &error); @@ -1418,6 +1516,14 @@ test_read_link (void) const gchar *link2 = "file-test-link2"; const gchar *link3 = "file-test-link3"; + if (g_test_undefined ()) + { + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*assertion*!= NULL*"); + newpath = g_file_read_link (NULL, &error); + g_test_assert_expected_messages (); + } + cwd = g_get_current_dir (); oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL); @@ -1460,12 +1566,14 @@ test_read_link (void) data = g_file_read_link (link1, &error); g_assert_nonnull (data); g_assert_cmpstr (data, ==, filename); + g_assert_no_error (error); g_free (data); error = NULL; data = g_file_read_link (link2, &error); g_assert_nonnull (data); g_assert_cmpstr (data, ==, link1); + g_assert_no_error (error); g_free (data); error = NULL; @@ -2030,6 +2138,7 @@ main (int argc, g_test_add_func ("/fileutils/dirname", test_dirname); g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp); g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp); + g_test_add_func ("/fileutils/file-test", test_file_test); g_test_add_func ("/fileutils/mkstemp", test_mkstemp); g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp); g_test_add_func ("/fileutils/get-contents", test_get_contents);