[PATCH v6] tests: Add test for decoding of PTP_* ioctl commands

Dmitry V. Levin ldv at altlinux.org
Sun Mar 11 23:43:23 UTC 2018


On Sun, Mar 11, 2018 at 07:00:50PM +0530, Harsha Sharma wrote:
> * tests/ioctl_ptp.c: New file.
> * tests/.gitignore: Add ioctl_ptp.
> * tests/gen_tests.in (ioctl_ptp): New entry.
> * tests/pure_executables.list: Likewise.
> ---
> Changes in v6:
> * Remove unnecessary checks
> 
> Chenges in v5:
> * change sysoff->n_samples = PTP_MAX_SAMPLES
> 
> Changes in v4:
> * Change 2017 to 2018 in copyright header
> * change position of ifdef
> * explicitly set value of sysoff->n_samples
> 
> Changes in v3:
> * check for macro HAVE_STRUCT_PTP_SYS_OFFSET
> * print field names as per fixed decoder
> * cover all cases like when call succeeds
> 
> Changes in v2:
> * Add decoding of non-zero arguments in test_no_device
> * Use TAIL_ALLOC_OBJECT_CONST_PTR
> * Change variable names and error message
> * Remove hunk from Makefile.am
> * Other minor changes
> 
>  tests/.gitignore            |   1 +
>  tests/gen_tests.in          |   1 +
>  tests/ioctl_ptp.c           | 157 ++++++++++++++++++++++++++++++++++++++++++++
>  tests/pure_executables.list |   1 +
>  4 files changed, 160 insertions(+)
>  create mode 100644 tests/ioctl_ptp.c
> 
> diff --git a/tests/.gitignore b/tests/.gitignore
> index dd0c8e12..fb240e32 100644
> --- a/tests/.gitignore
> +++ b/tests/.gitignore
> @@ -137,6 +137,7 @@ ioctl_loop-nv
>  ioctl_loop-v
>  ioctl_mtd
>  ioctl_nsfs
> +ioctl_ptp
>  ioctl_rtc
>  ioctl_rtc-v
>  ioctl_scsi
> diff --git a/tests/gen_tests.in b/tests/gen_tests.in
> index 55cc98de..b37adcf7 100644
> --- a/tests/gen_tests.in
> +++ b/tests/gen_tests.in
> @@ -136,6 +136,7 @@ ioctl_loop-nv	+ioctl.test -a22 -e verbose=none
>  ioctl_loop-v	+ioctl.test -v
>  ioctl_mtd	+ioctl.test
>  ioctl_nsfs	+ioctl.test -esignal=none
> +ioctl_ptp 	+ioctl.test
>  ioctl_rtc	+ioctl.test
>  ioctl_rtc-v	+ioctl.test -v
>  ioctl_scsi	+ioctl.test
> diff --git a/tests/ioctl_ptp.c b/tests/ioctl_ptp.c
> new file mode 100644
> index 00000000..9fbc320f
> --- /dev/null
> +++ b/tests/ioctl_ptp.c
> @@ -0,0 +1,157 @@
> +/*
> + * Check decoding of PTP_* commands of ioctl syscall.
> + *
> + * Copyright (c) 2018 Harsha Sharma <harshasharmaiitr at gmail.com>
> + * All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + * 3. The name of the author may not be used to endorse or promote products
> + *    derived from this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
> + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
> + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
> + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "tests.h"
> +
> +#ifdef HAVE_STRUCT_PTP_SYS_OFFSET
> +
> +#include "xlat.h"
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <inttypes.h>
> +#include <unistd.h>
> +#include <linux/ptp_clock.h>
> +
> +#include "xlat/ptp_flags_options.h"
> +
> +static void
> +test_no_device(void)
> +{
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_clock_caps, caps);
> +	fill_memory(caps, sizeof(*caps));
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_sys_offset, sysoff);
> +	fill_memory(sysoff, sizeof(*sysoff));
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_extts_request, extts);
> +	fill_memory(extts, sizeof(*extts));
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_perout_request, perout);
> +
> +	/* PTP_CLOCK_GETCAPS */
> +	ioctl(-1, PTP_CLOCK_GETCAPS, NULL);
> +	printf("ioctl(-1, PTP_CLOCK_GETCAPS, NULL) = -1 EBADF (%m)\n");
> +	ioctl(-1, PTP_CLOCK_GETCAPS, caps);
> +	printf("ioctl(-1, PTP_CLOCK_GETCAPS, %p) = -1 EBADF (%m)\n", caps);
> +
> +	/* PTP_SYS_OFFSET */
> +	ioctl(-1, PTP_SYS_OFFSET, NULL);
> +	printf("ioctl(-1, PTP_SYS_OFFSET, NULL) = -1 EBADF (%m)\n");
> +	ioctl(-1, PTP_SYS_OFFSET, sysoff);
> +	printf("ioctl(-1, PTP_SYS_OFFSET, {n_samples=%u}) = -1 EBADF (%m)\n",
> +	       sysoff->n_samples);
> +
> +	/* PTP_ENABLE_PPS */
> +	ioctl(-1, PTP_ENABLE_PPS, 0);
> +	printf("ioctl(-1, PTP_ENABLE_PPS, 0) = -1 EBADF (%m)\n");
> +	ioctl(-1, PTP_ENABLE_PPS, 1);
> +	printf("ioctl(-1, PTP_ENABLE_PPS, 1) = -1 EBADF (%m)\n");
> +
> +	/* PTP_EXTTS_REQUEST */
> +	ioctl(-1, PTP_EXTTS_REQUEST, NULL);
> +	printf("ioctl(-1, PTP_EXTTS_REQUEST, NULL) = -1 EBADF (%m)\n");
> +	ioctl(-1, PTP_EXTTS_REQUEST, extts);
> +	printf("ioctl(-1, PTP_EXTTS_REQUEST, {index=%d, flags=", extts->index);
> +	printflags(ptp_flags_options, extts->flags, "PTP_???");
> +	printf("}) = -1 EBADF (%m)\n");
> +
> +	/* PTP_PEROUT_REQUEST */
> +	ioctl(-1, PTP_PEROUT_REQUEST, NULL);
> +	printf("ioctl(-1, PTP_PEROUT_REQUEST, NULL) = -1 EBADF (%m)\n");
> +	ioctl(-1, PTP_PEROUT_REQUEST, perout);
> +	printf("ioctl(-1, PTP_PEROUT_REQUEST, {start={sec=%" PRId64 ""
> +	       ", nsec=%" PRIu32 "}, period={sec=%" PRId64 ", nsec=%" PRIu32 "}"
> +	       ", index=%d, flags=", (int64_t)perout->start.sec,
> +	       perout->start.nsec, (int64_t)perout->period.sec,
> +	       perout->period.nsec, perout->index);
> +	printflags(ptp_flags_options, perout->flags, "PTP_???");
> +	printf("}) = -1 EBADF (%m)\n");
> +}
> +static void
> +test_ptp0_device(void)
> +{
> +
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_clock_caps, caps);
> +	fill_memory(caps, sizeof(*caps));
> +	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_sys_offset, sysoff);
> +	fill_memory(sysoff, sizeof(*sysoff));
> +
> +	static const char device[] = "/dev/ptp0";
> +	int fd = open(device, O_RDWR);
> +	if (fd < 0) {
> +		perror(device);
> +		return;
> +	}
> +
> +	/* PTP_CLOCK_GETCAPS */
> +	int getcaps_ret = ioctl(fd, PTP_CLOCK_GETCAPS, caps);
> +	if (getcaps_ret < 0) {
> +		printf("ioctl(%d, PTP_CLOCK_GETCAPS, %p) = %s\n",
> +		       fd, caps,  sprintrc(getcaps_ret));
> +	} else {
> +		printf("ioctl(%d, PTP_CLOCK_GETCAPS, {max_adj=%d, n_alarm=%d"
> +		    ", n_ext_ts=%d, n_per_out=%d, pps=%d}) = %s\n",
> +		    fd, caps->max_adj, caps->n_alarm, caps->n_ext_ts,
> +		    caps->n_per_out, caps->pps, sprintrc(getcaps_ret));
> +	}
> +
> +	/* PTP_SYS_OFFSET */
> +	sysoff->n_samples = PTP_MAX_SAMPLES;
> +	int sysoff_ret = ioctl(fd, PTP_SYS_OFFSET, sysoff);
> +	if (sysoff_ret < 0) {
> +		printf("ioctl(%d, PTP_SYS_OFFSET, {n_samples=%u}) = %s\n",
> +		       fd, sysoff->n_samples, sprintrc(sysoff_ret));
> +	} else {
> +		unsigned int i;
> +
> +		printf("ioctl(%d, PTP_SYS_OFFSET, ts=[", fd);

I'm sure the success path of decoder prints the same structure that starts
with {n_samples=%u

Please find a system where PTP_SYS_OFFSET works and make sure the test
passes on that system.

> +		for (i = 0; i < 2 * sysoff->n_samples + 1; ++i) {
> +			if (i > 0)
> +				printf(", ");
> +			printf("{sec=%" PRId64 ", nsec=%" PRIu32 "}",
> +			       (int64_t)sysoff->ts[i].sec, sysoff->ts[i].nsec);
> +		}
> +		printf("]) = %s\n", sprintrc(sysoff_ret));

At this point errno may be clobbered by earlier printf calls, with
sprintrc returning garbage.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20180312/771681c5/attachment.bin>


More information about the Strace-devel mailing list