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 1/2] syscalls/clock_settime01.c: create syscall clock_settime test
Date: Thu, 6 Dec 2018 14:03:09 +0100	[thread overview]
Message-ID: <20181206130309.GA22250@rei> (raw)
In-Reply-To: <20181205193058.11146-1-rafael.tinoco@linaro.org>

Hi!
> ---
>  runtest/syscalls                              |   2 +
>  .../kernel/syscalls/clock_settime/.gitignore  |   1 +
>  .../kernel/syscalls/clock_settime/Makefile    |   8 +
>  .../syscalls/clock_settime/clock_settime01.c  | 187 ++++++++++++++++++
>  4 files changed, 198 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
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index ac1d2d2cd..4cbc13209 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -79,6 +79,8 @@ clock_nanosleep01 clock_nanosleep01
>  clock_nanosleep02 clock_nanosleep02
>  clock_nanosleep2_01 clock_nanosleep2_01
>  
> +clock_settime01 clock_settime01
> +
>  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..fcbb9fecc
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/.gitignore
> @@ -0,0 +1 @@
> +clock_settime01
> 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..591e5c723
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> @@ -0,0 +1,187 @@
> +// 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 clock_settime(2) on different clock types
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +static struct timespec clock_realtime_saved;
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_ret;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME		      */
> +	 .type = CLOCK_REALTIME,
> +	 .exp_ret = 0,
> +	 },

What about we separate the only positive case to a separate test, then
we can also do more a few more sanity checks there. I would probably go
for sequence of something as:

* get the realtime time
* increasing it by some value in seconds
* set the realtime time
* get the realtime time
* check that the value is aprox the first value increased by the value
* decrease by the value
* set it again
* check it again

That would cover both cases of setting time forward and backward and at
the end we would end up with the correct time as a bonus.

> +	{				/* case 02: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -2,
> +	 .exp_ret = -1,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 06: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 08: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 09: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 13: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_gettime, clk_id, tp);
> +}

Any reason why we avoid the clock_settime and clock_gettime libc
functions here?

> +static void cleanup(void)
> +{
> +	/* restore realtime clock */
> +
> +	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
> +				"current time back");
> +}
> +
> +static void setup(void)
> +{
> +	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
> +				"current time");
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec spec, *specptr;
> +
> +	if (tc[i].replace == 0) {
> +
> +		/* add 1 sec to test clock */
> +
> +		specptr = &spec;
> +		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
> +		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
> +
> +	} else {
> +
> +		/* bad pointer case */
> +
> +		if (tc[i].newtime.tv_sec == -2)
> +			specptr = tst_get_bad_addr(cleanup);
> +
> +		/* use given values */
> +
> +		else {
> +			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 (tc[i].exp_ret == TST_RET) {
> +
> +		if (TST_RET >= 0)
> +			tst_res(TPASS, "clock_settime(2): worked as expected");
> +
> +		else {
> +			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");
> +		}


LKML coding style prefers curly braces around both blocks if they are
around one of them, but that's a minor one.

> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
> +			tc[i].type);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test = verify_clock_settime,
> +	.cleanup = cleanup,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +};
> -- 
> 2.20.0.rc1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

  parent reply	other threads:[~2018-12-06 13:03 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-05 19:30 [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
2018-12-05 19:30 ` [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 13:03 ` Cyril Hrubis [this message]
2018-12-06 14:49   ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 19:11     ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-11 14:27     ` Cyril Hrubis
2018-12-11 16:05       ` Rafael David Tinoco
2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-24 15:12           ` Cyril Hrubis
2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2018-12-12 20:46           ` Rafael David Tinoco
2019-01-24 16:11           ` Cyril Hrubis
2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-30 13:53                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
2019-01-30 13:54                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2019-01-30 13:56                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 8/8] timers/clock_settime: remove " Rafael David Tinoco
2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
2019-01-30 14:50                 ` Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2019-01-08 12:04           ` Rafael David Tinoco
2019-01-08 12:42             ` Cyril Hrubis
2019-01-08 12:46               ` Rafael David Tinoco
2019-01-24 12:58             ` Rafael David Tinoco
2019-01-24 13:18               ` Cyril Hrubis
2019-01-24 15:06         ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function 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=20181206130309.GA22250@rei \
    --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