Skip to content

Commit 3ab8a0b

Browse files
committed
selftests: add listmount() iteration tests
Add a forward and backward iteration test for listmount(). Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 2ce2328 commit 3ab8a0b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0-or-later
22

33
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
4-
TEST_GEN_PROGS := statmount_test statmount_test_ns
4+
TEST_GEN_PROGS := statmount_test statmount_test_ns listmount_test
55

66
include ../../lib.mk
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (c) 2024 Christian Brauner <[email protected]>
3+
4+
#define _GNU_SOURCE
5+
#include <fcntl.h>
6+
#include <sched.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <sys/stat.h>
10+
#include <sys/mount.h>
11+
#include <unistd.h>
12+
13+
#include "statmount.h"
14+
#include "../../kselftest_harness.h"
15+
16+
#ifndef LISTMOUNT_REVERSE
17+
#define LISTMOUNT_REVERSE (1 << 0) /* List later mounts first */
18+
#endif
19+
20+
#define LISTMNT_BUFFER 10
21+
22+
/* Check that all mount ids are in increasing order. */
23+
TEST(listmount_forward)
24+
{
25+
uint64_t list[LISTMNT_BUFFER], last_mnt_id = 0;
26+
27+
for (;;) {
28+
ssize_t nr_mounts;
29+
30+
nr_mounts = listmount(LSMT_ROOT, 0, last_mnt_id,
31+
list, LISTMNT_BUFFER, 0);
32+
ASSERT_GE(nr_mounts, 0);
33+
if (nr_mounts == 0)
34+
break;
35+
36+
for (size_t cur = 0; cur < nr_mounts; cur++) {
37+
if (cur < nr_mounts - 1)
38+
ASSERT_LT(list[cur], list[cur + 1]);
39+
last_mnt_id = list[cur];
40+
}
41+
}
42+
}
43+
44+
/* Check that all mount ids are in decreasing order. */
45+
TEST(listmount_backward)
46+
{
47+
uint64_t list[LISTMNT_BUFFER], last_mnt_id = 0;
48+
49+
for (;;) {
50+
ssize_t nr_mounts;
51+
52+
nr_mounts = listmount(LSMT_ROOT, 0, last_mnt_id,
53+
list, LISTMNT_BUFFER, LISTMOUNT_REVERSE);
54+
ASSERT_GE(nr_mounts, 0);
55+
if (nr_mounts == 0)
56+
break;
57+
58+
for (size_t cur = 0; cur < nr_mounts; cur++) {
59+
if (cur < nr_mounts - 1)
60+
ASSERT_GT(list[cur], list[cur + 1]);
61+
last_mnt_id = list[cur];
62+
}
63+
}
64+
}
65+
66+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)