tests: add a simple test for the mountinfo parsing logic

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-03-16 10:12:50 +01:00
parent 95b862b054
commit 6fa392bf91
8 changed files with 122 additions and 16 deletions
+6 -16
View File
@@ -37,7 +37,6 @@
#include "fd-util.h"
#include "fstab-util.h"
#include "linux-3.13/dm-ioctl.h"
#include "list.h"
#include "mount-setup.h"
#include "mount-util.h"
#include "path-util.h"
@@ -52,15 +51,6 @@
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
typedef struct MountPoint {
char *path;
char *remount_options;
unsigned long remount_flags;
bool try_remount_ro;
dev_t devnum;
LIST_FIELDS(struct MountPoint, mount_point);
} MountPoint;
static void mount_point_free(MountPoint **head, MountPoint *m) {
assert(head);
assert(m);
@@ -72,14 +62,14 @@ static void mount_point_free(MountPoint **head, MountPoint *m) {
free(m);
}
static void mount_points_list_free(MountPoint **head) {
void mount_points_list_free(MountPoint **head) {
assert(head);
while (*head)
mount_point_free(head, *head);
}
static int mount_points_list_get(MountPoint **head) {
int mount_points_list_get(const char *mountinfo, MountPoint **head) {
_cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
_cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
int r;
@@ -91,9 +81,9 @@ static int mount_points_list_get(MountPoint **head) {
if (!t || !i)
return log_oom();
r = mnt_table_parse_mtab(t, NULL);
r = mnt_table_parse_mtab(t, mountinfo);
if (r < 0)
return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
return log_error_errno(r, "Failed to parse %s: %m", mountinfo);
for (;;) {
struct libmnt_fs *fs;
@@ -108,7 +98,7 @@ static int mount_points_list_get(MountPoint **head) {
if (r == 1)
break;
if (r < 0)
return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
return log_error_errno(r, "Failed to get next entry from %s: %m", mountinfo);
path = mnt_fs_get_target(fs);
if (!path)
@@ -661,7 +651,7 @@ static int umount_all_once(bool *changed, int umount_log_level) {
assert(changed);
LIST_HEAD_INIT(mp_list_head);
r = mount_points_list_get(&mp_list_head);
r = mount_points_list_get(NULL, &mp_list_head);
if (r < 0)
goto end;
+15
View File
@@ -20,6 +20,8 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "list.h"
int umount_all(bool *changed, int umount_log_level);
int swapoff_all(bool *changed);
@@ -27,3 +29,16 @@ int swapoff_all(bool *changed);
int loopback_detach_all(bool *changed, int umount_log_level);
int dm_detach_all(bool *changed, int umount_log_level);
/* This is exported just for testing */
typedef struct MountPoint {
char *path;
char *remount_options;
unsigned long remount_flags;
bool try_remount_ro;
dev_t devnum;
LIST_FIELDS(struct MountPoint, mount_point);
} MountPoint;
int mount_points_list_get(const char *mountinfo, MountPoint **head);
void mount_points_list_free(MountPoint **head);
+8
View File
@@ -652,6 +652,14 @@ tests += [
[],
[libdl],
'', 'manual'],
[['src/test/test-umount.c',
'src/core/mount-setup.c',
'src/core/mount-setup.h',
'src/core/umount.c',
'src/core/umount.h'],
[],
[libmount]],
]
############################################################
+38
View File
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "log.h"
#include "string-util.h"
#include "tests.h"
#include "umount.h"
#include "util.h"
static void test_mount_points_list(const char *fname) {
LIST_HEAD(MountPoint, mp_list_head);
MountPoint *m;
log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo");
LIST_HEAD_INIT(mp_list_head);
assert_se(mount_points_list_get(fname, &mp_list_head) >= 0);
LIST_FOREACH(mount_point, m, mp_list_head)
log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
m->path,
strempty(m->remount_options),
m->remount_flags,
yes_no(m->try_remount_ro),
major(m->devnum), minor(m->devnum));
mount_points_list_free(&mp_list_head);
}
int main(int argc, char **argv) {
log_set_max_level(LOG_DEBUG);
log_parse_environment();
log_open();
test_mount_points_list(NULL);
test_mount_points_list(get_testdata_dir("/test-umount/empty.mountinfo"));
test_mount_points_list(get_testdata_dir("/test-umount/garbled.mountinfo"));
test_mount_points_list(get_testdata_dir("/test-umount/rhbug-1554943.mountinfo"));
}