public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 8/9] containers/timens: Add basic error test
Date: Wed, 18 Mar 2020 16:38:00 +0100	[thread overview]
Message-ID: <20200318153801.3529-9-chrubis@suse.cz> (raw)
In-Reply-To: <20200318153801.3529-1-chrubis@suse.cz>

Add basic error handling test for the /proc/$PID/timens_offsets file.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 runtest/containers                            |  1 +
 testcases/kernel/containers/timens/.gitignore |  1 +
 testcases/kernel/containers/timens/Makefile   |  6 ++
 testcases/kernel/containers/timens/timens01.c | 68 +++++++++++++++++++
 4 files changed, 76 insertions(+)
 create mode 100644 testcases/kernel/containers/timens/.gitignore
 create mode 100644 testcases/kernel/containers/timens/Makefile
 create mode 100644 testcases/kernel/containers/timens/timens01.c

diff --git a/runtest/containers b/runtest/containers
index 1006d8d35..23e4a533d 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -90,3 +90,4 @@ userns07 userns07
 sysinfo03 sysinfo03
 clock_nanosleep03 clock_nanosleep03
 clock_gettime03 clock_gettime03
+timens01 timens01
diff --git a/testcases/kernel/containers/timens/.gitignore b/testcases/kernel/containers/timens/.gitignore
new file mode 100644
index 000000000..bcd2dd9dd
--- /dev/null
+++ b/testcases/kernel/containers/timens/.gitignore
@@ -0,0 +1 @@
+timens01
diff --git a/testcases/kernel/containers/timens/Makefile b/testcases/kernel/containers/timens/Makefile
new file mode 100644
index 000000000..5ea7d67db
--- /dev/null
+++ b/testcases/kernel/containers/timens/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/timens/timens01.c b/testcases/kernel/containers/timens/timens01.c
new file mode 100644
index 000000000..2dbfebfa8
--- /dev/null
+++ b/testcases/kernel/containers/timens/timens01.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+
+  Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
+
+ */
+/*
+
+  Basic test for timens_offsets error handling.
+
+  After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
+  process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
+  and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
+
+ */
+
+#define _GNU_SOURCE
+#include "lapi/namespaces_constants.h"
+#include "lapi/posix_clocks.h"
+#include "tst_test.h"
+
+static struct tcase {
+	const char *desc;
+	const char *offsets;
+	int exp_err;
+} tcases[] = {
+	{"Obvious garbage", "not an offset", EINVAL},
+	{"Missing nanoseconds", "1 10", EINVAL},
+	{"Negative nanoseconds", "1 10 -10", EINVAL},
+	{"Nanoseconds > 1s", "1 10 1000000001", EINVAL},
+	{"Unsupported CLOCK_REALTIME", "0 10 0", EINVAL},
+	{"Mess on the second line", "1 10 0\na", EINVAL},
+	{"Overflow kernel 64bit ns timer", "1 9223372036 0", ERANGE},
+	{"Overflow kernel 64bit ns timer", "1 -9223372036 0", ERANGE},
+};
+
+static void verify_ns_clock(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	int fd, ret;
+
+	SAFE_UNSHARE(CLONE_NEWTIME);
+
+	fd = SAFE_OPEN("/proc/self/timens_offsets", O_WRONLY);
+	ret = write(fd, tc->offsets, strlen(tc->offsets));
+
+	if (ret != -1) {
+		tst_res(TFAIL, "%s returned %i", tc->desc, ret);
+		return;
+	}
+
+	if (errno != tc->exp_err) {
+		tst_res(TFAIL | TERRNO, "%s should fail with %s, got:",
+			tc->desc, tst_strerrno(tc->exp_err));
+		return;
+	}
+
+	tst_res(TPASS | TERRNO, "%s", tc->desc);
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_ns_clock,
+	.needs_root = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_TIME_NS=y"
+	}
+};
-- 
2.24.1


  parent reply	other threads:[~2020-03-18 15:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-18 15:37 [LTP] [PATCH v2 0/9] Add basic time namespace testcases Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 1/9] lapi/namespace_constants.h: Add CLONE_NEWTIME Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 2/9] lapi: Add a configure check and fallback for setns Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 3/9] safe_macros: Add SAFE_SETNS() Cyril Hrubis
2020-03-23 16:11   ` Petr Vorel
2020-03-30 14:09     ` Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 4/9] include/tst_timer: Fix normalization Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 5/9] syscalls/sysinfo03: Add time namespace test Cyril Hrubis
2020-03-19  7:55   ` Li Wang
2020-03-19 22:00     ` Cyril Hrubis
2020-03-20  7:32       ` Li Wang
2020-03-23 18:47   ` Petr Vorel
2020-03-18 15:37 ` [LTP] [PATCH v2 6/9] syscalls/clock_nanosleep03: Add test for time namespace Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 7/9] syscalls/clock_gettime03: Add basic time namespace test Cyril Hrubis
2020-03-18 15:38 ` Cyril Hrubis [this message]
2020-03-18 15:38 ` [LTP] [PATCH v2 9/9] syscalls/timerfd04: Add " Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200318153801.3529-9-chrubis@suse.cz \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox