tests: Test against a sample mtab file in unix-mounts for getmntent()

The test in `unix-mounts` to see whether `g_unix_mounts_get_from_file()`
can parse an example file was working fine when GLib is built with
libmount, but not when built without it (and hence typically using
`getmntent()`).

This is because libmount supports mountinfo files (like
`/proc/self/mountinfo`), but `getmntent()` only supports mount files
(like `/proc/mounts`). The test was written only with the former.

So, change the test to use mount files when GLib is built without
libmount support.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #3456
This commit is contained in:
Philip Withnall 2024-08-27 12:13:35 +01:00
parent c242972043
commit 27c02a0be4
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -254,17 +254,28 @@ test_get_mount_entries (void)
int fd = -1; int fd = -1;
char *tmp_file = NULL; char *tmp_file = NULL;
gboolean res; gboolean res;
#ifdef HAVE_LIBMOUNT
/* This is an edited /proc/self/mountinfo file, used because thats what
* libmount parses by default, and it allows for the representation of root_path. */
const char *fake_mtab = "# Some comment\n" const char *fake_mtab = "# Some comment\n"
"67 1 253:1 / / rw,relatime shared:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n" "67 1 253:1 / / rw,relatime shared:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n"
"35 67 0:6 / /dev rw,nosuid shared:2 - devtmpfs devtmpfs rw,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64\n" "35 67 0:6 / /dev rw,nosuid shared:2 - devtmpfs devtmpfs rw,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64\n"
"1537 1080 253:1 /usr/share/fonts /run/host/fonts ro,nosuid,nodev,relatime master:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n"; "1537 1080 253:1 /usr/share/fonts /run/host/fonts ro,nosuid,nodev,relatime master:1 - ext4 /dev/mapper/fedora-root rw,seclabel\n";
#else
/* This is an edited /proc/mounts, used because thats what getmntent() parses
* (and it cant parse mountinfo files). Unfortunately this means that
* non-NULL root_path values are not representable. */
const char *fake_mtab = "# Some comment\n"
"/dev/mapper/fedora-root / ext4 rw,relatime,seclabel 0 0\n"
"devtmpfs /dev devtmpfs rw,nosuid,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64 0 0\n";
#endif
const struct const struct
{ {
const char *device_path; const char *device_path;
const char *fs_type; const char *fs_type;
const char *mount_path; const char *mount_path;
const char *options; const char *options;
const char *root_path; const char *root_path; /* if supported */
} }
expected_entries[] = expected_entries[] =
{ {
@ -282,6 +293,7 @@ test_get_mount_entries (void)
.options = "rw,nosuid,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64", .options = "rw,nosuid,seclabel,size=4096k,nr_inodes=1995515,mode=755,inode64",
.root_path = "/", .root_path = "/",
}, },
#ifdef HAVE_LIBMOUNT
{ {
.device_path = "/dev/mapper/fedora-root", .device_path = "/dev/mapper/fedora-root",
.fs_type = "ext4", .fs_type = "ext4",
@ -289,6 +301,7 @@ test_get_mount_entries (void)
.options = "ro,nosuid,nodev,relatime,seclabel", .options = "ro,nosuid,nodev,relatime,seclabel",
.root_path = "/usr/share/fonts", .root_path = "/usr/share/fonts",
}, },
#endif
}; };
g_test_summary ("Basic test of g_unix_mounts_get_from_file()"); g_test_summary ("Basic test of g_unix_mounts_get_from_file()");
@ -322,7 +335,13 @@ test_get_mount_entries (void)
g_assert_cmpstr (g_unix_mount_get_fs_type (entries[i]), ==, expected_entries[i].fs_type); g_assert_cmpstr (g_unix_mount_get_fs_type (entries[i]), ==, expected_entries[i].fs_type);
g_assert_cmpstr (g_unix_mount_get_mount_path (entries[i]), ==, expected_entries[i].mount_path); g_assert_cmpstr (g_unix_mount_get_mount_path (entries[i]), ==, expected_entries[i].mount_path);
g_assert_cmpstr (g_unix_mount_get_options (entries[i]), ==, expected_entries[i].options); g_assert_cmpstr (g_unix_mount_get_options (entries[i]), ==, expected_entries[i].options);
/* root_path is only supported by libmount */
#ifdef HAVE_LIBMOUNT
g_assert_cmpstr (g_unix_mount_get_root_path (entries[i]), ==, expected_entries[i].root_path); g_assert_cmpstr (g_unix_mount_get_root_path (entries[i]), ==, expected_entries[i].root_path);
#else
g_assert_null (g_unix_mount_get_root_path (entries[i]));
#endif
} }
for (size_t i = 0; i < n_entries; i++) for (size_t i = 0; i < n_entries; i++)