diff mbox series

selftests/landlock: skip overlayfs test when kernel not support it

Message ID 20220820004547.2135627-1-jeffxu@google.com (mailing list archive)
State Handled Elsewhere
Headers show
Series selftests/landlock: skip overlayfs test when kernel not support it | expand

Commit Message

Jeff Xu Aug. 20, 2022, 12:45 a.m. UTC
From: Jeff Xu <jeffxu@google.com>

Overlayfs can be disabled in kernel config, causing related tests to fail.
Add check for overlayfs’s supportability at runtime, so we can call SKIP()
when needed.

Signed-off-by: Jeff Xu <jeffxu@chromium.org>
---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)

Comments

Mickaël Salaün Aug. 22, 2022, 12:55 p.m. UTC | #1
On 20/08/2022 02:45, jeffxu@google.com wrote:
> From: Jeff Xu <jeffxu@google.com>
> 
> Overlayfs can be disabled in kernel config, causing related tests to fail.
> Add check for overlayfs’s supportability at runtime, so we can call SKIP()
> when needed.
> 
> Signed-off-by: Jeff Xu <jeffxu@chromium.org>
> ---
>   tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
>   1 file changed, 53 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
> index 21a2ce8fa739..f604165dbd21 100644
> --- a/tools/testing/selftests/landlock/fs_test.c
> +++ b/tools/testing/selftests/landlock/fs_test.c
> @@ -11,6 +11,7 @@
>   #include <fcntl.h>
>   #include <linux/landlock.h>
>   #include <sched.h>
> +#include <stdio.h>
>   #include <string.h>
>   #include <sys/capability.h>
>   #include <sys/mount.h>
> @@ -3398,12 +3399,53 @@ static const char (*merge_sub_files[])[] = {
>    *         └── work
>    */
>   
> +static char *fgrep(FILE *inf, const char *str)

Please move these two helpers just before prepare_layout(). I plan to 
use them for other filesystems.


> +{
> +	char line[256];

I guess we can safely set this array size to 32 for now.


> +	int slen = strlen(str);
> +
> +	while (!feof(inf)) {
> +		if (!fgets(line, 256, inf))

Please use sizeof(line)

> +			break;
> +		if (strncmp(line, str, slen))
> +			continue;
> +
> +		return strdup(line);

No need to duplicate the string, just return a boolean.


> +	}
> +
> +	return NULL;
> +}
> +
> +static bool check_overlayfs_support(void)

Can be renamed to supports_overlayfs().


> +{
> +	FILE *inf = fopen("/proc/filesystems", "r");

Just move the fopen() call at the end of variable declaration and add a 
const.


> +	char *res;
> +	bool ret = false;
> +
> +	if (!inf)
> +		return false;

Let's assume that a failed attempt to open /proc/filesystems means the 
filesystem is supported (default behavior). This can help detect such 
missing file (which should not happen). You can add a comment to explain 
the rational.


> +
> +	res = fgrep(inf, "nodev\toverlay\n");
> +
> +	if (res) {
> +		ret = true;
> +		free(res);
> +	}
> +
> +	fclose(inf);
> +
> +	return ret;
> +}
> +
>   /* clang-format off */
>   FIXTURE(layout2_overlay) {};
>   /* clang-format on */
>   
>   FIXTURE_SETUP(layout2_overlay)
>   {
> +	int rc;

Let's stick to "ret".


> +	bool support;

s/support/is_supported/


> +
>   	prepare_layout(_metadata);
>   
>   	create_directory(_metadata, LOWER_BASE);
> @@ -3431,9 +3473,17 @@ FIXTURE_SETUP(layout2_overlay)
>   	create_directory(_metadata, MERGE_DATA);
>   	set_cap(_metadata, CAP_SYS_ADMIN);
>   	set_cap(_metadata, CAP_DAC_OVERRIDE);
> -	ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
> -			   "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
> -			   ",workdir=" UPPER_WORK));
> +
> +	rc = mount("overlay", MERGE_DATA, "overlay", 0,
> +			"lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
> +			",workdir=" UPPER_WORK);

Please format with clang-format-14, otherwise I'll do it myself.


> +	if (rc < 0) {

Please check errno with ASSERT_EQ() to differentiate from a skippable 
mount and an unexpected error. The next TH_LOG() would then not be needed.


> +		TH_LOG("mount overlay failed: errorno=%s", strerror(errno));
> +		support = check_overlayfs_support();
> +		ASSERT_FALSE(support);

ASSERT_FALSE(supports_overlayfs());

> +		SKIP(return, "overlayfs is not supported");
> +	}
> +

Please keep these clear_cap() calls just after the mount call.

>   	clear_cap(_metadata, CAP_DAC_OVERRIDE);
>   	clear_cap(_metadata, CAP_SYS_ADMIN);
>   }
diff mbox series

Patch

diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 21a2ce8fa739..f604165dbd21 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -11,6 +11,7 @@ 
 #include <fcntl.h>
 #include <linux/landlock.h>
 #include <sched.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/capability.h>
 #include <sys/mount.h>
@@ -3398,12 +3399,53 @@  static const char (*merge_sub_files[])[] = {
  *         └── work
  */
 
+static char *fgrep(FILE *inf, const char *str)
+{
+	char line[256];
+	int slen = strlen(str);
+
+	while (!feof(inf)) {
+		if (!fgets(line, 256, inf))
+			break;
+		if (strncmp(line, str, slen))
+			continue;
+
+		return strdup(line);
+	}
+
+	return NULL;
+}
+
+static bool check_overlayfs_support(void)
+{
+	FILE *inf = fopen("/proc/filesystems", "r");
+	char *res;
+	bool ret = false;
+
+	if (!inf)
+		return false;
+
+	res = fgrep(inf, "nodev\toverlay\n");
+
+	if (res) {
+		ret = true;
+		free(res);
+	}
+
+	fclose(inf);
+
+	return ret;
+}
+
 /* clang-format off */
 FIXTURE(layout2_overlay) {};
 /* clang-format on */
 
 FIXTURE_SETUP(layout2_overlay)
 {
+	int rc;
+	bool support;
+
 	prepare_layout(_metadata);
 
 	create_directory(_metadata, LOWER_BASE);
@@ -3431,9 +3473,17 @@  FIXTURE_SETUP(layout2_overlay)
 	create_directory(_metadata, MERGE_DATA);
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	set_cap(_metadata, CAP_DAC_OVERRIDE);
-	ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
-			   "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
-			   ",workdir=" UPPER_WORK));
+
+	rc = mount("overlay", MERGE_DATA, "overlay", 0,
+			"lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
+			",workdir=" UPPER_WORK);
+	if (rc < 0) {
+		TH_LOG("mount overlay failed: errorno=%s", strerror(errno));
+		support = check_overlayfs_support();
+		ASSERT_FALSE(support);
+		SKIP(return, "overlayfs is not supported");
+	}
+
 	clear_cap(_metadata, CAP_DAC_OVERRIDE);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 }