LCOV - code coverage report
Current view: top level - gio/tests - unix-mounts.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 92.2 % 115 106
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 6 6
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLib testing framework examples and tests
       2                 :             :  *
       3                 :             :  * Copyright © 2017 Endless Mobile, Inc.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General
      18                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : 
      23                 :             : #include <glib.h>
      24                 :             : 
      25                 :             : #ifndef G_OS_UNIX
      26                 :             : #error This is a Unix-specific test
      27                 :             : #endif
      28                 :             : 
      29                 :             : #include <errno.h>
      30                 :             : #include <locale.h>
      31                 :             : 
      32                 :             : #ifdef HAVE_XLOCALE_H
      33                 :             : /* Needed on macOS and FreeBSD for uselocale() */
      34                 :             : #include <xlocale.h>
      35                 :             : #endif
      36                 :             : 
      37                 :             : #include <glib/gstdio.h>
      38                 :             : #include <gio/gio.h>
      39                 :             : #include <gio/gunixmounts.h>
      40                 :             : 
      41                 :             : /* We test all of the old g_unix_mount_*() API before it was renamed to
      42                 :             :  * g_unix_mount_entry_*(). The old API calls the new API, so both methods get
      43                 :             :  * tested at once. */
      44                 :             : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      45                 :             : 
      46                 :             : static void
      47                 :           1 : test_is_system_fs_type (void)
      48                 :             : {
      49                 :           1 :   g_assert_true (g_unix_is_system_fs_type ("tmpfs"));
      50                 :           1 :   g_assert_false (g_unix_is_system_fs_type ("ext4"));
      51                 :             : 
      52                 :             :   /* Check that some common network file systems aren’t considered ‘system’. */
      53                 :           1 :   g_assert_false (g_unix_is_system_fs_type ("cifs"));
      54                 :           1 :   g_assert_false (g_unix_is_system_fs_type ("nfs"));
      55                 :           1 :   g_assert_false (g_unix_is_system_fs_type ("nfs4"));
      56                 :           1 :   g_assert_false (g_unix_is_system_fs_type ("smbfs"));
      57                 :           1 : }
      58                 :             : 
      59                 :             : static void
      60                 :           1 : test_is_system_device_path (void)
      61                 :             : {
      62                 :           1 :   g_assert_true (g_unix_is_system_device_path ("devpts"));
      63                 :           1 :   g_assert_false (g_unix_is_system_device_path ("/"));
      64                 :           1 : }
      65                 :             : 
      66                 :             : static void
      67                 :           8 : assert_cmp_icon (GIcon    *icon,
      68                 :             :                  gboolean  expected_icon)
      69                 :             : {
      70                 :           8 :   if (expected_icon)
      71                 :             :     {
      72                 :           8 :       char *icon_str = NULL;
      73                 :             : 
      74                 :             :       /* While it would be nice to compare the icon value, that would make these
      75                 :             :        * tests depend on the icon themes installed. So just compare nullness. */
      76                 :           8 :       g_assert_nonnull (icon);
      77                 :           8 :       icon_str = g_icon_to_string (icon);
      78                 :           8 :       g_test_message ("Icon: %s", icon_str);
      79                 :           8 :       g_free (icon_str);
      80                 :             :     }
      81                 :             :   else
      82                 :             :     {
      83                 :           0 :       g_assert_null (icon);
      84                 :             :     }
      85                 :           8 : }
      86                 :             : 
      87                 :             : static void
      88                 :           1 : test_get_mount_points (void)
      89                 :             : {
      90                 :           1 :   GUnixMountPoint **points = NULL;
      91                 :           1 :   uint64_t time_read = 0;
      92                 :           1 :   size_t n_points = 0;
      93                 :           1 :   int fd = -1;
      94                 :           1 :   char *tmp_file = NULL;
      95                 :             :   gboolean res;
      96                 :             : #ifdef HAVE_USELOCALE
      97                 :             :   locale_t original_locale;
      98                 :             :   locale_t new_locale;
      99                 :             :   locale_t result;
     100                 :             : #endif
     101                 :           1 :   const char *fake_fstab = "# Some comment\n"
     102                 :             :     "/dev/mapper/fedora-root /                       ext4    defaults,x-systemd.device-timeout=0 1 1\n"
     103                 :             :     "UUID=1234-ABCD /boot                   ext4    defaults        1 2\n"
     104                 :             :     "UUID=ABCD-1234          /boot/efi               vfat    umask=0077,shortname=winnt,ro 0 2\n"
     105                 :             :     "/dev/mapper/fedora-home /home                   ext4    defaults,x-systemd.device-timeout=0 1 2\n"
     106                 :             :     "/dev/mapper/fedora-swap none                    swap    defaults,x-systemd.device-timeout=0 0 0\n"
     107                 :             :     "/dev/mapper/unused      none                    ext4    defaults\n";
     108                 :             :   const struct
     109                 :             :     {
     110                 :             :       const char *device_path;
     111                 :             :       const char *fs_type;
     112                 :             :       const char *options;
     113                 :             :       gboolean is_readonly;
     114                 :             :       gboolean is_user_mountable;
     115                 :             :       gboolean is_loopback;
     116                 :             :       gboolean guessed_icon;
     117                 :             :       gboolean guessed_symbolic_icon;
     118                 :             :       const char *guessed_name;
     119                 :             :       gboolean guessed_can_eject;
     120                 :             :     }
     121                 :           1 :   expected_points[] =
     122                 :             :     {
     123                 :             :       {
     124                 :             :         .device_path = "/dev/mapper/fedora-root",
     125                 :             :         .fs_type = "ext4",
     126                 :             :         .options = "defaults,x-systemd.device-timeout=0",
     127                 :             :         .is_readonly = FALSE,
     128                 :             :         .is_user_mountable = FALSE,
     129                 :             :         .is_loopback = FALSE,
     130                 :             :         .guessed_icon = TRUE,
     131                 :             :         .guessed_symbolic_icon = TRUE,
     132                 :             :         .guessed_name = "Filesystem root",
     133                 :             :         .guessed_can_eject = FALSE,
     134                 :             :       },
     135                 :             :       {
     136                 :             :         .device_path = "UUID=1234-ABCD",
     137                 :             :         .fs_type = "ext4",
     138                 :             :         .options = "defaults",
     139                 :             :         .is_readonly = FALSE,
     140                 :             :         .is_user_mountable = FALSE,
     141                 :             :         .is_loopback = FALSE,
     142                 :             :         .guessed_icon = TRUE,
     143                 :             :         .guessed_symbolic_icon = TRUE,
     144                 :             :         .guessed_name = "boot",
     145                 :             :         .guessed_can_eject = FALSE,
     146                 :             :       },
     147                 :             :       {
     148                 :             :         .device_path = "UUID=ABCD-1234",
     149                 :             :         .fs_type = "vfat",
     150                 :             :         .options = "umask=0077,shortname=winnt,ro",
     151                 :             :         .is_readonly = TRUE,
     152                 :             :         .is_user_mountable = FALSE,
     153                 :             :         .is_loopback = FALSE,
     154                 :             :         .guessed_icon = TRUE,
     155                 :             :         .guessed_symbolic_icon = TRUE,
     156                 :             :         .guessed_name = "efi",
     157                 :             :         .guessed_can_eject = FALSE,
     158                 :             :       },
     159                 :             :       {
     160                 :             :         .device_path = "/dev/mapper/fedora-home",
     161                 :             :         .fs_type = "ext4",
     162                 :             :         .options = "defaults,x-systemd.device-timeout=0",
     163                 :             :         .is_readonly = FALSE,
     164                 :             :         .is_user_mountable = FALSE,
     165                 :             :         .is_loopback = FALSE,
     166                 :             :         .guessed_icon = TRUE,
     167                 :             :         .guessed_symbolic_icon = TRUE,
     168                 :             :         .guessed_name = "home",
     169                 :             :         .guessed_can_eject = FALSE,
     170                 :             :       },
     171                 :             :       /* the swap partition is ignored */
     172                 :             :       /* as is the ignored unused partition */
     173                 :             :     };
     174                 :             : 
     175                 :           1 :   g_test_summary ("Basic test of g_unix_mount_points_get_from_file()");
     176                 :             : 
     177                 :           1 :   fd = g_file_open_tmp ("unix-mounts-XXXXXX", &tmp_file, NULL);
     178                 :           1 :   g_assert (fd != -1);
     179                 :           1 :   close (fd);
     180                 :             : 
     181                 :           1 :   res = g_file_set_contents (tmp_file, fake_fstab, -1, NULL);
     182                 :           1 :   g_assert (res);
     183                 :             : 
     184                 :           1 :   points = g_unix_mount_points_get_from_file (tmp_file, &time_read, &n_points);
     185                 :             : 
     186                 :           1 :   if (points == NULL)
     187                 :             :     {
     188                 :             :       /* Some platforms may not support parsing a specific mount point file */
     189                 :           0 :       g_assert_cmpuint (time_read, ==, 0);
     190                 :           0 :       g_assert_cmpuint (n_points, ==, 0);
     191                 :           0 :       g_test_skip ("Parsing mount points from a file not supported on this platform");
     192                 :           0 :       return;
     193                 :             :     }
     194                 :             : 
     195                 :           1 :   g_assert_nonnull (points);
     196                 :             : 
     197                 :             :   /* Check the properties of the mount points. This needs to be done in a known
     198                 :             :    * locale, because the guessed mount point name is translatable. */
     199                 :           1 :   g_assert_cmpuint (n_points, ==, G_N_ELEMENTS (expected_points));
     200                 :             : 
     201                 :             : #ifdef HAVE_USELOCALE
     202                 :           1 :   original_locale = uselocale ((locale_t) 0);
     203                 :           1 :   g_assert_true (original_locale != (locale_t) 0);
     204                 :           1 :   new_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0);
     205                 :           1 :   g_assert_true (new_locale != (locale_t) 0);
     206                 :           1 :   result = uselocale (new_locale);
     207                 :           1 :   g_assert_true (result == original_locale);
     208                 :             : #endif  /* HAVE_USELOCALE */
     209                 :             : 
     210                 :           5 :   for (size_t i = 0; i < n_points; i++)
     211                 :             :     {
     212                 :           4 :       GIcon *icon = NULL;
     213                 :           4 :       char *name = NULL;
     214                 :             : 
     215                 :           4 :       g_assert_cmpstr (g_unix_mount_point_get_device_path (points[i]), ==, expected_points[i].device_path);
     216                 :           4 :       g_assert_cmpstr (g_unix_mount_point_get_fs_type (points[i]), ==, expected_points[i].fs_type);
     217                 :           4 :       g_assert_cmpstr (g_unix_mount_point_get_options (points[i]), ==, expected_points[i].options);
     218                 :           4 :       g_assert_true (g_unix_mount_point_is_readonly (points[i]) == expected_points[i].is_readonly);
     219                 :           4 :       g_assert_true (g_unix_mount_point_is_user_mountable (points[i]) == expected_points[i].is_user_mountable);
     220                 :           4 :       g_assert_true (g_unix_mount_point_is_loopback (points[i]) == expected_points[i].is_loopback);
     221                 :             : 
     222                 :           4 :       icon = g_unix_mount_point_guess_icon (points[i]);
     223                 :           4 :       assert_cmp_icon (icon, expected_points[i].guessed_icon);
     224                 :           4 :       g_clear_object (&icon);
     225                 :             : 
     226                 :           4 :       icon = g_unix_mount_point_guess_symbolic_icon (points[i]);
     227                 :           4 :       assert_cmp_icon (icon, expected_points[i].guessed_symbolic_icon);
     228                 :           4 :       g_clear_object (&icon);
     229                 :             : 
     230                 :           4 :       name = g_unix_mount_point_guess_name (points[i]);
     231                 :             : #ifdef HAVE_USELOCALE
     232                 :           4 :       g_assert_cmpstr (name, ==, expected_points[i].guessed_name);
     233                 :             : #else
     234                 :             :       g_assert_nonnull (name);
     235                 :             : #endif
     236                 :           4 :       g_free (name);
     237                 :             : 
     238                 :           4 :       g_assert_true (g_unix_mount_point_guess_can_eject (points[i]) == expected_points[i].guessed_can_eject);
     239                 :             :     }
     240                 :             : 
     241                 :           5 :   for (size_t i = 0; i < n_points; i++)
     242                 :           4 :     g_unix_mount_point_free (points[i]);
     243                 :           1 :   g_free (points);
     244                 :           1 :   g_free (tmp_file);
     245                 :             : 
     246                 :             : #ifdef HAVE_USELOCALE
     247                 :           1 :   result = uselocale (original_locale);
     248                 :           1 :   g_assert_true (result == new_locale);
     249                 :           1 :   freelocale (new_locale);
     250                 :             : #endif
     251                 :             : }
     252                 :             : 
     253                 :             : static void
     254                 :           1 : test_get_mount_entries (void)
     255                 :             : {
     256                 :           1 :   GUnixMountEntry **entries = NULL;
     257                 :           1 :   uint64_t time_read = 0;
     258                 :           1 :   size_t n_entries = 0;
     259                 :           1 :   int fd = -1;
     260                 :           1 :   char *tmp_file = NULL;
     261                 :             :   gboolean res;
     262                 :             : #ifdef HAVE_LIBMOUNT
     263                 :             :   /* This is an edited /proc/self/mountinfo file, used because that’s what
     264                 :             :    * libmount parses by default, and it allows for the representation of root_path. */
     265                 :           1 :   const char *fake_mtab = "# Some comment\n"
     266                 :             :     "67 1 253:1 / / rw,relatime shared:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n"
     267                 :             :     "35 67 0:6 / /dev rw,nosuid shared:2 - devtmpfs devtmpfs rw,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64\n"
     268                 :             :     "1537 1080 253:1 /usr/share/fonts /run/host/fonts ro,nosuid,nodev,relatime master:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n";
     269                 :             : #else
     270                 :             :   /* This is an edited /proc/mounts, used because that’s what getmntent() parses
     271                 :             :    * (and it can’t parse mountinfo files). Unfortunately this means that
     272                 :             :    * non-NULL root_path values are not representable. */
     273                 :             :   const char *fake_mtab = "# Some comment\n"
     274                 :             :     "/dev/mapper/fedora-root / ext4 rw,relatime,seclabel 0 0\n"
     275                 :             :     "devtmpfs /dev devtmpfs rw,nosuid,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64 0 0\n";
     276                 :             : #endif
     277                 :             :   const struct
     278                 :             :     {
     279                 :             :       const char *device_path;
     280                 :             :       const char *fs_type;
     281                 :             :       const char *mount_path;
     282                 :             :       const char *options;
     283                 :             :       const char *root_path;  /* if supported */
     284                 :             :     }
     285                 :           1 :   expected_entries[] =
     286                 :             :     {
     287                 :             :       {
     288                 :             :         .device_path = "/dev/mapper/fedora-root",
     289                 :             :         .fs_type = "ext4",
     290                 :             :         .mount_path = "/",
     291                 :             :         .options = "rw,relatime,seclabel",
     292                 :             :         .root_path = "/",
     293                 :             :       },
     294                 :             :       {
     295                 :             :         .device_path = "devtmpfs",
     296                 :             :         .fs_type = "devtmpfs",
     297                 :             :         .mount_path = "/dev",
     298                 :             :         .options = "rw,nosuid,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64",
     299                 :             :         .root_path = "/",
     300                 :             :       },
     301                 :             : #ifdef HAVE_LIBMOUNT
     302                 :             :       {
     303                 :             :         .device_path = "/dev/mapper/fedora-root",
     304                 :             :         .fs_type = "ext4",
     305                 :             :         .mount_path = "/run/host/fonts",
     306                 :             :         .options = "ro,nosuid,nodev,relatime,seclabel",
     307                 :             :         .root_path = "/usr/share/fonts",
     308                 :             :       },
     309                 :             : #endif
     310                 :             :     };
     311                 :             : 
     312                 :           1 :   g_test_summary ("Basic test of g_unix_mounts_get_from_file()");
     313                 :             : 
     314                 :           1 :   fd = g_file_open_tmp ("unix-mounts-XXXXXX", &tmp_file, NULL);
     315                 :           1 :   g_assert (fd != -1);
     316                 :           1 :   close (fd);
     317                 :             : 
     318                 :           1 :   res = g_file_set_contents (tmp_file, fake_mtab, -1, NULL);
     319                 :           1 :   g_assert (res);
     320                 :             : 
     321                 :           1 :   entries = g_unix_mounts_get_from_file (tmp_file, &time_read, &n_entries);
     322                 :             : 
     323                 :           1 :   if (entries == NULL)
     324                 :             :     {
     325                 :             :       /* Some platforms may not support parsing a specific mount entry file */
     326                 :           0 :       g_assert_cmpuint (time_read, ==, 0);
     327                 :           0 :       g_assert_cmpuint (n_entries, ==, 0);
     328                 :           0 :       g_test_skip ("Parsing mount entries from a file not supported on this platform");
     329                 :           0 :       return;
     330                 :             :     }
     331                 :             : 
     332                 :           1 :   g_assert_nonnull (entries);
     333                 :             : 
     334                 :             :   /* Check the properties of the mount entries. */
     335                 :           1 :   g_assert_cmpuint (n_entries, ==, G_N_ELEMENTS (expected_entries));
     336                 :             : 
     337                 :           4 :   for (size_t i = 0; i < n_entries; i++)
     338                 :             :     {
     339                 :           3 :       g_assert_cmpstr (g_unix_mount_get_device_path (entries[i]), ==, expected_entries[i].device_path);
     340                 :           3 :       g_assert_cmpstr (g_unix_mount_get_fs_type (entries[i]), ==, expected_entries[i].fs_type);
     341                 :           3 :       g_assert_cmpstr (g_unix_mount_get_mount_path (entries[i]), ==, expected_entries[i].mount_path);
     342                 :           3 :       g_assert_cmpstr (g_unix_mount_get_options (entries[i]), ==, expected_entries[i].options);
     343                 :             : 
     344                 :             :       /* root_path is only supported by libmount */
     345                 :             : #ifdef HAVE_LIBMOUNT
     346                 :           3 :       g_assert_cmpstr (g_unix_mount_get_root_path (entries[i]), ==, expected_entries[i].root_path);
     347                 :             : #else
     348                 :             :       g_assert_null (g_unix_mount_get_root_path (entries[i]));
     349                 :             : #endif
     350                 :             :     }
     351                 :             : 
     352                 :           4 :   for (size_t i = 0; i < n_entries; i++)
     353                 :           3 :     g_unix_mount_free (entries[i]);
     354                 :           1 :   g_free (entries);
     355                 :           1 :   g_free (tmp_file);
     356                 :             : }
     357                 :             : 
     358                 :             : G_GNUC_END_IGNORE_DEPRECATIONS
     359                 :             : 
     360                 :             : int
     361                 :           1 : main (int   argc,
     362                 :             :       char *argv[])
     363                 :             : {
     364                 :           1 :   setlocale (LC_ALL, "");
     365                 :             : 
     366                 :           1 :   g_test_init (&argc, &argv, NULL);
     367                 :             : 
     368                 :           1 :   g_test_add_func ("/unix-mounts/is-system-fs-type", test_is_system_fs_type);
     369                 :           1 :   g_test_add_func ("/unix-mounts/is-system-device-path", test_is_system_device_path);
     370                 :           1 :   g_test_add_func ("/unix-mounts/get-mount-points", test_get_mount_points);
     371                 :           1 :   g_test_add_func ("/unix-mounts/get-mount-entries", test_get_mount_entries);
     372                 :             : 
     373                 :           1 :   return g_test_run ();
     374                 :             : }
        

Generated by: LCOV version 2.0-1