[v3,5/6] syscalls/clock_settime: create syscall clock_settime tests

Message ID 20181212203723.18810-5-rafael.tinoco@linaro.org
State Changes Requested
Headers show
Series
  • Untitled series #81489
Related show

Commit Message

Rafael David Tinoco Dec. 12, 2018, 8:37 p.m.
Fixes: 343

clock_settime01 creates a new test, using new API, based on existing
and older kernel/timers/clock_settime02 test. clock_settime02 creates
another test based on older kernel/timers/clock_settime03 test. Both
will be deleted in the next commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/clock_settime/.gitignore  |   2 +
 .../kernel/syscalls/clock_settime/Makefile    |   8 +
 .../syscalls/clock_settime/clock_settime01.c  |  75 +++++++++
 .../syscalls/clock_settime/clock_settime02.c  | 150 ++++++++++++++++++
 5 files changed, 238 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
 create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c

Comments

Rafael David Tinoco Dec. 12, 2018, 8:46 p.m. | #1
On 12/12/18 6:37 PM, Rafael David Tinoco wrote:
> Fixes: 343
> 
> clock_settime01 creates a new test, using new API, based on existing
> and older kernel/timers/clock_settime02 test. clock_settime02 creates
> another test based on older kernel/timers/clock_settime03 test. Both
> will be deleted in the next commits.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---
>  runtest/syscalls                              |   3 +
>  .../kernel/syscalls/clock_settime/.gitignore  |   2 +
>  .../kernel/syscalls/clock_settime/Makefile    |   8 +
>  .../syscalls/clock_settime/clock_settime01.c  |  75 +++++++++
>  .../syscalls/clock_settime/clock_settime02.c  | 150 ++++++++++++++++++
>  5 files changed, 238 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
>  create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 34b47f36b..dd03ad31a 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -79,6 +79,9 @@ clock_nanosleep01 clock_nanosleep01
>  clock_nanosleep02 clock_nanosleep02
>  clock_nanosleep2_01 clock_nanosleep2_01
>  
> +clock_settime01 clock_settime01
> +clock_settime02 clock_settime02
> +
>  clone01 clone01
>  clone02 clone02
>  clone03 clone03
> diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
> new file mode 100644
> index 000000000..281217550
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/.gitignore
> @@ -0,0 +1,2 @@
> +clock_settime01
> +clock_settime02
> diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
> new file mode 100644
> index 000000000..e6674a6b2
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/Makefile
> @@ -0,0 +1,8 @@
> +# Copyright (c) 2018 - Linaro Limited. All rights reserved.
> +# 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/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> new file mode 100644
> index 000000000..6e0ee47c4
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> @@ -0,0 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic test for clock_settime(2) on REALTIME clock:
> + *
> + *      1) advance DELTA_SEC seconds
> + *      2) go backwards DELTA_SEC seconds
> + *
> + * Restore wall clock at the end of test.
> + */
> +
> +#include "config.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10
> +#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
> +#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
> +#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)
> +
> +static void verify_clock_settime(void)
> +{
> +	long long elapsed;
> +	struct timespec begin, change, end;
> +
> +	/* test 01: move forward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_add_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
> +	}
> +
> +	/* test 02: move backward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (~(elapsed) > DELTA_SEC_VAR_NEG) {
> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
> +	}
> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_clock_settime,
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..25fcbfe09
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,150 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};

Cyril, I haven't done a global timespec, having a pointer inside the
test_case structure, just because there are a few other tests, like when
having sec to -1, nsec to -1, etc.. and I wouldn't be able to set those
during compilation time.. I hope you don't mind, test is functional and
working good.
Cyril Hrubis Jan. 24, 2019, 4:11 p.m. | #2
Hi!
> +#include "config.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10
> +#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
             ^
	     Maybe just DELTA_US having both SEC and US in the name is
	     confusing.

> +#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
> +#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)

It would be probably more clear to just define epsilon here as

#define DELTA_EPS (DELTA_US * 0.1)

Then use it as DELTA_US + DELTA_EPS and DELTA_US - DELTA_EPS

> +static void verify_clock_settime(void)
> +{
> +	long long elapsed;
> +	struct timespec begin, change, end;
> +
> +	/* test 01: move forward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_add_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
> +	}

The curly braces around these one line statements are useless here.

> +	/* test 02: move backward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (~(elapsed) > DELTA_SEC_VAR_NEG) {

We still does not check that the time wasn't set too much into history
so this should be adjusted just like the previous condition to:

	if (~(elapsed) < DELTA_US && ~(elapsed) > DELTA_US - DELTA_EPS)

> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
> +	}

Again useless curly braces.

> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_clock_settime,
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..25fcbfe09
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,150 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -2,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 02: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 06: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 09: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +/*
> + * Some tests may cause libc to segfault when passing bad arguments.
> + */
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec saved, spec, *specptr;


I would have initialized the specptr = &spec here and only replaced it
down there if we are hitting the EFAULT case.

> +	if (tc[i].replace == 0) {
> +
> +		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &saved);
> +
> +		/* add 1 sec to test clock */
> +		specptr = &spec;
> +		specptr->tv_sec = saved.tv_sec + 1;
> +		specptr->tv_nsec = saved.tv_nsec;

And we don't need the saved here anymore, so we can really do
clock_gettime() on the specptr here, right?

> +	} else {
> +
> +
> +		if (tc[i].newtime.tv_sec == -2) {
> +
> +			/* bad pointer case */
> +			specptr = tst_get_bad_addr(NULL);

Maybe we can just look for EFAULT errno instead of having magic tv_sec
value.

> +		} else {
> +
> +			/* use given values */
> +			specptr = &spec;
> +			specptr->tv_sec = tc[i].newtime.tv_sec;
> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;

You can do simple spec = tc[i].newtime here.

> +		}
> +	}



> +	TEST(sys_clock_settime(tc[i].type, specptr));
> +
> +	if (TST_RET == -1) {
> +
> +		if (tc[i].exp_err == TST_ERR) {
> +
> +			tst_res(TPASS, "clock_settime(2): failed as expected");
> +
> +		} else {
> +			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
> +					"failed with different error");

I would be better to print the expected errno here as well something as:

	tst_res(TFAIL | TTERRNO, "clock_settime(%s, ...) expected %s",
	        clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));
> +		}
> +
> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
> +			tc[i].type);

Shouldn't this say:

	tst_res(TFAIL, "clock_settime(%s, ...) passed unexpectedly, expected %s",
	        clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));

> +}
> +
> +static struct tst_test test = {
> +	.test = verify_clock_settime,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> -- 
> 2.20.0.rc1
>

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 34b47f36b..dd03ad31a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -79,6 +79,9 @@  clock_nanosleep01 clock_nanosleep01
 clock_nanosleep02 clock_nanosleep02
 clock_nanosleep2_01 clock_nanosleep2_01
 
+clock_settime01 clock_settime01
+clock_settime02 clock_settime02
+
 clone01 clone01
 clone02 clone02
 clone03 clone03
diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
new file mode 100644
index 000000000..281217550
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/.gitignore
@@ -0,0 +1,2 @@ 
+clock_settime01
+clock_settime02
diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
new file mode 100644
index 000000000..e6674a6b2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/Makefile
@@ -0,0 +1,8 @@ 
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# 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/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
new file mode 100644
index 000000000..6e0ee47c4
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
@@ -0,0 +1,75 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic test for clock_settime(2) on REALTIME clock:
+ *
+ *      1) advance DELTA_SEC seconds
+ *      2) go backwards DELTA_SEC seconds
+ *
+ * Restore wall clock at the end of test.
+ */
+
+#include "config.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define DELTA_SEC 10
+#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
+#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
+#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)
+
+static void verify_clock_settime(void)
+{
+	long long elapsed;
+	struct timespec begin, change, end;
+
+	/* test 01: move forward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_add_us(begin, DELTA_SEC_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
+		tst_res(TPASS, "clock_settime(2): was able to advance time");
+	} else {
+		tst_res(TFAIL, "clock_settime(2): could not advance time");
+	}
+
+	/* test 02: move backward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (~(elapsed) > DELTA_SEC_VAR_NEG) {
+		tst_res(TPASS, "clock_settime(2): was able to recede time");
+	} else {
+		tst_res(TFAIL, "clock_settime(2): could not recede time");
+	}
+}
+
+static struct tst_test test = {
+	.test_all = verify_clock_settime,
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
new file mode 100644
index 000000000..25fcbfe09
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
@@ -0,0 +1,150 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic tests for errors of clock_settime(2) on different clock types.
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+
+#define DELTA_SEC 10
+#define NSEC_PER_SEC (1000000000L)
+#define MAX_CLOCKS 16
+
+struct test_case {
+	clockid_t type;
+	struct timespec newtime;
+	int exp_err;
+	int replace;
+};
+
+struct test_case tc[] = {
+	{				/* case 01: REALTIME: timespec NULL   */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -2,
+	 .exp_err = EFAULT,
+	 .replace = 1,
+	 },
+	{				/* case 02: REALTIME: tv_sec = -1     */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 03: REALTIME: tv_nsec = -1    */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 05: MONOTONIC		      */
+	 .type = CLOCK_MONOTONIC,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 06: MAXCLOCK		      */
+	 .type = MAX_CLOCKS,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 07: MAXCLOCK+1		      */
+	 .type = MAX_CLOCKS + 1,
+	 .exp_err = EINVAL,
+	 },
+	/* Linux specific */
+	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
+	 .type = CLOCK_MONOTONIC_COARSE,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 09: CLOCK_MONOTONIC_RAW       */
+	 .type = CLOCK_MONOTONIC_RAW,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 10: CLOCK_BOOTTIME	      */
+	 .type = CLOCK_BOOTTIME,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
+	 .type = CLOCK_PROCESS_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
+	 .type = CLOCK_THREAD_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+};
+
+/*
+ * Some tests may cause libc to segfault when passing bad arguments.
+ */
+static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_settime, clk_id, tp);
+}
+
+static void verify_clock_settime(unsigned int i)
+{
+	struct timespec saved, spec, *specptr;
+
+	if (tc[i].replace == 0) {
+
+		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &saved);
+
+		/* add 1 sec to test clock */
+		specptr = &spec;
+		specptr->tv_sec = saved.tv_sec + 1;
+		specptr->tv_nsec = saved.tv_nsec;
+
+	} else {
+
+
+		if (tc[i].newtime.tv_sec == -2) {
+
+			/* bad pointer case */
+			specptr = tst_get_bad_addr(NULL);
+		} else {
+
+			/* use given values */
+			specptr = &spec;
+			specptr->tv_sec = tc[i].newtime.tv_sec;
+			specptr->tv_nsec = tc[i].newtime.tv_nsec;
+		}
+	}
+
+	TEST(sys_clock_settime(tc[i].type, specptr));
+
+	if (TST_RET == -1) {
+
+		if (tc[i].exp_err == TST_ERR) {
+
+			tst_res(TPASS, "clock_settime(2): failed as expected");
+
+		} else {
+			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
+					"failed with different error");
+		}
+
+		return;
+	}
+
+	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
+			tc[i].type);
+}
+
+static struct tst_test test = {
+	.test = verify_clock_settime,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};